Personal Telegram developer console for GitHub
test(hardening): bind kill switch test actions to plugin
davoodmehraban89 committed
Oct 5, 2026 at 13:25 UTC
929fdd725cafc4a4a4431c9d6303116144935bb5
1 file changed
+6
-68
test/kill-switch.test.js
+6
-68
@@ -3,71 +3,9 @@ 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
-});
6
+function registryFor(risk,execute){const actionId=`test.${risk.toLowerCase()}`;const plugin={manifest:{id:'test'},async resolveCallback(){return{actionId,execute};}};return{getPlugins:()=>[plugin],getAction:()=>({pluginId:'test',actionId,risk})};}
7
+const audit={log(){}};const policy={evaluate:({action})=>({allowed:true,requiresApproval:false,risk:action.risk})};
8
+test('LOCKED kill switch allows READ but blocks WRITE before provider mutation',async()=>{let reads=0,writes=0;const killSwitch=new KillSwitch({store:{get:async()=>({mode:'LOCKED'})}});const readRouter=new PluginRouter({registry:registryFor('READ',async()=>{reads++;return{ok:true};}),policy,audit,killSwitch});const writeRouter=new PluginRouter({registry:registryFor('WRITE',async()=>{writes++;return{ok:true};}),policy,audit,killSwitch});assert.equal((await readRouter.dispatchCallback('x',{isOwner:true})).ok,true);const blocked=await writeRouter.dispatchCallback('x',{isOwner:true,approved:true});assert.equal(blocked.ok,false);assert.equal(blocked.killSwitchLocked,true);assert.equal(reads,1);assert.equal(writes,0);});
9
+test('kill switch fails closed for WRITE when durable state cannot be read',async()=>{let writes=0;const killSwitch=new KillSwitch({store:{get:async()=>{throw new Error('durable unavailable');}}});const router=new PluginRouter({registry:registryFor('WRITE',async()=>{writes++;return{ok:true};}),policy,audit,killSwitch});const blocked=await router.dispatchCallback('x',{isOwner:true,approved:true});assert.equal(blocked.ok,false);assert.equal(blocked.killSwitchLocked,true);assert.equal(writes,0);});
10
+test('LOCKED kill switch blocks automation advance before routing any step',async()=>{let routed=0;const runner=new AutomationRunner({router:{dispatchCallback:async()=>{routed++;return{ok:true};}},store:{put:async state=>state},killSwitch:new KillSwitch({store:{get:async()=>({mode:'LOCKED'})}})});const state={status:'ready',cursor:0,ownerId:'123',steps:[{callback:'write:x',key:'k1'}]};const blocked=await runner.advance(state);assert.equal(blocked.status,'paused_kill_switch');assert.equal(routed,0);});
11
+test('only owner-approved control can change lock state and unlock is sensitive',async()=>{const calls=[];const store={get:async()=>({mode:'NORMAL'}),set:async next=>{calls.push(next);return next;}};const killSwitch=new KillSwitch({store});await assert.rejects(()=>killSwitch.lock({isOwner:false,approved:true}),/owner/i);await assert.rejects(()=>killSwitch.lock({isOwner:true,approved:false}),/approval/i);await killSwitch.lock({isOwner:true,approved:true});await assert.rejects(()=>killSwitch.unlock({isOwner:true,approved:false}),/approval/i);await killSwitch.unlock({isOwner:true,approved:true});assert.deepEqual(calls.map(x=>x.mode),['LOCKED','NORMAL']);});