package provider import ( "errors" "os" "path/filepath" "strings" "testing" "golang.org/x/sys/unix" ) func TestClaudePrivateWriteStaging(t *testing.T) { for _, name := range []string{".cc-writes", ".claude/bridge-spawn"} { t.Run(name, func(t *testing.T) { for _, kind := range []string{"empty", "file", "child", "link", "mode", "version"} { t.Run(kind, func(t *testing.T) { l := &AccountLease{source: Binding{Provider: "claude", Expected: Identity{ProviderVersion: ClaudeVersion}}, runtime: t.TempDir()} path := filepath.Join(l.runtime, name) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } switch kind { case "file": if err := os.WriteFile(path, nil, 0600); err != nil { t.Fatal(err) } case "link": if err := os.Symlink(t.TempDir(), path); err != nil { t.Fatal(err) } default: if err := os.Mkdir(path, 0700); err != nil { t.Fatal(err) } } if kind == "child" { if err := os.WriteFile(filepath.Join(path, "settings.json.tmp.12345678"), nil, 0600); err != nil { t.Fatal(err) } } if kind == "mode" { if err := os.Chmod(path, 0777); err != nil { t.Fatal(err) } } if kind == "version" { l.source.Expected.ProviderVersion = "other" } err := l.validateRuntimeEntries() if (kind == "empty") != (err == nil) { t.Fatalf("%s: %v", kind, err) } }) } }) } } func TestClaudeGitIgnoreState(t *testing.T) { for _, kind := range []string{"observed", "private", "same-size-content", "empty", "extra-pattern", "missing-newline", "mode", "directory-mode", "version", "child", "symlink", "hardlink", "directory", "fifo", "setuid", "setgid"} { t.Run(kind, func(t *testing.T) { l := &AccountLease{source: Binding{Provider: "claude", Expected: Identity{ProviderVersion: ClaudeVersion}}, runtime: t.TempDir()} must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } dir := filepath.Join(l.runtime, ".config/git") must(os.MkdirAll(dir, 0775)) must(os.Chmod(dir, 0775)) path := filepath.Join(dir, "ignore") content := "**/.claude/.cc-writes/\n" switch kind { case "same-size-content": content = strings.Repeat("x", len(content)) case "empty": content = "" case "extra-pattern": content += "*\n" case "missing-newline": content = strings.TrimSuffix(content, "\n") } must(os.WriteFile(path, []byte(content), 0664)) must(os.Chmod(path, 0664)) switch kind { case "private": must(os.Chmod(dir, 0700)) must(os.Chmod(path, 0600)) case "mode": must(os.Chmod(path, 0666)) case "directory-mode": must(os.Chmod(dir, 0777)) case "version": l.source.Expected.ProviderVersion = "other" case "child": must(os.WriteFile(filepath.Join(dir, "config"), nil, 0600)) case "symlink": must(os.Remove(path)) must(os.Symlink("/workspace/ignore", path)) case "hardlink": must(os.Link(path, filepath.Join(t.TempDir(), "link"))) case "directory": must(os.Remove(path)) must(os.Mkdir(path, 0664)) case "fifo": must(os.Remove(path)) must(unix.Mkfifo(path, 0664)) case "setuid": must(os.Chmod(path, 0664|os.ModeSetuid)) case "setgid": must(os.Chmod(path, 0664|os.ModeSetgid)) } err := l.validateRuntimeEntries() if (kind == "observed" || kind == "private") != (err == nil) { t.Fatalf("%s: %v", kind, err) } if err != nil && !errors.Is(err, ErrContainment) { t.Fatalf("wrong refusal: %v", err) } }) } } func TestClaudeGitIgnoreReadback(t *testing.T) { for _, change := range []string{"unchanged", "missing", "replaced", "mode", "size", "hardlink", "symlink", "parent-symlink", "unreadable"} { t.Run(change, func(t *testing.T) { must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } l := &AccountLease{runtime: t.TempDir()} dir := filepath.Join(l.runtime, ".config/git") must(os.MkdirAll(dir, 0700)) path := filepath.Join(dir, "ignore") content := []byte("**/.claude/.cc-writes/\n") must(os.WriteFile(path, content, 0600)) info, err := os.Lstat(path) must(err) switch change { case "missing": must(os.Remove(path)) case "replaced": must(os.Rename(path, path+".old")) must(os.WriteFile(path, content, 0600)) case "mode": must(os.Chmod(path, 0664)) case "size": must(os.WriteFile(path, append(content, '\n'), 0600)) case "hardlink": must(os.Link(path, filepath.Join(t.TempDir(), "link"))) case "symlink": must(os.Rename(path, path+".old")) must(os.Symlink(path+".old", path)) case "parent-symlink": must(os.Rename(dir, dir+".old")) must(os.Symlink(dir+".old", dir)) case "unreadable": must(os.Chmod(path, 0000)) } if got := l.claudeGitIgnore(info); got != (change == "unchanged") { t.Fatalf("%s accepted=%v", change, got) } }) } }