// Host integration tests: instantiate the real MoonBit components (built by // scripts/build.sh) under the Node host and exercise the desktop end to end. import test from 'node:test'; import assert from 'node:assert/strict'; import { readFile, stat, writeFile, mkdtemp } from 'node:fs/promises'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; import { bootDesktop, ROOT } from '../src/headless.mjs'; import { PermissionError } from '../src/desktop.mjs'; import { tokensFromJson } from '../src/theme.mjs'; const NOW = new Date(2026, 10, 24, 14, 7, 37); // Tue 24 Nov 2026, local time const fixed = () => NOW; const texts = (ops) => ops.filter((o) => o.tag === 'text').map((o) => o.val.text); const configWith = async (mutate) => { const cfg = JSON.parse(await readFile(join(ROOT, 'config', 'desktop.json'), 'utf8')); mutate(cfg); const dir = await mkdtemp(join(tmpdir(), 'chandra-')); const p = join(dir, 'desktop.json'); await writeFile(p, JSON.stringify(cfg)); return p; }; test('boots shell + widgets and composes a frame', async () => { const { desktop } = await bootDesktop({ now: fixed }); const f = desktop.frame(); assert.deepEqual(f.screen, { w: 1280, h: 720 }); assert.ok(f.ops.length > 50); const t = texts(f.ops); assert.ok(t.includes('2:07 PM'), `clock text missing (Digital Clock starts in 12-hour mode): ${t}`); assert.ok(t.includes('Tue 24 Nov')); assert.ok(t.includes('chandra')); assert.ok(t.includes('Applications')); assert.equal(desktop.faults.length, 0); }); test('shell lays the panel out at the bottom, launcher left, clock right', async () => { const { desktop } = await bootDesktop({ now: fixed }); const { layout } = desktop.frame(); assert.deepEqual(layout.panel, { x: 0, y: 720 - 44, w: 1280, h: 44 }); const byId = Object.fromEntries(layout.placements.map((p) => [p.id, p.rect])); assert.ok(byId.launcher.x < 20); assert.equal(byId.clock.x + byId.clock.w, 1280 - 5); for (const r of Object.values(byId)) { assert.ok(r.y >= layout.panel.y && r.y + r.h <= 720, 'widget inside the panel'); } }); test('launcher: click opens popup, row click launches via host capability, popup closes', async () => { const { desktop, platform } = await bootDesktop({ now: fixed }); desktop.frame(); const l = desktop.hit.widgets.find((w) => w.widget.id === 'launcher').rect; assert.equal(desktop.hit.popup, null); assert.equal(desktop.pointer('down', l.x + 5, l.y + 5), true); const f = desktop.frame(); assert.ok(desktop.hit.popup, 'popup is open'); assert.ok(texts(f.ops).includes('Terminal')); const pr = desktop.hit.popup.rect; assert.ok(pr.y + pr.h <= 720 - 44, 'popup sits above the panel'); // second row (52px header + 46px rows) desktop.pointer('move', pr.x + 20, pr.y + 52 + 46 + 5); desktop.pointer('down', pr.x + 20, pr.y + 52 + 46 + 5); assert.deepEqual(platform.launches, ['org.chandra.files']); desktop.frame(); assert.equal(desktop.hit.popup, null, 'popup closed after launch'); }); test('launcher: keyboard search narrows the list and Enter launches the first match', async () => { const { desktop, platform } = await bootDesktop({ now: fixed }); desktop.frame(); const l = desktop.hit.widgets.find((w) => w.widget.id === 'launcher').rect; desktop.pointer('down', l.x + 5, l.y + 5); desktop.frame(); for (const ch of 'calc') desktop.key(ch); const f = desktop.frame(); const t = texts(f.ops); assert.ok(t.includes('Calculator') && !t.includes('Terminal')); desktop.key('Enter'); assert.deepEqual(platform.launches, ['org.chandra.calc']); }); test('clicking outside the popup dismisses it (focus-lost)', async () => { const { desktop } = await bootDesktop({ now: fixed }); desktop.frame(); const l = desktop.hit.widgets.find((w) => w.widget.id === 'launcher').rect; desktop.pointer('down', l.x + 5, l.y + 5); desktop.frame(); assert.ok(desktop.hit.popup); desktop.pointer('down', 900, 200); desktop.frame(); assert.equal(desktop.hit.popup, null); }); test('permissions: a component importing an ungranted capability is refused', async () => { const cfg = await configWith((c) => { c.panel[1].grants = ['theme']; }); // clock needs "clock" await assert.rejects(bootDesktop({ configPath: cfg }), (e) => { assert.ok(e instanceof PermissionError); assert.match(e.message, /clock-digital.*imports \[clock\]/); return true; }); }); test('permissions: unknown capability names in config are rejected', async () => { const cfg = await configWith((c) => { c.panel[1].grants.push('filesystem'); }); await assert.rejects(bootDesktop({ configPath: cfg }), /unknown capability "filesystem"/); }); test('a component cannot act in a role it does not export', async () => { const cfg = await configWith((c) => { c.panel[1].component = 'shell'; c.panel[1].grants = ['theme']; }); await assert.rejects(bootDesktop({ configPath: cfg }), /does not export plasmoid/); }); test('hot-swap: clock-digital -> clock-binary at runtime, other components untouched', async () => { const files = ['shell', 'launcher', 'clock-digital', 'clock-binary'].map((n) => join(ROOT, 'build', 'components', `${n}.wasm`)); const before = await Promise.all(files.map(async (f) => (await stat(f)).mtimeMs)); const { desktop } = await bootDesktop({ now: fixed }); const a = desktop.frame(); assert.ok(texts(a.ops).includes('2:07 PM')); const launcherInstance = desktop.widgets.find((w) => w.id === 'launcher').plasmoid; await desktop.swap('clock', 'clock-binary'); const b = desktop.frame(); assert.ok(!texts(b.ops).includes('2:07 PM')); assert.ok(b.ops.filter((o) => o.tag === 'circle').length >= 20, 'binary clock dots drawn'); assert.equal(desktop.widgets.find((w) => w.id === 'launcher').plasmoid, launcherInstance, 'launcher instance reused'); const after = await Promise.all(files.map(async (f) => (await stat(f)).mtimeMs)); assert.deepEqual(after, before, 'no component binary was rebuilt'); }); test('config-level swap (desktop.binary-clock.json) picks the other implementation', async () => { const { desktop } = await bootDesktop({ configPath: join(ROOT, 'config', 'desktop.binary-clock.json'), now: fixed }); assert.equal(desktop.widgets.find((w) => w.id === 'clock').componentName, 'clock-binary'); assert.ok(desktop.frame().ops.some((o) => o.tag === 'circle' && o.val.filled)); }); test('theme tokens flow through to components (live theme switch)', async () => { const { desktop, platform } = await bootDesktop({ now: fixed }); const firstFill = (ops) => ops.find((o) => o.tag === 'fill-rect').val.color; const night = firstFill(desktop.frame().ops); const day = JSON.parse(await readFile(join(ROOT, 'config', 'themes', 'moon-day.json'), 'utf8')); platform.tokens = tokensFromJson(day); const light = firstFill(desktop.frame().ops); assert.notDeepEqual(night, light); assert.deepEqual(light, { r: 0xcf, g: 0xe3, b: 0xff, a: 255 }); // desktop-top of moon-day }); test('component faults are surfaced loudly and isolated to the widget', async () => { const { desktop, platform } = await bootDesktop({ now: fixed }); platform.now = () => { throw new Error('rtc exploded'); }; const f = desktop.frame(); // does not take the desktop down assert.equal(desktop.faults.length, 1); assert.match(desktop.faults[0].message, /clock-digital.*render/); assert.ok(texts(f.ops).includes('clock-digital crashed'), 'visible error tile'); assert.ok(texts(f.ops).includes('Applications'), 'other widgets still render'); }); test('build manifests match scripts/components.txt', async () => { const lines = (await readFile(join(ROOT, 'scripts', 'components.txt'), 'utf8')).split('\n') .filter((l) => l.trim() && !l.startsWith('#')); for (const l of lines) { const [name, world] = l.trim().split(/\s+/); const meta = JSON.parse(await readFile(join(ROOT, 'build', 'components', `${name}.meta.json`), 'utf8')); assert.equal(meta.world, world); assert.ok(meta.imports.includes('theme')); } });