Personal Telegram developer console for GitHub

feat/studio-g0-foundation
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 209 lines 5.69 KB
Raw
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) =>
24 command === '/go'
25 ? {
26 actionId: 'second.read',
27 execute: async () => {
28 executed += 1;
29 return { view: 'ok' };
30 }
31 }
32 : null
33 };
34 const third = {
35 manifest: {
36 id: 'third',
37 actions: { 'third.read': { risk: 'READ' } }
38 },
39 resolveCommand: () => ({
40 actionId: 'third.read',
41 execute: async () => {
42 throw new Error('must not run');
43 }
44 })
45 };
46 const audit = auditCollector();
47 const router = new PluginRouter({
48 registry: new PluginRegistry([first, second, third]),
49 policy: new ActionPolicy(),
50 audit: audit.logger
51 });
52
53 const result = await router.dispatchCommand('/go', { isOwner: true });
54
55 assert.deepEqual(result, { view: 'ok' });
56 assert.equal(executed, 1);
57 assert.equal(audit.events[0].event, 'action_attempt');
58 assert.equal(audit.events[0].outcome, 'allowed');
59 assert.equal(audit.events[1].event, 'action_result');
60 assert.equal(audit.events[1].outcome, 'success');
61 });
62
63 test('PluginRouter dispatches callbacks through the same policy boundary', async () => {
64 let executed = 0;
65 const plugin = {
66 manifest: {
67 id: 'sample',
68 actions: { 'sample.callback.read': { risk: 'READ' } }
69 },
70 resolveCallback: (data) =>
71 data === 'sample:1'
72 ? {
73 actionId: 'sample.callback.read',
74 execute: async () => {
75 executed += 1;
76 return { callback: 'ok' };
77 }
78 }
79 : null
80 };
81 const router = new PluginRouter({
82 registry: new PluginRegistry([plugin]),
83 policy: new ActionPolicy(),
84 audit: { log() {} }
85 });
86
87 assert.deepEqual(
88 await router.dispatchCallback('sample:1', { isOwner: true }),
89 { callback: 'ok' }
90 );
91 assert.equal(executed, 1);
92 });
93
94 test('PluginRouter returns null when no plugin owns the route', async () => {
95 const plugin = {
96 manifest: { id: 'sample', actions: {} },
97 resolveCommand: () => null,
98 resolveCallback: () => null
99 };
100 const router = new PluginRouter({
101 registry: new PluginRegistry([plugin]),
102 policy: new ActionPolicy(),
103 audit: { log() {} }
104 });
105
106 assert.equal(
107 await router.dispatchCommand('/unknown', { isOwner: true }),
108 null
109 );
110 assert.equal(
111 await router.dispatchCallback('unknown', { isOwner: true }),
112 null
113 );
114 });
115
116 test('PluginRouter denies undeclared actions without executing plugin code', async () => {
117 let executed = 0;
118 const plugin = {
119 manifest: { id: 'sample', actions: {} },
120 resolveCommand: () => ({
121 actionId: 'sample.missing',
122 execute: async () => {
123 executed += 1;
124 }
125 })
126 };
127 const router = new PluginRouter({
128 registry: new PluginRegistry([plugin]),
129 policy: new ActionPolicy(),
130 audit: { log() {} }
131 });
132
133 const result = await router.dispatchCommand('/go', { isOwner: true });
134
135 assert.equal(result.denied, true);
136 assert.equal(result.requiresApproval, false);
137 assert.equal(result.reason, 'unknown_action');
138 assert.equal(executed, 0);
139 });
140
141 test('PluginRouter never executes WRITE or DESTRUCTIVE actions before approval', async () => {
142 for (const risk of ['WRITE', 'DESTRUCTIVE']) {
143 let executed = 0;
144 const actionId = `sample.${risk.toLowerCase()}`;
145 const plugin = {
146 manifest: {
147 id: 'sample',
148 actions: { [actionId]: { risk } }
149 },
150 resolveCommand: () => ({
151 actionId,
152 execute: async () => {
153 executed += 1;
154 }
155 })
156 };
157 const router = new PluginRouter({
158 registry: new PluginRegistry([plugin]),
159 policy: new ActionPolicy(),
160 audit: { log() {} }
161 });
162
163 const result = await router.dispatchCommand('/danger', { isOwner: true });
164
165 assert.equal(result.requiresApproval, true);
166 assert.equal(result.denied, false);
167 assert.equal(result.risk, risk);
168 assert.equal(executed, 0);
169 }
170 });
171
172 test('PluginRouter executes an approved owner WRITE action exactly once', async () => {
173 let executed = 0;
174 const audit = auditCollector();
175 const plugin = {
176 manifest: {
177 id: 'sample',
178 actions: { 'sample.write': { risk: 'WRITE' } }
179 },
180 resolveCallback: (data) =>
181 data === 'write:1'
182 ? {
183 actionId: 'sample.write',
184 execute: async () => {
185 executed += 1;
186 return { ok: true, view: 'done' };
187 }
188 }
189 : null
190 };
191 const router = new PluginRouter({
192 registry: new PluginRegistry([plugin]),
193 policy: new ActionPolicy(),
194 audit: audit.logger
195 });
196
197 const result = await router.dispatchCallback('write:1', {
198 isOwner: true,
199 approved: true
200 });
201
202 assert.deepEqual(result, { ok: true, view: 'done' });
203 assert.equal(executed, 1);
204 assert.equal(audit.events[0].reason, 'owner_write_approved');
205 assert.equal(audit.events[0].outcome, 'allowed');
206 assert.equal(audit.events[1].event, 'action_result');
207 assert.equal(audit.events[1].outcome, 'success');
208 assert.equal(JSON.stringify(audit.events).includes('token'), false);
209 });