Tests
// server/index.test.ts: the rules
const t = testApp(app) // in memory, synchronous, clock at 2026-01-15 12:00 UTC
// the clock moves 1 ms each time the app reads it (rows made one after another get increasing createdAt)
const room = t.as("ana").mutationOk("createRoom", { name: "General" })
expect(t.as("bob").mutation("createRoom", { name: "General" })).toMatchObject({ _tag: "Failure", error: { _tag: "NameTaken" } })
await t.advance(DAY); t.today(); t.run("internalMutation"); missingId("rooms") // a well-formed id no row has
// advance runs the scheduled jobs and crons due on the way, each at its time; t.jobs() lists the pending ones,
// crons' next runs included ({ name, at, args, cron: the cron's name, or null for runAfter })
// actions: no network in tests; stub API operations by name, or fetch
const t2 = testApp(app, { apis: { github: { issuesListForRepo: () => [issue] } }, fetch: (url) => Response.json({ quote: "Hi" }) })
await t2.as("ana").actionOk("syncIssues", { repo: "a/b" }); await t2.runAction("syncAll")
// src/app.test.tsx: scenes, the app's real pages used like a person would (in-process, milliseconds)
import { as, choose, click, label, meanwhile, notSee, role, scene, see, testApp, text, type, visit, within } from "canica/testing"
import { routes } from "./routes.gen.ts"
await scene(t, routes,
as("ana"), visit("/"),
click(role("button", { name: "New room" })), // forms are in modals
within(role("dialog"), type(label("Name"), "General"), choose(label("Visibility"), "Public"), click(role("button", { name: "Create" }))),
see(text("General")),
meanwhile(() => t.as("bob").mutationOk("send", { room, body: "hi" })), // another user, live
see(text("hi")),
click(role("button", { name: "Rename" })), within(role("dialog"), type(label("Name"), "Lobby"), click(role("button", { name: "OK" }))),
)
text("…") is an element's whole text; text(/part/) matches part of it. click/type act on the first match
(narrow with a role's name or within(…)); as(name) switches user on the page you are on and waits for it to load. Test users are signed in
(guests in public apps: { signedIn: true } there for an account); as("visitor", { signedIn: false }) has not signed
in (the sign-in screen); seed users are signed in. Scenes run at the test clock (t.advance), without network. A value made by an earlier step: keep it in a variable
(let room = missingId("rooms"), set in meanwhile) and pass a function wherever a value or a locator text goes
(type(label("Code"), () => code), role("link", { name: () => title }), visit("/rooms/$room", () => ({ room }))).