All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
110 lines
3.8 KiB
Go
110 lines
3.8 KiB
Go
package spc
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestExtractProductTextCleansPreBlock(t *testing.T) {
|
|
raw := string(readTestFile(t, "day1_prt.html"))
|
|
|
|
got, err := ExtractProductText(raw)
|
|
if err != nil {
|
|
t.Fatalf("ExtractProductText() error = %v", err)
|
|
}
|
|
if strings.Contains(got, "<script") || strings.Contains(got, "<pre") {
|
|
t.Fatalf("ExtractProductText() retained HTML: %q", got)
|
|
}
|
|
if strings.Contains(got, "ignore me") {
|
|
t.Fatalf("ExtractProductText() retained script content: %q", got)
|
|
}
|
|
if !strings.Contains(got, "Day 1 Convective Outlook") {
|
|
t.Fatalf("ExtractProductText() missing headline: %q", got)
|
|
}
|
|
if !strings.HasPrefix(got, "SPC AC 111234") {
|
|
t.Fatalf("ExtractProductText() = %q, want product code prefix", got)
|
|
}
|
|
if strings.HasPrefix(got, "\n") || strings.HasSuffix(got, "\n") {
|
|
t.Fatalf("ExtractProductText() retained surrounding blank lines: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestParseDiscussionHTMLExtractsHeadlineSummaryAndUpdated(t *testing.T) {
|
|
got, err := ParseDiscussionHTML(string(readTestFile(t, "day1_prt.html")))
|
|
if err != nil {
|
|
t.Fatalf("ParseDiscussionHTML() error = %v", err)
|
|
}
|
|
if got.ProductTitle != "Day 1 Convective Outlook" {
|
|
t.Fatalf("ProductTitle = %q", got.ProductTitle)
|
|
}
|
|
if got.Headline != "Day 1 Convective Outlook" {
|
|
t.Fatalf("Headline = %q", got.Headline)
|
|
}
|
|
wantSummary := "Severe thunderstorms are possible across parts of the central Plains\nand mid Mississippi Valley this afternoon and evening."
|
|
if got.Summary != wantSummary {
|
|
t.Fatalf("Summary = %q, want %q", got.Summary, wantSummary)
|
|
}
|
|
if !strings.Contains(got.Discussion, "...DISCUSSION...") {
|
|
t.Fatalf("Discussion missing full text: %q", got.Discussion)
|
|
}
|
|
if !strings.HasPrefix(got.Discussion, "SPC AC 111234") {
|
|
t.Fatalf("Discussion = %q, want product code prefix", got.Discussion)
|
|
}
|
|
wantUpdated := time.Date(2026, 6, 11, 12, 45, 0, 0, time.UTC)
|
|
if got.UpdatedAt == nil || !got.UpdatedAt.Equal(wantUpdated) {
|
|
t.Fatalf("UpdatedAt = %v, want %s", got.UpdatedAt, wantUpdated)
|
|
}
|
|
}
|
|
|
|
func TestParseProductTitleSkipsSPCProductCode(t *testing.T) {
|
|
got := ParseProductTitle("SPC AC 101959\nDay 1 Convective Outlook\nNWS Storm Prediction Center Norman OK")
|
|
if got != "Day 1 Convective Outlook" {
|
|
t.Fatalf("ParseProductTitle() = %q, want Day 1 Convective Outlook", got)
|
|
}
|
|
}
|
|
|
|
func TestParseDiscussionTextPreservesCorrectionMarker(t *testing.T) {
|
|
got, err := ParseDiscussionHTML(string(readTestFile(t, "day2_prt_corr.html")))
|
|
if err != nil {
|
|
t.Fatalf("ParseDiscussionHTML() error = %v", err)
|
|
}
|
|
|
|
if !strings.Contains(got.Headline, "CORR 1") {
|
|
t.Fatalf("Headline = %q, want correction marker", got.Headline)
|
|
}
|
|
if !strings.Contains(got.Discussion, "CORR 1") {
|
|
t.Fatalf("Discussion = %q, want correction marker", got.Discussion)
|
|
}
|
|
wantUpdated := time.Date(2026, 6, 11, 17, 30, 0, 0, time.UTC)
|
|
if got.UpdatedAt == nil || !got.UpdatedAt.Equal(wantUpdated) {
|
|
t.Fatalf("UpdatedAt = %v, want %s", got.UpdatedAt, wantUpdated)
|
|
}
|
|
}
|
|
|
|
func TestParseUpdatedTimestampReturnsNilWhenAbsent(t *testing.T) {
|
|
text, err := ExtractProductText(string(readTestFile(t, "day2_prt_corr.html")))
|
|
if err != nil {
|
|
t.Fatalf("ExtractProductText() error = %v", err)
|
|
}
|
|
if got := ParseUpdatedTimestamp(text); got != nil {
|
|
t.Fatalf("ParseUpdatedTimestamp() = %v, want nil", got)
|
|
}
|
|
}
|
|
|
|
func TestParseUpdatedTimestampAcceptsSPCUTCFormat(t *testing.T) {
|
|
got := ParseUpdatedTimestamp("Updated: 1945 UTC Thu Jun 11 2026")
|
|
want := time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC)
|
|
if got == nil || !got.Equal(want) {
|
|
t.Fatalf("ParseUpdatedTimestamp() = %v, want %s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParsePageUpdatedTimestampAcceptsLiveSPCShape(t *testing.T) {
|
|
got := ParsePageUpdatedTimestamp(string(readTestFile(t, "day3_prt.html")))
|
|
want := time.Date(2026, 6, 11, 20, 0, 0, 0, time.UTC)
|
|
if got == nil || !got.Equal(want) {
|
|
t.Fatalf("ParsePageUpdatedTimestamp() = %v, want %s", got, want)
|
|
}
|
|
}
|