The local test server
The combined server on 127.0.0.1:4790 that Exact starts for tests: what it mounts, what it proxies, and what it refuses.

Exact runs one server per app during test, verify and agent --test, and sends a test's request steps to the first loopback origin granted in app.ts. Seed Exact uses that single slot for a combined server that hosts every mock and also acts as a Seed site, so a test can point the app's server setting at it.

Files

File

Role

tools/test-server/index.ts

Entry point named by app.json commands.server. Binds the port and starts the agents mock

tools/test-server/routes.ts

handle(req): routing, proxying and CORS. Importable without binding a port

tools/test-server/routes.test.ts

Five Bun tests of the browser boundary, part of bun run test

Startup

index.ts calls Bun.serve({port: 4790, hostname: '127.0.0.1', fetch: handle}), then imports ../mock-agents/server, which binds its own port as a side effect. The import is wrapped in try: if another checkout holds the agents port, 4790 still serves, so only agents tests fail (index.ts L9-L20).

Start it by hand with bun exact.mjs server or bun tools/test-server/index.ts. The test verb refuses to run while a server already answers there.

Routing order

route(req) in routes.ts L26-L45 tries, in order:

    The notify mock handler, then the vault mock handler. Each returns a Response or null. Notify owns /activity/* (and, in its thread scenario, three /api/* paths); vault owns /vault/*.

    /dev/*: forwarded with method, headers and body to the agents mock at MOCK_AGENTS_URL (http://127.0.0.1:3068, from settings.ts). This lets a test's request POST "/dev/reset" reach a server on another port.

    /hm/api/config: rewritten to /activity/hm/api/config and answered by the notify mock, so the app discovers a local notify URL when its server is http://127.0.0.1:4790.

    Any path starting with /hm/api/notify or /hm/api/notifications, any method: 403 {"error": "Hosted notify is disabled in tests."}. The comment reads "Even GET must never proxy a notify endpoint to the hosted service."

    GET on /hm/* or /api/*: proxied to https://hyper.media with the same path and query.

    Everything else: 404 not found.

flowchart TD R["Request to 127.0.0.1:4790"] --> O{"OPTIONS?"} O -->|yes| P["204 with CORS"] O -->|no| N{"notify or vault mock answers?"} N -->|yes| X["mock response"] N -->|no| D{"/dev/*"} D -->|yes| G["forward to 127.0.0.1:3068"] D -->|no| C{"/hm/api/config"} C -->|yes| X C -->|no| F{"hosted notify path"} F -->|yes| E["403"] F -->|no| U{"GET /hm/* or /api/*"} U -->|yes| H["proxy to hyper.media"] U -->|no| Z["404"]

Proxying rules

forward(req, target) (routes.ts L12-L24):

    Copies the request headers but deletes host. "Bun preserves an explicit Host header; the local origin would select the wrong virtual host at hyper.media (404 instead of the requested document)."

    Sends a body only for methods other than GET and HEAD.

    Uses redirect: 'error', so an upstream redirect becomes a failure.

    Buffers the reply and drops content-encoding and content-length, because the body is already decoded.

Only GET is proxied upstream, so a publish (POST /api/PublishBlobs) aimed at 4790 gets 404 unless a mock scenario answers it. Tests cannot write to hyper.media through this server.

CORS and errors

The web app under test runs on a different, dynamically assigned origin, so handle (routes.ts L47-L59) answers every OPTIONS with 204 and sets these headers on every response, including errors:

Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Content-Type, Accept, Authorization Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS

A thrown error inside routing (for example the upstream is unreachable) becomes 502 {"error": "Test upstream unavailable."}.

Stale port comments

The comments and the startup log line in index.ts say the agents mock is on port 3058. The mock binds 3068 (MOCK_AGENTS_URL in both tools/mock-agents/settings.ts and src/agents/sources.ts), and agents.test.contract asserts agentServer == "http://127.0.0.1:3068". The app.ts grant list also still names http://127.0.0.1:3058; the app reaches 3068 through its general net.fetch http: grant.

Tests of the server

routes.test.ts checks: the proxy uses the upstream Host (a live read of the hyper.media home through handle), /hm/api/config is readable cross origin and makes meCheckServer and resolveNotifyUrl accept the local origin, the CBOR POST preflight passes, errors carry CORS, and both hosted notify paths answer 403 for GET and POST.

Related

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime