Branchwell

Inkspring / website / tests / callback.test.mjs

1import { test } from 'node:test';
2import assert from 'node:assert/strict';
3import { readFileSync } from 'node:fs';
4import vm from 'node:vm';
5const script = readFileSync(new URL('../public/desktop-connect/callback.js', import.meta.url), 'utf8');
6function render(query) {
7 const elements = { message: { textContent: '' }, return: { hidden: true } }, history = [];
8 const current = new URL('https://inkspring.ai/desktop-connect/?' + query);
9 vm.runInNewContext(script, { URL, window: { location: current, history: { replaceState: (...args) => history.push(args) } }, document: { getElementById: id => elements[id] } });
10 return { elements, history };
11}
12test('static OAuth return validates state, clears browser history and builds only the desktop return', () => {
13 const state = 'instagram.' + 'a'.repeat(43), code = 'one-time-code';
14 const { elements, history } = render(new URLSearchParams({ state, code }).toString());
15 const target = new URL(elements.return.href);
16 assert.equal(target.protocol, 'inkspring:'); assert.equal(target.hostname, 'oauth'); assert.equal(target.pathname, '/instagram'); assert.equal(target.searchParams.get('code'), code); assert.equal(target.searchParams.get('state'), state); assert.equal(elements.return.hidden, false); assert.equal(history[0][2], '/desktop-connect/');
17});
18test('static return rejects missing or malformed state and contains no network or persistence calls', () => {
19 for (const query of ['', 'code=missing-state', 'state=javascript:bad&code=x', 'state=x.short&code=x', ...['reddit','tiktok','substack'].map(p => 'state='+p+'.'+'a'.repeat(43)+'&code=x')]) assert.equal(render(query).elements.return.hidden, true);
20 assert.doesNotMatch(script, /fetch\(|XMLHttpRequest|localStorage|sessionStorage|document\.cookie|sendBeacon/);
21});