From 0fc534136ba6c65018950e26e3dbbf59eba6b82e Mon Sep 17 00:00:00 2001 From: Costa Tsaousis Date: Tue, 9 Jun 2026 10:44:06 +0300 Subject: [PATCH] test quality AI findings sanitizer mins relTime Triage and fix the three real Go test issues from the GitHub code quality AI findings surface (security/quality/ai-findings). 11 findings were reported across 5 files; 1 was a false positive (helper_contract tie-break), 1 conflicted with project status conventions, 2 were defensive/duplicates, and 4 were SOW-level concerns already addressed or out of scope for a single test PR. The remaining 3 are fixed here: - pkg/downloader: TestSanitizeReaderNormalizesProcessedStream now exercises empty input, BOM-only input, and mid-stream BOM via a table-driven t.Run pattern. - pkg/markdown: TestTemplateMinsFunction gains name-based subtests and matches the t.Parallel()/t.Run pattern used by sibling tests. - pkg/markdown: TestTemplateRelTimeFunction gains t.Parallel(), name-based subtests, and the 'current' case uses a 100ms-in-the-past offset to remove the exact-now flakiness when the runner crosses the 1-minute 'just now' boundary under load. --- pkg/downloader/canonical_test.go | 48 ++++++++++++++++++++++----- pkg/markdown/funcs_test.go | 56 ++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/pkg/downloader/canonical_test.go b/pkg/downloader/canonical_test.go index dabb6d8..53bfc8f 100644 --- a/pkg/downloader/canonical_test.go +++ b/pkg/downloader/canonical_test.go @@ -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) + } + }) } } diff --git a/pkg/markdown/funcs_test.go b/pkg/markdown/funcs_test.go index 0f05444..679221b 100644 --- a/pkg/markdown/funcs_test.go +++ b/pkg/markdown/funcs_test.go @@ -126,27 +126,39 @@ 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 @@ -154,7 +166,7 @@ func TestTemplateRelTimeFunction(t *testing.T) { 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"}, @@ -162,13 +174,17 @@ func TestTemplateRelTimeFunction(t *testing.T) { {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) + } + }) } }