Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions pkg/downloader/canonical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,46 @@ func TestPrepareCanonicalFeedBody(t *testing.T) {
}

func TestSanitizeReaderNormalizesProcessedStream(t *testing.T) {
input := "\ufeff 1.2.3.4\r\n\n0.0.0.0\n5.6.7.0/0\n 8.8.8.8\t\r\n9.9.9.9 10.10.10.10\r\n"
got, err := io.ReadAll(newSanitizeReader(strings.NewReader(input)))
if err != nil {
t.Fatalf("ReadAll(newSanitizeReader): %v", err)
}
want := "1.2.3.4\n8.8.8.8\n9.9.9.9 10.10.10.10\n"
if string(got) != want {
t.Fatalf("sanitized stream = %q, want %q", got, want)
t.Parallel()

cases := []struct {
name string
input string
want string
}{
{
name: "normalizes processed stream",
input: "\ufeff 1.2.3.4\r\n\n0.0.0.0\n5.6.7.0/0\n 8.8.8.8\t\r\n9.9.9.9 10.10.10.10\r\n",
want: "1.2.3.4\n8.8.8.8\n9.9.9.9 10.10.10.10\n",
},
{
name: "empty input returns empty stream",
input: "",
want: "",
},
{
name: "bom only is stripped to empty stream",
input: "\ufeff",
want: "",
},
{
name: "bom mid stream is preserved not stripped",
input: "1.1.1.1\n\ufeff2.2.2.2\n",
want: "1.1.1.1\n\ufeff2.2.2.2\n",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := io.ReadAll(newSanitizeReader(strings.NewReader(tc.input)))
if err != nil {
t.Fatalf("ReadAll(newSanitizeReader): %v", err)
}
if string(got) != tc.want {
t.Fatalf("sanitized stream = %q, want %q", got, tc.want)
}
})
}
}

Expand Down
56 changes: 36 additions & 20 deletions pkg/markdown/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,49 +126,65 @@ func TestTemplateMinsFunction(t *testing.T) {
t.Parallel()

cases := []struct {
name string
mins int
want string
}{
{30, "30m"},
{60, "1h"},
{90, "1.5h"},
{1440, "1d"},
{0, ""},
{name: "30 minutes", mins: 30, want: "30m"},
{name: "60 minutes", mins: 60, want: "1h"},
{name: "90 minutes", mins: 90, want: "1.5h"},
{name: "one day", mins: 1440, want: "1d"},
{name: "zero", mins: 0, want: ""},
}
for _, tc := range cases {
got, err := markdown.ExecuteInline("{{mins .}}", tc.mins)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Fatalf("mins(%d)=%q; want %q", tc.mins, got, tc.want)
}
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := markdown.ExecuteInline("{{mins .}}", tc.mins)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Fatalf("mins(%d)=%q; want %q", tc.mins, got, tc.want)
}
})
}
}

func TestTemplateRelTimeFunction(t *testing.T) {
t.Parallel()

// Capture `now` once and use a small negative offset for the "current" case
// so the test stays within the function's "just now" (< 1 minute) bucket
// even when the test runner is under heavy load. Capturing exact `now` and
// asserting exact "just now" was flaky because the wall-clock could cross
// the 1-minute boundary between capture and assertion.
now := time.Now()
cases := []struct {
name string
in int64
want string
}{
{name: "zero", in: 0, want: ""},
{name: "current", in: now.UnixMilli(), want: "just now"},
{name: "current", in: now.Add(-100 * time.Millisecond).UnixMilli(), want: "just now"},
{name: "minutes", in: now.Add(-5 * time.Minute).UnixMilli(), want: "5m ago"},
{name: "hours", in: now.Add(-2 * time.Hour).UnixMilli(), want: "2h ago"},
{name: "days", in: now.Add(-3 * 24 * time.Hour).UnixMilli(), want: "3d ago"},
{name: "months", in: now.Add(-60 * 24 * time.Hour).UnixMilli(), want: "2mo ago"},
{name: "years", in: now.Add(-400 * 24 * time.Hour).UnixMilli(), want: "1y ago"},
}
for _, tc := range cases {
got, err := markdown.ExecuteInline("{{relTime .}}", tc.in)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Fatalf("%s: relTime(%d)=%q; want %q", tc.name, tc.in, got, tc.want)
}
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := markdown.ExecuteInline("{{relTime .}}", tc.in)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Fatalf("%s: relTime(%d)=%q; want %q", tc.name, tc.in, got, tc.want)
}
})
}
}

Expand Down