Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
600 lines
20 KiB
Go
600 lines
20 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/tutti-os/tutti/packages/agent/daemon/providerregistry"
|
|
"github.com/tutti-os/tutti/services/tuttid/biz/agentprovider"
|
|
)
|
|
|
|
func TestChangedProvidersReportsProvidersWithChangedFiles(t *testing.T) {
|
|
entries := []ProviderAuthWatchEntry{
|
|
{Provider: agentprovider.Codex, Paths: []string{"/tmp/codex/auth.json", "/tmp/codex/config.toml"}},
|
|
{Provider: agentprovider.ClaudeCode, Paths: []string{"/tmp/claude/settings.json"}},
|
|
}
|
|
baseline := map[string]providerAuthFileFingerprint{
|
|
"/tmp/codex/auth.json": {exists: true, modTime: time.UnixMilli(1000), size: 10},
|
|
"/tmp/codex/config.toml": {exists: true, modTime: time.UnixMilli(1000), size: 20},
|
|
"/tmp/claude/settings.json": {exists: true, modTime: time.UnixMilli(1000), size: 30},
|
|
}
|
|
|
|
unchanged := changedProviders(entries, baseline, cloneFingerprints(baseline))
|
|
if len(unchanged) != 0 {
|
|
t.Fatalf("changedProviders with identical fingerprints = %v, want empty", unchanged)
|
|
}
|
|
|
|
next := cloneFingerprints(baseline)
|
|
next["/tmp/codex/auth.json"] = providerAuthFileFingerprint{exists: true, modTime: time.UnixMilli(2000), size: 12}
|
|
changed := changedProviders(entries, baseline, next)
|
|
if len(changed) != 1 || changed[0] != agentprovider.Codex {
|
|
t.Fatalf("changedProviders = %v, want [codex]", changed)
|
|
}
|
|
}
|
|
|
|
func TestChangedProvidersTreatsCreateAndDeleteAsChanges(t *testing.T) {
|
|
entries := []ProviderAuthWatchEntry{
|
|
{Provider: agentprovider.Codex, Paths: []string{"/tmp/codex/auth.json"}},
|
|
{Provider: agentprovider.ClaudeCode, Paths: []string{"/tmp/claude/settings.json"}},
|
|
}
|
|
baseline := map[string]providerAuthFileFingerprint{
|
|
"/tmp/codex/auth.json": {},
|
|
"/tmp/claude/settings.json": {exists: true, modTime: time.UnixMilli(1000), size: 30},
|
|
}
|
|
next := map[string]providerAuthFileFingerprint{
|
|
"/tmp/codex/auth.json": {exists: true, modTime: time.UnixMilli(2000), size: 5},
|
|
"/tmp/claude/settings.json": {},
|
|
}
|
|
|
|
changed := changedProviders(entries, baseline, next)
|
|
if len(changed) != 2 {
|
|
t.Fatalf("changedProviders = %v, want both providers", changed)
|
|
}
|
|
}
|
|
|
|
func TestChangedProviderAuthFilesReportsExactPathAndKind(t *testing.T) {
|
|
path := "/tmp/codex/config.toml"
|
|
secondPath := "/tmp/codex/auth.json"
|
|
entries := []ProviderAuthWatchEntry{{
|
|
Provider: agentprovider.Codex,
|
|
Paths: []string{secondPath, path},
|
|
}}
|
|
previous := map[string]providerAuthFileFingerprint{
|
|
path: {exists: true, size: 10},
|
|
secondPath: {exists: true, size: 10},
|
|
}
|
|
next := map[string]providerAuthFileFingerprint{
|
|
path: {exists: true, size: 20},
|
|
secondPath: {exists: true, size: 20},
|
|
}
|
|
changes := changedProviderAuthFiles(entries, previous, next)
|
|
if len(changes) != 2 {
|
|
t.Fatalf("changes = %#v, want both changed files", changes)
|
|
}
|
|
for _, change := range changes {
|
|
if change.Provider != agentprovider.Codex || change.Kind != "metadata_changed" {
|
|
t.Fatalf("change = %#v, want codex metadata_changed", change)
|
|
}
|
|
}
|
|
if changes[0].Path != secondPath || changes[1].Path != path {
|
|
t.Fatalf("changes = %#v, want sorted auth then config paths", changes)
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthWatcherReportsFileRewrites(t *testing.T) {
|
|
dir := t.TempDir()
|
|
authPath := filepath.Join(dir, "auth.json")
|
|
if err := os.WriteFile(authPath, []byte(`{"mode":"chatgpt"}`), 0o600); err != nil {
|
|
t.Fatalf("write baseline auth file: %v", err)
|
|
}
|
|
|
|
changes := make(chan []string, 8)
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{
|
|
{Provider: agentprovider.Codex, Paths: []string{authPath}},
|
|
},
|
|
Interval: 5 * time.Millisecond,
|
|
CoalesceDelay: -1,
|
|
OnChange: func(providers []string) {
|
|
changes <- providers
|
|
},
|
|
}
|
|
watcher.Start()
|
|
defer watcher.Close()
|
|
|
|
// Give the watcher time to record the baseline, then rewrite the file the
|
|
// way credential switchers do (replace content; size change guarantees a
|
|
// fingerprint diff even on coarse mtime filesystems).
|
|
time.Sleep(20 * time.Millisecond)
|
|
if err := os.WriteFile(authPath, []byte(`{"mode":"api-key","key":"sk-test"}`), 0o600); err != nil {
|
|
t.Fatalf("rewrite auth file: %v", err)
|
|
}
|
|
|
|
select {
|
|
case providers := <-changes:
|
|
if len(providers) != 1 || providers[0] != agentprovider.Codex {
|
|
t.Fatalf("OnChange providers = %v, want [codex]", providers)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("watcher did not report the auth file rewrite")
|
|
}
|
|
}
|
|
|
|
func TestDefaultProviderAuthWatcherReportsTuttiAgentLogin(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("TUTTI_AGENT_HOME", filepath.Join(home, "ignored-tutti-agent-home"))
|
|
|
|
var tuttiAgentEntry *ProviderAuthWatchEntry
|
|
for _, entry := range DefaultProviderAuthWatchEntries() {
|
|
if entry.Provider == agentprovider.TuttiAgent {
|
|
entry := entry
|
|
tuttiAgentEntry = &entry
|
|
break
|
|
}
|
|
}
|
|
if tuttiAgentEntry == nil {
|
|
t.Fatal("default auth watcher is missing tutti-agent")
|
|
return
|
|
}
|
|
|
|
changes := make(chan []string, 1)
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{*tuttiAgentEntry},
|
|
Interval: 5 * time.Millisecond,
|
|
CoalesceDelay: -1,
|
|
OnChange: func(providers []string) {
|
|
changes <- providers
|
|
},
|
|
}
|
|
watcher.Start()
|
|
defer watcher.Close()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
authPath := filepath.Join(home, ".tutti-agent", "auth.json")
|
|
if err := os.MkdirAll(filepath.Dir(authPath), 0o700); err != nil {
|
|
t.Fatalf("create tutti-agent home: %v", err)
|
|
}
|
|
if err := os.WriteFile(authPath, []byte(`{"tutti_llm":{"access_token":"test"}}`), 0o600); err != nil {
|
|
t.Fatalf("write tutti-agent auth: %v", err)
|
|
}
|
|
|
|
select {
|
|
case providers := <-changes:
|
|
if len(providers) != 1 || providers[0] != agentprovider.TuttiAgent {
|
|
t.Fatalf("OnChange providers = %v, want [tutti-agent]", providers)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("watcher did not report tutti-agent auth creation")
|
|
}
|
|
}
|
|
|
|
func TestDefaultProviderAuthWatcherReportsCursorAtomicReplacementButIgnoresSameContent(t *testing.T) {
|
|
home := t.TempDir()
|
|
authPath := filepath.Join(home, ".cursor", "cli-config.json")
|
|
if err := os.MkdirAll(filepath.Dir(authPath), 0o700); err != nil {
|
|
t.Fatalf("create Cursor auth directory: %v", err)
|
|
}
|
|
if err := os.WriteFile(authPath, []byte(`{"account":"free"}`), 0o600); err != nil {
|
|
t.Fatalf("write baseline Cursor auth: %v", err)
|
|
}
|
|
|
|
descriptor, ok := providerregistry.Find(agentprovider.Cursor)
|
|
if !ok {
|
|
t.Fatal("Cursor provider descriptor missing")
|
|
}
|
|
cursorEntry, ok := providerAuthWatchEntryFromDescriptor(descriptor, home)
|
|
if !ok || cursorEntry.ContentFingerprint == nil {
|
|
t.Fatal("default auth watcher is missing content-aware Cursor entry")
|
|
}
|
|
|
|
changes := make(chan []string, 2)
|
|
scanRequests := make(chan chan struct{})
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{cursorEntry},
|
|
Interval: time.Hour,
|
|
CoalesceDelay: 250 * time.Millisecond,
|
|
scanRequests: scanRequests,
|
|
OnChange: func(providers []string) {
|
|
changes <- providers
|
|
},
|
|
}
|
|
watcher.Start()
|
|
defer watcher.Close()
|
|
scanProviderAuthWatcher(t, scanRequests)
|
|
|
|
replaceCursorAuthFileWithWindowsFallback(t, scanRequests, authPath, []byte(`{"account":"free"}`))
|
|
select {
|
|
case providers := <-changes:
|
|
t.Fatalf("watcher reported identical Cursor credentials: %v", providers)
|
|
case <-time.After(400 * time.Millisecond):
|
|
}
|
|
|
|
replaceCursorAuthFileWithWindowsFallback(t, scanRequests, authPath, []byte(`{"account":"pro"}`))
|
|
select {
|
|
case providers := <-changes:
|
|
if len(providers) != 1 || providers[0] != agentprovider.Cursor {
|
|
t.Fatalf("OnChange providers = %v, want [cursor]", providers)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("watcher did not report Cursor credential replacement")
|
|
}
|
|
select {
|
|
case providers := <-changes:
|
|
t.Fatalf("watcher reported Cursor credential replacement more than once: %v", providers)
|
|
case <-time.After(500 * time.Millisecond):
|
|
}
|
|
}
|
|
|
|
func replaceCursorAuthFileWithWindowsFallback(
|
|
t *testing.T,
|
|
scanRequests chan chan struct{},
|
|
path string,
|
|
content []byte,
|
|
) {
|
|
t.Helper()
|
|
staged := path + ".next"
|
|
if err := os.WriteFile(staged, content, 0o600); err != nil {
|
|
t.Fatalf("write staged Cursor auth file: %v", err)
|
|
}
|
|
// Windows does not guarantee replace-existing behavior for Rename. Exercise
|
|
// its remove-then-rename fallback and synchronously prove that the watcher
|
|
// observed the transient missing state before restoring the file.
|
|
if err := os.Remove(path); err != nil {
|
|
t.Fatalf("remove previous Cursor auth file: %v", err)
|
|
}
|
|
scanProviderAuthWatcher(t, scanRequests)
|
|
if err := os.Rename(staged, path); err != nil {
|
|
t.Fatalf("rename staged Cursor auth file: %v", err)
|
|
}
|
|
scanProviderAuthWatcher(t, scanRequests)
|
|
}
|
|
|
|
func scanProviderAuthWatcher(t *testing.T, scanRequests chan chan struct{}) {
|
|
t.Helper()
|
|
done := make(chan struct{})
|
|
select {
|
|
case scanRequests <- done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("provider auth watcher did not accept scan request")
|
|
}
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("provider auth watcher did not complete scan request")
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthWatcherStartWithoutCallbackIsInert(_ *testing.T) {
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{
|
|
{Provider: agentprovider.Codex, Paths: []string{"/tmp/does-not-matter"}},
|
|
},
|
|
}
|
|
watcher.Start()
|
|
watcher.Close()
|
|
}
|
|
|
|
func TestProviderAuthWatcherCloseRunsHookOnce(t *testing.T) {
|
|
closeCalls := 0
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{
|
|
{Provider: agentprovider.Codex, Paths: []string{filepath.Join(t.TempDir(), "auth.json")}},
|
|
},
|
|
OnChange: func([]string) {},
|
|
OnClose: func() {
|
|
closeCalls++
|
|
},
|
|
}
|
|
watcher.Start()
|
|
watcher.Close()
|
|
watcher.Close()
|
|
if closeCalls != 1 {
|
|
t.Fatalf("OnClose calls = %d, want one", closeCalls)
|
|
}
|
|
}
|
|
|
|
func TestDefaultProviderAuthWatchEntriesCoverCredentialBackedCatalogs(t *testing.T) {
|
|
home := t.TempDir()
|
|
configDir := filepath.Join(home, "opencode-config")
|
|
dataDir := filepath.Join(home, "opencode-data")
|
|
configPath := filepath.Join(home, "custom-opencode.json")
|
|
codexHome := filepath.Join(home, "custom-codex")
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("CODEX_HOME", codexHome)
|
|
t.Setenv("TUTTI_AGENT_HOME", filepath.Join(home, "ignored-tutti-agent-home"))
|
|
t.Setenv("OPENCODE_CONFIG", configPath)
|
|
t.Setenv("OPENCODE_CONFIG_DIR", configDir)
|
|
t.Setenv("XDG_DATA_HOME", dataDir)
|
|
|
|
entries := DefaultProviderAuthWatchEntries()
|
|
byProvider := make(map[string][]string, len(entries))
|
|
for _, entry := range entries {
|
|
byProvider[entry.Provider] = entry.Paths
|
|
}
|
|
codexPaths := byProvider[agentprovider.Codex]
|
|
for _, want := range []string{
|
|
filepath.Join(codexHome, "auth.json"),
|
|
filepath.Join(codexHome, "config.toml"),
|
|
} {
|
|
if !containsString(codexPaths, want) {
|
|
t.Fatalf("codex paths = %v, want %q", codexPaths, want)
|
|
}
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.Provider == agentprovider.Codex && entry.ContentFingerprint == nil {
|
|
t.Fatal("codex auth watch entry must fingerprint full file content")
|
|
}
|
|
}
|
|
if len(byProvider[agentprovider.ClaudeCode]) == 0 {
|
|
t.Fatal("expected claude-code watch paths")
|
|
}
|
|
if !containsString(byProvider[agentprovider.ClaudeCode], filepath.Join(home, ".claude", ".credentials.json")) {
|
|
t.Fatalf("claude-code watch paths = %v, want credentials file", byProvider[agentprovider.ClaudeCode])
|
|
}
|
|
opencodePaths := byProvider[agentprovider.OpenCode]
|
|
if len(opencodePaths) == 0 {
|
|
t.Fatal("expected opencode watch paths")
|
|
}
|
|
for _, want := range []string{
|
|
configPath,
|
|
filepath.Join(configDir, "opencode.json"),
|
|
filepath.Join(configDir, "config.json"),
|
|
filepath.Join(dataDir, "opencode", "auth.json"),
|
|
} {
|
|
if !containsString(opencodePaths, want) {
|
|
t.Fatalf("opencode paths = %v, want %q", opencodePaths, want)
|
|
}
|
|
}
|
|
tuttiAgentPaths := byProvider[agentprovider.TuttiAgent]
|
|
if !containsString(tuttiAgentPaths, filepath.Join(home, ".tutti-agent", "auth.json")) {
|
|
t.Fatalf("tutti-agent paths = %v, want auth file", tuttiAgentPaths)
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthWatchEntryUsesDescriptorMetadata(t *testing.T) {
|
|
descriptor, ok := providerregistry.Find(agentprovider.Codex)
|
|
if !ok {
|
|
t.Fatal("codex descriptor missing")
|
|
}
|
|
root := t.TempDir()
|
|
descriptor.Identity.ID = "poison-provider"
|
|
descriptor.Status.AuthWatch = providerregistry.AuthWatchDescriptor{
|
|
Sources: []providerregistry.AuthWatchSourceDescriptor{
|
|
{
|
|
RootCandidates: []providerregistry.AuthWatchRootCandidateDescriptor{
|
|
{EnvVar: "POISON_PROVIDER_HOME"},
|
|
},
|
|
DefaultRoot: "~/unused",
|
|
Paths: []string{"credential.json", "settings.toml"},
|
|
},
|
|
},
|
|
}
|
|
t.Setenv("POISON_PROVIDER_HOME", root)
|
|
entry, ok := providerAuthWatchEntryFromDescriptor(descriptor, "/unused-home")
|
|
if !ok {
|
|
t.Fatal("providerAuthWatchEntryFromDescriptor() ok = false")
|
|
}
|
|
if entry.Provider != "poison-provider" || len(entry.Paths) != 2 {
|
|
t.Fatalf("entry = %#v", entry)
|
|
}
|
|
if entry.Paths[0] != filepath.Join(root, "credential.json") || entry.Paths[1] != filepath.Join(root, "settings.toml") {
|
|
t.Fatalf("entry paths = %#v", entry.Paths)
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthWatcherIgnoresNonAuthClaudeStateChurn(t *testing.T) {
|
|
dir := t.TempDir()
|
|
statePath := filepath.Join(dir, ".claude.json")
|
|
writeState := func(content string) {
|
|
t.Helper()
|
|
tempPath := filepath.Join(dir, ".claude.json.next")
|
|
if err := os.WriteFile(tempPath, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write staged claude state file: %v", err)
|
|
}
|
|
if err := os.Rename(tempPath, statePath); err != nil {
|
|
t.Fatalf("write claude state file: %v", err)
|
|
}
|
|
}
|
|
writeState(`{"oauthAccount":{"emailAddress":"a@b.c"},"history":["one"]}`)
|
|
|
|
changes := make(chan []string, 8)
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{
|
|
{
|
|
Provider: agentprovider.ClaudeCode,
|
|
Paths: []string{statePath},
|
|
ContentFingerprint: claudeProviderAuthContentFingerprint(statePath),
|
|
},
|
|
},
|
|
Interval: 5 * time.Millisecond,
|
|
CoalesceDelay: -1,
|
|
OnChange: func(providers []string) {
|
|
changes <- providers
|
|
},
|
|
}
|
|
watcher.Start()
|
|
defer watcher.Close()
|
|
|
|
// Non-auth churn (history grows, mtime/size change) must stay quiet.
|
|
time.Sleep(20 * time.Millisecond)
|
|
writeState(`{"oauthAccount":{"emailAddress":"a@b.c"},"history":["one","two","three"]}`)
|
|
select {
|
|
case providers := <-changes:
|
|
t.Fatalf("watcher reported %v for non-auth state churn", providers)
|
|
case <-time.After(100 * time.Millisecond):
|
|
}
|
|
|
|
// A credential switch (oauthAccount changes) must fire.
|
|
writeState(`{"oauthAccount":{"emailAddress":"other@b.c"},"history":["one","two","three"]}`)
|
|
select {
|
|
case providers := <-changes:
|
|
if len(providers) != 1 || providers[0] != agentprovider.ClaudeCode {
|
|
t.Fatalf("OnChange providers = %v, want [claude-code]", providers)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("watcher did not report the credential switch")
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthWatcherReportsOpenCodeConfigRewrites(t *testing.T) {
|
|
dir := t.TempDir()
|
|
configPath := filepath.Join(dir, "opencode.json")
|
|
if err := os.WriteFile(configPath, []byte(`{"model":"openai/gpt-5"}`), 0o600); err != nil {
|
|
t.Fatalf("write baseline config file: %v", err)
|
|
}
|
|
|
|
changes := make(chan []string, 8)
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{
|
|
{
|
|
Provider: agentprovider.OpenCode,
|
|
Paths: []string{configPath},
|
|
ContentFingerprint: hashProviderAuthFileContent,
|
|
},
|
|
},
|
|
Interval: 5 * time.Millisecond,
|
|
CoalesceDelay: -1,
|
|
OnChange: func(providers []string) {
|
|
changes <- providers
|
|
},
|
|
}
|
|
watcher.Start()
|
|
defer watcher.Close()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
if err := os.WriteFile(configPath, []byte(`{"model":"openai/gpt-5.3-codex-spark"}`), 0o600); err != nil {
|
|
t.Fatalf("rewrite config file: %v", err)
|
|
}
|
|
|
|
select {
|
|
case providers := <-changes:
|
|
if len(providers) != 1 || providers[0] != agentprovider.OpenCode {
|
|
t.Fatalf("OnChange providers = %v, want [opencode]", providers)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("watcher did not report the opencode config rewrite")
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthFileChangedPrefersContentKey(t *testing.T) {
|
|
previous := providerAuthFileFingerprint{
|
|
exists: true, modTime: time.UnixMilli(1000), size: 10, contentKey: "same",
|
|
}
|
|
next := providerAuthFileFingerprint{
|
|
exists: true, modTime: time.UnixMilli(2000), size: 99, contentKey: "same",
|
|
}
|
|
if providerAuthFileChanged(previous, next) {
|
|
t.Fatal("identical content keys must suppress mtime/size churn")
|
|
}
|
|
next.contentKey = "different"
|
|
if !providerAuthFileChanged(previous, next) {
|
|
t.Fatal("content key change must report")
|
|
}
|
|
// Missing content keys (no fingerprinter or read failure) keep the
|
|
// conservative mtime/size semantics.
|
|
previous.contentKey = ""
|
|
next.contentKey = ""
|
|
if !providerAuthFileChanged(previous, next) {
|
|
t.Fatal("stat change without content keys must report")
|
|
}
|
|
}
|
|
|
|
func TestProviderAuthWatcherCoalescesProviderChanges(t *testing.T) {
|
|
dir := t.TempDir()
|
|
codexAuthPath := filepath.Join(dir, "codex-auth.json")
|
|
claudeAuthPath := filepath.Join(dir, "claude-auth.json")
|
|
if err := os.WriteFile(codexAuthPath, []byte(`{"mode":"chatgpt"}`), 0o600); err != nil {
|
|
t.Fatalf("write baseline codex auth file: %v", err)
|
|
}
|
|
if err := os.WriteFile(claudeAuthPath, []byte(`{"mode":"claude"}`), 0o600); err != nil {
|
|
t.Fatalf("write baseline claude auth file: %v", err)
|
|
}
|
|
|
|
changes := make(chan []string, 8)
|
|
watcher := &ProviderAuthWatcher{
|
|
Entries: []ProviderAuthWatchEntry{
|
|
{Provider: agentprovider.Codex, Paths: []string{codexAuthPath}},
|
|
{Provider: agentprovider.ClaudeCode, Paths: []string{claudeAuthPath}},
|
|
},
|
|
Interval: 5 * time.Millisecond,
|
|
CoalesceDelay: 50 * time.Millisecond,
|
|
OnChange: func(providers []string) {
|
|
changes <- providers
|
|
},
|
|
}
|
|
watcher.Start()
|
|
defer watcher.Close()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
if err := os.WriteFile(codexAuthPath, []byte(`{"mode":"api-key"}`), 0o600); err != nil {
|
|
t.Fatalf("rewrite codex auth file: %v", err)
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
if err := os.WriteFile(claudeAuthPath, []byte(`{"mode":"oauth"}`), 0o600); err != nil {
|
|
t.Fatalf("rewrite claude auth file: %v", err)
|
|
}
|
|
|
|
select {
|
|
case providers := <-changes:
|
|
if len(providers) != 2 ||
|
|
providers[0] != agentprovider.Codex ||
|
|
providers[1] != agentprovider.ClaudeCode {
|
|
t.Fatalf("OnChange providers = %v, want [codex claude-code]", providers)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("watcher did not report the coalesced provider changes")
|
|
}
|
|
select {
|
|
case providers := <-changes:
|
|
t.Fatalf("watcher reported a second coalesced callback: %v", providers)
|
|
case <-time.After(100 * time.Millisecond):
|
|
}
|
|
}
|
|
|
|
func containsString(values []string, want string) bool {
|
|
for _, value := range values {
|
|
if value == want {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestJSONSubsetFingerprintTracksOnlySelectedKeys(t *testing.T) {
|
|
base, ok := jsonSubsetFingerprint(
|
|
[]byte(`{"oauthAccount":{"a":1},"history":[1,2,3]}`),
|
|
claudeAuthRelevantStateKeys,
|
|
)
|
|
if !ok {
|
|
t.Fatal("expected fingerprint for JSON object")
|
|
}
|
|
churned, ok := jsonSubsetFingerprint(
|
|
[]byte(`{"oauthAccount":{"a":1},"history":[1,2,3,4,5]}`),
|
|
claudeAuthRelevantStateKeys,
|
|
)
|
|
if !ok || churned != base {
|
|
t.Fatalf("non-auth churn changed fingerprint: %q vs %q", churned, base)
|
|
}
|
|
switched, ok := jsonSubsetFingerprint(
|
|
[]byte(`{"oauthAccount":{"a":2},"history":[1,2,3]}`),
|
|
claudeAuthRelevantStateKeys,
|
|
)
|
|
if !ok || switched == base {
|
|
t.Fatal("auth change did not change fingerprint")
|
|
}
|
|
if _, ok := jsonSubsetFingerprint([]byte(`not-json`), claudeAuthRelevantStateKeys); ok {
|
|
t.Fatal("expected failure for non-JSON payload")
|
|
}
|
|
}
|
|
|
|
func cloneFingerprints(fingerprints map[string]providerAuthFileFingerprint) map[string]providerAuthFileFingerprint {
|
|
cloned := make(map[string]providerAuthFileFingerprint, len(fingerprints))
|
|
for key, value := range fingerprints {
|
|
cloned[key] = value
|
|
}
|
|
return cloned
|
|
}
|