|
6 | 6 | "strings" |
7 | 7 | "testing" |
8 | 8 |
|
| 9 | + "github.com/steveyegge/gastown/internal/config" |
9 | 10 | "github.com/steveyegge/gastown/internal/hooks" |
10 | 11 | ) |
11 | 12 |
|
@@ -297,3 +298,141 @@ func TestRunHooksSyncFailsClosedOnIntegrityViolation(t *testing.T) { |
297 | 298 | t.Fatalf("expected fail-closed error, got: %v", err) |
298 | 299 | } |
299 | 300 | } |
| 301 | + |
| 302 | +func TestRunHooksSyncNonClaudeAgent(t *testing.T) { |
| 303 | + tmpDir := t.TempDir() |
| 304 | + t.Setenv("HOME", tmpDir) |
| 305 | + |
| 306 | + townRoot := filepath.Join(tmpDir, "town") |
| 307 | + |
| 308 | + // Scaffold workspace: mayor, deacon, and a rig with a crew worktree |
| 309 | + if err := os.MkdirAll(filepath.Join(townRoot, "mayor"), 0755); err != nil { |
| 310 | + t.Fatal(err) |
| 311 | + } |
| 312 | + if err := os.MkdirAll(filepath.Join(townRoot, "deacon"), 0755); err != nil { |
| 313 | + t.Fatal(err) |
| 314 | + } |
| 315 | + if err := os.MkdirAll(filepath.Join(townRoot, "myrig", "crew", "alice"), 0755); err != nil { |
| 316 | + t.Fatal(err) |
| 317 | + } |
| 318 | + |
| 319 | + // Workspace marker |
| 320 | + if err := os.WriteFile( |
| 321 | + filepath.Join(townRoot, "mayor", "town.json"), |
| 322 | + []byte(`{"type":"town","version":1,"name":"test"}`), |
| 323 | + 0644, |
| 324 | + ); err != nil { |
| 325 | + t.Fatal(err) |
| 326 | + } |
| 327 | + |
| 328 | + // Configure crew role to use opencode |
| 329 | + townSettings := config.NewTownSettings() |
| 330 | + townSettings.RoleAgents = map[string]string{"crew": "opencode"} |
| 331 | + settingsDir := filepath.Join(townRoot, "settings") |
| 332 | + if err := os.MkdirAll(settingsDir, 0755); err != nil { |
| 333 | + t.Fatal(err) |
| 334 | + } |
| 335 | + if err := config.SaveTownSettings(config.TownSettingsPath(townRoot), townSettings); err != nil { |
| 336 | + t.Fatal(err) |
| 337 | + } |
| 338 | + |
| 339 | + // Base hooks config (needed for Claude targets to not error) |
| 340 | + base := &hooks.HooksConfig{ |
| 341 | + SessionStart: []hooks.HookEntry{ |
| 342 | + {Matcher: "", Hooks: []hooks.Hook{{Type: "command", Command: "echo test"}}}, |
| 343 | + }, |
| 344 | + } |
| 345 | + if err := hooks.SaveBase(base); err != nil { |
| 346 | + t.Fatalf("SaveBase failed: %v", err) |
| 347 | + } |
| 348 | + |
| 349 | + cwd, err := os.Getwd() |
| 350 | + if err != nil { |
| 351 | + t.Fatal(err) |
| 352 | + } |
| 353 | + defer func() { _ = os.Chdir(cwd) }() |
| 354 | + if err := os.Chdir(townRoot); err != nil { |
| 355 | + t.Fatal(err) |
| 356 | + } |
| 357 | + |
| 358 | + hooksSyncDryRun = false |
| 359 | + if err := runHooksSync(nil, nil); err != nil { |
| 360 | + t.Fatalf("runHooksSync failed: %v", err) |
| 361 | + } |
| 362 | + |
| 363 | + // Verify OpenCode plugin was synced to the worktree (not the parent) |
| 364 | + pluginPath := filepath.Join(townRoot, "myrig", "crew", "alice", ".opencode", "plugins", "gastown.js") |
| 365 | + if _, err := os.Stat(pluginPath); os.IsNotExist(err) { |
| 366 | + t.Error("opencode plugin not created in worktree alice") |
| 367 | + } |
| 368 | + |
| 369 | + // Verify it was NOT created in the parent (crew/) since useSettingsDir=false |
| 370 | + parentPlugin := filepath.Join(townRoot, "myrig", "crew", ".opencode", "plugins", "gastown.js") |
| 371 | + if _, err := os.Stat(parentPlugin); !os.IsNotExist(err) { |
| 372 | + t.Error("opencode plugin should not be in the parent crew/ directory") |
| 373 | + } |
| 374 | +} |
| 375 | + |
| 376 | +func TestRunHooksSyncNonClaudeAgentDryRun(t *testing.T) { |
| 377 | + tmpDir := t.TempDir() |
| 378 | + t.Setenv("HOME", tmpDir) |
| 379 | + |
| 380 | + townRoot := filepath.Join(tmpDir, "town") |
| 381 | + |
| 382 | + if err := os.MkdirAll(filepath.Join(townRoot, "mayor"), 0755); err != nil { |
| 383 | + t.Fatal(err) |
| 384 | + } |
| 385 | + if err := os.MkdirAll(filepath.Join(townRoot, "deacon"), 0755); err != nil { |
| 386 | + t.Fatal(err) |
| 387 | + } |
| 388 | + if err := os.MkdirAll(filepath.Join(townRoot, "myrig", "crew", "alice"), 0755); err != nil { |
| 389 | + t.Fatal(err) |
| 390 | + } |
| 391 | + |
| 392 | + if err := os.WriteFile( |
| 393 | + filepath.Join(townRoot, "mayor", "town.json"), |
| 394 | + []byte(`{"type":"town","version":1,"name":"test"}`), |
| 395 | + 0644, |
| 396 | + ); err != nil { |
| 397 | + t.Fatal(err) |
| 398 | + } |
| 399 | + |
| 400 | + townSettings := config.NewTownSettings() |
| 401 | + townSettings.RoleAgents = map[string]string{"crew": "opencode"} |
| 402 | + if err := os.MkdirAll(filepath.Join(townRoot, "settings"), 0755); err != nil { |
| 403 | + t.Fatal(err) |
| 404 | + } |
| 405 | + if err := config.SaveTownSettings(config.TownSettingsPath(townRoot), townSettings); err != nil { |
| 406 | + t.Fatal(err) |
| 407 | + } |
| 408 | + |
| 409 | + base := &hooks.HooksConfig{ |
| 410 | + SessionStart: []hooks.HookEntry{ |
| 411 | + {Matcher: "", Hooks: []hooks.Hook{{Type: "command", Command: "echo test"}}}, |
| 412 | + }, |
| 413 | + } |
| 414 | + if err := hooks.SaveBase(base); err != nil { |
| 415 | + t.Fatalf("SaveBase failed: %v", err) |
| 416 | + } |
| 417 | + |
| 418 | + cwd, err := os.Getwd() |
| 419 | + if err != nil { |
| 420 | + t.Fatal(err) |
| 421 | + } |
| 422 | + defer func() { _ = os.Chdir(cwd) }() |
| 423 | + if err := os.Chdir(townRoot); err != nil { |
| 424 | + t.Fatal(err) |
| 425 | + } |
| 426 | + |
| 427 | + hooksSyncDryRun = true |
| 428 | + defer func() { hooksSyncDryRun = false }() |
| 429 | + if err := runHooksSync(nil, nil); err != nil { |
| 430 | + t.Fatalf("runHooksSync dry-run failed: %v", err) |
| 431 | + } |
| 432 | + |
| 433 | + // Dry run should NOT create the file |
| 434 | + pluginPath := filepath.Join(townRoot, "myrig", "crew", "alice", ".opencode", "plugins", "gastown.js") |
| 435 | + if _, err := os.Stat(pluginPath); !os.IsNotExist(err) { |
| 436 | + t.Error("dry-run should not create opencode plugin file") |
| 437 | + } |
| 438 | +} |
0 commit comments