Artifact da2ccc005b02747048d07006b76eeb0a02b538948de26203944a497826c5c747:
- File
leaf/agent/code_artifact_test.go
— part of check-in
[93966ff081]
at
2026-05-04 20:41:47
on branch fix/125-commit-parent-id
— fix(leaf/agent): chain Commit onto trunk tip by default (ParentID/MergeParents)
Adds ParentID and MergeParents to agent.CommitOpts. When ParentID is 0, Agent.Commit auto-resolves the current trunk tip and uses that as the parent — matching `fossil ci`'s default behavior. On an empty repo (BranchTip returns sql.ErrNoRows), the resolution falls through to 0 and the commit is a legitimate orphan root (the first commit).
Before this fix, agent.Commit always passed ParentID=0 to libfossil, producing P-card-less manifests. Every commit became its own root, fragmenting the timeline and breaking trunk-based development for downstream consumers (notably bones).
Tests added: - TestAgent_Commit_ChainsOntoTrunkTipByDefault — two consecutive Commit calls produce a chained pair with linked Parents. - TestAgent_Commit_ExplicitParentIDOverride — caller-supplied ParentID is used verbatim.
The hub-leaf e2e test (sim/hub_leaf_test.go) was reading by "trunk" tip on the hub; switched to reading by the explicit RevID returned from Agent.Commit so the test exercises the transport contract (file lands on hub) without coupling to branch-tip resolution semantics on the hub side after sync — which appears to be a separate concern worth its own investigation.
Closes #125 (user: [email protected], size: 9172) [annotate] [blame] [check-ins using] [more...]
package agent import ( "bytes" "context" "path/filepath" "slices" "testing" libfossil "github.com/danmestas/libfossil" ) // newAgentForCommitTests opens a fresh repo and builds an Agent without // touching networking. The state-machine fields are unused for code-artifact // tests; only repo + projectCode are needed. func newAgentForCommitTests(t *testing.T) *Agent { t.Helper() path := filepath.Join(t.TempDir(), "test.fossil") r, err := libfossil.Create(path, libfossil.CreateOpts{User: "testuser"}) if err != nil { t.Fatalf("libfossil.Create: %v", err) } t.Cleanup(func() { r.Close() }) applySQLiteTuning(r) projCode, err := r.Config("project-code") if err != nil { t.Fatalf("read project-code: %v", err) } return &Agent{repo: r, projectCode: projCode} } func TestAgent_Commit_RoundtripsThroughReadAndFiles(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() rev, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{ {Name: "hello.txt", Content: []byte("hello\n")}, {Name: "sub/world.txt", Content: []byte("world\n")}, }, Message: "initial", Author: "testuser", }) if err != nil { t.Fatalf("Commit: %v", err) } if rev == "" { t.Fatal("Commit returned empty RevID") } got, err := a.Read(ctx, "hello.txt") if err != nil { t.Fatalf("Read: %v", err) } if !bytes.Equal(got, []byte("hello\n")) { t.Errorf("Read hello.txt = %q, want %q", got, "hello\n") } files, err := a.Files(ctx) if err != nil { t.Fatalf("Files: %v", err) } if !slices.Contains(files, "hello.txt") || !slices.Contains(files, "sub/world.txt") { t.Errorf("Files = %v, want hello.txt + sub/world.txt", files) } } func TestAgent_Commit_RejectsEmptyAuthor(t *testing.T) { a := newAgentForCommitTests(t) _, err := a.Commit(context.Background(), CommitOpts{ Files: []FileToCommit{{Name: "x", Content: []byte("y")}}, Message: "no author", }) if err == nil { t.Fatal("Commit with empty Author returned nil") } } func TestAgent_Tip_ReturnsCommitUUID(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() rev, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f", Content: []byte("v1")}}, Message: "c1", Author: "testuser", }) if err != nil { t.Fatalf("Commit: %v", err) } tip, err := a.Tip(ctx, "trunk") if err != nil { t.Fatalf("Tip: %v", err) } if tip != rev { t.Errorf("Tip = %q, want %q", tip, rev) } } func TestAgent_Tip_EmptyBranchReturnsEmptyNoError(t *testing.T) { a := newAgentForCommitTests(t) tip, err := a.Tip(context.Background(), "no-such-branch") if err != nil { t.Fatalf("Tip: %v (want nil)", err) } if tip != "" { t.Errorf("Tip = %q, want empty", tip) } } func TestAgent_Diff_BetweenTwoRevs(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() revA, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f.txt", Content: []byte("alpha\n")}}, Message: "v1", Author: "testuser", }) if err != nil { t.Fatalf("Commit v1: %v", err) } revB, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f.txt", Content: []byte("beta\n")}}, Message: "v2", Author: "testuser", }) if err != nil { t.Fatalf("Commit v2: %v", err) } diff, err := a.Diff(ctx, revA, revB) if err != nil { t.Fatalf("Diff: %v", err) } if !bytes.Contains(diff, []byte("alpha")) || !bytes.Contains(diff, []byte("beta")) { t.Errorf("Diff missing alpha/beta markers: %s", diff) } } // TestAgent_Diff_RenameAcrossRevs exercises the case the pre-cleanup // implementation got wrong: same content, different name. The old // hand-rolled file enumeration diffed both files as full additions/ // removals; libfossil's whole-checkin Diff should handle the rename // natively. We don't pin the exact rename-marker shape — we just // confirm Diff doesn't error and produces non-empty output for the // pair. func TestAgent_Diff_RenameAcrossRevs(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() revA, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "before.txt", Content: []byte("same content\n")}}, Message: "rev A", Author: "testuser", }) if err != nil { t.Fatalf("Commit rev A: %v", err) } revB, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "after.txt", Content: []byte("same content\n")}}, Message: "rev B (renamed)", Author: "testuser", }) if err != nil { t.Fatalf("Commit rev B: %v", err) } if _, err := a.Diff(ctx, revA, revB); err != nil { t.Fatalf("Diff across rename: %v", err) } } // TestAgent_Commit_ChainsOntoTrunkTipByDefault proves that two consecutive // Agent.Commit calls without explicit ParentID produce a chained pair — // the second commit's primary parent is the first commit. Without the fix // for issue #125 every commit was an orphan root and every commit's // Parents slice was empty. func TestAgent_Commit_ChainsOntoTrunkTipByDefault(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() rev1, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f", Content: []byte("v1")}}, Message: "first", Author: "testuser", }) if err != nil { t.Fatalf("Commit 1: %v", err) } rev2, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f", Content: []byte("v2")}}, Message: "second", Author: "testuser", }) if err != nil { t.Fatalf("Commit 2: %v", err) } // Walk the timeline and find rev2; its Parents must contain rev1. tipRID, err := a.repo.BranchTip("trunk") if err != nil { t.Fatalf("BranchTip: %v", err) } timeline, err := a.repo.Timeline(libfossil.LogOpts{Start: tipRID, Limit: 10}) if err != nil { t.Fatalf("Timeline: %v", err) } var second *libfossil.LogEntry for i := range timeline { if timeline[i].UUID == string(rev2) { second = &timeline[i] break } } if second == nil { t.Fatalf("rev2 %q not found in timeline %+v", rev2, timeline) } if !slices.Contains(second.Parents, string(rev1)) { t.Errorf("rev2.Parents = %v, want to contain rev1 %q", second.Parents, rev1) } } // TestAgent_Commit_ExplicitParentIDOverride proves that a caller-supplied // ParentID is used verbatim — useful for committing onto a non-trunk // branch via Agent.Tip(ctx, branchName). func TestAgent_Commit_ExplicitParentIDOverride(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() rev1, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f", Content: []byte("v1")}}, Message: "first", Author: "testuser", }) if err != nil { t.Fatalf("Commit 1: %v", err) } rid1, err := a.repo.ResolveVersion(string(rev1)) if err != nil { t.Fatalf("ResolveVersion rev1: %v", err) } rev2, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{{Name: "f", Content: []byte("v2")}}, Message: "explicit parent", Author: "testuser", ParentID: rid1, }) if err != nil { t.Fatalf("Commit 2 with explicit ParentID: %v", err) } tipRID, err := a.repo.BranchTip("trunk") if err != nil { t.Fatalf("BranchTip: %v", err) } timeline, err := a.repo.Timeline(libfossil.LogOpts{Start: tipRID, Limit: 10}) if err != nil { t.Fatalf("Timeline: %v", err) } for _, e := range timeline { if e.UUID == string(rev2) { if !slices.Contains(e.Parents, string(rev1)) { t.Errorf("rev2.Parents = %v, want to contain rev1 %q", e.Parents, rev1) } return } } t.Errorf("rev2 %q not found in timeline", rev2) } func TestAgent_ExtractTo_PopulatesDir(t *testing.T) { a := newAgentForCommitTests(t) ctx := context.Background() rev, err := a.Commit(ctx, CommitOpts{ Files: []FileToCommit{ {Name: "a.txt", Content: []byte("AAA")}, {Name: "b/c.txt", Content: []byte("CCC")}, }, Message: "initial", Author: "testuser", }) if err != nil { t.Fatalf("Commit: %v", err) } dir := t.TempDir() if err := a.ExtractTo(ctx, dir, rev); err != nil { t.Fatalf("ExtractTo: %v", err) } matches, err := filepath.Glob(filepath.Join(dir, "*.txt")) if err != nil { t.Fatalf("glob: %v", err) } if len(matches) == 0 { t.Errorf("ExtractTo produced no top-level .txt files in %s", dir) } } func TestAgent_ExtractTo_EmptyRevIsNoop(t *testing.T) { a := newAgentForCommitTests(t) dir := t.TempDir() if err := a.ExtractTo(context.Background(), dir, ""); err != nil { t.Errorf("ExtractTo with empty rev should be no-op, got: %v", err) } } func TestAgent_Config_ReadsProjectCode(t *testing.T) { a := newAgentForCommitTests(t) got, err := a.Config("project-code") if err != nil { t.Fatalf("Config: %v", err) } if got == "" || got != a.projectCode { t.Errorf("Config(project-code) = %q, want non-empty matching agent.projectCode (%q)", got, a.projectCode) } } func TestAgent_Sync_NoTransportReturnsError(t *testing.T) { a := newAgentForCommitTests(t) if _, err := a.Sync(context.Background()); err == nil { t.Fatal("Sync with no transport should error") } } func TestAgent_SyncTo_RejectsEmptyURL(t *testing.T) { a := newAgentForCommitTests(t) if _, err := a.SyncTo(context.Background(), "", SyncOpts{Push: true}); err == nil { t.Fatal("SyncTo with empty URL should error") } }