rob-revisited/ide/tests/remote-allow.test.mjs

23 lines
1.7 KiB
JavaScript

// Every command of REMOTE_ALLOW exists in the IDE's PHP (register('name' ...)), and none of the forbidden ones is allowed.
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node ide/tests/remote-allow.test.mjs
import { readFileSync, readdirSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { REMOTE_ALLOW, REMOTE_EVENTS } from '../ui/js/remote/allow.js';
const files = [];
const walk = (d) => { for (const f of readdirSync(d)) { const p = join(d, f); statSync(p).isDirectory() ? walk(p) : p.endsWith('.php') && files.push(p); } };
walk('ide/src'); walk('games/ide/src');
const registered = new Set();
for (const f of files) for (const m of readFileSync(f, 'utf8').matchAll(/register\('([A-Za-z0-9_.]+)'/g)) registered.add(m[1]);
let bad = 0;
const fail = (m) => { console.error('FAIL: ' + m); bad++; };
for (const [scope, names] of Object.entries(REMOTE_ALLOW)) {
for (const n of names) if (!registered.has(n)) fail(`${scope}: "${n}" is not registered by the IDE`);
}
const forbidden = /^(file\.(write|create|delete|rename)|git\.(?!status$)|run\.|tool\.|plugins\.|update\.|agent\.|ai\.|claude\.(mcp|session|close|terminal|window|dock|history\.(delete|save|markdown))|debug\.|edits\.|project\.(?!info$)|remote\.)/;
for (const [scope, names] of Object.entries(REMOTE_ALLOW)) for (const n of names) if (forbidden.test(n)) fail(`${scope}: "${n}" must never be remote`);
for (const [scope, names] of Object.entries(REMOTE_EVENTS)) if (!names.length) fail(`${scope}: no events`);
if (!REMOTE_ALLOW.all.every((n) => n.startsWith('git.status') || !n.startsWith('git.'))) fail('all: only git.status');
if (bad) process.exit(1);
console.log(`remote-allow: ${Object.values(REMOTE_ALLOW).flat().length} names checked, all registered`);