|
| 1 | +//go:build acceptance_a |
| 2 | + |
| 3 | +package acceptance_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + helpers "github.com/gastownhall/gascity/test/acceptance/helpers" |
| 13 | +) |
| 14 | + |
| 15 | +type namedSessionListEntry struct { |
| 16 | + Template string `json:"Template"` |
| 17 | + SessionName string `json:"SessionName"` |
| 18 | +} |
| 19 | + |
| 20 | +func TestImportedNamedSessionsUseSafeRuntimeNames(t *testing.T) { |
| 21 | + c := helpers.NewCity(t, testEnv) |
| 22 | + c.Init("claude") |
| 23 | + |
| 24 | + rigDir := filepath.Join(c.Dir, "repo") |
| 25 | + mustWriteTestFile(t, filepath.Join(c.Dir, "pack.toml"), ` |
| 26 | +[pack] |
| 27 | +name = "import-regression" |
| 28 | +schema = 2 |
| 29 | +
|
| 30 | +[imports.gs] |
| 31 | +source = "./assets/sidecar" |
| 32 | +`) |
| 33 | + mustWriteTestFile(t, filepath.Join(c.Dir, "city.toml"), ` |
| 34 | +[workspace] |
| 35 | +name = "import-regression" |
| 36 | +start_command = "sleep 300" |
| 37 | +
|
| 38 | +[[rigs]] |
| 39 | +name = "repo" |
| 40 | +path = "./repo" |
| 41 | +
|
| 42 | +[rigs.imports.gs] |
| 43 | +source = "./assets/sidecar" |
| 44 | +`) |
| 45 | + mustWriteTestFile(t, filepath.Join(c.Dir, "assets", "sidecar", "pack.toml"), ` |
| 46 | +[pack] |
| 47 | +name = "sidecar" |
| 48 | +schema = 2 |
| 49 | +
|
| 50 | +[[named_session]] |
| 51 | +template = "captain" |
| 52 | +scope = "city" |
| 53 | +mode = "always" |
| 54 | +
|
| 55 | +[[named_session]] |
| 56 | +template = "watcher" |
| 57 | +scope = "rig" |
| 58 | +mode = "always" |
| 59 | +`) |
| 60 | + mustWriteTestFile(t, filepath.Join(c.Dir, "assets", "sidecar", "agents", "captain", "agent.toml"), "scope = \"city\"\n") |
| 61 | + mustWriteTestFile(t, filepath.Join(c.Dir, "assets", "sidecar", "agents", "captain", "prompt.md"), "You are the imported captain.\n") |
| 62 | + mustWriteTestFile(t, filepath.Join(c.Dir, "assets", "sidecar", "agents", "watcher", "agent.toml"), "scope = \"rig\"\n") |
| 63 | + mustWriteTestFile(t, filepath.Join(c.Dir, "assets", "sidecar", "agents", "watcher", "prompt.md"), "You are the imported watcher.\n") |
| 64 | + if err := os.MkdirAll(rigDir, 0o755); err != nil { |
| 65 | + t.Fatalf("creating rig dir: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + if out, err := c.GC("unregister", c.Dir); err != nil { |
| 69 | + t.Fatalf("gc unregister: %v\n%s", err, out) |
| 70 | + } |
| 71 | + |
| 72 | + c.StartForeground() |
| 73 | + |
| 74 | + var sessions []namedSessionListEntry |
| 75 | + deadline := time.Now().Add(20 * time.Second) |
| 76 | + for time.Now().Before(deadline) { |
| 77 | + out, err := c.GC("session", "list", "--json") |
| 78 | + if err == nil { |
| 79 | + if unmarshalErr := json.Unmarshal([]byte(out), &sessions); unmarshalErr == nil { |
| 80 | + if hasNamedSession(sessions, "gs.captain", "gs__captain") && |
| 81 | + hasNamedSession(sessions, "repo/gs.watcher", "repo--gs__watcher") { |
| 82 | + return |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + time.Sleep(500 * time.Millisecond) |
| 87 | + } |
| 88 | + |
| 89 | + out, err := c.GC("session", "list", "--json") |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("gc session list --json: %v\n%s", err, out) |
| 92 | + } |
| 93 | + t.Fatalf("imported named sessions never reached safe runtime names:\n%s", out) |
| 94 | +} |
| 95 | + |
| 96 | +func hasNamedSession(sessions []namedSessionListEntry, template, sessionName string) bool { |
| 97 | + for _, s := range sessions { |
| 98 | + if s.Template == template && s.SessionName == sessionName { |
| 99 | + return true |
| 100 | + } |
| 101 | + } |
| 102 | + return false |
| 103 | +} |
| 104 | + |
| 105 | +func mustWriteTestFile(t *testing.T, path, contents string) { |
| 106 | + t.Helper() |
| 107 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 108 | + t.Fatalf("creating %s: %v", filepath.Dir(path), err) |
| 109 | + } |
| 110 | + if err := os.WriteFile(path, []byte(contents), 0o644); err != nil { |
| 111 | + t.Fatalf("writing %s: %v", path, err) |
| 112 | + } |
| 113 | +} |
0 commit comments