| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "runtime" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestCanonicalSessionPathMatchesLeaseRegistryKey(t *testing.T) { |
| 12 | path := filepath.Join(t.TempDir(), "Mixed-Case", "20260705-Test.jsonl") |
| 13 | identity, err := resolveSessionPathIdentity(path) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | if got, want := CanonicalSessionPath(path), identity.Key; got != want { |
| 18 | t.Fatalf("CanonicalSessionPath(%q) = %q, want identity key %q", path, got, want) |
| 19 | } |
| 20 | if got, want := canonicalSessionSavePath(path), identity.PhysicalPath; got != want { |
| 21 | t.Fatalf("canonicalSessionSavePath(%q) = %q, want access path %q", path, got, want) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestCanonicalSessionPathResolvesDirectorySymlink(t *testing.T) { |
| 26 | root := t.TempDir() |
| 27 | realDir := filepath.Join(root, "real") |
| 28 | if err := os.MkdirAll(realDir, 0o755); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | aliasDir := filepath.Join(root, "alias") |
| 32 | if err := os.Symlink(realDir, aliasDir); err != nil { |
| 33 | t.Skipf("symlink unavailable: %v", err) |
| 34 | } |
| 35 | realPath := filepath.Join(realDir, "session.jsonl") |
| 36 | aliasPath := filepath.Join(aliasDir, "session.jsonl") |
| 37 | if got, want := CanonicalSessionPath(aliasPath), CanonicalSessionPath(realPath); got != want { |
| 38 | t.Fatalf("directory alias split session identity: %q != %q", got, want) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestCanonicalSessionPathResolvesNearestExistingAncestor(t *testing.T) { |
| 43 | root := t.TempDir() |
| 44 | realDir := filepath.Join(root, "real") |
| 45 | if err := os.MkdirAll(realDir, 0o755); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | aliasDir := filepath.Join(root, "alias") |
| 49 | if err := os.Symlink(realDir, aliasDir); err != nil { |
| 50 | t.Skipf("symlink unavailable: %v", err) |
| 51 | } |
| 52 | realPath := filepath.Join(realDir, "not-created", "nested", "session.jsonl") |
| 53 | aliasPath := filepath.Join(aliasDir, "not-created", "nested", "session.jsonl") |
| 54 | if got, want := CanonicalSessionPath(aliasPath), CanonicalSessionPath(realPath); got != want { |
| 55 | t.Fatalf("nearest existing ancestor alias split session identity: %q != %q", got, want) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestCanonicalSessionPathIdempotentAndEmptySafe(t *testing.T) { |
| 60 | if got := CanonicalSessionPath(""); got != "" { |
| 61 | t.Fatalf("empty path resolved to %q; must stay empty", got) |
| 62 | } |
| 63 | if got := CanonicalSessionPath(" "); got != "" { |
| 64 | t.Fatalf("blank path resolved to %q; must stay empty", got) |
| 65 | } |
| 66 | path := filepath.Join(t.TempDir(), "A", "b.jsonl") |
| 67 | key := CanonicalSessionPath(path) |
| 68 | if again := CanonicalSessionPath(key); again != key { |
| 69 | t.Fatalf("not idempotent: %q -> %q", key, again) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestCanonicalSessionPathFoldsCaseOnWindows(t *testing.T) { |
| 74 | if runtime.GOOS != "windows" { |
| 75 | t.Skip("case folding is Windows-only") |
| 76 | } |
| 77 | path := filepath.Join(t.TempDir(), "Sessions", "20260705-Test.jsonl") |
| 78 | if CanonicalSessionPath(path) != CanonicalSessionPath(strings.ToUpper(path)) { |
| 79 | t.Fatal("case variants of one file produced distinct keys") |
| 80 | } |
| 81 | } |
| 82 |