package versionhistory import ( "context" "fmt" "knowledge-governance/internal/governance" "net/http" "strings" "testing" "time" ) func TestGiteaSnapshotRefIsExplicitAndNotesArePreserved(t *testing.T) { server := fakeGitea(t, func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/v1/repos/Weknora/knowledge-governance/commits" { if r.URL.Query().Get("sha") != shaB { t.Error("snapshot commit history silently used default branch") } fmt.Fprintf(w, `[{"sha":%q,"parents":[],"commit":{"message":"fixture"}}]`, shaB) return } if r.URL.Path == "/api/v1/repos/Weknora/knowledge-governance/git/refs/tags" { fmt.Fprintf(w, `[{"ref":"refs/tags/snapshot/v0.1.1","object":{"type":"tag","sha":%q}}]`, shaT) return } fmt.Fprintf(w, `{"message":"development notes","object":{"type":"commit","sha":%q}}`, shaB) }) c, _ := NewGiteaClient(validConfig(server.URL)) if _, _, e := c.ListCommitsAtRef(context.Background(), shaB, 1, 50); e != nil { t.Fatal(e) } tags, e := c.ListTags(context.Background()) if e != nil || len(tags) != 1 || tags[0].Message != "development notes" { t.Fatal("annotated snapshot notes lost", e) } } func TestDevelopmentVersionsHaveTheirOwnChangelogNotes(t *testing.T) { a, b := shaA, shaB now := time.Now() text := "# Changelog\n\n## [Unreleased]\n\n### Added\n\n- unrelated future work\n\n## 开发快照 snapshot/v0.1.1 - 2026-10-18\n\n### Fixed\n\n- fix specifically delivered here\n\n## [0.1.0] - 2026-10-13\n\n### Added\n\n- baseline\n" result := DeriveVersions(DeriveInput{Commits: []RawCommit{{SHA: a, CommittedAt: now}, {SHA: b, Parents: []string{a}, CommittedAt: now}}, Tags: []RawTag{{Name: "v0.1.0", TargetSHA: a, Annotated: true}}, Changelog: func(string) (string, error) { return text, nil }}) for _, v := range result.Records { if v.CommitSHA == b && (v.ReleaseNotes != "### Fixed\n\n- fix specifically delivered here" || v.RecordKind != governance.RecordKindDevelopment) { t.Fatalf("development version lacks its own notes: %q", v.ReleaseNotes) } } } func TestHistoricDevelopmentArchiveBindsExactCommit(t *testing.T) { archive := "## 开发快照 snapshot/v0.1.1 - 2026-10-18\n\n目标提交:" + shaB + "\n\n### Fixed\n\n- historic delivery notes\n" input := DeriveInput{DeliveryArchive: archive, Commits: []RawCommit{{SHA: shaA}, {SHA: shaB, Parents: []string{shaA}}}, Tags: []RawTag{{Name: "v0.1.0", TargetSHA: shaA, Annotated: true}}, Changelog: func(string) (string, error) { return "## [0.1.0] - 2026-10-13\n\n### Added\n\n- baseline", nil }} result := DeriveVersions(input) for _, v := range result.Records { if v.CommitSHA == shaB && !strings.Contains(v.ReleaseNotes, "historic delivery notes") { t.Fatal("archived notes missing") } } // Parallel branch histories may legitimately have the same derived number. input.DeliveryArchive = strings.ReplaceAll(archive, shaB, strings.Repeat("c", 40)) + "\n" + archive result = DeriveVersions(input) for _, v := range result.Records { if v.CommitSHA == shaB && !strings.Contains(v.ReleaseNotes, "historic delivery notes") { t.Fatal("same-number archive hid notes for exact commit") } } input.DeliveryArchive = strings.ReplaceAll(archive, shaB, strings.Repeat("c", 40)) result = DeriveVersions(input) for _, v := range result.Records { if v.CommitSHA == shaB && v.ReleaseNotes != "" { t.Fatal("different commit reused same-number archive notes") } } } type snapshotRefSource struct { fakeSource other []RawCommit } func (s *snapshotRefSource) ListCommitsAtRef(_ context.Context, _ string, page, limit int) ([]RawCommit, int, error) { if page == 1 { return s.other, len(s.other), nil } return nil, len(s.other), nil } func TestSyncIncludesDeliveredSnapshotOutsideMain(t *testing.T) { a := RawCommit{SHA: shaA} b := RawCommit{SHA: shaB, Parents: []string{shaA}} src := &snapshotRefSource{fakeSource: fakeSource{commits: []RawCommit{a}, total: 1, pageSize: 50, head: shaA, tags: []RawTag{{Name: "snapshot/v0.1.1", TargetSHA: shaB, Annotated: true}}}, other: []RawCommit{b, a}} store := &fakeStore{} run, e := newTestSyncer(t, src, store).Sync(context.Background(), "fixture", "admin") if e != nil || len(store.saved) != 1 || len(store.saved[0].records) != 2 || !run.Complete { t.Fatal("delivered non-main snapshot omitted", e, run.FetchedCount) } }