Tools
Testing
Mental model#
Tests in the program#
Examplea whole program
model Person id: id = me name: string muted: bool = false read anyone add if me != nonemodel Message text: string by: id = me at: time = now read if me != none add if me != none// What no page may read.model Sent to: string at: time = nowserver before Message.add(m) let who = Person.get(m.by)? if who?.muted == true fail("you are muted here")?job fn welcome(who: id) retries=2 let said = net.send("POST", "https://mail.example/send", {body: {to: who}})? if said.status != 200 fail(`the mail service said ${said.status}`)? Sent.add({to: who})?server after Person.add(p) welcome(p.id)server fn join(name: string): Person requires signedIn return Person.add({name})?server fn mute(who: id): number Person.set(who, {muted: true})? return 1// Each night, messages a month old go.server at "03:00" Message.del({at: before(now() - 30 * 86400000)})?test "a muted person cannot post" given net "https://mail.example/*" answers {status: 200} as "ada@example.com" let ada = join("Ada")? expect Message.add({text: "hello"}).text == "hello" mute(ada.id)? expect Message.add({text: "again"}) fails "you are muted here" expect Message.count({}) == 1 as none expect join("Nobody") fails kind "signin"test "a welcome is queued, sent, and tried again if the mail service is down" given net "https://mail.example/*" answers {status: 503} clock "2026-03-01T09:00" as "bob@example.com" join("Bob")? expect queued(welcome).length == 1 run jobs expect sent(net).length == 1 && sent(net)[0].url.startsWith("https://mail.") expect queued(welcome)[0].words == "the mail service said 503" given net "https://mail.example/*" answers {status: 200} clock +1 second run jobs expect queued(welcome).length == 0 trusted expect Sent.count({}) == 1test "old messages go in the night" given net "https://mail.example/*" answers {status: 200} clock "2026-03-01T23:42" as "cy@example.com" join("Cy")? Message.add({text: "late"})? clock +29 days expect Message.count({}) == 1 clock +2 days as "cy@example.com" expect Message.count({}) == 0text "A shop"Settings for one test#
Examplea whole program
rule staff = me.email in setting.list("SHOP_STAFF")model Note text: string read if staff add if stafffn keepDays(): number = Number(env("KEEP_DAYS")) || 90server fn days(): number requires staff return keepDays()test "staff are who the setting says" given env "SHOP_STAFF" is "ada@shop.test" as "ada@shop.test" Note.add({text: "hello"})? expect days() == 90 as "bo@shop.test" expect Note.add({text: "no"}) fails expect days() fails kind "refused"test "the setting is read for each test, and so is how long things are kept" given env "SHOP_STAFF" is "bo@shop.test" given env "KEEP_DAYS" is "30" as "bo@shop.test" expect days() == 30text "Notes"A test of the page#
Examplea whole program
app "Notes" routes=pathsmodel Note text: string by: id = me read if me != none add if me != nonelive notes: Note[] = Note.find({}) else []state draft = ""state gone = falseaction add() Note.add({text: draft})? draft = ""action later() time.sleep(5000) gone = trueinput value=draft placeholder="New note"button "Add" press={add()}button "Later" press={later()}if gone text "It went"for n in notes key=n.id text n.texttest "a note is added by the page, and is on the back end" page as "ada@example.com" page.type("New note", "Milk") page.press("Add") expect page.has("Milk") expect Note.count({}) == 1test "a note added behind the page comes to it" page as "bo@example.com" page.open("/") Note.add({text: "From behind"})? page.see("From behind")test "five seconds, at once" page page.press("Later") expect !page.has("It went") clock +5 seconds expect page.has("It went")tests/all.sh#
Which compiler#
Running several at once: -j#
--affected#
The big ones#
Driving a page with no waits#
tests/real.mjs#
Violations of a policy#
tests/slowproxy.mjs#
Writing a test#
Examplea whole program
state n = 0state name = ""action add n += 1column gap=8 padding=16 input value=name id="name" button "Add" press=add text `${name} pressed ${n} times` id="say"The compiler's unit tests#
docs/check.sh#
Pitfalls#
In the source#
Edit this page:
docs/testing.md