| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestProjectStateCollisionAssignmentPreservesLegacyDirectory(t *testing.T) { |
| 11 | state := t.TempDir() |
| 12 | root := t.TempDir() |
| 13 | first := filepath.Join(root, "front-end", "app") |
| 14 | second := filepath.Join(root, "front", "end-app") |
| 15 | if WorkspaceSlug(first) != WorkspaceSlug(second) { |
| 16 | t.Fatalf("fixture paths do not collide: %q vs %q", first, second) |
| 17 | } |
| 18 | legacy := filepath.Join(state, "projects", WorkspaceSlug(first)) |
| 19 | if got := ProjectStateDir(state, first); got != legacy { |
| 20 | t.Fatalf("unassigned first project = %q, want %q", got, legacy) |
| 21 | } |
| 22 | if got := ProjectStateDir(state, second); got != legacy { |
| 23 | t.Fatalf("unassigned second project = %q, want %q", got, legacy) |
| 24 | } |
| 25 | if _, err := os.Stat(filepath.Join(state, "projects")); !os.IsNotExist(err) { |
| 26 | t.Fatalf("read-only path resolution created state: %v", err) |
| 27 | } |
| 28 | if err := os.MkdirAll(filepath.Join(legacy, "sessions"), 0o700); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | oldSession := filepath.Join(legacy, "sessions", "existing.jsonl") |
| 32 | if err := os.WriteFile(oldSession, []byte("existing session\n"), 0o600); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if err := AssignProjectStateCollision(state, second); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := AssignProjectStateCollision(state, second); err != nil { |
| 39 | t.Fatalf("repeat assignment: %v", err) |
| 40 | } |
| 41 | newDir := ProjectStateDir(state, second) |
| 42 | if newDir == legacy || !strings.HasPrefix(filepath.Base(newDir), "@") { |
| 43 | t.Fatalf("colliding project directory = %q, want distinct assigned path", newDir) |
| 44 | } |
| 45 | if got := ProjectStateDir(state, first); got != legacy { |
| 46 | t.Fatalf("existing project moved to %q from %q", got, legacy) |
| 47 | } |
| 48 | if got, err := os.ReadFile(oldSession); err != nil || string(got) != "existing session\n" { |
| 49 | t.Fatalf("legacy session changed: %q, %v", got, err) |
| 50 | } |
| 51 | if _, err := os.Stat(filepath.Join(newDir, "sessions", "existing.jsonl")); !os.IsNotExist(err) { |
| 52 | t.Fatalf("new project sees old session: %v", err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestProjectStateNonCollidingRootKeepsHistoricalPath(t *testing.T) { |
| 57 | state := t.TempDir() |
| 58 | root := filepath.Join(t.TempDir(), "backend") |
| 59 | want := filepath.Join(state, "projects", WorkspaceSlug(root)) |
| 60 | if got := ProjectStateDir(state, root); got != want { |
| 61 | t.Fatalf("non-colliding project = %q, want %q", got, want) |
| 62 | } |
| 63 | } |
| 64 |