Skip to content

Commit bc10927

Browse files
steveyeggehtristan
authored andcommitted
fix: repair pre-existing reap test bugs exposed by PR gastownhall#3294 merge
Three test infrastructure issues in polecat_health_test.go: - writeFakeTestBD: return [] for 'list' commands so hasAssignedOpenWork doesn't false-positive on agent bead JSON - writeFakeTmuxWithAgent: use $* glob matching instead of $1, since tmux.run() prepends -u flag before the subcommand - TestReapIdlePolecat_ReapsIdleNoHook: write heartbeat to .runtime/heartbeats/ (not heartbeats/) and use NewTmuxWithSocket to avoid socket mismatch Also register 'myr' test prefix in both reap tests so session name resolves correctly (PrefixFor fallback returns 'gt', not 'myr'). (gt-2fr)
1 parent 82393d2 commit bc10927

1 file changed

Lines changed: 30 additions & 9 deletions

File tree

internal/daemon/polecat_health_test.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/steveyegge/gastown/internal/polecat"
15+
"github.com/steveyegge/gastown/internal/session"
1516
"github.com/steveyegge/gastown/internal/tmux"
1617
)
1718

@@ -40,7 +41,9 @@ func writeFakeTestBD(t *testing.T, dir, descState, dbState, hookBead, updatedAt
4041
// JSON matches the structure that getAgentBeadInfo expects from bd show --json
4142
bdJSON := fmt.Sprintf(`[{"id":"gt-myr-polecat-mycat","issue_type":"agent","labels":["gt:agent"],"description":"%s","hook_bead":"%s","agent_state":"%s","updated_at":"%s"}]`,
4243
desc, hookBead, dbState, updatedAt)
43-
script := "#!/bin/sh\necho '" + bdJSON + "'\n"
44+
// Return agent bead JSON for "show", empty array for "list" (so
45+
// hasAssignedOpenWork doesn't false-positive on the agent bead).
46+
script := "#!/bin/sh\nif [ \"$1\" = \"list\" ]; then echo '[]'; exit 0; fi\necho '" + bdJSON + "'\n"
4447
path := filepath.Join(dir, "bd")
4548
if err := os.WriteFile(path, []byte(script), 0755); err != nil {
4649
t.Fatalf("writing fake bd: %v", err)
@@ -58,6 +61,7 @@ func writeFakeBDWithHookBead(t *testing.T, dir, agentState, hookBeadID, hookBead
5861
agentState, hookBeadID, agentState, updatedAt)
5962
hookJSON := fmt.Sprintf(`[{"id":"%s","status":"%s"}]`, hookBeadID, hookBeadStatus)
6063
script := fmt.Sprintf("#!/bin/sh\n"+
64+
"if [ \"$1\" = \"list\" ]; then echo '[]'; exit 0; fi\n"+
6165
"case \"$2\" in\n"+
6266
" gt-myr-polecat-mycat) echo '%s';;\n"+
6367
" %s) echo '%s';;\n"+
@@ -384,11 +388,13 @@ func TestCheckPolecatHealth_SkipsNukedPolecat(t *testing.T) {
384388
// given paneCommand (e.g., "claude" or "codex") so IsAgentRunning returns true.
385389
func writeFakeTmuxWithAgent(t *testing.T, dir, paneCommand string) {
386390
t.Helper()
391+
// Use $* glob matching (not $1) because tmux.run() prepends -u (and
392+
// optionally -L <socket>) before the subcommand.
387393
script := fmt.Sprintf("#!/bin/sh\n"+
388-
"case \"$1\" in\n"+
389-
" has-session) exit 0;;\n"+
390-
" display-message) echo '%s';;\n"+
391-
" kill-session) exit 0;;\n"+
394+
"case \"$*\" in\n"+
395+
" *has-session*) exit 0;;\n"+
396+
" *display-message*) echo '%s';;\n"+
397+
" *kill-session*) exit 0;;\n"+
392398
" *) exit 1;;\n"+
393399
"esac\n", paneCommand)
394400
if err := os.WriteFile(filepath.Join(dir, "tmux"), []byte(script), 0755); err != nil {
@@ -413,6 +419,13 @@ func TestReapIdlePolecat_SkipsActiveAgent(t *testing.T) {
413419
if runtime.GOOS == "windows" {
414420
t.Skip("test uses Unix shell script mocks for tmux and bd")
415421
}
422+
// Register "myr" prefix so session name resolves to "myr-mycat"
423+
old := session.DefaultRegistry()
424+
reg := session.NewPrefixRegistry()
425+
reg.Register("myr", "myr")
426+
session.SetDefaultRegistry(reg)
427+
defer session.SetDefaultRegistry(old)
428+
416429
binDir := t.TempDir()
417430
// Fake tmux: session alive, agent (codex) running in pane
418431
writeFakeTmuxWithAgent(t, binDir, "codex")
@@ -457,6 +470,13 @@ func TestReapIdlePolecat_ReapsIdleNoHook(t *testing.T) {
457470
if runtime.GOOS == "windows" {
458471
t.Skip("test uses Unix shell script mocks for tmux and bd")
459472
}
473+
// Register "myr" prefix so session name resolves to "myr-mycat"
474+
old := session.DefaultRegistry()
475+
reg := session.NewPrefixRegistry()
476+
reg.Register("myr", "myr")
477+
session.SetDefaultRegistry(reg)
478+
defer session.SetDefaultRegistry(old)
479+
460480
binDir := t.TempDir()
461481
// Fake tmux: session alive, but only a shell running (no agent)
462482
writeFakeTmuxIdleSession(t, binDir)
@@ -471,13 +491,14 @@ func TestReapIdlePolecat_ReapsIdleNoHook(t *testing.T) {
471491
d := &Daemon{
472492
config: &Config{TownRoot: townRoot},
473493
logger: log.New(&logBuf, "", 0),
474-
tmux: tmux.NewTmux(),
494+
tmux: tmux.NewTmuxWithSocket(""),
475495
bdPath: bdPath,
476496
}
477497

478-
// Write a stale heartbeat
479-
hbPath := filepath.Join(townRoot, "heartbeats", "myr-mycat.json")
480-
_ = os.MkdirAll(filepath.Dir(hbPath), 0755)
498+
// Write a stale heartbeat (working state, 20 minutes old) so the reaper considers it
499+
polecat.TouchSessionHeartbeatWithState(townRoot, "myr-mycat", polecat.HeartbeatWorking, "", "")
500+
// Backdate the heartbeat to make it stale
501+
hbPath := filepath.Join(townRoot, ".runtime", "heartbeats", "myr-mycat.json")
481502
staleHB := polecat.SessionHeartbeat{
482503
Timestamp: time.Now().UTC().Add(-20 * time.Minute),
483504
State: polecat.HeartbeatWorking,

0 commit comments

Comments
 (0)