|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { testScreen } from "@intent-framework/testing" |
| 3 | +import { inspectScreen } from "@intent-framework/core" |
| 4 | +import { RegistrationForm } from "./RegistrationForm.js" |
| 5 | + |
| 6 | +describe("RegistrationForm", () => { |
| 7 | + it("text state can be answered through the testing harness", async () => { |
| 8 | + await testScreen(RegistrationForm, async app => { |
| 9 | + await app.answer("Name", "Ada Lovelace") |
| 10 | + await app.answer("Email", "ada@example.com") |
| 11 | + app.act("Register").toBeBlockedBy("Enter a valid password") |
| 12 | + }) |
| 13 | + }) |
| 14 | + |
| 15 | + it("choice state can be answered and read", async () => { |
| 16 | + await testScreen(RegistrationForm, async app => { |
| 17 | + await app.answer("Role", "admin") |
| 18 | + const roleAsk = app.state().asks.find(a => a.label === "Role")! |
| 19 | + const value = (roleAsk.state as { value: string }).value |
| 20 | + expect(value).toBe("admin") |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + it("boolean state can be toggled", async () => { |
| 25 | + await testScreen(RegistrationForm, async app => { |
| 26 | + const termsNode = app.state().asks.find(a => a.label === "Accept terms")! |
| 27 | + const state = termsNode.state as unknown as { value: boolean; toggle: () => void } |
| 28 | + expect(state.value).toBe(false) |
| 29 | + state.toggle() |
| 30 | + expect(state.value).toBe(true) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it("secret ask participates in validity", async () => { |
| 35 | + await testScreen(RegistrationForm, async app => { |
| 36 | + await app.answer("Password", "short") |
| 37 | + app.act("Register").toBeBlocked() |
| 38 | + const passAsk = app.state().asks.find(a => a.label === "Password")! |
| 39 | + expect(passAsk.error).toBe("Password must be at least 8 characters") |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it("submit is blocked before required inputs are valid", async () => { |
| 44 | + await testScreen(RegistrationForm, async app => { |
| 45 | + app.act("Register").toBeBlocked() |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + it("submit becomes enabled after valid inputs and accepted terms", async () => { |
| 50 | + await testScreen(RegistrationForm, async app => { |
| 51 | + await app.answer("Name", "Ada Lovelace") |
| 52 | + await app.answer("Email", "ada@example.com") |
| 53 | + await app.answer("Password", "securepass123") |
| 54 | + await app.answer("Role", "admin") |
| 55 | + |
| 56 | + const termsNode = app.state().asks.find(a => a.label === "Accept terms")! |
| 57 | + const termsState = termsNode.state as unknown as { set: (v: boolean) => void; value: boolean } |
| 58 | + termsState.set(false) |
| 59 | + |
| 60 | + app.act("Register").toBeBlockedBy("Accept the terms to continue") |
| 61 | + |
| 62 | + termsState.set(true) |
| 63 | + |
| 64 | + app.act("Register").toBeEnabled() |
| 65 | + |
| 66 | + await app.act("Register").run() |
| 67 | + expect(app.feedback()).toBe("Registration complete!") |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + it("inspectScreen reports multiple surfaces", () => { |
| 72 | + const graph = inspectScreen(RegistrationForm) |
| 73 | + expect(graph.surfaces).toHaveLength(2) |
| 74 | + expect(graph.surfaces[0]!.name).toBe("main") |
| 75 | + expect(graph.surfaces[1]!.name).toBe("sidebar") |
| 76 | + }) |
| 77 | + |
| 78 | + it("inspectScreen reports no flow diagnostics for this correct example", () => { |
| 79 | + const graph = inspectScreen(RegistrationForm) |
| 80 | + const flowWarnings = graph.diagnostics.filter( |
| 81 | + d => d.code === "flow-step-not-surfaced" || d.code === "orphaned-flow", |
| 82 | + ) |
| 83 | + expect(flowWarnings).toHaveLength(0) |
| 84 | + }) |
| 85 | +}) |
0 commit comments