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