Personal Telegram developer console for GitHub

test(hardening): define kill switch fail-closed contract

davoodmehraban89 committed Oct 5, 2026 at 13:21 UTC bb60e2431c674c98a62e465f71efafb4df2f7774
1 file changed +73
test/kill-switch.test.js new
+73
@@ -0,0 +1,73 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { KillSwitch } from '../src/core/kill-switch.js';
4 +import { PluginRouter } from '../src/core/router.js';
5 +import { AutomationRunner } from '../src/core/automation-runner.js';
6 +
7 +function registryFor(risk, execute) {
8 + const plugin = {
9 + manifest: { id: 'test' },
10 + async resolveCallback() { return { actionId: `test.${risk.toLowerCase()}`, execute }; }
11 + };
12 + return {
13 + getPlugins: () => [plugin],
14 + getAction: () => ({ id: `test.${risk.toLowerCase()}`, risk })
15 + };
16 +}
17 +
18 +const audit = { log() {} };
19 +const policy = { evaluate: ({ action }) => ({ allowed: true, requiresApproval: false, risk: action.risk }) };
20 +
21 +test('LOCKED kill switch allows READ but blocks WRITE before provider mutation', async () => {
22 + let reads = 0;
23 + let writes = 0;
24 + const killSwitch = new KillSwitch({ store: { get: async () => ({ mode: 'LOCKED' }) } });
25 +
26 + const readRouter = new PluginRouter({ registry: registryFor('READ', async () => { reads++; return { ok: true }; }), policy, audit, killSwitch });
27 + const writeRouter = new PluginRouter({ registry: registryFor('WRITE', async () => { writes++; return { ok: true }; }), policy, audit, killSwitch });
28 +
29 + assert.equal((await readRouter.dispatchCallback('x', { isOwner: true })).ok, true);
30 + const blocked = await writeRouter.dispatchCallback('x', { isOwner: true, approved: true });
31 + assert.equal(blocked.ok, false);
32 + assert.equal(blocked.killSwitchLocked, true);
33 + assert.equal(reads, 1);
34 + assert.equal(writes, 0, 'provider mutation must never run while locked');
35 +});
36 +
37 +test('kill switch fails closed for WRITE when durable state cannot be read', async () => {
38 + let writes = 0;
39 + const killSwitch = new KillSwitch({ store: { get: async () => { throw new Error('durable unavailable'); } } });
40 + const router = new PluginRouter({ registry: registryFor('WRITE', async () => { writes++; return { ok: true }; }), policy, audit, killSwitch });
41 + const blocked = await router.dispatchCallback('x', { isOwner: true, approved: true });
42 + assert.equal(blocked.ok, false);
43 + assert.equal(blocked.killSwitchLocked, true);
44 + assert.equal(writes, 0);
45 +});
46 +
47 +test('LOCKED kill switch blocks automation advance before routing any step', async () => {
48 + let routed = 0;
49 + const runner = new AutomationRunner({
50 + router: { dispatchCallback: async () => { routed++; return { ok: true }; } },
51 + store: { put: async state => state },
52 + killSwitch: new KillSwitch({ store: { get: async () => ({ mode: 'LOCKED' }) } })
53 + });
54 + const state = { status: 'ready', cursor: 0, ownerId: '123', steps: [{ callback: 'write:x', key: 'k1' }] };
55 + const blocked = await runner.advance(state);
56 + assert.equal(blocked.status, 'paused_kill_switch');
57 + assert.equal(routed, 0);
58 +});
59 +
60 +test('only owner-approved control can change lock state and unlock is sensitive', async () => {
61 + const calls = [];
62 + const store = {
63 + get: async () => ({ mode: 'NORMAL' }),
64 + set: async next => { calls.push(next); return next; }
65 + };
66 + const killSwitch = new KillSwitch({ store });
67 + await assert.rejects(() => killSwitch.lock({ isOwner: false, approved: true }), /owner/i);
68 + await assert.rejects(() => killSwitch.lock({ isOwner: true, approved: false }), /approval/i);
69 + await killSwitch.lock({ isOwner: true, approved: true });
70 + await assert.rejects(() => killSwitch.unlock({ isOwner: true, approved: false }), /approval/i);
71 + await killSwitch.unlock({ isOwner: true, approved: true });
72 + assert.deepEqual(calls.map(x => x.mode), ['LOCKED', 'NORMAL']);
73 +});