package sqex import ( "strings" "testing" ) // SE's list has no trailing newline: the two footer lines are the last two // elements, which is how the parser knows where entries end. A leading blank // line would shift that positional layout too, so neither appears here. const nineFieldList = "header line 1\n" + "header line 2\n" + "header line 3\n" + "header line 4\n" + "header line 5\n" + "1000\t0\t0\t0\t2026.09.15.0000.0000\t0\t\t\thttp://patch.example/sqpack.ex5.sqp\n" + "2000\t1\t0\t0\t2026.09.14.0000.0000\t1\t512\tabc123\thttp://patch.example/another.sqp\n" + "footer line 1\n" + "footer line 2" func TestParsePatchListNineFields(t *testing.T) { got, err := ParsePatchList(nineFieldList) if err != nil { t.Fatal(err) } want0 := PatchEntry{VersionID: "2026.09.15.0000.0000", URL: "http://patch.example/sqpack.ex5.sqp"} want1 := PatchEntry{VersionID: "2026.09.14.0000.0000", URL: "http://patch.example/another.sqp"} if len(got) != 2 || got[0] != want0 || got[1] != want1 { t.Fatalf("got %#v, want [%#v %#v]", got, want0, want1) } if tv := TargetVersion(got); tv != "2026.09.14.0000.0000" { t.Fatalf("TargetVersion = %q", tv) } } func TestParsePatchListSixFields(t *testing.T) { list := "h1\nh2\nh3\nh4\nh5\n" + "4096\t0\tx\ty\t2026.10.01.0000.0000\thttps://example.invalid/boot.zip\n" + "f1\nf2" got, err := ParsePatchList(list) if err != nil { t.Fatal(err) } want := PatchEntry{VersionID: "2026.10.01.0000.0000", URL: "https://example.invalid/boot.zip"} if len(got) != 1 || got[0] != want { t.Fatalf("got %#v, want %#v", got, want) } } func TestParsePatchListCRLF(t *testing.T) { got, err := ParsePatchList(strings.ReplaceAll(nineFieldList, "\n", "\r\n")) if err != nil { t.Fatal(err) } if len(got) != 2 { t.Fatalf("got %d entries, want 2: %#v", len(got), got) } for _, e := range got { if e.URL == "" || e.VersionID == "" { t.Fatalf("empty field in %#v", e) } } } func TestParsePatchListErrors(t *testing.T) { if _, err := ParsePatchList("a\nb\nc"); err == nil { t.Fatal("expected error for short list") } bad := "h1\nh2\nh3\nh4\nh5\nnot-enough-fields\nf1\nf2" if _, err := ParsePatchList(bad); err == nil { t.Fatal("expected error for malformed row") } } func TestTargetVersionEmpty(t *testing.T) { if tv := TargetVersion(nil); tv != "" { t.Fatalf("TargetVersion(nil) = %q, want empty", tv) } }