Files
yellowjacket/frontend/test/harness.test.ts
T
yonluandClaude Opus 5 162c68769f feat(wails): move the frontend onto v3's generated bindings
frontend/wailsjs/ is deleted and frontend/bindings/ takes its place —
a real TypeScript module tree nested by Go import path, generated by
wails3's static analyser rather than by building the app and running
it.  The @go alias absorbs the constant prefix, so a call site imports
'@go/library/library.js' and the codemod over all 93 sites was a
specifier rewrite plus splitting @go/models' namespaces into one
import per package.

The 12 SetContext bindings and the fake `context` model are gone, as
Phase 2's ServiceStartup port promised: 272 methods across 12
services, none of them plumbing.

@runtime/runtime is now a local shim (src/wails/runtime.ts) over
@wailsio/runtime, so the 22 EventsOn imports are untouched.  It
unwraps v3's WailsEvent into v2's callback shape, which is exact here:
nothing in backend/events passes more than one data argument, and v3
only packs arguments into a slice when there is more than one.

v3 tells the truth about two things v2 lied about, and that is most of
the diff.  A Go nil slice really does arrive as JSON null, and a Go
named string type really is an enum; v2 typed them as T[] and string.
utils/binding.ts states the app's actual contract — an absent list is
an empty list — once, at the boundary where it is true, and also drops
the CancellablePromise the app never cancels.  Four test fixtures
widen an enum field back to its value union.

Not done, and Phase 5's to fix: frontend/test/support/wails-fake.ts
still fakes window.go, which v3 does not have, so `make ui-test` is
broken and harness.test.ts fails to compile on EventsEmit.  That test
also asserts v2 ordering that no longer holds — v3's Events.Emit calls
the backend and does not notify in-page listeners at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UDCbcCZQepnpSQYJ6SxxZm
2026-08-14 17:48:38 -04:00

97 lines
3.0 KiB
TypeScript

/**
* Self-tests for the component tier, in the spirit of e2e/specs/
* harness.spec.ts: prove the rig is what it claims before trusting a
* single assertion built on it.
*/
import { describe, expect, it } from 'vitest';
import { Events } from '../src/events';
import { emit, calls, wails, flush } from '@test/support/harness';
// Importing a store must be enough to make it start listening.
import { queueStore } from '@store/queue-store';
describe('component-tier harness', () => {
it('runs in a real browser with a real shadow DOM', () => {
const host = document.createElement('div');
host.attachShadow({ mode: 'open' }).innerHTML = '<b>x</b>';
expect(host.shadowRoot?.querySelector('b')?.textContent).toBe('x');
});
it('routes generated bindings through the fake, not a module mock', async () => {
// The import path under test is the real generated stub, which does
// window['go']['queue']['Queue']['GetState']().
const Queue = await import('@go/queue/queue.js');
wails.stub('queue.Queue.GetState', { currentIndex: 4 });
await expect(Queue.GetState()).resolves.toEqual({ currentIndex: 4 });
expect(calls('queue.Queue.GetState')).toHaveLength(1);
});
it('resolves an unstubbed binding instead of hanging', async () => {
const Queue = await import('@go/queue/queue.js');
// The real trap this pays for is the reverse: a *real* binding
// called with wrong argument types never settles. Here, silence is
// an immediate undefined so a test fails on the assertion rather
// than on a timeout.
await expect(Queue.Play()).resolves.toBeUndefined();
});
it('registers listeners merely by importing a store', () => {
expect(wails.listenerNames()).toContain(Events.QueueChanged);
});
it('delivers events to store listeners with their payload', () => {
emit(Events.QueueIndexChanged, { currentIndex: 11 });
expect(queueStore.getState().currentIndex).toBe(11);
});
it('expires a once-listener after a single delivery', () => {
let fired = 0;
wails.on('SyntheticEvent', () => {
fired += 1;
}, 1);
wails.notify('SyntheticEvent', []);
wails.notify('SyntheticEvent', []);
expect(fired).toBe(1);
});
it('notifies local listeners on a frontend-side EventsEmit', async () => {
// Wails' own runtime notifies JS listeners before it notifies Go
// (desktop/events.js), so a frontend emit is observable in-page.
const { EventsEmit } = await import('@runtime/runtime');
let seen: unknown;
wails.on('SyntheticEmit', (data) => {
seen = data;
}, -1);
EventsEmit('SyntheticEmit', 42);
expect(seen).toBe(42);
});
it('flushes the microtask queue stores notify on', async () => {
let notified = false;
const off = queueStore.subscribe(() => {
notified = true;
});
emit(Events.QueueIndexChanged, { currentIndex: 1 });
const beforeFlush = notified;
await flush();
off();
expect([beforeFlush, notified]).toEqual([false, true]);
});
});