Skip to content
edgesync Artifact [da2ccc005b]
Login

Artifact [da2ccc005b]

Artifact da2ccc005b02747048d07006b76eeb0a02b538948de26203944a497826c5c747:


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")
	}
}