Files
knowledge-governance/internal/versionhistory/development_notes_history_test.go
T
姚章浩 dfbf3336b2
build / build (push) Successful in 1m29s
docs(version): 修正0.1.28同号说明归属
f33730ed 按第一父真实边界说明开发版本历史修复并标注QA分支为集成。
保留另一0.1.28提交自身说明;新增同号不同SHA选择回归。
2026-11-04 17:05:51 +08:00

112 lines
4.4 KiB
Go

package versionhistory
import (
"encoding/json"
"os"
"strings"
"testing"
"knowledge-governance/internal/governance"
)
func TestAllHistoricalDevelopmentNotesBindExactCommit(t *testing.T) {
changelogBytes, err := os.ReadFile("../../CHANGELOG.md")
if err != nil {
t.Fatal(err)
}
changelog := string(changelogBytes)
fixtureBytes, err := os.ReadFile("testdata/development_notes_history.json")
if err != nil {
t.Fatal(err)
}
var entries []struct {
Version string `json:"version"`
SHA string `json:"sha"`
}
if err := json.Unmarshal(fixtureBytes, &entries); err != nil {
t.Fatal(err)
}
if len(entries) != 270 {
t.Fatalf("history fixture must contain 270 missing development versions, got %d", len(entries))
}
seen := map[string]string{}
wrong := strings.Repeat("f", 40)
for _, entry := range entries {
if prev, ok := seen[entry.Version]; ok && prev != entry.SHA {
t.Fatalf("version %s bound to multiple commits", entry.Version)
}
seen[entry.Version] = entry.SHA
notes := governance.ExtractDevelopmentNotesForCommit(changelog, entry.Version, entry.SHA)
if strings.TrimSpace(notes) == "" {
t.Fatalf("version %s sha %s has no exact-SHA notes", entry.Version, entry.SHA)
}
if strings.Contains(notes, "目标提交:"+entry.SHA) == false {
t.Fatalf("version %s body does not carry its exact target line", entry.Version)
}
if leaked := governance.ExtractDevelopmentNotesForCommit(changelog, entry.Version, wrong); leaked != "" {
t.Fatalf("version %s notes leaked to wrong SHA: %q", entry.Version, leaked)
}
}
// 相邻/前缀版本不能互相命中。
if notes := governance.ExtractDevelopmentNotesForCommit(changelog, "0.1.35", entries[0].SHA); notes != "" {
t.Fatalf("0.1.35 leaked notes for another commit: %q", notes)
}
}
func TestMergeNotesUseFirstParentFactsAndRenameStats(t *testing.T) {
body, err := os.ReadFile("../../CHANGELOG.md")
if err != nil {
t.Fatal(err)
}
changelog := string(body)
for _, tc := range []struct {
version string
sha string
wants []string
minLen int
}{
{"0.1.207", "034c0b3a1c0b9fc5884750cc0d9929c0df4ac314", []string{"第一父", "108 个文件", "+10643/-548", "cmd/worker/library_baseline.go"}, 500},
{"0.1.35", "330fd12b3f30376c3aee757ba2a9f4ba3f8bcf27", []string{"第一父", "121 个文件", "+9065/-376", "qa_mirror.go"}, 400},
{"0.1.340", "5e1482e349e23728f8f2f1aeb770422eba2e5b5f", []string{"统计:47 个文件,+2485/-336", "canary_test.go"}, 500},
} {
notes := governance.ExtractDevelopmentNotesForCommit(changelog, tc.version, tc.sha)
if len(notes) < tc.minLen {
t.Fatalf("%s notes too shallow: %d chars", tc.version, len(notes))
}
for _, want := range tc.wants {
if !strings.Contains(notes, want) {
t.Fatalf("%s notes missing %q: %q", tc.version, want, notes)
}
}
}
}
func TestSameVersionDifferentSHAsKeepOwnNotes(t *testing.T) {
body, err := os.ReadFile("../../CHANGELOG.md")
if err != nil {
t.Fatal(err)
}
changelog := string(body)
f337 := "f33730ed7437536c3d1e01cc2f20231af076b531"
notes := governance.ExtractDevelopmentNotesForCommit(changelog, "0.1.28", f337)
for _, want := range []string{"开发版本历史", "第一父", "development_notes.go"} {
if !strings.Contains(notes, want) {
t.Fatalf("f337 notes missing real version-history scope %q: %q", want, notes)
}
}
for _, forbidden := range []string{"持久本地副本", "每 5 分钟", "安全 Markdown"} {
if strings.Contains(notes, forbidden) {
t.Fatalf("f337 notes still carry the other 0.1.28 commit's body %q: %q", forbidden, notes)
}
}
// Same version number, two target SHAs: exact-SHA selection must not cross.
dual := "## 开发快照 snapshot/v0.1.28 - 2026-10-18\n目标提交:" + f337 + "\n\n### Changed\n\n- 版本历史修复\n\n" +
"## 开发快照 snapshot/v0.1.28 - 2026-10-18\n目标提交:3e7fcce0b1b077dd6c44f2a4d535813232b941fb\n\n### Added\n\n- 持久本地问答副本\n"
if got := governance.ExtractDevelopmentNotesForCommit(dual, "0.1.28", f337); !strings.Contains(got, "版本历史修复") || strings.Contains(got, "持久本地") {
t.Fatalf("same-version f337 selected wrong block: %q", got)
}
if got := governance.ExtractDevelopmentNotesForCommit(dual, "0.1.28", "3e7fcce0b1b077dd6c44f2a4d535813232b941fb"); !strings.Contains(got, "持久本地") || strings.Contains(got, "版本历史修复") {
t.Fatalf("same-version 3e7 selected wrong block: %q", got)
}
}