import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import vm from 'node:vm'; const script = readFileSync(new URL('../public/desktop-connect/callback.js', import.meta.url), 'utf8'); function render(query) { const elements = { message: { textContent: '' }, return: { hidden: true } }, history = []; const current = new URL('https://inkspring.ai/desktop-connect/?' + query); vm.runInNewContext(script, { URL, window: { location: current, history: { replaceState: (...args) => history.push(args) } }, document: { getElementById: id => elements[id] } }); return { elements, history }; } test('static OAuth return validates state, clears browser history and builds only the desktop return', () => { const state = 'instagram.' + 'a'.repeat(43), code = 'one-time-code'; const { elements, history } = render(new URLSearchParams({ state, code }).toString()); const target = new URL(elements.return.href); 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/'); }); test('static return rejects missing or malformed state and contains no network or persistence calls', () => { 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); assert.doesNotMatch(script, /fetch\(|XMLHttpRequest|localStorage|sessionStorage|document\.cookie|sendBeacon/); });