Testing & Automation
App automation & headless testing
Section titled “App automation & headless testing”MCP automation harness
Section titled “MCP automation harness”Apps register a renderer-side MCP automation harness wired to the in-process MCP HTTP endpoint (my-app: apps/my-app/src/mcp/setup.ts) built on createStandardAutomationTools() — exposing the standard tool set plus app-specific tools without importing the Node-only @downdraft/engine/mcp server bundle into the renderer. Injected input is merged with real input in processInput() so the app loop can’t tell human from test.
Connecting an MCP client to the app
Section titled “Connecting an MCP client to the app”The app’s MCP HTTP transport (packages/engine/mcp/src/http-transport.ts) supports both Streamable HTTP and HTTP+SSE transports. By default it binds to an ephemeral OS-assigned port (port 0) so multiple app instances never collide. The endpoint is dev/test infrastructure only — scripts/package-native.mjs (draft release) defines __DD_MCP_STRIP__ so it’s compiled out of distributed binaries; --mcp retains it (still runtime-gated by DOWNDRAFT_MCP=1). Local MCP clients (e.g. IDE integrations) typically use stdio, so draft mcp stdio forwards JSON-RPC messages from stdin/stdout to the app’s HTTP endpoint.
Instance discovery: Each running app writes ~/.downdraft/port/<pid> (content = the bound port number; <pid>.token holds the bearer token when auth is required). AppClient.connect() / draft mcp / the stdio bridge all read this directory, prune dead-PID files, and connect to the newest live instance. An app that registers createMcpModule({ transport: "http" }) additionally serves the full editor toolset (MCPServer — tools + resources + prompts) on a second port via McpHttpTransport direct mode, advertised as <pid>.editor (+ <pid>.editor.token); reach it with draft mcp --editor or AppClient.connect({ endpoint: "editor" }). Optional env vars:
MCP_APP_ID=<appId>— narrow to instances of a specific app (matches--user-data-dir=<path>basename on Linux; e.g.downdraft-my-app).MCP_PID=<pid>— connect to a specific PID.DOWNDRAFT_MCP_URL/MCP_HTTP_URL=<url>— explicit URL override (skips discovery; used by the e2e test harness anddraft mcp run).DOWNDRAFT_MCP_ENDPOINT=editor— connect to the instance’s editor endpoint instead of the automation endpoint.MCP_EDITOR_PORT=<port>— pin the editor endpoint’s port (createMcpModule http transport; default ephemeral).
To connect:
- Start the app:
cd apps/my-app && DOWNDRAFT_GPU=swiftshader DOWNDRAFT_DETERMINISTIC=1 draft dev - The MCP config (
.devin/mcp_config.json) runsbun packages/cli/src/index.ts mcp stdio. - The bridge discovers the port from
~/.downdraft/port/<pid>and forwardsinitialize,tools/list,tools/calltohttp://localhost:<port>/mcp. - Notifications (messages without an
idfield, likenotifications/initialized) are silently ignored by the bridge. resources/listandprompts/listreturn empty lists (the app doesn’t expose resources or prompts).
Multiple instances: Launch as many apps as you like — each gets its own ephemeral port and PID file. Clients connect to the newest by default; use --app/--pid (or MCP_APP_ID/MCP_PID) to target a specific app.
Programmatic client (@downdraft/engine/mcp/client): the single implementation of discovery + handshake + envelope unwrapping — used by draft mcp, the stdio bridge, and the e2e harness. Node- and Bun-compatible.
import { AppClient, launchApp } from "@downdraft/engine/mcp/client";
const client = await AppClient.connect(); // PID-file discoveryconst tools = await client.listTools();const state = await client.callJson("get_world_state");await client.screenshot("shot.png");
const app = await launchApp({ app: "my-app", deterministic: true });await app.client.callText("get_player_state");await app.kill(); // process-tree killOne-shot shell access: draft mcp calls app MCP tools directly from bash (replaces the old scripts/mcp-call.mjs). Output contract: payload on stdout, diagnostics on stderr, text capped at --max-bytes (256 KiB), image blocks never inline — --out <file> writes them to disk, --json prints the raw envelope with data elided to {bytes}.
draft mcp instances # live instances (pid, port, appId)draft mcp tools # tools/list (name + description)draft mcp tools --schema get_world_state # one tool's inputSchemadraft mcp call get_world_state # tools/call, no argsdraft mcp call set_test_state '{"weather":1}' # tools/call with JSON argsdraft mcp screenshot shot.png # capture_screenshot → filedraft mcp run script.ts --app my-app # launch app → run script (gets DOWNDRAFT_MCP_URL) → killKey bridge fixes:
- Notifications (no
id) must not receive a response — the bridge silently drops them. - The proxy handler wraps errors in the
resultfield; the bridge detectsresult.errorand converts it to a proper MCPerrorresponse.
Running the smoke test
Section titled “Running the smoke test”No app repo? The tests/e2e/<app>-smoke.spec.ts specs require a local
clone under apps/<name> (apps are separate repositories, not shipped in
this repo). A self-contained e2e path exists for contributors:
bun test tests/e2e/packaging-smoke-smoke.spec.ts launches the tracked
examples/packaging-smoke fixture headless and asserts its worker/asset
pipeline completes; .github/workflows/packaging.yml exercises the full
package matrix against the same fixture. draft test --app=packaging-smoke
also resolves it via examples/.
The draft test CLI command (packages/cli/src/test.ts) sets DOWNDRAFT_DETERMINISTIC=1 (fixed seed, paused render loop, no autosave, no window) and DOWNDRAFT_GPU=swiftshader by default, then spawns bun test <spec>. It does not launch the app or wait for the MCP endpoint itself — that is the spec’s job (via the harness). The default smoke specs (tests/e2e/<app>-smoke.spec.ts) use the in-app MCP RPC harness to launch and drive the app, but --spec can point at any bun:test file; the MCP harness is not required.
# CPU rendering (SwiftShader, headless) — default, for CIbun run draft:test-cpu
# GPU rendering (hardware Vulkan, headless)bun run draft:test
# Show the window while testing (useful for debugging)bun run draft:test -- --headedbun run draft:test-cpu -- --headed
# Direct CLI usagedraft test --renderer=gpu --headeddraft test --renderer=cpu --spec tests/e2e/my-app.spec.tsCLI flags:
--renderer <gpu|cpu>— WebGPU backend.cpu= SwiftShader software (default),gpu= hardware Vulkan.--headed— Show the native winit window instead of running headless. SetsDOWNDRAFT_HEADED=1.--app <name>— App to test. Resolvesapps/<name>orexamples/<name>from the engine root, or the app dir from cwd. Resolves spec totests/e2e/<app>-smoke.spec.ts.--spec <path>— Override the spec file path.--port <n>— MCP port (default:0= auto-assign a free port).--no-deterministic— Disable fixed seed / render loop pause / window hiding.
draft test sets the env vars in the consolidated table below (DOWNDRAFT_DETERMINISTIC, DOWNDRAFT_GPU, DOWNDRAFT_HEADED, MCP_PORT, MCP_TIMEOUT_MS).
Headless / CI without a display: The CLI auto-detects missing DISPLAY and wraps in xvfb-run if available. Install it with sudo apt install xvfb. winit still needs an X server even when the window is hidden — wgpu renders to the window surface via SwiftShader when DOWNDRAFT_GPU=swiftshader.
Legacy scripts (still available, bypass the CLI): bun run test:e2e (raw bun test, inherits env), test:e2e:headless (forces DOWNDRAFT_GPU=swiftshader), test:e2e:local (hardware GPU).
tests/e2e/harness.ts launches bun apps/<app>/src/native-entry.ts, waits for the native MCP HTTP health endpoint, and drives the app through MCP tool calls. The smoke test (tests/e2e/<app>-smoke.spec.ts) verifies that the tool surface exists, the simulation ticks, injected input advances the world, and a screenshot can be captured.
Build mode: draft release --stage=build (or --build-only) compiles the native binary without packaging. Production-build e2e uses draft release.
Dynamic ports: draft test and the harness auto-allocate a free MCP port via findFreePort() (binds port 0 — OS-assigned), enabling parallel spec execution. Specs read the port from process.env.MCP_PORT (set by draft test --port or the auto-allocated default). To run multiple specs simultaneously, omit --port and let each spec pick its own.
Process cleanup: The harness kills the entire process group (the app process and its worker threads) on test completion. It uses process.kill(-pid, SIGTERM) with a SIGKILL fallback after 5s.
Retry logic: The harness callToolWithRetry() method retries MCP operations on transport errors (connection refused, timeouts) with exponential backoff. Tool-level errors (isError: true) are not retried.
CLI environment variables — consolidated reference
Section titled “CLI environment variables — consolidated reference”The CLI reads and sets a number of environment variables. This is the complete list; see docs/site/src/content/docs/reference/cli.md for the full CLI flag reference.
Set by the CLI (draft test):
| Variable | Value | Purpose |
|---|---|---|
MCP_PORT | <port> | MCP HTTP transport port (unset = ephemeral, auto-discovered via ~/.downdraft/port/<pid>; draft test auto-allocates a free port via findFreePort()) |
MCP_TIMEOUT_MS | 120000 | MCP proxy IPC round-trip timeout (ms) — must exceed the longest wait_for_condition call |
DOWNDRAFT_GPU | swiftshader | hardware | auto | Read by the GPU adapter policy (platform-native/src/gpu/adapter-policy.ts): swiftshader forces wgpu’s fallback/software adapter (SwiftShader/lavapipe/llvmpipe), hardware insists on the real adapter (no software failover), auto tries hardware first and falls back when the driver is denied/fails (default for draft dev). Also forwarded to the app env as the harness’s gpu: launch option |
DOWNDRAFT_DETERMINISTIC | 1 | Skip autosave, no devtools auto-open/error dialogs, paused render loop (on-demand via set_test_state/capture_screenshot). Reaches the app as downdraft.deterministic on the bridge; sim seed stays app-supplied (simConfig.seed) |
DOWNDRAFT_HEADED | 1 | Show the winit window in deterministic mode (without it window.ts suppresses win.show()) |
DOWNDRAFT_TEST_BUILT | 1 | Launch the built app instead of the dev server |
Read by the CLI:
| Variable | Used by | Purpose |
|---|---|---|
AWS_ACCESS_KEY_ID | assets | S3 credentials fallback (manifest config wins) |
AWS_SECRET_ACCESS_KEY | assets | S3 credentials fallback |
DISPLAY | test | When absent, wraps in xvfb-run |
DOWNDRAFT_STRICT_DIAGNOSTICS | all commands | 1 → hard-error on unknown CLI flags (warns otherwise) |
Read by the runtime (set by CLI or user):
| Variable | Used by | Purpose |
|---|---|---|
DOWNDRAFT_STRICT_DIAGNOSTICS | packages/engine/core/src/module/diagnostics.ts | 0/1 force-disable/enable module DI validation (else = Vite dev mode) |
DOWNDRAFT_MCP | packages/engine/core/src/util/logger.ts | 1 routes logs to stderr (keeps stdout clean for MCP JSON-RPC) |
DOWNDRAFT_DISABLE_DEVTOOLS | packages/cli/src/debug.ts | 1 disables the devtools UI entirely — forces devtools.ui to "none" (set by draft debug --no-devtools) |
DOWNDRAFT_MULTI_INSTANCE | packages/platform-native/src/host-lifecycle.ts | 1 opts out of the per-app singleton.lock |
DOWNDRAFT_GPU_TIMESTAMPS | packages/platform-native/src/native-host.ts | 1 enables the extended wgpu timestamp features (timestamp-query-inside-passes) on non-CPU adapters — off by default due to device-loss on lavapipe-class rasterizers and some discrete Vulkan drivers |
DOWNDRAFT_GPU_BACKEND | packages/platform-native/src/gpu/adapter-policy.ts | auto | vulkan | gl | metal | dx12 — restricts the wgpu instance to one backend at creation. gl selects the GLES backend (virgl under virglrenderer) — experimental: Wayland presentation works but with caveats (no view-format reinterpretation → gamma shift on srgb views, no COPY_SRC on the surface → captureScreenshot unavailable, narrower feature coverage) |
DOWNDRAFT_GPU_MODE | packages/platform-native/src/gpu/adapter-policy.ts | performance | stability — graphics-mode preset (same as "gpu": { "mode" } in downdraft.config.json). performance = hardware-only, no fallback (apps); stability = graceful chain vulkan → gl(virgl) → software (desktop apps) |
DOWNDRAFT_GPU_FALLBACK | packages/platform-native/src/gpu/adapter-policy.ts | 0/1 — enables/disables the automatic software-adapter failover in auto adapter mode (default 1) |
DOWNDRAFT_GPU_DENY | packages/platform-native/src/gpu/adapter-policy.ts | Comma-separated substrings (matched against adapter driver/name/backend) extending the built-in denylist (venus, gfxstream) of drivers that abort the process inside requestDevice |
E2E test verification — checking for JS errors
Section titled “E2E test verification — checking for JS errors”Do NOT rely solely on test pass/fail to verify correctness. The e2e tests drive the app through MCP tool calls and assert on returned state, but uncaught JS errors in the app process (uncaught Promise rejections, TypeError from polyfill gaps) will NOT cause test failures unless explicitly checked.
The harness (tests/e2e/harness.ts) captures console output from the app process and exposes it via app.getConsoleErrors(). Tests should include a final assertion that no JS errors occurred:
it("no uncaught JS errors during the test run", async () => { await sleep(500); // wait for pending async errors to surface const errors = app!.getConsoleErrors(); if (errors.length > 0) console.error("Console errors:\n" + errors.join("\n")); expect(errors).toEqual([]);});When verifying changes, always:
- Run
bun run tsc— type-check both web and node configs - Run the e2e test with
DOWNDRAFT_GPU=swiftshader(CPU rendering for CI) - Grep the full test output for error patterns:
grep -E "Uncaught|TypeError|ReferenceError|WrongDocumentError|is not a function|is not defined" /tmp/downdraft-test-*.log - Do NOT ignore errors that appear “during teardown” — they may indicate real bugs (e.g. uncaught Promise rejections from
requestPointerLock())
Common false positives to filter out: GTK module warnings, WebSocket connection failures during teardown, Vulkan/ICD loader chatter.
E2E test gotchas
Section titled “E2E test gotchas”- MCP port conflicts: Ports are ephemeral by default, so this only bites if you pinned
--port. Find the stuck instance viadraft mcp instances/~/.downdraft/port/, free it withfuser -k <port>/tcp, or kill only that app instance as described in “Killing app processes” above — do NOT use a genericpkill -9 bun(it will kill unrelated Bun processes). - Save store hangs in test environments:
createSaveStore()can hang when OPFS is not available (SwiftShader/headless). The MCP harness must be registered BEFORE the save store init so e2e tests can connect. requestPointerLock()may return a Promise:RenderSurface.requestPointerLock?()is typedPromise<void> | void— when it returns a promise it can reject (detached surface, ESC cooldown). Always.catch()the return value to avoid uncaught rejections.
Why not a WebGL2 fallback?
Section titled “Why not a WebGL2 fallback?”The engine relies on WebGPU-specific features (bindless texture_2d_array, storage buffers, compute passes, GBuffer MRT). A WebGL2 renderer would be a second, incompatible implementation. For testing, we instead use SwiftShader’s Vulkan backend to run the unmodified WebGPU pipeline in software, and inject input through the renderer so the MCP harness never has to manipulate pointer lock or raw GPU output.