const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const path = require('node:path'); const workspaceModule = fs.existsSync(path.join(__dirname, '../workspace.js')) ? '../workspace.js' : '../public/workspace.js'; const { scopedStorage, escapeText } = require(workspaceModule); const a = '11111111-1111-4111-8111-111111111111'; const b = '22222222-2222-4222-8222-222222222222'; function memory() { const values = new Map(); return { getItem: key => values.has(key) ? values.get(key) : null, setItem: (key, value) => values.set(key, String(value)), removeItem: key => values.delete(key), values }; } test('private browser keys are scoped by verified client, with no automatic legacy migration', () => { const storage = memory(); storage.setItem('opal_gemini_api_key', 'legacy-test-key'); const first = scopedStorage(storage, a), other = scopedStorage(storage, b), staff = scopedStorage(storage, a.toUpperCase()); assert.equal(first.getItem('opal_gemini_api_key'), null); first.setItem('opal_gemini_api_key', 'client-a-test-key'); assert.equal(staff.getItem('opal_gemini_api_key'), 'client-a-test-key'); assert.equal(other.getItem('opal_gemini_api_key'), null); other.removeItem('opal_gemini_api_key'); assert.equal(first.getItem('opal_gemini_api_key'), 'client-a-test-key'); assert.equal(storage.getItem('opal_gemini_api_key'), 'legacy-test-key'); assert.throws(() => scopedStorage(storage, '../tenant'), /Verified/); }); test('imported markup is text in both quoted attributes and HTML content', () => { assert.equal(escapeText(' & \'SKU\''), '<img src=x onerror="alert(1)"> & 'SKU''); }); async function bootstrap(response, config = { loginUrl: 'https://auth.fixture/login' }) { const nodes = new Map(), requests = [], scripts = [], events = []; function node() { return { hidden: true, value: '', textContent: '', inert: false, prepend() {} }; } const get = id => { if (!nodes.has(id)) nodes.set(id, node()); return nodes.get(id); }; let start; const context = { URL, URLSearchParams, Event, Object, encodeURIComponent, location: { href: 'https://studio.fixture/?tenant=chosen', origin: 'https://studio.fixture', search: '?tenant=chosen', reload() {}, assign() {} }, localStorage: memory(), sessionStorage: memory(), fetch: async url => { requests.push(url); return { ok: url === '/api/studio-config' ? true : response.status === 200, status: response.status, json: async () => url === '/api/studio-config' ? config : response.body }; }, document: { getElementById: get, querySelectorAll: () => [], querySelector: () => null, createElement: tag => ({ tag }), addEventListener: (event, callback) => { if (event === 'DOMContentLoaded') start = callback; }, dispatchEvent: event => events.push(event.type), body: { appendChild: script => scripts.push(script) } } }; context.window = context; vm.runInNewContext(fs.readFileSync(require.resolve(workspaceModule), 'utf8'), context); await start(); return { context, nodes, requests, scripts, events }; } test('login and workspace selection failures do not load app code or expose saved client state', async () => { const login = await bootstrap({ status: 401, body: { code: 'LOGIN_REQUIRED', error: 'Login first' } }); assert.equal(login.scripts.length, 0); assert.equal(login.context.AuraStudio, undefined); assert.equal(login.nodes.get('workspaceLogin').hidden, false); assert.equal(new URL(login.nodes.get('workspaceLogin').href).searchParams.get('next'), login.context.location.href); const owner = await bootstrap({ status: 409, body: { code: 'WORKSPACE_REQUIRED', error: 'Choose workspace' } }); assert.equal(owner.nodes.get('workspaceForm').hidden, false); assert.equal(owner.scripts.length, 0); const invalid = await bootstrap({ status: 200, body: { success: true, workspace: { clientId: 'bad', tenant: 'chosen' } } }); assert.equal(invalid.scripts.length, 0); }); test('validated workspace loads app once and preserves canonical tenant in API requests', async () => { const ready = await bootstrap({ status: 200, body: { success: true, workspace: { clientId: a, name: 'Fixture shop', tenant: 'canonical' } } }); assert.equal(ready.scripts.length, 1); assert.equal(ready.scripts[0].src, '/app.js?v=workspace-1'); assert.equal(ready.context.AuraStudio.apiUrl('/api/sync-store?tenant=attacker'), '/api/sync-store?tenant=canonical'); assert.throws(() => ready.context.AuraStudio.apiUrl('https://attacker.fixture/api/x')); ready.scripts[0].onload(); assert.deepEqual(ready.events, ['aura-studio-ready']); assert.equal(ready.nodes.get('studioWorkspaceGate').hidden, true); });