Inkspring / website / tests / callback.test.mjs
| 1 | import { test } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { readFileSync } from 'node:fs'; |
| 4 | import vm from 'node:vm'; |
| 5 | const script = readFileSync(new URL('../public/desktop-connect/callback.js', import.meta.url), 'utf8'); |
| 6 | function 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 | } |
| 12 | test('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 | }); |
| 18 | test('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 | }); |