Testing headlessly
Tests render through the real GPU pipeline (Metal on macOS, DirectX on Windows) without opening a
window, and gpuix-svelte/test gives you mount, click, type and read-back helpers over it. A test
is a plain script that you run through the bin, as in gpuix-svelte test.ts:
import { mount_headless, click_test_id, press, all_text, check, finish } from 'gpuix-svelte/test';
import Counter from './Counter.svelte';
mount_headless(Counter, { width: 400, height: 300 });
click_test_id('plus'); // a real click, through GPUI's hit testing
press('cmd-k');
check('the click reached the counter', all_text().includes('1'));
finish('counter', 1); // prints the verdict; exits 1 on a failed check or a wrong count
check(label, actual, expected = true) compares the two as JSON and prints ok or FAIL.
finish(name, expected) exits 1 if any check failed, or if the number of checks that ran differs
from expected. That count is what catches assertions that quietly stopped running, so bump it
when you add or remove a check. Leave it out, and finish only asks for at least one.
Headless tests need macOS or Windows. The Linux addon has no headless renderer, so there
mount_headless throws; drive a real window with launch() instead (see
Driving a running app).
Mounting, reading and clicking
mount_headless(Component, { props, width, height }) mounts the component and waits for the
first paint. It returns { native, root, component, unmount }. It also takes rootStyle,
keyboardFocusDim and tabNavigation, as render() does, so press('tab') moves focus unless
you pass tabNavigation: false. It draws no title bar unless you pass titlebar: 'Title', and
that bar's close button unmounts the component rather than exiting. settle() flushes Svelte's
effects and lets GPUI paint again. For timers and promises, await wait(ms) lets the time pass,
delivering what native reported (onlayout, for one) on both sides of it, as a window would.
To find elements, find_text (an exact match on one text node), find_test_id and element_of
look them up, and tree() returns the whole element tree. text_content(target) reads an
element's text as the DOM's textContent does: its own and every descendant's, joined in document
order, from the tree as it is now (it throws if the element is gone). painted() returns everything
GPUI drew as one string, all_text() the text nodes as an array, bounds() an element's
{ x, y, width, height }, and screenshot(path) settles, then writes a PNG.
For input, click, click_text, click_test_id, click_at and drop_files(target, paths) go
through GPUI's real hit testing, so they pass only when the real window would. press(keystroke)
and type('h i') (space-separated keystrokes) go to whatever has focus. (dispatch() from
gpuix-svelte fires an event at an element directly, skipping hit testing, so reach for the
helpers first.)
The headless viewport has two limits. On macOS its height caps at about 540 px (the exact number
depends on the display), so keep test layouts short; click tells you when a target falls outside.
Windows ignores the requested size and opens a viewport sized from the display (1024×749 on CI).
So read the real size from the native that mount_headless returns, with
native.getWindowSize(), rather than asserting the one you asked for.
focus() moves native focus, and the focus and blur events it causes reach your handlers as
they would in a window. The test renderer has no way to blur, so unfocus() reports a blur for
the focused element itself (native still holds focus, and its own blur for that element is not
reported again when focus later moves).
Other input
Pointer helpers take a node (aimed at its centre) or a { x, y } point in window coordinates:
hover(target),mouse_move(target, { pressed_button })(a move with a button held is a drag move),mouse_downandmouse_up.mouse_exit(at): the pointer leaves the window atat(the top-left corner if you leave it out), with no move after it, which ends every hover and firesonmouseleave.wheel(target, dx, dy), where a negativedyscrolls down.drag(from, to, { steps }): a press,stepsmoves with the button held, then a release.drag_select(from, to), which returns the selected text.
And without a target: key_up(keystroke), the key-up half that press() leaves out;
selected_text() reads the selection and clear_selection() clears it.
Each one, like click, returns only once GPUI has nothing left to paint, so follow-on events such
as a hover change or a finished motion have landed too (pump() does that wait on its own).
center(target) and point_of(target) give the point a helper would aim at. A scroll container's
bounds move with its own offset, so aim repeated wheels at a point you measured before scrolling.
painted_highlights() reads back highlight washes with their rects. automation_tree() is the
tree as the automation driver sees it, with painted bounds on every node but no styles or
events; tree() is the other way round.
Time
clock_pause(), clock_set(ms), clock_fast_forward(ms) and clock_resume() drive the clock
that motion animates on, and each returns the clock's time. advance_time(ms) runs GPUI's
internal timers (caret blink, drag autoscroll). setTimeout and promises keep real time, so for
code that waits on those, await wait_for(() => find_text('ready'), { timeout, interval, label })
polls, pumping between tries, and rejects with the label once the timeout (2000 ms by default)
passes.
Accessibility
a11y() returns the accessibility tree GPUI built from the last painted frame. Each entry in its
nodes has an aria object: the role as AccessKit names it (Button, Heading, Label,
Switch, ListBoxOption), and label, value (a text's string), expanded, selected and
level where they are set. The keys of nodes (a, b, …) name a node for that one dump only,
so assert that a node with the fields you expect exists:
import { a11y } from 'gpuix-svelte/test';
const roles = () => Object.values(a11y().nodes ?? {}).map((n) => n.aria);
check('the switch is labelled', roles().some((a) => a?.role === 'Switch' && a.label === 'Wi-Fi'), true);
Call settle() after a change and before reading the tree again. A button named by the text inside
it has no label of its own here: the platform takes the name from the Label inside it.
Accessibility lists what reaches the tree. To hear
what a screen reader says, run the app with VoiceOver on (⌘F5), or inspect the window with Xcode's
Accessibility Inspector.
Driving a running app
gpuix-svelte/automation is a Playwright-style App and Locator for a gpuix-svelte app, in two
flavours. launch() starts an app and drives its real window from another process, and
test_app() runs the same script against whatever mount_headless mounted. This drives the
repository's counter example:
import { launch } from 'gpuix-svelte/automation';
const app = await launch({ entry: 'examples/counter/main.ts' }); // bun: true runs the app on Bun
for (let i = 0; i < 3; i++) await app.getByTestId('plus').click();
await app.getByText('3 clicks').waitFor();
await app.screenshot({ path: '/tmp/counter.png' });
await app.close(); // ends the app's stdin; it answers what is in flight and exits 0
Run the driver through the bin like any test (npx gpuix-svelte drive.ts). Locators re-resolve on
every call:
getByTestId,getByText(a substring of a text node) andgetByTypechain, scoping to descendants.element()throws anAutomationErrorwhosecodeisNotFoundorAmbiguous.click,hover,wheel,dragTo,dragBy,fill,press,textContentandwaitFor({ timeoutMs })(5000 ms by default) act on the single match, andcount()counts the matches.app.mouse,app.clockandapp.screenshot()cover the rest, andapp.processis the app's child process. If the app is still running two seconds afterclose(), it gets killed.launch()works on Linux too, wheremount_headlessdoes not, exceptapp.screenshot(): the Linux addon has no screenshot support, so it throws there.
launch() runs the entry through the gpuix-svelte bin with GPUIX_AUTOMATION=1. That variable
is the one switch for the automation server; 0 or an empty value leaves it off. Under it,
stdout carries the protocol. console.log, info and debug move to stderr, which
launch() passes through, so don't write to process.stdout yourself. When stdin ends, the app
answers whatever is in flight and exits. Each input replies after two more frames, so a read
straight after a click sees the result. The window takes focus like any other. Opened with
focus: false, it is not the key window, so a clicked field gets no focus or blur and draws
no caret, and window key handlers see editing: false while you type into it.
Any language can drive it. Requests and replies are SSE data: frames of JSON with numeric ids
(the method names are exported as methods from @gpuix/native/automation), and a request that
can't be parsed but carries an id gets a Protocol error back:
printf 'data: {"id":1,"method":"initialize","params":{"protocolVersion":1,"client":"sh"}}\n\ndata: {"id":2,"method":"getAllText","params":{}}\n\n' \
| GPUIX_AUTOMATION=1 npx gpuix-svelte app.ts 2>/dev/null
Headlessly, test_app() (from the same module) returns the same App over the headless renderer.
Inputs go through the same real hit testing as the helpers above, pointer input outside the
viewport throws NotFound, and each read settles first, so waitFor sees an update that lands
on a timer.