Elffuss BraidHub

utopia-ia/elffuss-code public

main · 4.2 KiBRawPermalink
1// Palette and menus render their rows as TEXT: a file named like an HTML tag
2// shows up literally and never becomes an element (no image, no handler).
3// It held before level 2 too: file names reach the palette (Cmd+P), the "+"
4// attach menu and the menu bar.
5//
6// node tests/palette_text.mjs (starts its own server: tests/lib/x2-server.mjs)
7import { chromium } from 'playwright';
8import { startServer } from './lib/x2-server.mjs';
9
10let fails = 0;
11const ok = (name, c, extra = '') => { console.log((c ? '✅' : '❌') + ' ' + name + (extra ? ' — ' + extra : '')); if (!c) fails++; };
12const sleep = ms => new Promise(r => setTimeout(r, ms));
13async function until(fn, { timeout = 15000, every = 150 } = {}) {
14 const end = Date.now() + timeout;
15 let v;
16 while (Date.now() < end) { try { v = await fn(); if (v) return v; } catch { /* retry */ } await sleep(every); }
17 return v;
18}
19
20const HOSTILE = '<img src=x onerror="window.__xss=1">.js';
21const srv = await startServer();
22const browser = await chromium.launch();
23const ctx = await browser.newContext();
24await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch { /* no storage */ } });
25const page = await ctx.newPage();
26const errors = [];
27page.on('pageerror', e => { if (e.message !== 'Canceled') errors.push(e.message); });
28await page.goto(srv.codeOrigin + '/', { waitUntil: 'domcontentloaded' });
29const created = await page.evaluate(async name => {
30 const o = await navigator.storage.getDirectory();
31 try {
32 const f = await o.getFileHandle(name, { create: true });
33 const s = await f.createWritable(); await s.write('x'); await s.close();
34 return true;
35 } catch (e) { return String(e && e.message || e); }
36}, HOSTILE);
37ok('the browser accepts the hostile file name in the test project', created === true, String(created));
38
39await page.goto(srv.codeOrigin + '/?test-opfs', { waitUntil: 'domcontentloaded' });
40await page.waitForFunction(() => !!window.monaco?.editor?.getEditors?.().length, null, { timeout: 30000 });
41
42// palette (files)
43await page.evaluate(() => document.dispatchEvent(new KeyboardEvent('keydown', { key: 'p', ctrlKey: true, bubbles: true })));
44const pal = await until(() => page.evaluate(() => {
45 const list = document.getElementById('pal-list');
46 const rows = [...list.querySelectorAll('.pal-item')];
47 return rows.length ? { texts: rows.map(r => r.querySelector('.pi-name')?.textContent), imgs: list.querySelectorAll('img').length, spans: rows.every(r => r.children.length === 2) } : null;
48}));
49ok('palette: the name appears as text', !!pal && pal.texts.includes(HOSTILE), JSON.stringify(pal?.texts));
50ok('palette: no element was created from it', !!pal && pal.imgs === 0 && pal.spans);
51await page.keyboard.press('Escape');
52
53// "+" attach menu (openMenu)
54await page.evaluate(() => document.getElementById('btn-plus').click());
55const plus = await until(() => page.evaluate(() => {
56 const m = document.getElementById('menu');
57 const rows = [...m.querySelectorAll('.menu-item')];
58 return rows.length ? { texts: rows.map(r => r.querySelector('b')?.textContent), imgs: m.querySelectorAll('img').length } : null;
59}));
60ok('attach menu: the name appears as text, with no element', !!plus && plus.texts.includes('@' + HOSTILE) && plus.imgs === 0, JSON.stringify(plus));
61
62// menu bar (openMenuAt): same row builder; check one menu still looks the same
63await page.evaluate(() => document.querySelector('#menubar button[data-menu="view"]').click());
64const view = await until(() => page.evaluate(() => {
65 const m = document.getElementById('topmenu');
66 const rows = [...m.querySelectorAll('.menu-item')];
67 return rows.length ? rows.map(r => [r.querySelector('b')?.textContent, r.querySelector('span')?.textContent ?? null]) : null;
68}));
69ok('menu bar rows keep their label and hint', !!view && view.some(([l, h]) => /^Command palette$/i.test(l || '') && /^(⌘⇧P|Ctrl\+Shift\+P|Cmd\+Shift\+P)$/.test(h || '')), JSON.stringify(view?.slice(0, 6)));
70
71await sleep(300);
72ok('no handler from a file name ever ran', await page.evaluate(() => window.__xss === undefined));
73ok('no page errors', errors.length === 0, errors.slice(0, 3).join(' | '));
74
75await browser.close();
76await srv.close();
77console.log(fails ? `\n❌ ${fails} failed` : '\n✅ all passed');
78process.exit(fails ? 1 : 0);