import test from 'node:test'; import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { join, dirname } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { createWasiGfxHost } from '../src/wasi-gfx.mjs'; const HERE = dirname(fileURLToPath(import.meta.url)); const BUILD = join(HERE, '..', '..', 'build'); test('wasi-gfx host: Surface, Buffer, Context, and event streams', async () => { const gfx = createWasiGfxHost(); const surf = new gfx.Surface({ width: 100, height: 50 }); assert.equal(surf.width(), 100); assert.equal(surf.height(), 50); const resizes = []; const reader = surf.onResize().getReader(); surf.requestSetSize(80, 120); const { value } = await reader.read(); resizes.push(value); await reader.cancel(); assert.deepEqual(resizes, [{ width: 120, height: 80 }]); const ctx = new gfx.Context(surf); const buf = ctx.getCurrentBuffer(); const pixels = new Uint8Array(120 * 80 * 4); pixels[0] = 0x11; pixels[1] = 0x22; pixels[2] = 0x33; pixels[3] = 0xff; buf.setWithCopy(pixels); ctx.present(); assert.equal(surf.presentedFrames, 1); assert.equal(surf.lastPresented[0], 0x11); assert.equal(surf.lastPresented[3], 0xff); }); test('wasi-gfx component (gfx-gui.wasm): loads GUI, handles pointer/resize/key, presents RGBA frames', async () => { const gfx = createWasiGfxHost(); const jcoDir = join(BUILD, 'jco', 'gfx-gui'); const mod = await import(pathToFileURL(join(jcoDir, 'gfx-gui.js')).href); const getCoreModule = async (p) => WebAssembly.compile(await readFile(join(jcoDir, p))); const inst = await mod.instantiate(getCoreModule, gfx.imports); await inst.initGui(320, 180); assert.equal(gfx.surfaces.length, 1); assert.equal(gfx.contexts.length, 1); const surf = gfx.surfaces[0]; assert.equal(surf.width(), 320); assert.equal(surf.height(), 180); assert.equal(surf.presentedFrames, 1); assert.equal(surf.lastPresented.length, 320 * 180 * 4); // Verify non-empty rendered GUI pixels (alpha=255 and multiple distinct colors). const colors = new Set(); for (let i = 0; i < surf.lastPresented.length; i += 4) { assert.equal(surf.lastPresented[i + 3], 255); colors.add((surf.lastPresented[i] << 16) | (surf.lastPresented[i + 1] << 8) | surf.lastPresented[i + 2]); } assert.ok(colors.size > 5, `expected rich GUI palette, got ${colors.size} colors`); // Pointer click on launcher area -> opens launcher popup and presents new frame. await inst.onPointer(1, 20, 170); assert.equal(surf.presentedFrames, 2); // Resize -> updates surface size and presents resized frame. await inst.onResize(400, 240); assert.equal(surf.width(), 400); assert.equal(surf.height(), 240); assert.equal(surf.presentedFrames, 3); assert.equal(surf.lastPresented.length, 400 * 240 * 4); // Escape key -> dismisses launcher and presents frame. await inst.onKey(true, 'Escape'); assert.equal(surf.presentedFrames, 4); });