|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { testScreen } from "@intent-framework/testing" |
| 3 | +import { createScreenRuntime, inspectScreen } from "@intent-framework/core" |
| 4 | +import { ResourceDemo, teamLoadCount } from "./ResourceDemo.js" |
| 5 | + |
| 6 | +const testServices = { |
| 7 | + route: { name: "demo", path: "/:id", params: { id: "team_1" } }, |
| 8 | + navigate: () => {}, |
| 9 | +} as const |
| 10 | + |
| 11 | +describe("ResourceDemo", () => { |
| 12 | + it("autoLoad resource reaches ready", async () => { |
| 13 | + await testScreen(ResourceDemo, async app => { |
| 14 | + expect(app.resource("team").status()).toBe("ready") |
| 15 | + }, { services: testServices as any }) |
| 16 | + }) |
| 17 | + |
| 18 | + it("autoLoad: false resource starts idle", async () => { |
| 19 | + await testScreen(ResourceDemo, async app => { |
| 20 | + expect(app.resource("auditLog").status()).toBe("idle") |
| 21 | + }, { services: testServices as any }) |
| 22 | + }) |
| 23 | + |
| 24 | + it("manual load transitions resource to ready", async () => { |
| 25 | + await testScreen(ResourceDemo, async app => { |
| 26 | + expect(app.resource("auditLog").status()).toBe("idle") |
| 27 | + await app.resource("auditLog").load() |
| 28 | + expect(app.resource("auditLog").status()).toBe("ready") |
| 29 | + }, { services: testServices as any }) |
| 30 | + }) |
| 31 | + |
| 32 | + it("reload increments load count", async () => { |
| 33 | + await testScreen(ResourceDemo, async app => { |
| 34 | + const before = teamLoadCount |
| 35 | + await app.resource("team").reload() |
| 36 | + expect(teamLoadCount).toBe(before + 1) |
| 37 | + }, { services: testServices as any }) |
| 38 | + }) |
| 39 | + |
| 40 | + it("invalidate marks resource stale without reloading", async () => { |
| 41 | + await testScreen(ResourceDemo, async app => { |
| 42 | + const team = app.resource("team") |
| 43 | + expect(team.stale()).toBe(false) |
| 44 | + const before = teamLoadCount |
| 45 | + team.invalidate() |
| 46 | + expect(team.stale()).toBe(true) |
| 47 | + expect(teamLoadCount).toBe(before) |
| 48 | + }, { services: testServices as any }) |
| 49 | + }) |
| 50 | + |
| 51 | + it("reload clears stale", async () => { |
| 52 | + await testScreen(ResourceDemo, async app => { |
| 53 | + const team = app.resource("team") |
| 54 | + team.invalidate() |
| 55 | + expect(team.stale()).toBe(true) |
| 56 | + await team.reload() |
| 57 | + expect(team.stale()).toBe(false) |
| 58 | + }, { services: testServices as any }) |
| 59 | + }) |
| 60 | + |
| 61 | + it("action .invalidates() marks resource stale after success", async () => { |
| 62 | + await testScreen(ResourceDemo, async app => { |
| 63 | + const team = app.resource("team") |
| 64 | + expect(team.stale()).toBe(false) |
| 65 | + await app.act("Save team").run() |
| 66 | + expect(team.stale()).toBe(true) |
| 67 | + }, { services: testServices as any }) |
| 68 | + }) |
| 69 | + |
| 70 | + it("failed loader produces failed status and error", async () => { |
| 71 | + await testScreen(ResourceDemo, async app => { |
| 72 | + expect(app.resource("unstableReport").status()).toBe("failed") |
| 73 | + }, { services: testServices as any }) |
| 74 | + }) |
| 75 | + |
| 76 | + it("failed action does not invalidate resources", async () => { |
| 77 | + await testScreen(ResourceDemo, async app => { |
| 78 | + const team = app.resource("team") |
| 79 | + expect(team.stale()).toBe(false) |
| 80 | + await app.act("Broken save").run() |
| 81 | + expect(team.stale()).toBe(false) |
| 82 | + }, { services: testServices as any }) |
| 83 | + }) |
| 84 | + |
| 85 | + it("inspectScreen reports resources with status/stale/error", async () => { |
| 86 | + const runtime = createScreenRuntime(ResourceDemo, { services: testServices as any }) |
| 87 | + await runtime.start() |
| 88 | + const graph = runtime.graph |
| 89 | + const resources = graph.resources |
| 90 | + expect(resources).toHaveLength(3) |
| 91 | + const teamRes = resources.find(r => r.name === "team")! |
| 92 | + expect(teamRes.status).toBe("ready") |
| 93 | + expect(teamRes.hasValue).toBe(true) |
| 94 | + expect(teamRes.stale).toBe(false) |
| 95 | + const auditRes = resources.find(r => r.name === "auditLog")! |
| 96 | + expect(auditRes.status).toBe("idle") |
| 97 | + expect(auditRes.hasValue).toBe(false) |
| 98 | + expect(auditRes.stale).toBe(false) |
| 99 | + const failedRes = resources.find(r => r.name === "unstableReport")! |
| 100 | + expect(failedRes.status).toBe("failed") |
| 101 | + expect(failedRes.hasValue).toBe(false) |
| 102 | + expect(failedRes.stale).toBe(false) |
| 103 | + expect(failedRes.error).toBe("Report generation failed") |
| 104 | + runtime.dispose() |
| 105 | + }) |
| 106 | + |
| 107 | + it("no unrelated graph diagnostics are introduced", () => { |
| 108 | + const graph = inspectScreen(ResourceDemo) |
| 109 | + expect(graph.diagnostics).toHaveLength(0) |
| 110 | + }) |
| 111 | +}) |
0 commit comments