|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('node:assert/strict'); |
| 4 | +const fs = require('node:fs'); |
| 5 | +const os = require('node:os'); |
| 6 | +const path = require('node:path'); |
| 7 | +const test = require('node:test'); |
| 8 | + |
| 9 | +const cockpit = require('../src/cockpit'); |
| 10 | +const { |
| 11 | + applyCockpitAction, |
| 12 | + runSelectedLaneAction, |
| 13 | +} = require('../src/cockpit/control'); |
| 14 | +const { createKittyCockpitPlan } = require('../src/cockpit/kitty-layout'); |
| 15 | +const { buildLaneMenu } = require('../src/cockpit/menu'); |
| 16 | +const { |
| 17 | + initRepo, |
| 18 | + runNodeWithEnv, |
| 19 | +} = require('./helpers/install-test-helpers'); |
| 20 | + |
| 21 | +function escapeRegExp(value) { |
| 22 | + return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 23 | +} |
| 24 | + |
| 25 | +function fakeBin(name, scriptBody) { |
| 26 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), `guardex-fake-${name}-`)); |
| 27 | + const bin = path.join(dir, name); |
| 28 | + const log = path.join(dir, `${name}.log`); |
| 29 | + fs.writeFileSync(bin, `#!/usr/bin/env bash\nset -euo pipefail\nLOG=${JSON.stringify(log)}\n${scriptBody}\n`, 'utf8'); |
| 30 | + fs.chmodSync(bin, 0o755); |
| 31 | + return { bin, log }; |
| 32 | +} |
| 33 | + |
| 34 | +function fakeKitty() { |
| 35 | + return fakeBin('kitty', [ |
| 36 | + 'printf "%s\\n" "$PWD :: $*" >> "$LOG"', |
| 37 | + 'if [[ "${1:-}" == "@" && "${2:-}" == "ls" ]]; then exit 0; fi', |
| 38 | + 'if [[ "${1:-}" == "@" && "${2:-}" == "launch" ]]; then exit 0; fi', |
| 39 | + 'exit 9', |
| 40 | + ].join('\n')); |
| 41 | +} |
| 42 | + |
| 43 | +function fakeTmux() { |
| 44 | + return fakeBin('tmux', [ |
| 45 | + 'printf "%s\\n" "$PWD :: $*" >> "$LOG"', |
| 46 | + 'if [[ "${1:-}" == "-V" ]]; then echo "tmux 3.4"; exit 0; fi', |
| 47 | + 'if [[ "${1:-}" == "has-session" ]]; then exit 1; fi', |
| 48 | + 'if [[ "${1:-}" == "new-session" ]]; then exit 0; fi', |
| 49 | + 'if [[ "${1:-}" == "send-keys" ]]; then exit 0; fi', |
| 50 | + 'exit 9', |
| 51 | + ].join('\n')); |
| 52 | +} |
| 53 | + |
| 54 | +function readLogLines(log) { |
| 55 | + return fs.readFileSync(log, 'utf8').trim().split('\n'); |
| 56 | +} |
| 57 | + |
| 58 | +function assertKittyLaunchLine(line, repoDir) { |
| 59 | + assert.match(line, / :: @ launch --type=window --cwd /); |
| 60 | + assert.match(line, new RegExp(`--cwd ${escapeRegExp(repoDir)}`)); |
| 61 | + assert.match(line, /--title gx cockpit/); |
| 62 | + assert.match(line, /-- sh -lc gx cockpit control --target /); |
| 63 | +} |
| 64 | + |
| 65 | +test('gx cockpit --backend auto command path opens Kitty when remote control answers', () => { |
| 66 | + const repoDir = initRepo(); |
| 67 | + const kitty = fakeKitty(); |
| 68 | + const missingTmux = path.join(os.tmpdir(), `guardex-missing-tmux-${process.pid}-${Date.now()}`); |
| 69 | + |
| 70 | + const result = runNodeWithEnv(['cockpit', '--backend', 'auto', '--session', 'guardex-auto', '--target', repoDir], repoDir, { |
| 71 | + GUARDEX_KITTY_BIN: kitty.bin, |
| 72 | + GUARDEX_TMUX_BIN: missingTmux, |
| 73 | + }); |
| 74 | + |
| 75 | + assert.equal(result.status, 0, result.stderr || result.stdout); |
| 76 | + assert.match(result.stdout, /Created kitty cockpit window 'guardex-auto'/); |
| 77 | + const lines = readLogLines(kitty.log); |
| 78 | + assert.equal(lines.length, 2); |
| 79 | + assert.match(lines[0], / :: @ ls$/); |
| 80 | + assertKittyLaunchLine(lines[1], repoDir); |
| 81 | +}); |
| 82 | + |
| 83 | +test('gx cockpit --backend kitty dry-run layout plan is deterministic and does not execute Kitty', () => { |
| 84 | + const repoRoot = '/repo/gitguardex'; |
| 85 | + const controlCommand = cockpit.cockpitControlCommand(repoRoot); |
| 86 | + const plan = createKittyCockpitPlan({ |
| 87 | + repoRoot, |
| 88 | + sessionName: 'guardex-kitty', |
| 89 | + controlCommand, |
| 90 | + welcomeCommand: 'gx', |
| 91 | + agents: [{ |
| 92 | + id: 'alpha', |
| 93 | + agent: 'codex', |
| 94 | + worktreePath: '/repo/.omx/agent-worktrees/alpha', |
| 95 | + command: 'exec codex', |
| 96 | + }], |
| 97 | + kittyBin: '/usr/bin/kitty', |
| 98 | + dryRun: true, |
| 99 | + }); |
| 100 | + |
| 101 | + assert.equal(plan.backend, 'kitty'); |
| 102 | + assert.equal(plan.dryRun, true); |
| 103 | + assert.deepEqual( |
| 104 | + plan.steps.map((step) => step.id), |
| 105 | + ['launch-control', 'launch-agent-area', 'launch-agent-1', 'focus-control'], |
| 106 | + ); |
| 107 | + assert.deepEqual(plan.commands[0], { |
| 108 | + cmd: '/usr/bin/kitty', |
| 109 | + args: [ |
| 110 | + '@', |
| 111 | + 'launch', |
| 112 | + '--type=window', |
| 113 | + '--cwd', |
| 114 | + repoRoot, |
| 115 | + '--title', |
| 116 | + 'guardex-kitty: control', |
| 117 | + '--', |
| 118 | + 'sh', |
| 119 | + '-lc', |
| 120 | + "gx cockpit control --target '/repo/gitguardex'", |
| 121 | + ], |
| 122 | + }); |
| 123 | + assert.equal(plan.layout.agents[0].cwd, '/repo/.omx/agent-worktrees/alpha'); |
| 124 | +}); |
| 125 | + |
| 126 | +test('gx cockpit --backend tmux preserves the existing tmux control path', () => { |
| 127 | + const repoDir = initRepo(); |
| 128 | + const kitty = fakeKitty(); |
| 129 | + const tmux = fakeTmux(); |
| 130 | + |
| 131 | + const result = runNodeWithEnv(['cockpit', '--backend', 'tmux', '--session', 'guardex-tmux', '--target', repoDir], repoDir, { |
| 132 | + GUARDEX_KITTY_BIN: kitty.bin, |
| 133 | + GUARDEX_TMUX_BIN: tmux.bin, |
| 134 | + }); |
| 135 | + |
| 136 | + assert.equal(result.status, 0, result.stderr || result.stdout); |
| 137 | + assert.match(result.stdout, /Created tmux session 'guardex-tmux'/); |
| 138 | + assert.equal(fs.existsSync(kitty.log), false, 'tmux backend must not probe or launch Kitty'); |
| 139 | + const logged = fs.readFileSync(tmux.log, 'utf8'); |
| 140 | + assert.match(logged, /has-session -t guardex-tmux/); |
| 141 | + assert.match(logged, /new-session -d -s guardex-tmux/); |
| 142 | + assert.match(logged, /send-keys -t guardex-tmux gx cockpit control --target .* C-m/); |
| 143 | +}); |
| 144 | + |
| 145 | +test('cockpit pane menu opens and selects a lane terminal action', () => { |
| 146 | + let state = applyCockpitAction({}, { |
| 147 | + type: 'refresh', |
| 148 | + cockpitState: { |
| 149 | + repoPath: '/repo/gitguardex', |
| 150 | + baseBranch: 'main', |
| 151 | + sessions: [{ |
| 152 | + id: 'pane-1', |
| 153 | + agentName: 'codex', |
| 154 | + branch: 'agent/codex/pane-1', |
| 155 | + worktreePath: '/repo/.omx/agent-worktrees/pane-1', |
| 156 | + worktreeExists: true, |
| 157 | + }], |
| 158 | + }, |
| 159 | + }); |
| 160 | + |
| 161 | + state = applyCockpitAction(state, { type: 'key', key: 'm' }); |
| 162 | + assert.equal(state.mode, 'menu'); |
| 163 | + |
| 164 | + state = applyCockpitAction(state, { type: 'key', key: 'down' }); |
| 165 | + state = applyCockpitAction(state, { type: 'key', key: '\r' }); |
| 166 | + |
| 167 | + assert.deepEqual(state.lastIntent, { |
| 168 | + type: 'terminal:open', |
| 169 | + sessionId: 'pane-1', |
| 170 | + branch: 'agent/codex/pane-1', |
| 171 | + worktreePath: '/repo/.omx/agent-worktrees/pane-1', |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +test('lane menu selected action id reaches the cockpit action dispatcher', () => { |
| 176 | + const session = { |
| 177 | + id: 'pane-1', |
| 178 | + branch: 'agent/codex/pane-1', |
| 179 | + worktreePath: '/repo/.omx/agent-worktrees/pane-1', |
| 180 | + worktreeExists: true, |
| 181 | + }; |
| 182 | + const menu = buildLaneMenu(session); |
| 183 | + const selected = menu.items.find((item) => item.shortcut === 'f'); |
| 184 | + const calls = []; |
| 185 | + |
| 186 | + const result = runSelectedLaneAction({ id: selected.id }, { |
| 187 | + repoRoot: '/repo', |
| 188 | + baseBranch: 'main', |
| 189 | + session, |
| 190 | + runCommand(cmd, args) { |
| 191 | + calls.push({ cmd, args }); |
| 192 | + return { status: 0, stdout: `${cmd} ok\n`, stderr: '' }; |
| 193 | + }, |
| 194 | + }); |
| 195 | + |
| 196 | + assert.equal(selected.id, 'finish-pr'); |
| 197 | + assert.equal(result.ok, true); |
| 198 | + assert.deepEqual(calls, [{ |
| 199 | + cmd: 'gx', |
| 200 | + args: [ |
| 201 | + 'agents', |
| 202 | + 'finish', |
| 203 | + '--target', |
| 204 | + '/repo', |
| 205 | + '--branch', |
| 206 | + 'agent/codex/pane-1', |
| 207 | + '--via-pr', |
| 208 | + '--wait-for-merge', |
| 209 | + '--cleanup', |
| 210 | + ], |
| 211 | + }]); |
| 212 | +}); |
0 commit comments