Branchwell

Jotley / app · jotley.ai / scripts / mcp-desktop-bridge-check.cjs

1'use strict';
2
3// Runs only against the integration harness's synthetic, in-memory library.
4const { spawn } = require('node:child_process');
5const readline = require('node:readline');
6const assert = require('node:assert/strict');
7const nativeBridge = process.env.JOTLEY_NATIVE_BRIDGE_BINARY;
8const bridge = spawn(nativeBridge || process.execPath, nativeBridge ? ['--mcp-stdio'] : [process.env.JOTLEY_BRIDGE_SCRIPT], {
9 env: process.env, stdio: ['pipe', 'pipe', 'pipe']
10});
11const pending = new Map();
12let nextID = 0;
13let diagnostics = '';
14bridge.stderr.on('data', chunk => { diagnostics += chunk; });
15readline.createInterface({ input: bridge.stdout }).on('line', line => {
16 const message = JSON.parse(line);
17 const request = pending.get(message.id);
18 assert.ok(request, 'Unexpected or duplicate response');
19 pending.delete(message.id);
20 clearTimeout(request.timer);
21 if (message.error) request.reject(new Error(message.error.message));
22 else request.resolve(message.result);
23});
24bridge.on('exit', () => {
25 for (const request of pending.values()) {
26 clearTimeout(request.timer);
27 request.reject(new Error('Bridge exited before responding'));
28 }
29});
30
31function call(method, params) {
32 return new Promise((resolve, reject) => {
33 const id = ++nextID;
34 const timer = setTimeout(() => reject(new Error('Bridge request timed out')), 15000);
35 pending.set(id, { resolve, reject, timer });
36 bridge.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n');
37 });
38}
39function tool(name, args) { return call('tools/call', { name, arguments: args }); }
40function json(result) {
41 assert.notEqual(result.isError, true, 'Tool failed');
42 return JSON.parse(result.content.filter(item => item.type === 'text').map(item => item.text).join('\n'));
43}
44
45async function main() {
46 const initialized = await call('initialize', {
47 protocolVersion: '2025-11-25', capabilities: {},
48 clientInfo: { name: 'Jotley Claude extension test', version: '1.0.0' }
49 });
50 assert.equal(initialized.serverInfo.name, 'Jotley');
51 assert.match(initialized.instructions, /Code tab/);
52 assert.match(initialized.instructions, /Codex/);
53 bridge.stdin.write(JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' }) + '\n');
54 const tools = await call('tools/list', {});
55 assert.equal(tools.tools.length, 25);
56 const found = json(await tool('search_notes', { query: 'Maya' }));
57 assert.ok(found.notes.length > 0);
58 if (process.env.JOTLEY_BRIDGE_WRITE === '1') {
59 const created = json(await tool('create_note', { title: 'Desktop extension test', summary: '- [ ] Review onboarding', date: '2026-11-10T12:30:00Z' }));
60 const read = json(await tool('read_note', { id: created.id }));
61 assert.match(JSON.stringify(read), /Review onboarding/);
62 const updated = json(await tool('update_note', { id: created.id, expected_revision: read.revision, changes: { title: 'Desktop extension verified', description: 'Changed through the real bridge' } }));
63 assert.equal(updated.title, 'Desktop extension verified');
64 const deleted = json(await tool('delete_note', { id: created.id, expected_revision: updated.revision }));
65 assert.equal(deleted.deleted, true);
66 } else {
67 const denied = await tool('create_note', { title: 'Must not be created' });
68 assert.equal(denied.isError, true);
69 }
70 assert.equal(diagnostics, '');
71 console.log('PASS: ' + (nativeBridge ? 'Bundled native STDIO' : 'Claude extension') + ' bridge handshake, 25 tools, reading and ' + (process.env.JOTLEY_BRIDGE_WRITE === '1' ? 'create/edit/delete' : 'read-only enforcement'));
72}
73main().catch(error => { console.error(error.message); process.exitCode = 1; }).finally(() => {
74 bridge.stdin.end();
75 bridge.kill();
76});