|
1 | 1 | import { describe, it, expect } from "vitest" |
2 | 2 | import { testScreen } from "@intent-framework/testing" |
3 | 3 | import { createScreenRuntime, inspectScreen } from "@intent-framework/core" |
4 | | -import { ResourceDemo, teamLoadCount, cachedTeamLoadCount, dedupeReportLoadCount } from "./ResourceDemo.js" |
| 4 | +import { ResourceDemo, teamLoadCount, cachedTeamLoadCount, dedupeReportLoadCount, keyedTeamLoadCount } from "./ResourceDemo.js" |
5 | 5 |
|
6 | 6 | const testServices = { |
7 | 7 | route: { name: "demo", path: "/:id", params: { id: "team_1" } }, |
@@ -116,12 +116,67 @@ describe("ResourceDemo", () => { |
116 | 116 | }, { services: testServices as any }) |
117 | 117 | }) |
118 | 118 |
|
| 119 | + it("cache.key derives resource value from route context", async () => { |
| 120 | + const runtime = createScreenRuntime(ResourceDemo, { services: testServices as any }) |
| 121 | + await runtime.start() |
| 122 | + const keyed = runtime.resources.find(r => r.name === "keyedTeam")! |
| 123 | + expect(keyed.status).toBe("ready") |
| 124 | + expect((keyed.value as any)?.id).toBe("team_1") |
| 125 | + expect((keyed.value as any)?.name).toBe("Team-team_1") |
| 126 | + runtime.dispose() |
| 127 | + }) |
| 128 | + |
| 129 | + it("cache.key: different keys are independent", async () => { |
| 130 | + const runtime = createScreenRuntime(ResourceDemo, { services: testServices as any }) |
| 131 | + await runtime.start() |
| 132 | + const keyed = runtime.resources.find(r => r.name === "keyedTeam")! |
| 133 | + const before = keyedTeamLoadCount |
| 134 | + // Load with a different key |
| 135 | + await keyed.load({ route: { name: "demo", path: "/:id", params: { id: "team_b" } } }) |
| 136 | + expect(keyed.status).toBe("ready") |
| 137 | + expect((keyed.value as any)?.id).toBe("team_b") |
| 138 | + expect((keyed.value as any)?.name).toBe("Team-team_b") |
| 139 | + expect(keyedTeamLoadCount).toBe(before + 1) |
| 140 | + runtime.dispose() |
| 141 | + }) |
| 142 | + |
| 143 | + it("cache.key: switching active key updates visible value/status", async () => { |
| 144 | + const runtime = createScreenRuntime(ResourceDemo, { services: testServices as any }) |
| 145 | + await runtime.start() |
| 146 | + const keyed = runtime.resources.find(r => r.name === "keyedTeam")! |
| 147 | + // Initially keyed by route param "team_1" |
| 148 | + expect((keyed.value as any)?.id).toBe("team_1") |
| 149 | + // Switch to team_b |
| 150 | + await keyed.load({ route: { name: "demo", path: "/:id", params: { id: "team_b" } } }) |
| 151 | + expect(keyed.status).toBe("ready") |
| 152 | + expect((keyed.value as any)?.id).toBe("team_b") |
| 153 | + // Switch back to team_1 |
| 154 | + await keyed.load({ route: { name: "demo", path: "/:id", params: { id: "team_1" } } }) |
| 155 | + expect(keyed.status).toBe("ready") |
| 156 | + expect((keyed.value as any)?.id).toBe("team_1") |
| 157 | + runtime.dispose() |
| 158 | + }) |
| 159 | + |
| 160 | + it("cache.key: no-arg reload reuses last active key", async () => { |
| 161 | + const runtime = createScreenRuntime(ResourceDemo, { services: testServices as any }) |
| 162 | + await runtime.start() |
| 163 | + const keyed = runtime.resources.find(r => r.name === "keyedTeam")! |
| 164 | + // Load team_b — changes active key |
| 165 | + await keyed.load({ route: { name: "demo", path: "/:id", params: { id: "team_b" } } }) |
| 166 | + const afterFirst = keyedTeamLoadCount |
| 167 | + // no-arg reload uses lastContext → reloads team_b |
| 168 | + await keyed.reload() |
| 169 | + expect(keyedTeamLoadCount).toBe(afterFirst + 1) |
| 170 | + expect((keyed.value as any)?.id).toBe("team_b") |
| 171 | + runtime.dispose() |
| 172 | + }) |
| 173 | + |
119 | 174 | it("inspectScreen reports resources with status/stale/error", async () => { |
120 | 175 | const runtime = createScreenRuntime(ResourceDemo, { services: testServices as any }) |
121 | 176 | await runtime.start() |
122 | 177 | const graph = runtime.graph |
123 | 178 | const resources = graph.resources |
124 | | - expect(resources).toHaveLength(5) |
| 179 | + expect(resources).toHaveLength(6) |
125 | 180 | const teamRes = resources.find(r => r.name === "team")! |
126 | 181 | expect(teamRes.status).toBe("ready") |
127 | 182 | expect(teamRes.hasValue).toBe(true) |
|
0 commit comments