Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

test(canvas-control): derive the drift alarm's verb lists instead of hand-writing them

enes7f80b0c322da
diff --git a/src/renderer/canvas/control-destructive.test.ts b/src/renderer/canvas/control-destructive.test.ts
index 495f46f87..2bd03f5a5 100644
--- a/src/renderer/canvas/control-destructive.test.ts
+++ b/src/renderer/canvas/control-destructive.test.ts
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest'
import { readFileSync } from 'node:fs'
import { isDestructiveVerb, DESTRUCTIVE_VERBS } from '@shared/control-verbs'
-import { isWaivableVerb } from '@shared/control-confirm'
+import { CONFIRM_WAIVABLE_VERBS, isWaivableVerb } from '@shared/control-confirm'
/**
* A STRUCTURAL test on purpose.
@@ -35,17 +35,58 @@ function caseBody(verb: string): string {
}
/**
- * The body of an EARLY-HANDLED verb's block — `open-project` is dispatched before the
- * source-routing machinery (a STORE_ANSWERED_VERBS member, spec §2.3), so it has no `case` label.
- * Delimited by its `if (verb === '<verb>')` guard and the next section-comment rule (`// ──`),
- * the same way the switch slice above is delimited by the next case label.
+ * Every verb dispatched BEFORE the `switch (verb)` — derived from the source, never listed here.
+ *
+ * The list used to be hand-written (`['open-project']`, then `['open-project', 'settings']`), and
+ * that is a trap with a confusing failure: the final assertion below compares the gated verbs it
+ * FINDS against `DESTRUCTIVE_VERBS`, so an early-handled verb missing from the list makes a
+ * CORRECT implementation red with "two Sets differ" and no hint that a list in the test is the
+ * problem. It fails closed, which is the right direction, and it costs a diagnosis every time.
+ *
+ * COMPOUND GUARDS are the reason this is not a one-line regex: `send`/`reply`/`notify` share a
+ * single `if (verb === 'send' || verb === 'reply' || verb === 'notify')`, so a scan keyed on
+ * `if (verb === '<verb>')` discovers `send` and silently misses the other two.
+ */
+const EARLY_GUARD_RE = /\n {6}if \(verb === '([a-z-]+)'((?: \|\| verb === '[a-z-]+')*)\)/g
+
+/** verb → the index in `src` where its early-handled guard begins (shared for a compound guard). */
+function earlyGuardStarts(): Map<string, number> {
+ const out = new Map<string, number>()
+ for (const m of src.matchAll(EARLY_GUARD_RE)) {
+ const at = m.index! + 1
+ for (const alt of m[0].matchAll(/verb === '([a-z-]+)'/g)) out.set(alt[1], at)
+ }
+ return out
+}
+
+/** The early-handled verbs, in source order. */
+function earlyHandledVerbs(): string[] {
+ return [...earlyGuardStarts().keys()]
+}
+
+/**
+ * The body of an EARLY-HANDLED verb's block, delimited by its guard and the next section-comment
+ * rule (`// ──`), the same way the switch slice above is delimited by the next case label.
+ *
+ * A MISSING terminator THROWS rather than returning the rest of the slice. The old version fell
+ * back to `rest` — the entire remainder of the file — which does not merely swallow the next
+ * block: it makes `/isDestructiveVerb\(verb\)/.test(earlyBody(v))` match some later, unrelated
+ * call and report the verb as GATED when it is not. A false PASS in a security drift alarm is the
+ * one outcome worth an exception, so the convention is enforced instead of assumed.
*/
function earlyBody(verb: string): string {
- const start = src.indexOf(`if (verb === '${verb}')`)
- if (start === -1) return ''
+ const start = earlyGuardStarts().get(verb)
+ if (start === undefined) return ''
const rest = src.slice(start)
const end = rest.indexOf('// ──', 10)
- return end === -1 ? rest : rest.slice(0, end)
+ if (end === -1) {
+ throw new Error(
+ `the early-handled block for '${verb}' has no '// ──' section rule after it, so its body ` +
+ 'cannot be delimited. Slicing to the end of the file would make the isDestructiveVerb ' +
+ 'check below a FALSE PASS. Add the section rule, or teach earlyBody a second terminator.'
+ )
+ }
+ return rest.slice(0, end)
}
/** A verb's dispatch body wherever it lives: its switch case, or its early-handled block. */
@@ -58,7 +99,9 @@ describe('the confirm-gated set and the dispatch that reads it stay in agreement
expect(src).toMatch(/import \{[^}]*isDestructiveVerb[^}]*\} from '@shared\/control-verbs'/)
})
- for (const verb of ['write', 'close', 'open-project', 'settings'] as const) {
+ // Derived from the set itself: a verb added to DESTRUCTIVE_VERBS must bring its confirm with
+ // it, and nobody should have to remember to extend a list in this file to find that out.
+ for (const verb of DESTRUCTIVE_VERBS) {
it(`${verb} reaches its confirm through isDestructiveVerb`, () => {
expect(isDestructiveVerb(verb)).toBe(true)
const body = dispatchBody(verb)
@@ -80,7 +123,9 @@ describe('the confirm-gated set and the dispatch that reads it stay in agreement
* the wrong thing (or hard-coded a skip) would be a destructive verb with no human gate and no
* failing test. So it is pinned in both directions, exactly like the set above.
*/
- for (const verb of ['write', 'close'] as const) {
+ // Derived from the waivable table too, so the last hand-written verb list in this file is gone.
+ // A list here fails the same silent way the destructive one did: by covering less.
+ for (const verb of CONFIRM_WAIVABLE_VERBS) {
it(`${verb} reaches its confirm through the shared waiver decision`, () => {
expect(isWaivableVerb(verb)).toBe(true)
const body = dispatchBody(verb)
@@ -134,7 +179,7 @@ describe('the confirm-gated set and the dispatch that reads it stay in agreement
it('no case hard-codes a skip of its confirm', () => {
// The only admissible way past one of these dialogs is `controlConfirmDecision`. A literal
// shortcut (an env check, a `true`, a settings flag read inline) would be a silent loosening.
- for (const verb of ['write', 'close', 'open-project', 'settings'] as const) {
+ for (const verb of DESTRUCTIVE_VERBS) {
const body = dispatchBody(verb)
expect(body).not.toMatch(/skipConfirm|dontAskAgain|SKIP_CONFIRM/)
}
@@ -152,6 +197,53 @@ describe('the confirm-gated set and the dispatch that reads it stay in agreement
expect(site).not.toContain('controlConfirmWaivers')
})
+ /**
+ * The DERIVATION is now load-bearing, so it gets its own assertions. Both of these would have
+ * been satisfied by the hand-written list too — the point is that they fail when the SCAN breaks,
+ * which the list could not tell us.
+ */
+ it('the per-verb checks are driven by the SET, not by a list in this file', () => {
+ // MEASURED, and it is why a pin is needed rather than an assertion: reverting the loop below
+ // to a hand-written `['write', 'close', 'open-project']` generated one FEWER test and the
+ // suite stayed GREEN (14 → 13). A list here fails by covering LESS, and nothing inside the
+ // remaining tests can see that — the coverage itself has to be the thing asserted.
+ const self = readFileSync(new URL('./control-destructive.test.ts', import.meta.url), 'utf8')
+ expect(self).toContain('for (const verb of DESTRUCTIVE_VERBS) {')
+ // …and no hand-written verb list creeps back in beside it.
+ expect(self).not.toMatch(/for \(const verb of \[[^\]]*\] as const\)/)
+ })
+
+ it('finds every early-handled verb, compound guards included', () => {
+ const early = earlyHandledVerbs()
+ // `send`/`reply`/`notify` share one `if (... || ... || ...)`. A scan keyed on
+ // `if (verb === '<verb>')` finds only the first, which is exactly the miss this closes.
+ for (const v of ['send', 'reply', 'notify']) expect(early).toContain(v)
+ // …and the single-verb guards still resolve.
+ for (const v of ['open-project', 'settings']) expect(early).toContain(v)
+ // Nothing invented: every discovered verb must really be a control verb the dispatch handles.
+ for (const v of early) expect(src).toContain(`verb === '${v}'`)
+ })
+
+ it('every early-handled body stops at its own block', () => {
+ // The guard that makes the scan safe. A block whose `// ──` terminator is missing slices
+ // onward and reports itself as gated off some later, unrelated `isDestructiveVerb` call — a
+ // false PASS. `earlyBody` throws in that case; these are the structural properties that catch
+ // a body which ran away WITHOUT reaching the end of the file, which a length heuristic missed
+ // (measured: an early guard sits ~2/3 through Canvas.tsx, so "less than half the file" is
+ // satisfied by a body that swallowed everything after it).
+ const starts = earlyGuardStarts()
+ for (const v of earlyHandledVerbs()) {
+ const body = earlyBody(v)
+ expect(body).toContain(`verb === '${v}'`)
+ // It must end before the dispatch switch: a body that reaches it has left its own block.
+ expect(body).not.toContain('switch (verb)')
+ // …and it must not contain ANOTHER guard's start, i.e. it never swallows the next block.
+ for (const [other, at] of starts) {
+ if (at !== starts.get(v)) expect(body).not.toContain(`if (verb === '${other}'`)
+ }
+ }
+ })
+
it('no other case reads isDestructiveVerb', () => {
// Every `isDestructiveVerb(verb)` in the dispatch must sit in a case the set actually holds.
// If a third case ever grows one, either the set or the dispatch is wrong — say so here rather
@@ -162,8 +254,9 @@ describe('the confirm-gated set and the dispatch that reads it stay in agreement
// is confirm-gated"; it is "no other case claims to be gated by this set".
const labels = [...src.matchAll(/\n {10}case '([a-z-]+)': \{/g)].map((m) => m[1])
const gated = labels.filter((v) => /isDestructiveVerb\(verb\)/.test(caseBody(v)))
- // The early-handled block (`open-project`) is counted the same way, off its own slice.
- for (const early of ['open-project', 'settings']) {
+ // The early-handled blocks are counted the same way, off their own slices — and WHICH verbs
+ // those are is read from the source (see `earlyHandledVerbs`), so a new one cannot be missed.
+ for (const early of earlyHandledVerbs()) {
if (/isDestructiveVerb\(verb\)/.test(earlyBody(early))) gated.push(early)
}
expect(new Set(gated)).toEqual(new Set(DESTRUCTIVE_VERBS))