Personal Telegram developer console for GitHub

test: define policy enforcing plugin router contract

davoodmehraban89 committed Oct 3, 2026 at 01:28 UTC 4e3fca87139310c24c3c83a5181fc638f887882d
1 file changed +162
test/plugin-router.test.js new
+162
@@ -0,0 +1,162 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { PluginRegistry } from '../src/core/plugin-registry.js';
4 +import { ActionPolicy } from '../src/core/action-policy.js';
5 +import { PluginRouter } from '../src/core/router.js';
6 +
7 +function auditCollector() {
8 + const events = [];
9 + return { events, logger: { log: event => events.push(event) } };
10 +}
11 +
12 +test('PluginRouter dispatches the first matching READ command after policy evaluation', async () => {
13 + let executed = 0;
14 + const first = {
15 + manifest: { id: 'first', actions: {} },
16 + resolveCommand: () => null
17 + };
18 + const second = {
19 + manifest: {
20 + id: 'second',
21 + actions: { 'second.read': { risk: 'READ' } }
22 + },
23 + resolveCommand: command => command === '/go'
24 + ? {
25 + actionId: 'second.read',
26 + execute: async () => {
27 + executed += 1;
28 + return { view: 'ok' };
29 + }
30 + }
31 + : null
32 + };
33 + const third = {
34 + manifest: {
35 + id: 'third',
36 + actions: { 'third.read': { risk: 'READ' } }
37 + },
38 + resolveCommand: () => ({
39 + actionId: 'third.read',
40 + execute: async () => {
41 + throw new Error('must not run');
42 + }
43 + })
44 + };
45 + const audit = auditCollector();
46 + const router = new PluginRouter({
47 + registry: new PluginRegistry([first, second, third]),
48 + policy: new ActionPolicy(),
49 + audit: audit.logger
50 + });
51 +
52 + const result = await router.dispatchCommand('/go', { isOwner: true });
53 +
54 + assert.deepEqual(result, { view: 'ok' });
55 + assert.equal(executed, 1);
56 + assert.equal(audit.events[0].event, 'action_attempt');
57 + assert.equal(audit.events[0].outcome, 'allowed');
58 + assert.equal(audit.events[1].event, 'action_result');
59 + assert.equal(audit.events[1].outcome, 'success');
60 +});
61 +
62 +test('PluginRouter dispatches callbacks through the same policy boundary', async () => {
63 + let executed = 0;
64 + const plugin = {
65 + manifest: {
66 + id: 'sample',
67 + actions: { 'sample.callback.read': { risk: 'READ' } }
68 + },
69 + resolveCallback: data => data === 'sample:1'
70 + ? {
71 + actionId: 'sample.callback.read',
72 + execute: async () => {
73 + executed += 1;
74 + return { callback: 'ok' };
75 + }
76 + }
77 + : null
78 + };
79 + const router = new PluginRouter({
80 + registry: new PluginRegistry([plugin]),
81 + policy: new ActionPolicy(),
82 + audit: { log() {} }
83 + });
84 +
85 + assert.deepEqual(
86 + await router.dispatchCallback('sample:1', { isOwner: true }),
87 + { callback: 'ok' }
88 + );
89 + assert.equal(executed, 1);
90 +});
91 +
92 +test('PluginRouter returns null when no plugin owns the route', async () => {
93 + const plugin = {
94 + manifest: { id: 'sample', actions: {} },
95 + resolveCommand: () => null,
96 + resolveCallback: () => null
97 + };
98 + const router = new PluginRouter({
99 + registry: new PluginRegistry([plugin]),
100 + policy: new ActionPolicy(),
101 + audit: { log() {} }
102 + });
103 +
104 + assert.equal(await router.dispatchCommand('/unknown', { isOwner: true }), null);
105 + assert.equal(await router.dispatchCallback('unknown', { isOwner: true }), null);
106 +});
107 +
108 +test('PluginRouter denies undeclared actions without executing plugin code', async () => {
109 + let executed = 0;
110 + const plugin = {
111 + manifest: { id: 'sample', actions: {} },
112 + resolveCommand: () => ({
113 + actionId: 'sample.missing',
114 + execute: async () => {
115 + executed += 1;
116 + }
117 + })
118 + };
119 + const router = new PluginRouter({
120 + registry: new PluginRegistry([plugin]),
121 + policy: new ActionPolicy(),
122 + audit: { log() {} }
123 + });
124 +
125 + const result = await router.dispatchCommand('/go', { isOwner: true });
126 +
127 + assert.equal(result.denied, true);
128 + assert.equal(result.requiresApproval, false);
129 + assert.equal(result.reason, 'unknown_action');
130 + assert.equal(executed, 0);
131 +});
132 +
133 +test('PluginRouter never executes WRITE or DESTRUCTIVE actions before approval', async () => {
134 + for (const risk of ['WRITE', 'DESTRUCTIVE']) {
135 + let executed = 0;
136 + const actionId = `sample.${risk.toLowerCase()}`;
137 + const plugin = {
138 + manifest: {
139 + id: 'sample',
140 + actions: { [actionId]: { risk } }
141 + },
142 + resolveCommand: () => ({
143 + actionId,
144 + execute: async () => {
145 + executed += 1;
146 + }
147 + })
148 + };
149 + const router = new PluginRouter({
150 + registry: new PluginRegistry([plugin]),
151 + policy: new ActionPolicy(),
152 + audit: { log() {} }
153 + });
154 +
155 + const result = await router.dispatchCommand('/danger', { isOwner: true });
156 +
157 + assert.equal(result.requiresApproval, true);
158 + assert.equal(result.denied, false);
159 + assert.equal(result.risk, risk);
160 + assert.equal(executed, 0);
161 + }
162 +});