Files
..

src/mock/ — the in-browser mock backend

A fake backend that runs in the page, behind the same orval-generated DTOs the real one serves. It exists so the whole PWA can be demoed and driven by an automated frontend test with no FastAPI, no sidecar, no transcripts on disk.

npm run dev:mock            # Vite with the mock forced on (VITE_MOCK=1)
# …or, against any build (including the deployed one):
open https://ai-agent.lab.gabvdl.xyz/?mock=1     # sticky until ?mock=0

The mock browser is the owner. Four in-memory guests fill Settings → Users — Dom (may start projects), Claire (never signed in), Camille Martin (her own Claude login, near her budget) and Léo (disabled) — each with a page whose spend charts and recent conversations come from mockUserActivity (real mock conversations, so the links open; the 30 days follow the real clock). Add &mockGuest=1 (sticky until ?mockGuest=0) to be Dom — the guest composer with its project pill and + New project pick — and &mockOwnClaude=1 (sticky, =0 clears) to give Dom their own Claude: the home strip's gauges and budget tile (/api/me/usage), the composer's rings. &mockNoProjects=1 (sticky, =0 clears) takes Dom's projects away: the composer's pill then reads No project (no Changes chip). /api/me carries real perms (the owner: every catalog id; Dom: the Guest role + her overrides, roles.ts); &mockRole=member|viewer|admin (sticky, =0 clears) makes the mock browser Dom holding that built-in role instead — what the Settings navigation shows each role (docs/settings.md).

How it works

installMock() (called from main.tsx before the app tree is imported, since several modules fetch at import time) swaps two globals:

  • window.fetch — every same-origin /api/* request is answered by handlers.ts from the in-memory database; anything else falls through to the real fetch.
  • window.EventSource — /api/events is served by a tiny hub that emits the same {type: "meta" | "transcript", ids} events backend/events.py does, so the app's SSE-driven React Query invalidation runs for real.
file role
enabled.ts the ?mock=1 / VITE_MOCK flag (tiny — the only part in the main bundle)
db.ts the database: one object, persisted to localStorage, plus the SSE hub and the detail→summary projections
seed.ts the combinatorial fixture (below)
handlers.ts the routing table — every /api/* route, reads and writes
sim.ts spawn / resume / interrupt: a scripted turn that streams
usage.ts plan usage (/api/usage/*): the /usage % series, daily meters and weekly calibration fits
security.ts Settings → Security (/api/security/*): a log seeded on the real clock (every kind, real conversations' sessions), the push picks, Lock guests and Stop every run
computers.ts guests' own computers (/api/me/computers, /api/users/{id}/computers, and the owner workers /api/workers adds for the owner): Dom has two of three, Camille a paused one; Add a computer pairs a new one in 8 s; ?mockWaiting=plan|budget (sticky, =0 clears) puts them on hold for the Me banner
roles.ts + rolesCatalog.json Settings → Roles and a guest's Role & permissions (/api/roles…, the role / allow / deny / perms of /api/users): the catalog and built-in roles are a dump of the real /api/roles — regenerate it after changing backend/authz/ (the one-liner in docs/permissions.md); edits and clones are in memory. Seed guests spelled with the old switches get the overrides the backend's migration would give them
hostFiles.ts the host files the file-explorer plugin reads (its hostfiles.read bridge): one small tree per project, one file per kind
filesPlugins.ts the file-explorer (host files, the project Files card, file chips' target) and context-editor (context tree + editor) plugins: pages, slot, actions; the real documents when copied to /mock-plugin-ui/<plugin>/, else a stand-in; ?mockFiles=0 turns both off
index.ts installs the interceptors, window.__mock, the MOCK API badge

Writes really mutate the db (and persist): archiving a conversation, editing a file, saving settings (the server-backed Zustand stores hit /api/ui-state/*), uploading an attachment. A spawn or resume actually runs — items are appended to the conversation one at a time, each publishing a transcript event, until the run stamps notified + a message ending in DONE and flips to finished. interrupt drops the rest of the script and records an interruption. Opening the seeded subagent (conv-subagent, whose title is a prompt) or the unnamed top-level conv-long-title simulates the backend's Haiku short title: 1.5 s later a short-title event swaps the header (never after a rename). The kitchen sink's Subagents card then shows conv-subagent's cached short title; sub-explore-1 carries one from the seed.

The seed is combinatorial

seed.ts doesn't hand-write a few plausible rows — it enumerates the cross-product of every field the UI branches on, so every visual state is reachable from a cold seed:

  • conversations — harness (claude, pi) × state (running, finished, idle) × lifecycle (nothing / committed+pushed / merged+deployed+notified), plus an archived one per harness, a subagent conversation linked to its parent, an empty transcript, and a kitchen-sink thread that contains every ThreadItemKind (text, thinking, tool_use, tool_result, interrupted, paused), every role, an error result, and one card per rendered tool: Read (+ image and screenshot widgets, and a path wider than a phone), Edit, Write (plan widget), Bash (+ a deploy URL), Grep, Glob, WebSearch, WebFetch, Skill (a repo skill, a built-in one, and a typed /init), Task (subagent), TaskCreate. The built-ins' prompts on those cards are what /api/skills/{name}/prompt serves.
  • files — every FileEntryKind × editable × estimated.
  • services — the services plugin's catalogue (servicesPlugin.ts: Projects → Services, /api/plugins/services-index; ?mockServices=0 switches it off): the lab's named ones (traefik, ai-agent…), then every auth value (auth, public, api, mixed, none) × router kind (http, tcp). Its page is the real plugin's document when copied to /mock-plugin-ui/services/services.html, else a small stand-in.
  • plugin batches — GET /api/plugins/ui-bundle?where= (every slot document of a place) and GET|POST /api/plugins/batch-read (several read actions) are answered in handlers.ts by calling the per-slot / per-action mock routes, so the Details drawer's one-bundle + one-batch path runs under ?mock=1.
  • projects — with/without costs, url, package.json; both OgImage kinds.
  • diffs — every FileDiffStatus and DiffLineType, plus a binary and a truncated file.
  • agents — a scheduled repo agent (two seeded conversations are its cron runs, meta.cron), a never-run one, a project-local one, and a built-in type with no file, plus the runs behind their counters in all three origins (task / cron / manual).

It is deterministic (fixed clock, no Math.random), so a test can assert on ids, counts and totals.

The one exception to the fixed clock is plan usage (usage.ts): its % series, gauges and fits are anchored on the request time (rounded down to the hour), so the countdowns to a window's reset read live. The values are still RNG-free — a fixed daily rhythm plus a hash of the hour — and include the cases the page branches on: an untracked evening, a night with no readings, a stale work-seat reading and two low-confidence personal fits (Sep 11–18).

Test hooks

window.__mock.db()          // the live database object
window.__mock.reset()       // wipe localStorage + reseed
window.__mock.speed(0)      // 0 = don't stream on a timer…
window.__mock.flush()       // …land every in-flight run instantly (deterministic)
window.__mock.publish({type: "meta"})   // fire an SSE event by hand

A Puppeteer/Playwright pass therefore needs no fixtures of its own: load /?mock=1, walk the routes, POST /api/spawn, flush(), assert.

Keeping it honest

The mock's rows are typed with the generated src/generated/model types (re-exported by types.ts), so a backend schema change that lands in openapi.json → npm run gen:api breaks the seed at tsc time instead of silently drifting from the API it pretends to be.