|
2 | 2 | // Issue #86: a nudge must not fire while the room poller is down, and the |
3 | 3 | // supervisor must say so once (and once more when it is back). |
4 | 4 | import assert from 'node:assert/strict'; |
5 | | -import { mkdtempSync, rmSync, writeFileSync, existsSync, utimesSync, readFileSync, chmodSync, statSync } from 'node:fs'; |
| 5 | +import { mkdtempSync, rmSync, writeFileSync, existsSync, utimesSync, readFileSync, chmodSync, statSync, mkdirSync } from 'node:fs'; |
6 | 6 | import { tmpdir } from 'node:os'; |
7 | 7 | import path from 'node:path'; |
8 | 8 | import { spawnSync, execFile } from 'node:child_process'; |
@@ -151,3 +151,37 @@ test('codex_gui_nudge debounces a second nudge inside the window, and only a rea |
151 | 151 | assert.equal(statSync(stamp).mtimeMs, before, 'abort paths never write the stamp'); |
152 | 152 | } finally { rmSync(dir, { recursive: true, force: true }); } |
153 | 153 | }); |
| 154 | + |
| 155 | +test('codex_gui_nudge: a reservation held by another nudge debounces this one right before injection', () => { |
| 156 | + const dir = mkdtempSync(path.join(tmpdir(), 'iak-reserve-')); |
| 157 | + try { |
| 158 | + const log = path.join(dir, 'nudge.log'); |
| 159 | + const hb = path.join(dir, 'hb'); writeHeartbeat(hb); |
| 160 | + const stamp = path.join(dir, 'last'); |
| 161 | + mkdirSync(stamp + '.lock'); // the other process is injecting right now |
| 162 | + // stubs so the run passes the screen-lock and human-idle checks and reaches |
| 163 | + // the reservation: ioreg (idle guard), a fake python that reports |
| 164 | + // "unlocked" and "idle", and an osascript that must never be reached |
| 165 | + const stubDir = path.join(dir, 'bin'); mkdirSync(stubDir); |
| 166 | + const mk = (name, body) => { const f = path.join(stubDir, name); writeFileSync(f, body); chmodSync(f, 0o755); return f; }; |
| 167 | + mk('ioreg', '#!/bin/sh\necho \'| "HIDIdleTime" = 999999999999\'\n'); |
| 168 | + const py = mk('fakepy', '#!/bin/sh\n[ "$1" = "-" ] && echo unlocked\nexit 0\n'); |
| 169 | + mk('osascript', '#!/bin/sh\necho "MUST NOT RUN" >&2; exit 99\n'); |
| 170 | + const idleCheck = mk('idle.py', '#!/bin/sh\nexit 0\n'); |
| 171 | + const r = spawnSync('bash', [nudge], { encoding: 'utf8', env: { ...process.env, PATH: `${stubDir}:${process.env.PATH}`, IAK_POLLER_HEARTBEAT: hb, IAK_NUDGE_STAMP: stamp, IAK_CODEX_NUDGE_LOG: log, IAK_CLICLICK_BIN: '/nonexistent', IAK_PYTHON_BIN: py, IAK_HUMAN_IDLE_CHECK: idleCheck } }); |
| 172 | + const out = readFileSync(log, 'utf8'); |
| 173 | + assert.equal(r.status, 0, r.stderr); |
| 174 | + assert.ok(!/MUST NOT RUN/.test(r.stderr), 'no injection while another nudge holds the reservation'); |
| 175 | + assert.match(out, /debounced \(another nudge is injecting right now\)/); |
| 176 | + assert.ok(existsSync(stamp + '.lock'), 'a reservation held by another process is not released by us'); |
| 177 | + assert.ok(!existsSync(stamp), 'no stamp written by a debounced run'); |
| 178 | + // positive control: with no reservation the same stubs DO reach the |
| 179 | + // injection (the osascript stub fails), and a failed send leaves no stamp |
| 180 | + rmSync(stamp + '.lock', { recursive: true, force: true }); |
| 181 | + const r2 = spawnSync('bash', [nudge], { encoding: 'utf8', env: { ...process.env, PATH: `${stubDir}:${process.env.PATH}`, IAK_POLLER_HEARTBEAT: hb, IAK_NUDGE_STAMP: stamp, IAK_CODEX_NUDGE_LOG: log, IAK_CLICLICK_BIN: '/nonexistent', IAK_PYTHON_BIN: py, IAK_HUMAN_IDLE_CHECK: idleCheck } }); |
| 182 | + assert.match(r2.stderr, /MUST NOT RUN/, 'control: the stubs reach the injection point'); |
| 183 | + assert.equal(r2.status, 1, 'a failed injection exits 1'); |
| 184 | + assert.ok(!existsSync(stamp), 'a failed injection writes no stamp'); |
| 185 | + assert.ok(!existsSync(stamp + '.lock'), 'the reservation is released on exit'); |
| 186 | + } finally { rmSync(dir, { recursive: true, force: true }); } |
| 187 | +}); |
0 commit comments