|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { testScreen } from "@intent-framework/testing" |
| 3 | +import { inspectScreen } from "@intent-framework/core" |
| 4 | +import { LoginScreen } from "./LoginScreen.js" |
| 5 | +import { VaultScreen } from "./VaultScreen.js" |
| 6 | +import { RecoveryScreen } from "./RecoveryScreen.js" |
| 7 | +import { vaultRouter, type VaultServices } from "./router.js" |
| 8 | + |
| 9 | +function navServices(routeName: string): VaultServices { |
| 10 | + return { |
| 11 | + navigate: () => {}, |
| 12 | + route: { name: routeName, path: `/${routeName}`, params: {} }, |
| 13 | + } as unknown as VaultServices |
| 14 | +} |
| 15 | + |
| 16 | +describe("LoginScreen", () => { |
| 17 | + it("flow has steps in expected order", () => { |
| 18 | + const flow = LoginScreen.flows.find(f => f.name === "login")! |
| 19 | + expect(flow.steps).toHaveLength(3) |
| 20 | + expect(flow.steps[0]!.node).toMatchObject({ label: "Username or email" }) |
| 21 | + expect(flow.steps[1]!.node).toMatchObject({ label: "Password" }) |
| 22 | + expect(flow.steps[2]!.node).toMatchObject({ label: "Unlock vault" }) |
| 23 | + }) |
| 24 | + |
| 25 | + it("password ask is secret and private", () => { |
| 26 | + const ask = LoginScreen.asks.find(a => a.label === "Password")! |
| 27 | + expect(ask.kind).toBe("secret") |
| 28 | + expect(ask.isPrivate).toBe(true) |
| 29 | + }) |
| 30 | + |
| 31 | + it("login action is blocked until username/email and password are valid", async () => { |
| 32 | + await testScreen(LoginScreen, async app => { |
| 33 | + app.act("Unlock vault").toBeBlocked() |
| 34 | + }, { services: navServices("login") }) |
| 35 | + }) |
| 36 | + |
| 37 | + it("login action enables after valid inputs", async () => { |
| 38 | + await testScreen(LoginScreen, async app => { |
| 39 | + await app.answer("Username or email", "user@example.com") |
| 40 | + await app.answer("Password", "password123") |
| 41 | + app.act("Unlock vault").toBeEnabled() |
| 42 | + }, { services: navServices("login") }) |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe("VaultScreen", () => { |
| 47 | + it("secret key ask is secret and private", () => { |
| 48 | + const ask = VaultScreen.asks.find(a => a.label === "Secret key")! |
| 49 | + expect(ask.kind).toBe("secret") |
| 50 | + expect(ask.isPrivate).toBe(true) |
| 51 | + }) |
| 52 | + |
| 53 | + it("decrypt action is blocked until secret key is valid", async () => { |
| 54 | + await testScreen(VaultScreen, async app => { |
| 55 | + app.act("Decrypt vault").toBeBlocked() |
| 56 | + }, { services: navServices("vault") }) |
| 57 | + }) |
| 58 | + |
| 59 | + it("BooleanState toggles locked/unlocked state", async () => { |
| 60 | + await testScreen(VaultScreen, async app => { |
| 61 | + // Initially locked — lock is blocked, decrypt is blocked until keys entered |
| 62 | + app.act("Lock vault").toBeBlockedBy("Vault is already locked") |
| 63 | + |
| 64 | + await app.answer("Secret key", "my-secret-key") |
| 65 | + await app.answer("Confirm key", "my-secret-key") |
| 66 | + |
| 67 | + // Decrypt then verify unlocked |
| 68 | + await app.act("Decrypt vault").run() |
| 69 | + app.act("Decrypt vault").toBeBlockedBy("Vault is already unlocked") |
| 70 | + app.act("Lock vault").toBeEnabled() |
| 71 | + |
| 72 | + // Lock back |
| 73 | + await app.act("Lock vault").run() |
| 74 | + app.act("Lock vault").toBeBlockedBy("Vault is already locked") |
| 75 | + }, { services: navServices("vault") }) |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +describe("RecoveryScreen", () => { |
| 80 | + it("reset action is blocked until valid recovery email", async () => { |
| 81 | + await testScreen(RecoveryScreen, async app => { |
| 82 | + app.act("Reset vault").toBeBlocked() |
| 83 | + |
| 84 | + await app.answer("Recovery email", "not-an-email") |
| 85 | + app.act("Reset vault").toBeBlocked() |
| 86 | + |
| 87 | + await app.answer("Recovery email", "user@example.com") |
| 88 | + app.act("Reset vault").toBeEnabled() |
| 89 | + }, { services: navServices("recovery") }) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +describe("Router", () => { |
| 94 | + it("has login, vault, and recovery routes", () => { |
| 95 | + const routes = vaultRouter.routes() |
| 96 | + expect(routes).toHaveLength(3) |
| 97 | + |
| 98 | + const names = routes.map(r => r.name) |
| 99 | + expect(names).toContain("login") |
| 100 | + expect(names).toContain("vault") |
| 101 | + expect(names).toContain("recovery") |
| 102 | + }) |
| 103 | + |
| 104 | + it("navigation flow can move login → vault → recovery → login", async () => { |
| 105 | + const navigated: string[] = [] |
| 106 | + const trackNav = (name: string) => navigated.push(name) |
| 107 | + |
| 108 | + // Login → Vault |
| 109 | + await testScreen(LoginScreen, async app => { |
| 110 | + await app.answer("Username or email", "user@example.com") |
| 111 | + await app.answer("Password", "password123") |
| 112 | + await app.act("Unlock vault").run() |
| 113 | + }, { |
| 114 | + services: { navigate: trackNav, route: { name: "login", path: "/login", params: {} } } as unknown as VaultServices, |
| 115 | + }) |
| 116 | + expect(navigated).toContain("vault") |
| 117 | + navigated.length = 0 |
| 118 | + |
| 119 | + // Vault → Recovery |
| 120 | + await testScreen(VaultScreen, async app => { |
| 121 | + await app.act("Forgot key?").run() |
| 122 | + }, { |
| 123 | + services: { navigate: trackNav, route: { name: "vault", path: "/vault", params: {} } } as unknown as VaultServices, |
| 124 | + }) |
| 125 | + expect(navigated).toContain("recovery") |
| 126 | + navigated.length = 0 |
| 127 | + |
| 128 | + // Recovery → Login |
| 129 | + await testScreen(RecoveryScreen, async app => { |
| 130 | + await app.act("Back to login").run() |
| 131 | + }, { |
| 132 | + services: { navigate: trackNav, route: { name: "recovery", path: "/recovery", params: {} } } as unknown as VaultServices, |
| 133 | + }) |
| 134 | + expect(navigated).toContain("login") |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +describe("Diagnostics", () => { |
| 139 | + it("inspectScreen reports no orphaned-flow or flow-step-not-surfaced for correct screens", () => { |
| 140 | + for (const screen of [LoginScreen, VaultScreen, RecoveryScreen]) { |
| 141 | + const graph = inspectScreen(screen) |
| 142 | + const flowIssues = graph.diagnostics.filter( |
| 143 | + d => d.code === "orphaned-flow" || d.code === "flow-step-not-surfaced", |
| 144 | + ) |
| 145 | + expect(flowIssues).toHaveLength(0) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + it("inspectScreen reports flow stepCount for each screen", () => { |
| 150 | + const loginGraph = inspectScreen(LoginScreen) |
| 151 | + const vaultGraph = inspectScreen(VaultScreen) |
| 152 | + const recoveryGraph = inspectScreen(RecoveryScreen) |
| 153 | + |
| 154 | + expect(loginGraph.flows.find(f => f.name === "login")!.stepCount).toBe(3) |
| 155 | + expect(vaultGraph.flows.find(f => f.name === "vaultAccess")!.stepCount).toBe(3) |
| 156 | + expect(recoveryGraph.flows.find(f => f.name === "recovery")!.stepCount).toBe(2) |
| 157 | + }) |
| 158 | +}) |
0 commit comments