figma-rust-mcp 5.0.0 → 5.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -422,21 +422,49 @@ recently used entry when its 64-entry capacity is reached.
422
422
 
423
423
  ## 📏 Benchmarks
424
424
 
425
- `npm run bench` builds the release binary, spawns it on a random port and prints:
425
+ `npm run bench` builds the release binary and prints a JSON report from a fresh
426
+ server: cold start, process startup, server RAM and MCP dispatch latency. A fresh
427
+ server has no Figma session, so plugin and index measurements are explicitly
428
+ skipped.
426
429
 
427
- | Metric | How it is measured |
430
+ To measure real work, connect the Figma plugin and use the existing server:
431
+
432
+ ```bash
433
+ # Server report, including its current index and plugin round trip
434
+ node scripts/bench.mjs --url http://127.0.0.1:41730
435
+
436
+ # Read all descendants of a frame, index that frame, and export it as a 1x PNG
437
+ node scripts/bench.mjs --url http://127.0.0.1:41730 \
438
+ --target 'SESSION_ID=FRAME_ID' --samples 3 > benchmark.json
439
+
440
+ # Run the same workloads concurrently on two tabs
441
+ node scripts/bench.mjs --url http://127.0.0.1:41730 \
442
+ --target 'FIRST_SESSION=FIRST_FRAME' --target 'SECOND_SESSION=SECOND_FRAME'
443
+ ```
444
+
445
+ Find session IDs at `GET /sessions` and frame IDs in the plugin selection card.
446
+ Use one frame per session and 1–10 samples per workload. Workloads read Figma
447
+ content; they do not modify canvas nodes. Indexing updates the bridge's cache,
448
+ and PNG export may briefly move the viewport before restoring it.
449
+
450
+ | Measurement | What it includes |
428
451
  |---|---|
429
- | Cold start | `spawn` → first successful `GET /health`, from outside the process |
430
- | Startup | `main()` entry → HTTP listener bound |
431
- | RAM | Resident set size (macOS/Linux; not reported on Windows) |
432
452
  | MCP `tools/list` | 200 in-process dispatches, p50/p95 |
433
- | Index search | 200 searches over the current tab's index, p50/p95 (plugin tab only) |
434
- | Plugin round trip | 10 `get_viewport` calls through Figma, p50/p95 (plugin tab only) |
435
-
436
- The plugin's **Bench** tab runs the same report (`POST /benchmark`) against the
437
- live server, so index and plugin numbers reflect your real file. Sample run on an
438
- Apple Silicon Mac (v4.0.1): cold start 45–530ms, startup 26–126ms, RSS ~7.6MB,
439
- `tools/list` p50 0.07ms. Your numbers will differ; quote your own.
453
+ | Index search | 200 searches over the current index, when available |
454
+ | Plugin round trip | 10 `get_viewport` calls, when connected |
455
+ | Frame read | All paginated `read_nodes` responses, node/page counts and JSON bytes |
456
+ | Frame index | Plugin scan and response time; bridge commit acknowledgement is separate |
457
+ | PNG export | Figma export, transfer and decoded image bytes |
458
+ | Concurrent tabs | Workloads run sequentially per tab and concurrently across tabs |
459
+ | RAM | Server RSS before/after workloads, not peak RAM or Figma's memory |
460
+
461
+ Reports contain each sample plus p50/p95/max. Reads use the normal cache, so the
462
+ samples are not guaranteed cold reads. Truncated reads remain marked incomplete;
463
+ failed workloads retain their error and the CLI exits with status 1. Real Figma
464
+ results depend on your file; simulated integration tests only verify the path.
465
+
466
+ The plugin's existing **Bench** tab retains the basic `POST /benchmark` report.
467
+ Use the CLI for the heavier frame and concurrent-tab workloads.
440
468
 
441
469
  ## 💻 Development & Testing
442
470
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "figma-rust-mcp",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "description": "Claude Code to Figma — Bidirectional MCP bridge. AI draws UI on Figma canvas, reads designs back. Works with Claude, Cursor, VS Code, Windsurf, Antigravity. No API key needed.",
5
5
  "mcpName": "io.github.BuiHung1612/figma-rust-mcp",
6
6
  "type": "module",
@@ -0,0 +1,85 @@
1
+ // Read-only live workloads. The server and Figma plugin must already be running.
2
+ export async function requestJson(base, route, body) {
3
+ const response = await fetch(new URL(route, base), {
4
+ method: body === undefined ? 'GET' : 'POST',
5
+ headers: body === undefined ? {} : { 'Content-Type': 'application/json' },
6
+ body: body === undefined ? undefined : JSON.stringify(body),
7
+ signal: AbortSignal.timeout(120000),
8
+ });
9
+ if (!response.ok) throw new Error(`${route}: HTTP ${response.status}`);
10
+ return response.json();
11
+ }
12
+
13
+ export function summarize(samples) {
14
+ const times = samples.map(sample => sample.durationMs).sort((a, b) => a - b);
15
+ const pick = q => times[Math.round((times.length - 1) * q)];
16
+ return { n: times.length, p50Ms: pick(.5), p95Ms: pick(.95), maxMs: times.at(-1), samples };
17
+ }
18
+
19
+ export async function benchmarkTargets(base, targets, samples = 3) {
20
+ if (!Number.isInteger(samples) || samples < 1 || samples > 10) throw new Error('samples must be from 1 to 10');
21
+ if (!targets.length || targets.some(target => !target.sessionId || !target.nodeId)) throw new Error('Each target needs sessionId and nodeId');
22
+ if (new Set(targets.map(target => target.sessionId)).size !== targets.length) throw new Error('Use one target per session for concurrent tab measurements');
23
+ const memoryBefore = (await requestJson(base, '/health')).stats?.memoryMb ?? null;
24
+ const started = performance.now();
25
+ const results = await Promise.all(targets.map(async ({ sessionId, nodeId }) => {
26
+ const workloads = {};
27
+ const exec = async (operation, params) => {
28
+ const result = await requestJson(base, `/exec?sessionId=${encodeURIComponent(sessionId)}`, { operation, params });
29
+ if (!result.success) throw new Error(result.error || `${operation} failed`);
30
+ return result.data;
31
+ };
32
+ const runs = {
33
+ readFrame: async () => {
34
+ let cursor, nodes = 0, pages = 0, responseBytes = 0, complete;
35
+ const cursors = new Set();
36
+ do {
37
+ const data = await exec('read_nodes', cursor ? { cursor, limit: 500 } :
38
+ { id: nodeId, depth: 'full', expandInstances: true, fields: ['geometry', 'content', 'style', 'text'], limit: 500 });
39
+ if (!Array.isArray(data.nodes)) throw new Error('read_nodes returned no node list');
40
+ nodes += data.nodes.length; pages++;
41
+ responseBytes += Buffer.byteLength(JSON.stringify(data));
42
+ complete = data.complete === true;
43
+ cursor = data.nextCursor;
44
+ if (cursor && cursors.has(cursor)) throw new Error('read_nodes repeated a cursor');
45
+ if (cursor) cursors.add(cursor);
46
+ if (cursor && pages >= 100) throw new Error('read_nodes exceeded 100 pages');
47
+ } while (cursor);
48
+ return { nodes, pages, responseBytes, complete };
49
+ },
50
+ indexFrame: async () => {
51
+ const data = await exec('index_scan', { id: nodeId, depth: 'full', expandInstances: true, deferComponents: true });
52
+ if (data.cancelled) throw new Error('Index cancelled by a document or page change');
53
+ return { complete: data.complete === true, nodesTruncated: data.nodesTruncated === true };
54
+ },
55
+ exportPng: async () => {
56
+ const data = await exec('export_image', { id: nodeId, format: 'PNG', scale: 1 });
57
+ if (typeof data.base64 !== 'string') throw new Error('export_image returned no image');
58
+ return { imageBytes: Buffer.from(data.base64, 'base64').length, width: data.width, height: data.height };
59
+ },
60
+ };
61
+ for (const [name, run] of Object.entries(runs)) {
62
+ const timings = [];
63
+ try {
64
+ for (let i = 0; i < samples; i++) {
65
+ const start = performance.now();
66
+ const data = await run();
67
+ timings.push({ durationMs: performance.now() - start, ...data });
68
+ }
69
+ workloads[name] = summarize(timings);
70
+ } catch (error) {
71
+ workloads[name] = { error: error.message, completedSamples: timings.length, samples: timings };
72
+ break; // Do not keep loading a tab after a timeout or disconnect.
73
+ }
74
+ }
75
+ return { sessionId, nodeId, workloads };
76
+ }));
77
+ const memoryAfter = (await requestJson(base, '/health')).stats?.memoryMb ?? null;
78
+ return {
79
+ mode: 'live', concurrentTabs: targets.length, samplesPerWorkload: samples,
80
+ wallTimeMs: performance.now() - started,
81
+ serverRssBeforeMb: memoryBefore, serverRssAfterMb: memoryAfter,
82
+ note: 'RSS is server memory at the boundaries, not peak memory or Figma memory. Reads may use the normal cache. Index timing includes plugin scan/response, not bridge commit acknowledgement.',
83
+ targets: results,
84
+ };
85
+ }
package/scripts/bench.mjs CHANGED
@@ -1,37 +1,59 @@
1
- // Benchmark: cold start measured from outside (spawn → /health answers), then the
2
- // server's in-process report from POST /benchmark (same numbers as the plugin "Bench" tab).
3
- // Usage: npm run bench or node scripts/bench.mjs [path/to/figma-rust-mcp]
4
- import { spawn } from "node:child_process";
5
- import { statSync } from "node:fs";
1
+ // Cold: node scripts/bench.mjs [binary]
2
+ // Live: node scripts/bench.mjs --url http://127.0.0.1:41730 --target SESSION=NODE [--target SESSION=NODE] [--samples 3]
3
+ import { spawn } from 'node:child_process';
4
+ import { createServer } from 'node:net';
5
+ import { statSync } from 'node:fs';
6
+ import { benchmarkTargets, requestJson } from './bench-workloads.mjs';
6
7
 
7
- const bin = process.argv[2] ?? `target/release/figma-rust-mcp${process.platform === "win32" ? ".exe" : ""}`;
8
- const port = 40000 + Math.floor(Math.random() * 10000);
9
- const base = `http://127.0.0.1:${port}`;
10
-
11
- const t0 = performance.now();
12
- const child = spawn(bin, ["--server", "--port", String(port)], { stdio: "ignore" });
13
- let coldStartMs;
8
+ const args = process.argv.slice(2), targets = [];
9
+ let base, samples = 3, bin = `target/release/figma-rust-mcp${process.platform === 'win32' ? '.exe' : ''}`;
10
+ for (let i = 0; i < args.length; i++) {
11
+ const arg = args[i];
12
+ if (arg === '--url') base = args[++i];
13
+ else if (arg === '--samples') samples = Number(args[++i]);
14
+ else if (arg === '--target') {
15
+ const target = args[++i] || '', separator = target.indexOf('=');
16
+ if (separator < 1 || separator === target.length - 1) throw new Error('--target must be SESSION=NODE');
17
+ targets.push({ sessionId: target.slice(0, separator), nodeId: target.slice(separator + 1) });
18
+ } else if (!arg.startsWith('-') && i === 0) bin = arg;
19
+ else throw new Error(`Unknown argument: ${arg}`);
20
+ }
21
+ if (args.includes('--url') && !base) throw new Error('--url needs a server URL');
22
+ if (!Number.isInteger(samples) || samples < 1 || samples > 10) throw new Error('--samples must be from 1 to 10');
23
+ if (targets.length && !base) throw new Error('--target requires --url for an existing Figma connection');
24
+ if (base && !['http:', 'https:'].includes(new URL(base).protocol)) throw new Error('--url must use HTTP or HTTPS');
25
+ let child, coldStartMs;
14
26
  try {
15
- while (coldStartMs === undefined) {
16
- if (performance.now() - t0 > 10_000) throw new Error("server did not answer /health within 10s");
17
- try {
18
- if ((await fetch(`${base}/health`)).ok) coldStartMs = performance.now() - t0;
19
- } catch {
20
- await new Promise((r) => setTimeout(r, 2));
27
+ if (!base) {
28
+ const reservation = createServer();
29
+ await new Promise(resolve => reservation.listen(0, '127.0.0.1', resolve));
30
+ const port = reservation.address().port;
31
+ await new Promise(resolve => reservation.close(resolve));
32
+ base = `http://127.0.0.1:${port}`;
33
+ const start = performance.now();
34
+ let spawnError;
35
+ child = spawn(bin, ['--server', '--port', String(port)], { stdio: 'ignore' });
36
+ child.on('error', error => { spawnError = error; });
37
+ while (coldStartMs === undefined) {
38
+ if (spawnError) throw spawnError;
39
+ if (child.exitCode !== null) throw new Error(`Server exited: ${child.exitCode}`);
40
+ if (performance.now() - start > 10000) throw new Error('Server did not answer /health within 10s');
41
+ try {
42
+ const response = await fetch(`${base}/health`, { signal: AbortSignal.timeout(500) });
43
+ if (response.ok) coldStartMs = performance.now() - start;
44
+ } catch {}
45
+ if (coldStartMs === undefined) await new Promise(resolve => setTimeout(resolve, 2));
21
46
  }
22
47
  }
23
- const r = await (await fetch(`${base}/benchmark`, { method: "POST" })).json();
24
- const lat = (s) => (s ? `p50 ${s.p50Ms.toFixed(3)}ms · p95 ${s.p95Ms.toFixed(3)}ms (n=${s.n})` : "skipped");
25
- const rows = {
26
- version: `v${r.version}`,
27
- "binary size": `${(statSync(bin).size / 1024 / 1024).toFixed(1)}MB`,
28
- "cold start (spawn → /health)": `${coldStartMs.toFixed(1)}ms`,
29
- "startup (main → listening)": r.startupMs != null ? `${r.startupMs.toFixed(2)}ms` : "—",
30
- "RAM (RSS)": r.memoryMb != null ? `${r.memoryMb.toFixed(1)}MB` : "n/a on this OS",
31
- "MCP tools/list (in-process)": lat(r.mcpToolsList),
32
- "plugin round trip": r.pluginRoundTrip ? lat(r.pluginRoundTrip) : "skipped (fresh server has no Figma session)",
33
- };
34
- for (const [k, v] of Object.entries(rows)) console.log(`${k.padEnd(30)} ${v}`);
48
+ const report = await requestJson(base, '/benchmark', {});
49
+ report.mode = child ? 'cold-start' : 'live';
50
+ if (child) {
51
+ report.coldStartMs = coldStartMs;
52
+ report.binarySizeMb = statSync(bin).size / 1024 / 1024;
53
+ }
54
+ if (targets.length) report.workloads = await benchmarkTargets(base, targets, samples);
55
+ console.log(JSON.stringify(report, null, 2));
56
+ if (report.workloads?.targets.some(target => Object.values(target.workloads).some(workload => workload.error || workload.samples?.some(sample => sample.complete === false)))) process.exitCode = 1;
35
57
  } finally {
36
- child.kill();
58
+ child?.kill();
37
59
  }
@@ -0,0 +1,22 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import vm from 'node:vm';
3
+
4
+ export function indexSyncUi(socket = { readyState: 1, send() {} }) {
5
+ const html = readFileSync('plugin-runtime/ui.html', 'utf8');
6
+ const element = () => {
7
+ const classes = new Set();
8
+ return { disabled: false, title: 'Sync index', attributes: {},
9
+ setAttribute(name, value) { this.attributes[name] = value; },
10
+ classList: { add: name => classes.add(name), remove: name => classes.delete(name),
11
+ contains: name => classes.has(name), toggle: (name, active) => active ? classes.add(name) : classes.delete(name) } };
12
+ };
13
+ const timers = new Map();
14
+ let timerId = 0;
15
+ const ui = vm.createContext({ reindexBtn: element(), indexProgressWrap: element(), indexProgressText: element(),
16
+ ws: socket, WebSocket: { OPEN: 1 }, log() {},
17
+ setTimeout(callback, delay) { timers.set(++timerId, { callback, delay }); return timerId; },
18
+ clearTimeout(id) { timers.delete(id); } });
19
+ vm.runInContext(html.slice(html.indexOf(' var activeIndexScan ='), html.indexOf(' function focusSelection()')), ui);
20
+ ui.fireTimers = delay => { for (const [id, timer] of timers) if (timer.delay === delay) { timers.delete(id); timer.callback(); } };
21
+ return ui;
22
+ }
@@ -0,0 +1,98 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { createServer } from 'node:http';
4
+ import { spawn } from 'node:child_process';
5
+ import { benchmarkTargets, summarize } from './bench-workloads.mjs';
6
+
7
+ async function fixture(t, exec) {
8
+ const calls = [];
9
+ const server = createServer(async (request, response) => {
10
+ try {
11
+ const url = new URL(request.url, 'http://localhost');
12
+ response.setHeader('Content-Type', 'application/json');
13
+ if (url.pathname === '/health') return response.end(JSON.stringify({ stats: { memoryMb: 12 } }));
14
+ if (url.pathname === '/benchmark') return response.end(JSON.stringify({ version: 'test', pluginRoundTrip: null }));
15
+ let body = '';
16
+ for await (const chunk of request) body += chunk;
17
+ const call = { sessionId: url.searchParams.get('sessionId'), ...JSON.parse(body) };
18
+ calls.push(call);
19
+ response.end(JSON.stringify(await exec(call)));
20
+ } catch (error) {
21
+ response.statusCode = 500; response.end(JSON.stringify({ error: error.message }));
22
+ }
23
+ });
24
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
25
+ t.after(() => { server.closeAllConnections(); server.close(); });
26
+ return { base: `http://127.0.0.1:${server.address().port}`, calls };
27
+ }
28
+
29
+ const result = data => ({ success: true, data });
30
+ function success(call) {
31
+ if (call.operation === 'read_nodes') return result({ nodes: [{ id: call.params.cursor ? 'child' : call.params.id }],
32
+ nextCursor: call.params.cursor ? null : `${call.sessionId}:next`, complete: !!call.params.cursor });
33
+ if (call.operation === 'index_scan') return result({ complete: true, nodesTruncated: false });
34
+ if (call.operation === 'export_image') return result({ base64: Buffer.from('png').toString('base64'), width: 100, height: 80 });
35
+ throw new Error(`Unexpected operation: ${call.operation}`);
36
+ }
37
+
38
+ function cli(args) {
39
+ return new Promise((resolve, reject) => {
40
+ const child = spawn(process.execPath, ['scripts/bench.mjs', ...args]);
41
+ let stdout = '', stderr = '';
42
+ child.stdout.on('data', chunk => { stdout += chunk; });
43
+ child.stderr.on('data', chunk => { stderr += chunk; });
44
+ child.on('error', reject);
45
+ child.on('exit', code => resolve({ code, stdout, stderr }));
46
+ });
47
+ }
48
+
49
+ test('live benchmark paginates reads, measures all workloads and runs tabs concurrently', { timeout: 5000 }, async t => {
50
+ let active = 0, peak = 0;
51
+ const { base, calls } = await fixture(t, async call => {
52
+ active++; peak = Math.max(peak, active);
53
+ await new Promise(resolve => setTimeout(resolve, 5));
54
+ active--;
55
+ return success(call);
56
+ });
57
+ const report = await benchmarkTargets(base, [{ sessionId: 'one', nodeId: 'frame:1' }, { sessionId: 'two', nodeId: 'frame:2' }], 2);
58
+ assert.equal(report.concurrentTabs, 2);
59
+ assert.ok(peak >= 2);
60
+ assert.equal(report.serverRssBeforeMb, 12);
61
+ for (const target of report.targets) {
62
+ assert.equal(target.workloads.readFrame.n, 2);
63
+ assert.equal(target.workloads.readFrame.samples[0].nodes, 2);
64
+ assert.equal(target.workloads.readFrame.samples[0].pages, 2);
65
+ assert.equal(target.workloads.readFrame.samples[0].complete, true);
66
+ assert.ok(target.workloads.readFrame.samples[0].responseBytes > 0);
67
+ assert.equal(target.workloads.indexFrame.samples[0].complete, true);
68
+ assert.equal(target.workloads.exportPng.samples[0].imageBytes, 3);
69
+ }
70
+ assert.equal(calls.length, 16);
71
+ assert.ok(calls.filter(call => call.params.cursor).every(call => call.params.cursor.startsWith(call.sessionId)));
72
+ });
73
+
74
+ test('live benchmark exposes bridge failures and stops loading the failed tab', async t => {
75
+ const { base, calls } = await fixture(t, () => ({ success: false, error: 'Plugin disconnected' }));
76
+ const report = await benchmarkTargets(base, [{ sessionId: 'one', nodeId: 'frame' }]);
77
+ assert.equal(report.targets[0].workloads.readFrame.error, 'Plugin disconnected');
78
+ assert.equal(calls.length, 1);
79
+ await assert.rejects(benchmarkTargets(base, [{ sessionId: 'one', nodeId: 'a' }, { sessionId: 'one', nodeId: 'b' }]), /one target per session/);
80
+ await assert.rejects(benchmarkTargets(base, [{ sessionId: 'one', nodeId: 'a' }], 0), /samples/);
81
+ });
82
+
83
+ test('benchmark rejects repeated cursors and CLI preserves failure status in JSON', async t => {
84
+ const { base } = await fixture(t, () => result({ nodes: [], nextCursor: 'same', complete: false }));
85
+ const output = await cli(['--url', base, '--target', 'one=frame', '--samples', '1']);
86
+ assert.equal(output.code, 1, output.stderr);
87
+ const report = JSON.parse(output.stdout);
88
+ assert.match(report.workloads.targets[0].workloads.readFrame.error, /repeated a cursor/);
89
+ const invalid = await cli(['--target', 'invalid']);
90
+ assert.equal(invalid.code, 1);
91
+ assert.match(invalid.stderr, /SESSION=NODE/);
92
+ });
93
+
94
+ test('benchmark reports sorted percentile samples', () => {
95
+ const report = summarize([5, 1, 3, 2, 4].map(durationMs => ({ durationMs })));
96
+ assert.equal(report.p50Ms, 3);
97
+ assert.equal(report.p95Ms, 5);
98
+ });
@@ -158,3 +158,121 @@ test('multiple tabs, reconnect ownership and independent frame tasks across MCP
158
158
  const restarted = decode(ok(await rpc('figma_task', { action: 'start', sessionId: 'tab-b', frameId: 'frame-c' })));
159
159
  ok(await rpc('figma_task', { action: 'end', taskId: restarted.taskId }));
160
160
  });
161
+
162
+ test('index sync travels from plugin through UI to bridge and returns a matching confirmation', { timeout: 20000 }, async t => {
163
+ const { indexSyncUi } = await import('./helpers/index-sync-ui.mjs');
164
+ const reservation = createServer();
165
+ await new Promise(resolve => reservation.listen(0, '127.0.0.1', resolve));
166
+ const port = reservation.address().port;
167
+ await new Promise(resolve => reservation.close(resolve));
168
+ const server = spawn(process.env.FIGMA_TEST_BINARY || 'target/debug/figma-rust-mcp',
169
+ ['--server', '--port', String(port)], { stdio: ['ignore', 'ignore', 'pipe'] });
170
+ let stderr = ''; server.stderr.on('data', bytes => { stderr = (stderr + bytes).slice(-4000); });
171
+ const sockets = [];
172
+ t.after(() => { sockets.forEach(socket => socket.close()); server.kill(); });
173
+ const base = `http://127.0.0.1:${port}`;
174
+ await until(async () => { try { return (await fetch(`${base}/health`)).ok; } catch { assert.equal(server.exitCode, null, stderr); return false; } });
175
+ const ui = indexSyncUi(null);
176
+ const page = { id: 'page', children: [{ id: 'frame', name: 'Frame', type: 'FRAME', width: 100, height: 100 }] };
177
+ const runtime = vm.createContext({ figma: { currentPage: page, root: { name: 'Sync test' },
178
+ ui: { postMessage: msg => ui.handleIndexMessage(msg) } }, handlers: {}, console, setTimeout, Map, Set, Uint8Array });
179
+ for (const file of ['utils', 'token-helpers', 'read-helpers', 'handlers-read-detail', 'handlers-read', 'handlers-tokens', 'handlers-write-ops', 'node-reader']) {
180
+ vm.runInContext(readFileSync(`plugin-src/${file}.js`, 'utf8'), runtime);
181
+ }
182
+ runtime.handlers.get_styles = async () => ({});
183
+ runtime.handlers.get_variables = async () => ({});
184
+ const html = readFileSync('plugin-runtime/ui.html', 'utf8');
185
+ Object.assign(ui, { WebSocket, BRIDGE: base, sessionId: 'sync-test', fileName: 'Sync test', documentId: 'sync-doc',
186
+ uiDisposed: false, wsConnected: false, everConnected: false, retryTimer: null, polling: false, consecutiveErrors: 0,
187
+ currentPort: port, window: {}, READ_OPS: [], setInterval: () => 1, clearInterval() {},
188
+ sendRuntimeCapabilities() {}, setStatus() {}, updateRuntimeVersion() {}, startLongPoll() {},
189
+ dispatchToMain() {}, showSetupGuide() {}, parent: { postMessage: () => runtime.handlers.index_scan({ deferComponents: true }) } });
190
+ vm.runInContext(html.slice(html.indexOf(' function connectWs()'), html.indexOf(' async function startLongPoll()')), ui);
191
+ const connect = async () => { ui.connectWs(); sockets.push(ui.ws); await until(() => ui.wsConnected); };
192
+ await connect();
193
+ const confirmed = ui.receiveIndexAck;
194
+ const acks = [];
195
+ ui.receiveIndexAck = ack => acks.push(ack);
196
+ await runtime.handlers.index_scan({ deferComponents: true });
197
+ await until(() => acks.length === 1);
198
+ assert.equal(acks[0].success, true);
199
+ assert.equal(ui.reindexBtn.disabled, true, 'local scan completion must wait for bridge ack');
200
+ confirmed({ ...acks[0], scanId: 'older-scan' });
201
+ assert.equal(ui.reindexBtn.disabled, true);
202
+ confirmed(acks.shift());
203
+ assert.equal(ui.reindexBtn.disabled, false);
204
+ assert.match(ui.reindexBtn.title, /Last synced/);
205
+ const lastSuccess = ui.reindexBtn.title;
206
+ ui.receiveIndexAck = ack => { acks.push(ack); confirmed(ack); };
207
+ const unmatched = { type: 'index-start', pageId: 'page', scanId: 'unmatched', revision: 0, scope: { id: 'page' } };
208
+ // The UI begins a scan but the bridge never receives its start/chunks.
209
+ ui.handleIndexMessage({ ...unmatched, type: 'index-progress', stage: 'starting' });
210
+ ui.handleIndexMessage({ ...unmatched, type: 'index-update', data: {} });
211
+ await until(() => acks.length === 1);
212
+ assert.equal(acks[0].success, false);
213
+ assert.equal(ui.reindexBtn.disabled, false);
214
+ assert.match(ui.indexProgressText.textContent, /no longer active/);
215
+ assert.equal(ui.reindexBtn.title, lastSuccess);
216
+
217
+ ui.handleIndexMessage({ ...unmatched, scanId: 'disconnect' });
218
+ ui.ws.close();
219
+ await until(() => !ui.wsConnected);
220
+ assert.equal(ui.reindexBtn.disabled, false);
221
+ assert.match(ui.indexProgressText.textContent, /disconnected/);
222
+ await connect();
223
+ acks.length = 0;
224
+ await ui.triggerManualReindex();
225
+ await until(() => acks.length === 1);
226
+ assert.equal(acks[0].success, true);
227
+ assert.equal(ui.reindexBtn.disabled, false);
228
+
229
+ runtime.handlers.get_styles = async () => { throw new Error('Styles unavailable'); };
230
+ await assert.rejects(runtime.handlers.index_scan({ deferComponents: true }), /Styles unavailable/);
231
+ assert.equal(ui.reindexBtn.disabled, false);
232
+ assert.match(ui.indexProgressText.textContent, /Styles unavailable/);
233
+ runtime.handlers.get_styles = async () => { runtime.figma.currentPage = { id: 'new-page', children: [] }; return {}; };
234
+ const cancelled = await runtime.handlers.index_scan({ deferComponents: true });
235
+ assert.equal(cancelled.cancelled, true);
236
+ assert.equal(ui.reindexBtn.disabled, false);
237
+ assert.match(ui.indexProgressText.textContent, /Document changed/);
238
+ runtime.handlers.get_styles = async () => ({});
239
+ acks.length = 0;
240
+ await runtime.handlers.index_scan({ deferComponents: true });
241
+ await until(() => acks.length === 1);
242
+ assert.equal(acks[0].pageId, 'new-page');
243
+ assert.equal(acks[0].success, true);
244
+ assert.equal(ui.reindexBtn.disabled, false);
245
+
246
+ const { benchmarkTargets } = await import('./bench-workloads.mjs');
247
+ page.type = 'PAGE';
248
+ const frame = page.children[0];
249
+ frame.parent = page;
250
+ frame.children = Array.from({ length: 600 }, (_, i) => ({ id: `bench:${i}`, type: 'TEXT',
251
+ name: `Child ${i}`, characters: `Text ${i}`, parent: frame, width: 10, height: 10 }));
252
+ let exported = 0;
253
+ frame.exportAsync = async () => { exported++; return new Uint8Array([1, 2, 3]); };
254
+ runtime.figma.currentPage = page;
255
+ runtime.figma.getNodeByIdAsync = async id => id === frame.id ? frame : null;
256
+ const operations = [];
257
+ ui.READ_OPS = ['read_nodes', 'index_scan', 'export_image'];
258
+ ui.dispatchToMain = async req => {
259
+ if (!req.operation) return;
260
+ operations.push(req.operation);
261
+ try {
262
+ const data = await runtime.handlers[req.operation](req.params);
263
+ ui.ws.send(JSON.stringify({ id: req.id, sessionId: ui.sessionId, success: true, data }));
264
+ } catch (error) {
265
+ ui.ws.send(JSON.stringify({ id: req.id, sessionId: ui.sessionId, success: false, error: error.message }));
266
+ }
267
+ };
268
+ const measurements = await benchmarkTargets(base, [{ sessionId: ui.sessionId, nodeId: frame.id }], 1);
269
+ const workloads = measurements.targets[0].workloads;
270
+ assert.equal(workloads.readFrame.samples[0].nodes, 601);
271
+ assert.equal(workloads.readFrame.samples[0].pages, 2);
272
+ assert.equal(workloads.readFrame.samples[0].complete, true);
273
+ assert.equal(workloads.indexFrame.samples[0].complete, true);
274
+ assert.equal(workloads.exportPng.samples[0].imageBytes, 3);
275
+ assert.equal(exported, 1);
276
+ assert.deepEqual(operations, ['read_nodes', 'read_nodes', 'index_scan', 'export_image']);
277
+
278
+ });
@@ -1,4 +1,5 @@
1
1
  import { test } from 'node:test';
2
+ import { indexSyncUi } from './helpers/index-sync-ui.mjs';
2
3
  import assert from 'node:assert/strict';
3
4
  import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
4
5
  import os from 'node:os';
@@ -185,6 +186,76 @@ test('UI badge uses release version initially and follows the server on reconnec
185
186
  assert.ok(html.includes('type: "runtime-ready"'));
186
187
  });
187
188
 
189
+ test('sync waits for a matching bridge confirmation and ignores final and silent progress', async () => {
190
+ const ui = indexSyncUi(), messages = [];
191
+ const page = { id: 'page', children: [] };
192
+ const r = runtime({ currentPage: page, root: { name: 'File' },
193
+ ui: { postMessage: message => { messages.push(message); ui.handleIndexMessage(message); } } });
194
+ r.handlers.get_styles = async () => ({});
195
+ r.handlers.get_variables = async () => ({});
196
+ for (let i = 0; i < 2; i++) {
197
+ await r.handlers.index_scan({ deferComponents: true });
198
+ assert.equal(messages.at(-1).stage, 'done');
199
+ assert.equal(ui.reindexBtn.disabled, true);
200
+ assert.match(ui.indexProgressText.textContent, /confirmation/);
201
+ ui.receiveIndexAck({ scanId: 'old', pageId: 'page', success: true });
202
+ assert.equal(ui.reindexBtn.disabled, true);
203
+ const update = messages.findLast(msg => msg.type === 'index-update');
204
+ ui.receiveIndexAck({ ...update, success: true });
205
+ assert.equal(ui.reindexBtn.disabled, false);
206
+ assert.equal(ui.reindexBtn.classList.contains('indexing'), false);
207
+ assert.match(ui.reindexBtn.title, /Last synced/);
208
+ ui.handleIndexMessage({ ...update, type: 'index-progress', stage: 'done' });
209
+ ui.handleIndexMessage({ ...update, type: 'index-progress', stage: 'starting', silent: true });
210
+ assert.equal(ui.reindexBtn.disabled, false);
211
+ ui.fireTimers(1200);
212
+ assert.equal(ui.indexProgressWrap.classList.contains('active'), false);
213
+ }
214
+ });
215
+
216
+ test('sync reports read failures, page cancellation, rejection, missing ack and disconnect', async () => {
217
+ const ui = indexSyncUi();
218
+ const page = { id: 'page', children: [] };
219
+ const r = runtime({ currentPage: page, root: { name: 'File' },
220
+ ui: { postMessage: message => ui.handleIndexMessage(message) } });
221
+ r.handlers.read_nodes = async () => { throw new Error('Cannot read layers'); };
222
+ await assert.rejects(r.handlers.index_scan({}), /Cannot read layers/);
223
+ assert.equal(ui.reindexBtn.disabled, false);
224
+ assert.match(ui.indexProgressText.textContent, /Cannot read layers/);
225
+ r.handlers.read_nodes = async () => {
226
+ r.figma.currentPage = { id: 'next' };
227
+ return { revision: 0, scope: { id: 'page' }, nodes: [] };
228
+ };
229
+ await r.handlers.index_scan({});
230
+ assert.equal(ui.reindexBtn.disabled, false);
231
+ assert.match(ui.indexProgressText.textContent, /Document changed/);
232
+ const start = { type: 'index-start', scanId: 'retry', pageId: 'page' };
233
+ ui.handleIndexMessage(start);
234
+ ui.receiveIndexAck({ ...start, success: false, error: 'Rejected stale revision' });
235
+ assert.match(ui.indexProgressText.textContent, /Rejected stale revision/);
236
+ ui.handleIndexMessage(start);
237
+ ui.handleIndexMessage({ ...start, type: 'index-update' });
238
+ ui.fireTimers(30000);
239
+ assert.equal(ui.reindexBtn.disabled, false);
240
+ assert.match(ui.indexProgressText.textContent, /No bridge confirmation/);
241
+ ui.ws.readyState = 3;
242
+ ui.handleIndexMessage(start);
243
+ assert.equal(ui.reindexBtn.disabled, false);
244
+ assert.match(ui.indexProgressText.textContent, /disconnected/);
245
+ assert.equal(ui.reindexBtn.title, 'Sync index');
246
+ ui.ws.readyState = 1;
247
+ ui.parent = { postMessage() {} };
248
+ ui.triggerManualReindex();
249
+ ui.fireTimers(30000);
250
+ assert.equal(ui.reindexBtn.disabled, false);
251
+ assert.match(ui.indexProgressText.textContent, /Canvas did not start/);
252
+ ui.handleIndexMessage(start);
253
+ ui.receiveIndexAck({ ...start, success: true });
254
+ ui.handleIndexMessage({ ...start, scanId: 'new' });
255
+ ui.fireTimers(1200);
256
+ assert.equal(ui.indexProgressWrap.classList.contains('active'), true);
257
+ });
258
+
188
259
  test('build rejects wrong release tags and checked-in bundle matches source', () => {
189
260
  const check = spawnSync(process.execPath, ['scripts/build-plugin.js', '--check'], { encoding: 'utf8' });
190
261
  assert.equal(check.status, 0, check.stderr);
@@ -539,11 +610,11 @@ test('old UI socket callbacks cannot clear or dispatch through a replacement soc
539
610
  close() {} send() {}
540
611
  }
541
612
  const r = vm.createContext({
542
- ws: null, wsConnected: false, sessionId: null, fileName: 'File', documentId: 'doc', BRIDGE: 'http://localhost:41730',
613
+ uiDisposed: false, ws: null, wsConnected: false, sessionId: null, fileName: 'File', documentId: 'doc', BRIDGE: 'http://localhost:41730',
543
614
  WebSocket: Socket, setInterval: () => 1, clearInterval: () => {}, setTimeout: () => 1, clearTimeout: () => {},
544
615
  sendRuntimeCapabilities: () => {}, consecutiveErrors: 0, everConnected: false, retryTimer: null, polling: true,
545
616
  setStatus: () => {}, log: () => {}, currentPort: 41730, READ_OPS: [],
546
- startLongPoll: () => assert.fail('stale socket started poll'), dispatchToMain: req => dispatched.push(req),
617
+ activeIndexScan: null, startLongPoll: () => assert.fail('stale socket started poll'), dispatchToMain: req => dispatched.push(req),
547
618
  });
548
619
  vm.runInContext(source, r);
549
620
  r.connectWs(); assert.equal(sockets.length, 0);
@@ -588,3 +659,156 @@ test('tree reads preserve Regular font weight, collect Regular font tokens, and
588
659
  assert.equal(tree.textStyle, 'Lato/18px/regular');
589
660
  assert.ok(tokenCollector.fonts.has('Lato/Regular/18px'));
590
661
  });
662
+
663
+ test('hot reload removes old Figma listeners and timers before installing the next runtime', async () => {
664
+ const events = new Map(), pageEvents = new Set(), timers = new Map(), messages = [];
665
+ let timerId = 0;
666
+ const page = { id: 'page', children: [], selection: [], on: (_, fn) => pageEvents.add(fn), off: (_, fn) => pageEvents.delete(fn) };
667
+ let styleName = 'Before';
668
+ const figma = { getLocalTextStylesAsync: async () => [{ id: 'text-style', name: styleName }], currentPage: page, root: { name: 'File', getPluginData: () => 'document' },
669
+ clientStorage: { getAsync: async () => null }, ui: { postMessage: msg => messages.push(msg) },
670
+ on: (event, fn) => { if (!events.has(event)) events.set(event, new Set()); events.get(event).add(fn); },
671
+ off: (event, fn) => events.get(event)?.delete(fn) };
672
+ const main = readFileSync('plugin-src/main.js', 'utf8');
673
+ const runtimes = [];
674
+ for (let i = 0; i < 3; i++) {
675
+ const r = runtime(figma);
676
+ r.setTimeout = (fn, delay) => { timers.set(++timerId, { fn, delay }); return timerId; };
677
+ r.clearTimeout = id => timers.delete(id);
678
+ r.__html__ = '';
679
+ vm.runInContext(main, r);
680
+ runtimes.push(r);
681
+ for (const listeners of events.values()) assert.equal(listeners.size, 1);
682
+ assert.equal(pageEvents.size, 1);
683
+ assert.equal(timers.size, 1, 'only the current startup timer survives');
684
+ }
685
+ assert.equal(runtimes[0].runtimeDisposed, true);
686
+ messages.length = 0;
687
+ for (const fn of events.get('selectionchange')) fn();
688
+ assert.equal(messages.filter(msg => msg.type === 'selection-change').length, 1);
689
+ const live = runtimes.at(-1);
690
+ assert.equal((await live.getStyleNameMapAsync())['text-style'], 'Before');
691
+ styleName = 'After';
692
+ for (const fn of events.get('stylechange')) fn({});
693
+ assert.equal((await live.getStyleNameMapAsync())['text-style'], 'After');
694
+ let releaseStyles;
695
+ live.handlers.get_styles = () => new Promise(resolve => { releaseStyles = resolve; });
696
+ live.handlers.get_variables = async () => ({});
697
+ const scan = live.handlers.index_scan({ deferComponents: true });
698
+ while (!releaseStyles) await new Promise(resolve => setTimeout(resolve, 0));
699
+ figma.ui.onmessage.dispose();
700
+ messages.length = 0;
701
+ releaseStyles({});
702
+ assert.equal((await scan).cancelled, true);
703
+ assert.ok(!messages.some(msg => msg.type === 'index-update'));
704
+ assert.equal(timers.size, 0);
705
+ assert.equal(pageEvents.size, 0);
706
+ for (const listeners of events.values()) assert.equal(listeners.size, 0);
707
+ });
708
+
709
+ test('style names share in-flight reads and refresh after invalidation or a failed getter', async () => {
710
+ const calls = {};
711
+ let name = 'Before', failPaint = false;
712
+ const figma = Object.fromEntries(['Paint', 'Text', 'Effect', 'Grid'].map(kind => [`getLocal${kind}StylesAsync`, async () => {
713
+ calls[kind] = (calls[kind] || 0) + 1;
714
+ if (kind === 'Paint' && failPaint) throw new Error('temporary failure');
715
+ return [{ id: kind, name }];
716
+ }]));
717
+ const r = runtime(figma);
718
+ const [first, second] = await Promise.all([r.makeWalkStateAsync({}), r.makeWalkStateAsync({})]);
719
+ assert.equal(first.styleMap.Text, 'Before');
720
+ assert.equal(second.styleMap.Paint, 'Before');
721
+ assert.deepEqual(Object.values(calls), [1, 1, 1, 1]);
722
+ name = 'After'; r.invalidateStyleNameMap();
723
+ assert.equal((await r.makeWalkStateAsync({})).styleMap.Text, 'After');
724
+ assert.deepEqual(Object.values(calls), [2, 2, 2, 2]);
725
+ failPaint = true; r.invalidateStyleNameMap();
726
+ assert.equal((await r.makeWalkStateAsync({})).styleMap.Paint, undefined);
727
+ failPaint = false;
728
+ assert.equal((await r.makeWalkStateAsync({})).styleMap.Paint, 'After');
729
+ assert.equal(calls.Paint, 4);
730
+ let release;
731
+ r.figma.getLocalPaintStylesAsync = () => new Promise(resolve => { release = resolve; });
732
+ r.invalidateStyleNameMap();
733
+ const old = r.getStyleNameMapAsync();
734
+ r.invalidateStyleNameMap();
735
+ r.figma.getLocalPaintStylesAsync = async () => [{ id: 'Paint', name: 'Newest' }];
736
+ assert.equal((await r.getStyleNameMapAsync()).Paint, 'Newest');
737
+ release([{ id: 'Paint', name: 'Obsolete' }]);
738
+ await old;
739
+ assert.equal((await r.getStyleNameMapAsync()).Paint, 'Newest');
740
+ });
741
+
742
+ for (const lineEnding of ['\n', '\r\n']) test(`UI reload clears previous timers, listeners and socket while keeping inline controls available (${lineEnding === '\n' ? 'LF' : 'CRLF'})`, () => {
743
+ const html = readFileSync('plugin-runtime/ui.html', 'utf8');
744
+ const source = html.match(/<script>([\s\S]*?)<\/script>/)[1].replace(/\r?\n/g, lineEnding);
745
+ const timers = new Set();
746
+ let timerId = 0, socketCloses = 0, cancelledRaf = 0;
747
+ const elements = new Map();
748
+ const target = () => {
749
+ const listeners = new Map();
750
+ return {
751
+ listeners, style: {}, dataset: {}, classList: { add() {}, remove() {}, contains: () => false, toggle() {} },
752
+ addEventListener(name, fn, options) {
753
+ if (options?.signal?.aborted) return;
754
+ if (!listeners.has(name)) listeners.set(name, new Set());
755
+ listeners.get(name).add(fn);
756
+ options?.signal?.addEventListener('abort', () => listeners.get(name).delete(fn), { once: true });
757
+ },
758
+ removeEventListener(name, fn) { listeners.get(name)?.delete(fn); },
759
+ setPointerCapture() {}, releasePointerCapture() {},
760
+ setAttribute() {}, querySelectorAll: () => [], querySelector: () => null,
761
+ };
762
+ };
763
+ const window = { ...target(), innerWidth: 360, innerHeight: 600,
764
+ setTimeout() { timers.add(++timerId); return timerId; }, clearTimeout: id => timers.delete(id),
765
+ setInterval() { timers.add(++timerId); return timerId; }, clearInterval: id => timers.delete(id),
766
+ };
767
+ const document = { ...target(), getElementById: id => {
768
+ if (!elements.has(id)) elements.set(id, target());
769
+ return elements.get(id);
770
+ }, querySelectorAll: () => [] };
771
+ class Socket {
772
+ static OPEN = 1;
773
+ constructor() { this.readyState = 1; }
774
+ close() { socketCloses++; }
775
+ send() {}
776
+ }
777
+ const r = vm.createContext({ window, document, WebSocket: Socket, AbortController, AbortSignal,
778
+ console, navigator: {}, fetch: () => new Promise(() => {}), parent: { postMessage() {} },
779
+ requestAnimationFrame: () => 42, cancelAnimationFrame: () => cancelledRaf++ });
780
+ for (let i = 0; i < 3; i++) {
781
+ const entryPoint = /^( {4})initConnection\(\);(?=\r?$)/m;
782
+ assert.match(source, entryPoint, 'startup entry point must be replaced by the socket fixture');
783
+ vm.runInContext(source.replace(entryPoint, "$1sessionId = 's'; connectWs();"), r);
784
+ assert.equal(timers.size, 1, 'only current stats interval remains');
785
+ assert.equal(window.listeners.get('focus').size, 1);
786
+ assert.equal(document.listeners.get('keydown').size, 1);
787
+ assert.equal(document.listeners.get('visibilitychange').size, 1);
788
+ for (const name of [...html.matchAll(/onclick="([A-Za-z][A-Za-z0-9_]*)\(/g)].map(match => match[1]).filter(name => name !== 'document')) {
789
+ assert.equal(typeof window[name], 'function', `${name} must remain available to inline controls`);
790
+ }
791
+ }
792
+ // Cleanup also covers a resize in progress.
793
+ const grip = elements.get('resize-grip');
794
+ for (const fn of grip.listeners.get('pointerdown')) fn({ preventDefault() {}, pointerId: 1, clientX: 0, clientY: 0 });
795
+ for (const fn of window.listeners.get('pointermove')) fn({ clientX: 10, clientY: 10 });
796
+ window.__disposeFigmaMcpUi();
797
+ assert.equal(timers.size, 0);
798
+ assert.equal(cancelledRaf, 1);
799
+ for (const listeners of window.listeners.values()) assert.equal(listeners.size, 0);
800
+ for (const listeners of document.listeners.values()) assert.equal(listeners.size, 0);
801
+ window.__disposeFigmaMcpUi();
802
+ assert.equal(cancelledRaf, 1, 'cleanup is idempotent');
803
+ assert.equal(socketCloses, 3);
804
+ });
805
+
806
+
807
+ test('creating a paint style invalidates the style names before the next design read', async () => {
808
+ const styles = [];
809
+ const r = runtime({ getLocalPaintStylesAsync: async () => styles,
810
+ createPaintStyle: () => { const style = { id: 'new-style' }; styles.push(style); return style; } });
811
+ assert.equal((await r.getStyleNameMapAsync())['new-style'], undefined);
812
+ await r.handlers.createPaintStyle({ name: 'Brand/New', color: '#ff0000' });
813
+ assert.equal((await r.getStyleNameMapAsync())['new-style'], 'Brand/New');
814
+ });