241 lines
8.4 KiB
Go
241 lines
8.4 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func storageTestFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestStorageInventoryDeduplicatesAndExcludesSymlinks(t *testing.T) {
|
|
root := t.TempDir()
|
|
data := filepath.Join(root, "data")
|
|
artifacts := filepath.Join(data, "artifacts")
|
|
database := filepath.Join(data, "state.db")
|
|
storageTestFile(t, database, "database")
|
|
storageTestFile(t, database+"-wal", "wal")
|
|
storageTestFile(t, filepath.Join(artifacts, "1", "output"), "artifact")
|
|
storageTestFile(t, filepath.Join(data, "notes"), "note")
|
|
outside := filepath.Join(root, "outside")
|
|
storageTestFile(t, outside, "must not count")
|
|
if err := os.Symlink(outside, filepath.Join(data, "symlink")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Link(filepath.Join(data, "notes"), filepath.Join(data, "hardlink")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inventory, err := inspectStorage(context.Background(), storageInventoryConfig{DatabasePath: database, DataRoot: data, ArtifactsRoot: artifacts, WorkspaceRoots: []string{data, data}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if inventory.TotalBytes != 23 {
|
|
t.Fatalf("bytes %d, want23: %+v", inventory.TotalBytes, inventory.Categories)
|
|
}
|
|
if inventory.Partial {
|
|
t.Fatalf("unexpected partial: %+v", inventory)
|
|
}
|
|
categories := map[string]int64{}
|
|
for _, item := range inventory.Categories {
|
|
categories[item.ID] = item.Bytes
|
|
}
|
|
if categories["database"] != 11 || categories["artifacts"] != 8 || categories["other_data"] != 4 || categories["workspaces"] != 0 {
|
|
t.Fatalf("categories %+v", categories)
|
|
}
|
|
}
|
|
|
|
func TestStorageMeasurementReportsCancellationAndIndirectRoots(t *testing.T) {
|
|
root := t.TempDir()
|
|
storageTestFile(t, filepath.Join(root, "file"), "content")
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, _, partial, problems := measureStoragePath(ctx, root)
|
|
if !partial || len(problems) == 0 {
|
|
t.Fatal("canceled measurement claimed complete")
|
|
}
|
|
link := filepath.Join(t.TempDir(), "link")
|
|
if err := os.Symlink(root, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, bytes, partial, problems := measureStoragePath(context.Background(), link)
|
|
if bytes != 0 || !partial || len(problems) == 0 {
|
|
t.Fatal("symlink root followed or not reported")
|
|
}
|
|
}
|
|
|
|
func storageTestGit(t *testing.T, root string, args ...string) string {
|
|
t.Helper()
|
|
out, err := storageGit(context.Background(), root, args...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return out
|
|
}
|
|
func storageTestRepo(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
base := t.TempDir()
|
|
main := filepath.Join(base, "main")
|
|
if err := os.Mkdir(main, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
storageTestGit(t, main, "init", "-b", "main")
|
|
storageTestGit(t, main, "config", "user.email", "fixture@example.invalid")
|
|
storageTestGit(t, main, "config", "user.name", "Fixture")
|
|
storageTestFile(t, filepath.Join(main, "tracked"), "initial")
|
|
storageTestGit(t, main, "add", "tracked")
|
|
storageTestGit(t, main, "commit", "-m", "initial")
|
|
linked := filepath.Join(base, "linked")
|
|
storageTestGit(t, main, "worktree", "add", "-b", "item", linked)
|
|
return main, linked
|
|
}
|
|
|
|
func TestStorageWorktreesRequireMergedCleanInactiveAndUnlocked(t *testing.T) {
|
|
ctx := context.Background()
|
|
main, linked := storageTestRepo(t)
|
|
discover := func(protected ...string) storageWorktree {
|
|
t.Helper()
|
|
trees, problems := discoverStorageWorktrees(ctx, []string{main, linked}, protected)
|
|
if len(problems) > 0 || len(trees) != 1 {
|
|
t.Fatalf("discovery %+v %+v", trees, problems)
|
|
}
|
|
return trees[0]
|
|
}
|
|
tree := discover()
|
|
canonicalMain, err := filepath.EvalSymlinks(main)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !tree.Eligible || tree.Repository != canonicalMain {
|
|
t.Fatalf("clean merged linked tree not eligible: %+v", tree)
|
|
}
|
|
if discover(linked).Eligible {
|
|
t.Fatal("open chat/run workspace eligible")
|
|
}
|
|
if discover(filepath.Join(linked, "subdirectory")).Eligible {
|
|
t.Fatal("nested active workspace eligible")
|
|
}
|
|
storageTestFile(t, filepath.Join(linked, "draft"), "do not delete")
|
|
if got := discover(); got.Eligible || !strings.Contains(got.Reason, "Uncommitted") {
|
|
t.Fatalf("dirty tree %+v", got)
|
|
}
|
|
if err := removeStorageWorktree(ctx, tree, nil); err == nil {
|
|
t.Fatal("stale candidate removed newly dirty tree")
|
|
}
|
|
if err := os.Remove(filepath.Join(linked, "draft")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
storageTestGit(t, main, "worktree", "lock", linked)
|
|
if discover().Eligible {
|
|
t.Fatal("locked tree eligible")
|
|
}
|
|
storageTestGit(t, main, "worktree", "unlock", linked)
|
|
storageTestFile(t, filepath.Join(linked, "tracked"), "unlanded")
|
|
storageTestGit(t, linked, "add", "tracked")
|
|
storageTestGit(t, linked, "commit", "-m", "unlanded")
|
|
if got := discover(); got.Eligible || !strings.Contains(got.Reason, "merged") {
|
|
t.Fatalf("unlanded tree %+v", got)
|
|
}
|
|
storageTestGit(t, main, "merge", "--ff-only", "item")
|
|
if !discover().Eligible {
|
|
t.Fatal("landed clean tree not eligible")
|
|
}
|
|
if err := removeStorageWorktree(ctx, discover(), nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(linked); !os.IsNotExist(err) {
|
|
t.Fatalf("worktree remains: %v", err)
|
|
}
|
|
storageTestGit(t, main, "show-ref", "--verify", "refs/heads/item")
|
|
if _, err := os.Stat(main); err != nil {
|
|
t.Fatal("main checkout removed")
|
|
}
|
|
}
|
|
|
|
func storageTestIgnoredParent(t *testing.T) (string, string, storageWorktree) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
main, parent := storageTestRepo(t)
|
|
storageTestFile(t, filepath.Join(parent, ".gitignore"), "nested/\n")
|
|
storageTestGit(t, parent, "add", ".gitignore")
|
|
storageTestGit(t, parent, "commit", "-m", "ignore nested workspaces")
|
|
storageTestGit(t, main, "merge", "--ff-only", "item")
|
|
trees, problems := discoverStorageWorktrees(ctx, []string{main}, nil)
|
|
if len(problems) > 0 || len(trees) != 1 || !trees[0].Eligible {
|
|
t.Fatalf("parent initially unavailable: %+v %+v", trees, problems)
|
|
}
|
|
return main, parent, trees[0]
|
|
}
|
|
|
|
func TestStorageNestedIgnoredRegisteredWorktreeProtectsParent(t *testing.T) {
|
|
ctx := context.Background()
|
|
main, parent, before := storageTestIgnoredParent(t)
|
|
child := filepath.Join(parent, "nested")
|
|
storageTestGit(t, main, "worktree", "add", "-b", "nested-child", child)
|
|
if status := storageTestGit(t, parent, "status", "--porcelain", "--untracked-files=all"); status != "" {
|
|
t.Fatalf("fixture parent dirty: %q", status)
|
|
}
|
|
trees, problems := discoverStorageWorktrees(ctx, []string{main}, nil)
|
|
if len(problems) > 0 || len(trees) != 2 {
|
|
t.Fatalf("trees %+v problems %+v", trees, problems)
|
|
}
|
|
found := false
|
|
for _, tree := range trees {
|
|
if tree.Path == before.Path {
|
|
found = true
|
|
if tree.Eligible || !strings.Contains(tree.Reason, "Contains another") {
|
|
t.Fatalf("parent not protected: %+v", tree)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("parent missing from inventory")
|
|
}
|
|
// Before has no knowledge of the child. Removal must read Git's live registry.
|
|
if err := removeStorageWorktree(ctx, before, nil); err == nil {
|
|
t.Fatal("stale parent candidate removed ignored child")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(child, "tracked")); err != nil {
|
|
t.Fatal("child checkout was damaged")
|
|
}
|
|
storageTestGit(t, main, "show-ref", "--verify", "refs/heads/nested-child")
|
|
}
|
|
|
|
func TestStorageNestedForeignPrimaryWorkspaceProtectsParent(t *testing.T) {
|
|
ctx := context.Background()
|
|
main, parent, before := storageTestIgnoredParent(t)
|
|
child := filepath.Join(parent, "nested")
|
|
if err := os.Mkdir(child, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
storageTestGit(t, child, "init", "-b", "main")
|
|
storageTestGit(t, child, "config", "user.email", "fixture@example.invalid")
|
|
storageTestGit(t, child, "config", "user.name", "Fixture")
|
|
storageTestFile(t, filepath.Join(child, "independent"), "preserve foreign primary")
|
|
storageTestGit(t, child, "add", "independent")
|
|
storageTestGit(t, child, "commit", "-m", "foreign primary")
|
|
trees, problems := discoverStorageWorktrees(ctx, []string{main, child}, nil)
|
|
if len(problems) > 0 || len(trees) != 1 || trees[0].Eligible {
|
|
t.Fatalf("foreign nested workspace not protected: %+v %+v", trees, problems)
|
|
}
|
|
// A manager passes the fresh full inventory object to destructive revalidation.
|
|
if err := removeStorageWorktree(ctx, trees[0], nil); err == nil {
|
|
t.Fatal("parent removed foreign recorded workspace")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(child, "independent")); err != nil {
|
|
t.Fatal("foreign primary damaged")
|
|
}
|
|
if trees[0].Path != before.Path {
|
|
t.Fatal("unexpected parent")
|
|
}
|
|
}
|