open-standards/wai/wai-studio/test/local-core.test.mjs
Transaction Science f85a0a7273 WAI Studio on the web: one front end, its core in WebAssembly, served by wai dev at /_wai/
wai/wai-studio: the panels (Inspect, Verify and receipts, Encode/decode/package, Rooms,
Run, Quantum) and the command palette; actions generated from the operation catalogue
with a drift check; the core (wai-studio-core) binding the wai crate for the tab. wai dev
serves Studio at /_wai/ on its own origin, with the dev stack's routes for it under
/_wai/api/.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 15:22:51 -04:00

41 lines
2.4 KiB
JavaScript

// The local core's calls (src/cores.js LocalCore) as the page makes them, against a stand-in
// for `wai dev`'s routes: the header every call carries, the operation's id in its path and
// its arguments in its query, the input as the body, and the files it wrote decoded.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { LocalCore } from '../src/cores.js';
test('each call reaches /_wai/api/ with X-WAI-Studio, and an operation\'s outputs come back as bytes', async () => {
const seen = [];
const server = createServer((req, res) => {
const chunks = [];
req.on('data', (c) => chunks.push(c));
req.on('end', () => {
seen.push({ method: req.method, url: req.url, header: req.headers['x-wai-studio'], type: req.headers['content-type'], body: Buffer.concat(chunks) });
res.setHeader('content-type', 'application/json');
if (req.url.startsWith('/_wai/api/state')) res.end(JSON.stringify({ ready: true }));
else if (req.url.startsWith('/_wai/api/log')) res.end(JSON.stringify({ text: 'x', next: 1 }));
else if (req.url.startsWith('/_wai/api/op/')) res.end(JSON.stringify({ op: 'media.encode', ok: true, exit: 0, argv: ['wai', 'encode'], stdout: '', stderr: '', outputs: [{ name: 'out.wai', data: Buffer.from([0, 1, 2, 255]).toString('base64') }] }));
else { res.statusCode = 403; res.end(JSON.stringify({ error: 'no' })); }
});
});
await new Promise((r) => server.listen(0, '127.0.0.1', r));
const core = new LocalCore(`http://127.0.0.1:${server.address().port}/_wai/api/`);
try {
assert.deepEqual(await core.state(), { ready: true });
await core.log('relay', 7);
const r = await core.op('media.encode', new Uint8Array([9, 9, 9]), { codec: 'avif', width: 1, height: 1, quality: 50, sample_rate: null });
assert.deepEqual(Array.from(r.outputs[0].bytes), [0, 1, 2, 255]);
await assert.rejects(core.get('/nothing'), /no/);
} finally {
server.close();
}
assert.ok(seen.every((s) => s.header === '1'), 'every call carries X-WAI-Studio: 1');
assert.equal(seen[1].url, '/_wai/api/log?name=relay&from=7');
const op = seen[2];
assert.equal(op.method, 'POST');
assert.equal(op.url, '/_wai/api/op/media.encode?codec=avif&width=1&height=1&quality=50', 'an argument with no value is left out');
assert.equal(op.type, 'application/octet-stream');
assert.deepEqual(Array.from(op.body), [9, 9, 9]);
});