@dreamboard-games/cli 0.1.30-alpha.2 → 0.1.30-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/agent-verifier/agent-workspace-verifier.mjs +227 -0
  2. package/dist/agent-verifier/agent-workspace-verifier.mjs.map +1 -0
  3. package/dist/agent-verifier/chunk-27EEIZCI.mjs +185 -0
  4. package/dist/agent-verifier/chunk-27EEIZCI.mjs.map +1 -0
  5. package/dist/agent-verifier/chunk-5NYBTZB4.mjs +226 -0
  6. package/dist/agent-verifier/chunk-5NYBTZB4.mjs.map +1 -0
  7. package/dist/agent-verifier/chunk-776W3UGV.mjs +167 -0
  8. package/dist/agent-verifier/chunk-776W3UGV.mjs.map +1 -0
  9. package/dist/agent-verifier/chunk-C3VW3DTA.mjs +2909 -0
  10. package/dist/agent-verifier/chunk-C3VW3DTA.mjs.map +1 -0
  11. package/dist/agent-verifier/chunk-F2DIOJJZ.mjs +302 -0
  12. package/dist/agent-verifier/chunk-F2DIOJJZ.mjs.map +1 -0
  13. package/dist/agent-verifier/chunk-G42BGGG2.mjs +70 -0
  14. package/dist/agent-verifier/chunk-G42BGGG2.mjs.map +1 -0
  15. package/dist/agent-verifier/chunk-H6XDQJ3N.mjs +11 -0
  16. package/dist/agent-verifier/chunk-H76MT5UR.mjs +57 -0
  17. package/dist/agent-verifier/chunk-H76MT5UR.mjs.map +1 -0
  18. package/dist/agent-verifier/chunk-IAYRNVUC.mjs +49 -0
  19. package/dist/agent-verifier/chunk-IAYRNVUC.mjs.map +1 -0
  20. package/dist/agent-verifier/chunk-IDVQXGAO.mjs +222 -0
  21. package/dist/agent-verifier/chunk-IDVQXGAO.mjs.map +1 -0
  22. package/dist/agent-verifier/chunk-JO5AMVZU.mjs +1744 -0
  23. package/dist/agent-verifier/chunk-JO5AMVZU.mjs.map +1 -0
  24. package/dist/agent-verifier/chunk-JZTH3EMV.mjs +14523 -0
  25. package/dist/agent-verifier/chunk-JZTH3EMV.mjs.map +1 -0
  26. package/dist/agent-verifier/chunk-KDBSVLCF.mjs +624 -0
  27. package/dist/agent-verifier/chunk-KDBSVLCF.mjs.map +1 -0
  28. package/dist/agent-verifier/chunk-MW2QIWWA.mjs +729 -0
  29. package/dist/agent-verifier/chunk-MW2QIWWA.mjs.map +1 -0
  30. package/dist/agent-verifier/chunk-NAK77WXW.mjs +767 -0
  31. package/dist/agent-verifier/chunk-NAK77WXW.mjs.map +1 -0
  32. package/dist/agent-verifier/chunk-ON62IGWK.mjs +3137 -0
  33. package/dist/agent-verifier/chunk-ON62IGWK.mjs.map +1 -0
  34. package/dist/agent-verifier/chunk-QBAF7EYR.mjs +214 -0
  35. package/dist/agent-verifier/chunk-QBAF7EYR.mjs.map +1 -0
  36. package/dist/agent-verifier/chunk-QZH6IEZS.mjs +39 -0
  37. package/dist/agent-verifier/chunk-QZH6IEZS.mjs.map +1 -0
  38. package/dist/agent-verifier/chunk-TAEQKBJB.mjs +107 -0
  39. package/dist/agent-verifier/chunk-TAEQKBJB.mjs.map +1 -0
  40. package/dist/agent-verifier/chunk-UIOLGH4A.mjs +150 -0
  41. package/dist/agent-verifier/chunk-UIOLGH4A.mjs.map +1 -0
  42. package/dist/agent-verifier/chunk-XKCJBIRY.mjs +75 -0
  43. package/dist/agent-verifier/chunk-XKCJBIRY.mjs.map +1 -0
  44. package/dist/agent-verifier/chunk-XQXDOBYB.mjs +382 -0
  45. package/dist/agent-verifier/chunk-XQXDOBYB.mjs.map +1 -0
  46. package/dist/agent-verifier/chunk-YDIOW2BO.mjs +45 -0
  47. package/dist/agent-verifier/chunk-YDIOW2BO.mjs.map +1 -0
  48. package/dist/agent-verifier/chunk-YE7UAO3T.mjs +129 -0
  49. package/dist/agent-verifier/chunk-YE7UAO3T.mjs.map +1 -0
  50. package/dist/agent-verifier/chunk-Z6OZWUIZ.mjs +261 -0
  51. package/dist/agent-verifier/chunk-Z6OZWUIZ.mjs.map +1 -0
  52. package/dist/agent-verifier/chunk-ZEELHSY3.mjs +20 -0
  53. package/dist/agent-verifier/chunk-ZEELHSY3.mjs.map +1 -0
  54. package/dist/agent-verifier/compile-576O7TYP.mjs +312 -0
  55. package/dist/agent-verifier/compile-576O7TYP.mjs.map +1 -0
  56. package/dist/agent-verifier/global-config-NYCSCAUI.mjs +18 -0
  57. package/dist/agent-verifier/keychain-backend-A3MRWLPF.mjs +135 -0
  58. package/dist/agent-verifier/keychain-backend-A3MRWLPF.mjs.map +1 -0
  59. package/dist/agent-verifier/local-files-QVJ2H3MH.mjs +45 -0
  60. package/dist/agent-verifier/local-files-QVJ2H3MH.mjs.map +1 -0
  61. package/dist/agent-verifier/local-typecheck-2JWG5IGL.mjs +10 -0
  62. package/dist/agent-verifier/local-typecheck-2JWG5IGL.mjs.map +1 -0
  63. package/dist/agent-verifier/materialize-workspace-OZKOQCSQ.mjs +89 -0
  64. package/dist/agent-verifier/materialize-workspace-OZKOQCSQ.mjs.map +1 -0
  65. package/dist/agent-verifier/project-state-XKUSCFSV.mjs +33 -0
  66. package/dist/agent-verifier/project-state-XKUSCFSV.mjs.map +1 -0
  67. package/dist/agent-verifier/prompt-VKHMCQT6.mjs +756 -0
  68. package/dist/agent-verifier/prompt-VKHMCQT6.mjs.map +1 -0
  69. package/dist/agent-verifier/reducer-bundle-preflight-7NYZF5ZT.mjs +20 -0
  70. package/dist/agent-verifier/reducer-bundle-preflight-7NYZF5ZT.mjs.map +1 -0
  71. package/dist/agent-verifier/reducer-contract-preflight-COD2CO22.mjs +11 -0
  72. package/dist/agent-verifier/reducer-contract-preflight-COD2CO22.mjs.map +1 -0
  73. package/dist/agent-verifier/reducer-native-test-harness-QC7HZUK4.mjs +50 -0
  74. package/dist/agent-verifier/reducer-native-test-harness-QC7HZUK4.mjs.map +1 -0
  75. package/dist/agent-verifier/static-scaffold-JBUE3ROP.mjs +27 -0
  76. package/dist/agent-verifier/static-scaffold-JBUE3ROP.mjs.map +1 -0
  77. package/dist/agent-verifier/sync-C6S3OGCD.mjs +588 -0
  78. package/dist/agent-verifier/sync-C6S3OGCD.mjs.map +1 -0
  79. package/dist/agent-verifier/test-Y5UGQV7J.mjs +353 -0
  80. package/dist/agent-verifier/test-Y5UGQV7J.mjs.map +1 -0
  81. package/dist/agent-verifier/workspace-codegen-WPZHMATU.mjs +10 -0
  82. package/dist/agent-verifier/workspace-codegen-WPZHMATU.mjs.map +1 -0
  83. package/dist/agent-verifier/workspace-dependencies-B6A2ZX55.mjs +15 -0
  84. package/dist/agent-verifier/workspace-dependencies-B6A2ZX55.mjs.map +1 -0
  85. package/dist/chunk-2H7UOFLK.js +11 -0
  86. package/dist/chunk-2H7UOFLK.js.map +1 -0
  87. package/dist/{chunk-N7XPNNUI.js → chunk-3NRROR4P.js} +3 -3
  88. package/dist/{chunk-TAQKH67O.js → chunk-M4SCKH5M.js} +8 -2224
  89. package/dist/chunk-M4SCKH5M.js.map +1 -0
  90. package/dist/{global-config-S4ZIPECE.js → global-config-YBFEGJQG.js} +3 -3
  91. package/dist/global-config-YBFEGJQG.js.map +1 -0
  92. package/dist/index.js +4 -4
  93. package/dist/internal.js +3 -3
  94. package/dist/{keychain-backend-HDF4TZDL.js → keychain-backend-JHTXAKWC.js} +2 -2
  95. package/dist/{prompt-NDV3AE5L.js → prompt-GMZABCJC.js} +2 -2
  96. package/package.json +3 -2
  97. package/dist/chunk-SEGVTWSK.js +0 -44
  98. package/dist/chunk-TAQKH67O.js.map +0 -1
  99. /package/dist/{chunk-SEGVTWSK.js.map → agent-verifier/chunk-H6XDQJ3N.mjs.map} +0 -0
  100. /package/dist/{global-config-S4ZIPECE.js.map → agent-verifier/global-config-NYCSCAUI.mjs.map} +0 -0
  101. /package/dist/{chunk-N7XPNNUI.js.map → chunk-3NRROR4P.js.map} +0 -0
  102. /package/dist/{keychain-backend-HDF4TZDL.js.map → keychain-backend-JHTXAKWC.js.map} +0 -0
  103. /package/dist/{prompt-NDV3AE5L.js.map → prompt-GMZABCJC.js.map} +0 -0
@@ -0,0 +1,3137 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ bundleTypeScriptModuleText,
4
+ importTypeScriptModule
5
+ } from "./chunk-XKCJBIRY.mjs";
6
+ import {
7
+ STALE_CONTRACT_ARTIFACT_CODE,
8
+ isStaleContractArtifactError,
9
+ toDreamboardApiError
10
+ } from "./chunk-IDVQXGAO.mjs";
11
+ import {
12
+ loadManifest
13
+ } from "./chunk-27EEIZCI.mjs";
14
+ import {
15
+ REDUCER_TESTING_TYPES_WRAPPER_CONTENT,
16
+ buildReducerTestingContractContent
17
+ } from "./chunk-F2DIOJJZ.mjs";
18
+ import {
19
+ createGameplayCapability,
20
+ createProjectSession,
21
+ createProjectSessionFromReducerSnapshot,
22
+ getSessionSnapshot,
23
+ hashContent,
24
+ startGame
25
+ } from "./chunk-C3VW3DTA.mjs";
26
+ import {
27
+ external_exports
28
+ } from "./chunk-JZTH3EMV.mjs";
29
+ import {
30
+ ensureDir,
31
+ exists,
32
+ readTextFileIfExists,
33
+ writeJsonFile,
34
+ writeTextFile
35
+ } from "./chunk-IAYRNVUC.mjs";
36
+ import {
37
+ PROJECT_DIR_NAME
38
+ } from "./chunk-H76MT5UR.mjs";
39
+
40
+ // src/services/testing/reducer-native-test-harness.ts
41
+ import path2 from "path";
42
+ import { randomUUID as randomUUID2 } from "crypto";
43
+ import {
44
+ existsSync,
45
+ mkdirSync,
46
+ readFileSync,
47
+ rmSync,
48
+ writeFileSync
49
+ } from "fs";
50
+ import { readdir } from "fs/promises";
51
+ import { isDeepStrictEqual } from "util";
52
+ import {
53
+ contractFingerprint,
54
+ createReducerBundle
55
+ } from "@dreamboard-games/sdk/reducer";
56
+ import {
57
+ ReducerWireZod as ReducerContractZod
58
+ } from "@dreamboard-games/sdk/reducer-contract";
59
+ import { materializeManifestTable } from "@dreamboard-games/sdk/codegen";
60
+
61
+ // src/services/gameplay-authority-submit.ts
62
+ import { randomUUID } from "crypto";
63
+
64
+ // ../../node_modules/.pnpm/@dreamboard-games+gameplay-authority-protocol@0.1.0-alpha.0/node_modules/@dreamboard-games/gameplay-authority-protocol/dist/frames.js
65
+ var GAMEPLAY_AUTHORITY_AUDIENCE = "dreamboard-gameplay-authority";
66
+ var GAMEPLAY_TUNNEL_AUDIENCE = "dreamboard-gameplay-authority-tunnel";
67
+ var GameplayPermissionSchema = external_exports.enum([
68
+ "observe",
69
+ "submit",
70
+ "restore-history"
71
+ ]);
72
+ var GameplayConnectionContextSchema = external_exports.object({
73
+ sessionId: external_exports.string().uuid(),
74
+ playerId: external_exports.string().min(1),
75
+ permissions: external_exports.array(GameplayPermissionSchema)
76
+ });
77
+ var GameplayActorKindSchema = external_exports.enum(["user", "demo", "perf"]);
78
+ var JsonPatchOperationSchema = external_exports.object({
79
+ op: external_exports.string().min(1),
80
+ path: external_exports.string()
81
+ }).catchall(external_exports.unknown());
82
+ var ProjectedDeltaSchema = external_exports.object({
83
+ kind: external_exports.literal("delta"),
84
+ generation: external_exports.number().int().nonnegative(),
85
+ baseVersion: external_exports.number().int().nonnegative(),
86
+ version: external_exports.number().int().nonnegative(),
87
+ patch: external_exports.array(JsonPatchOperationSchema)
88
+ });
89
+ var ProjectedSnapshotSchema = external_exports.object({
90
+ kind: external_exports.literal("snapshot"),
91
+ generation: external_exports.number().int().nonnegative(),
92
+ version: external_exports.number().int().nonnegative(),
93
+ view: external_exports.unknown()
94
+ });
95
+ var BaseGameplayCapabilityClaimsSchema = external_exports.object({
96
+ typ: external_exports.literal("gameplay-capability"),
97
+ aud: external_exports.literal(GAMEPLAY_AUTHORITY_AUDIENCE),
98
+ iss: external_exports.string().min(1),
99
+ exp: external_exports.number().int().positive(),
100
+ iat: external_exports.number().int().positive().optional(),
101
+ jti: external_exports.string().min(1).optional(),
102
+ sessionId: external_exports.string().uuid(),
103
+ playerId: external_exports.string().min(1),
104
+ permissions: external_exports.array(GameplayPermissionSchema)
105
+ });
106
+ var UserGameplayCapabilityClaimsSchema = BaseGameplayCapabilityClaimsSchema.extend({
107
+ actorKind: external_exports.literal("user")
108
+ });
109
+ var DemoGameplayCapabilityClaimsSchema = BaseGameplayCapabilityClaimsSchema.extend({
110
+ actorKind: external_exports.literal("demo")
111
+ });
112
+ var PerfGameplayCapabilityClaimsSchema = BaseGameplayCapabilityClaimsSchema.extend({
113
+ actorKind: external_exports.literal("perf"),
114
+ perfRunId: external_exports.string().uuid(),
115
+ laneId: external_exports.string().min(1)
116
+ });
117
+ var GameplayCapabilityClaimsSchema = external_exports.discriminatedUnion("actorKind", [
118
+ UserGameplayCapabilityClaimsSchema,
119
+ DemoGameplayCapabilityClaimsSchema,
120
+ PerfGameplayCapabilityClaimsSchema
121
+ ]);
122
+ var GameplayTunnelClaimsSchema = external_exports.object({
123
+ aud: external_exports.literal(GAMEPLAY_TUNNEL_AUDIENCE),
124
+ iss: external_exports.string().min(1),
125
+ exp: external_exports.number().int().positive(),
126
+ iat: external_exports.number().int().positive().optional(),
127
+ jti: external_exports.string().min(1).optional(),
128
+ targetInstanceId: external_exports.string().min(1),
129
+ sessionId: external_exports.string().uuid(),
130
+ playerId: external_exports.string().min(1),
131
+ // Permissions are cryptographically bound into the tunnel token so the owner
132
+ // task trusts the signed claim set rather than the unauthenticated
133
+ // `tunnel.bind` frame. An ingress task cannot grant a player capabilities the
134
+ // issued token did not attest.
135
+ permissions: external_exports.array(GameplayPermissionSchema)
136
+ });
137
+ var AuthConnectFrameSchema = external_exports.object({
138
+ type: external_exports.literal("auth.connect"),
139
+ capabilityToken: external_exports.string().min(1)
140
+ });
141
+ var AuthRefreshFrameSchema = external_exports.object({
142
+ type: external_exports.literal("auth.refresh"),
143
+ capabilityToken: external_exports.string().min(1)
144
+ });
145
+ var SessionResumeFrameSchema = external_exports.object({
146
+ type: external_exports.literal("session.resume"),
147
+ lastSeenGeneration: external_exports.number().int().nonnegative().nullable(),
148
+ lastSeenVersion: external_exports.number().int().nonnegative().nullable(),
149
+ unacknowledgedClientActionIds: external_exports.array(external_exports.string().min(1)).max(32)
150
+ });
151
+ var CommandSubmitFrameSchema = external_exports.object({
152
+ type: external_exports.literal("command.submit"),
153
+ clientActionId: external_exports.string().min(1).max(128),
154
+ expectedVersion: external_exports.number().int().nonnegative(),
155
+ actionSetVersion: external_exports.string().min(1),
156
+ interactionId: external_exports.string().min(1),
157
+ inputs: external_exports.record(external_exports.string(), external_exports.unknown())
158
+ });
159
+ var HistoryRestoreFrameSchema = external_exports.object({
160
+ type: external_exports.literal("history.restore"),
161
+ restoreId: external_exports.string().min(1).max(128),
162
+ targetGeneration: external_exports.number().int().nonnegative(),
163
+ targetVersion: external_exports.number().int().nonnegative()
164
+ });
165
+ var ClientGameplayFrameSchema = external_exports.discriminatedUnion("type", [
166
+ AuthConnectFrameSchema,
167
+ AuthRefreshFrameSchema,
168
+ SessionResumeFrameSchema,
169
+ CommandSubmitFrameSchema,
170
+ HistoryRestoreFrameSchema
171
+ ]);
172
+ var TunnelBindFrameSchema = external_exports.object({
173
+ type: external_exports.literal("tunnel.bind"),
174
+ context: GameplayConnectionContextSchema
175
+ });
176
+ var TunnelClientFrameSchema = external_exports.discriminatedUnion("type", [
177
+ TunnelBindFrameSchema,
178
+ SessionResumeFrameSchema,
179
+ CommandSubmitFrameSchema,
180
+ HistoryRestoreFrameSchema
181
+ ]);
182
+ var CommandAcceptedFrameSchema = external_exports.object({
183
+ type: external_exports.literal("command.accepted"),
184
+ clientActionId: external_exports.string().min(1),
185
+ generation: external_exports.number().int().nonnegative(),
186
+ version: external_exports.number().int().nonnegative(),
187
+ stateHash: external_exports.string().min(1),
188
+ update: ProjectedSnapshotSchema
189
+ });
190
+ var CommandRejectedFrameSchema = external_exports.object({
191
+ type: external_exports.literal("command.rejected"),
192
+ clientActionId: external_exports.string().min(1).optional(),
193
+ errorCode: external_exports.string().min(1),
194
+ message: external_exports.string().min(1),
195
+ currentGeneration: external_exports.number().int().nonnegative().optional(),
196
+ currentVersion: external_exports.number().int().nonnegative().optional()
197
+ });
198
+ var HistoryRestoredFrameSchema = external_exports.object({
199
+ type: external_exports.literal("history.restored"),
200
+ restoreId: external_exports.string().min(1),
201
+ generation: external_exports.number().int().nonnegative(),
202
+ version: external_exports.number().int().nonnegative(),
203
+ stateHash: external_exports.string().min(1),
204
+ update: ProjectedSnapshotSchema
205
+ });
206
+ var HistoryRestoreRejectedFrameSchema = external_exports.object({
207
+ type: external_exports.literal("history.restoreRejected"),
208
+ restoreId: external_exports.string().min(1).optional(),
209
+ errorCode: external_exports.string().min(1),
210
+ message: external_exports.string().min(1),
211
+ currentGeneration: external_exports.number().int().nonnegative().optional(),
212
+ currentVersion: external_exports.number().int().nonnegative().optional()
213
+ });
214
+ var AuthAcceptedFrameSchema = external_exports.object({
215
+ type: external_exports.literal("auth.accepted"),
216
+ expiresAt: external_exports.string().datetime()
217
+ });
218
+ var AuthorityRecoveringFrameSchema = external_exports.object({
219
+ type: external_exports.literal("authority.recovering"),
220
+ retryAfterMs: external_exports.number().int().positive(),
221
+ message: external_exports.string().min(1)
222
+ });
223
+ var SessionSnapshotFrameSchema = external_exports.object({
224
+ type: external_exports.literal("session.snapshot"),
225
+ generation: external_exports.number().int().nonnegative(),
226
+ version: external_exports.number().int().nonnegative(),
227
+ stateHash: external_exports.string().min(1),
228
+ update: ProjectedSnapshotSchema
229
+ });
230
+ var SessionDeltaFrameSchema = external_exports.object({
231
+ type: external_exports.literal("session.delta"),
232
+ clientActionId: external_exports.string().min(1).optional(),
233
+ update: ProjectedDeltaSchema
234
+ });
235
+ var ServerGameplayFrameSchema = external_exports.discriminatedUnion("type", [
236
+ AuthAcceptedFrameSchema,
237
+ AuthorityRecoveringFrameSchema,
238
+ SessionSnapshotFrameSchema,
239
+ SessionDeltaFrameSchema,
240
+ CommandAcceptedFrameSchema,
241
+ CommandRejectedFrameSchema,
242
+ HistoryRestoredFrameSchema,
243
+ HistoryRestoreRejectedFrameSchema
244
+ ]);
245
+ var TunnelBoundFrameSchema = external_exports.object({
246
+ type: external_exports.literal("tunnel.bound"),
247
+ sessionId: external_exports.string().uuid(),
248
+ playerId: external_exports.string().min(1)
249
+ });
250
+ var TunnelServerFrameSchema = external_exports.discriminatedUnion("type", [
251
+ TunnelBoundFrameSchema,
252
+ AuthorityRecoveringFrameSchema,
253
+ SessionSnapshotFrameSchema,
254
+ SessionDeltaFrameSchema,
255
+ CommandAcceptedFrameSchema,
256
+ CommandRejectedFrameSchema,
257
+ HistoryRestoredFrameSchema,
258
+ HistoryRestoreRejectedFrameSchema
259
+ ]);
260
+
261
+ // ../../node_modules/.pnpm/@dreamboard-games+gameplay-authority-client@0.1.0-alpha.1/node_modules/@dreamboard-games/gameplay-authority-client/dist/index.js
262
+ var DEFAULT_OPEN_TIMEOUT_MS = 5e3;
263
+ var DEFAULT_REQUEST_TIMEOUT_MS = 1e4;
264
+ var GameplayAuthorityRecoveringError = class extends Error {
265
+ retryAfterMs;
266
+ constructor(message, retryAfterMs) {
267
+ super(message);
268
+ this.name = "GameplayAuthorityRecoveringError";
269
+ this.retryAfterMs = retryAfterMs;
270
+ }
271
+ };
272
+ async function connectGameplayAuthority(input) {
273
+ const webSocketFactory = input.webSocketFactory ?? browserWebSocketFactory();
274
+ const openTimeoutMs = input.openTimeoutMs ?? DEFAULT_OPEN_TIMEOUT_MS;
275
+ const requestTimeoutMs = input.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
276
+ const socket = new webSocketFactory(input.websocketUrl, input.webSocketInit);
277
+ try {
278
+ await waitUntilOpen(socket, openTimeoutMs);
279
+ socket.send(JSON.stringify({
280
+ type: "auth.connect",
281
+ capabilityToken: input.capabilityToken
282
+ }));
283
+ await waitForServerFrame(socket, (frame) => frame.type === "auth.accepted", requestTimeoutMs);
284
+ return new WebSocketGameplayAuthorityClient(socket, requestTimeoutMs);
285
+ } catch (error) {
286
+ socket.close();
287
+ throw error;
288
+ }
289
+ }
290
+ var WebSocketGameplayAuthorityClient = class {
291
+ socket;
292
+ requestTimeoutMs;
293
+ constructor(socket, requestTimeoutMs) {
294
+ this.socket = socket;
295
+ this.requestTimeoutMs = requestTimeoutMs;
296
+ }
297
+ resume(input = {}) {
298
+ this.socket.send(JSON.stringify({
299
+ type: "session.resume",
300
+ lastSeenGeneration: input.lastSeenGeneration ?? null,
301
+ lastSeenVersion: input.lastSeenVersion ?? null,
302
+ unacknowledgedClientActionIds: input.unacknowledgedClientActionIds ?? []
303
+ }));
304
+ }
305
+ async submitCommand(input) {
306
+ this.socket.send(JSON.stringify({
307
+ type: "command.submit",
308
+ clientActionId: input.clientActionId,
309
+ expectedVersion: input.expectedVersion,
310
+ actionSetVersion: input.actionSetVersion,
311
+ interactionId: input.interactionId,
312
+ inputs: input.inputs
313
+ }));
314
+ const frame = await waitForServerFrame(this.socket, (candidate) => candidate.type === "authority.recovering" || candidate.type === "command.accepted" && candidate.clientActionId === input.clientActionId || candidate.type === "command.rejected" && (candidate.clientActionId === void 0 || candidate.clientActionId === input.clientActionId), this.requestTimeoutMs);
315
+ throwIfRecovering(frame);
316
+ if (frame.type !== "command.accepted" && frame.type !== "command.rejected") {
317
+ throw new Error("Unexpected gameplay authority command frame.");
318
+ }
319
+ return frame;
320
+ }
321
+ async restoreHistory(input) {
322
+ this.socket.send(JSON.stringify({
323
+ type: "history.restore",
324
+ restoreId: input.restoreId,
325
+ targetGeneration: input.targetGeneration,
326
+ targetVersion: input.targetVersion
327
+ }));
328
+ const frame = await waitForServerFrame(this.socket, (candidate) => candidate.type === "authority.recovering" || candidate.type === "history.restored" && candidate.restoreId === input.restoreId || candidate.type === "history.restoreRejected" && (candidate.restoreId === void 0 || candidate.restoreId === input.restoreId), this.requestTimeoutMs);
329
+ throwIfRecovering(frame);
330
+ if (frame.type !== "history.restored" && frame.type !== "history.restoreRejected") {
331
+ throw new Error("Unexpected gameplay authority history frame.");
332
+ }
333
+ return frame;
334
+ }
335
+ frames(signal) {
336
+ return readServerFrames(this.socket, signal);
337
+ }
338
+ close() {
339
+ this.socket.close();
340
+ }
341
+ };
342
+ function throwIfRecovering(frame) {
343
+ if (frame.type === "authority.recovering") {
344
+ throw new GameplayAuthorityRecoveringError(frame.message, frame.retryAfterMs);
345
+ }
346
+ }
347
+ function browserWebSocketFactory() {
348
+ return BrowserGameplayAuthorityWebSocket;
349
+ }
350
+ var BrowserGameplayAuthorityWebSocket = class {
351
+ socket;
352
+ listeners = /* @__PURE__ */ new Map();
353
+ constructor(url, init) {
354
+ if (init?.headers && Object.keys(init.headers).length > 0) {
355
+ throw new Error("Browser WebSocket does not support custom headers; provide a webSocketFactory for header-aware runtimes.");
356
+ }
357
+ this.socket = new WebSocket(url);
358
+ }
359
+ send(data) {
360
+ this.socket.send(data);
361
+ }
362
+ close() {
363
+ this.socket.close();
364
+ }
365
+ on(event, listener) {
366
+ const wrapped = (observed) => {
367
+ if (event === "message" && "data" in observed) {
368
+ listener(observed.data);
369
+ return;
370
+ }
371
+ listener(observed);
372
+ };
373
+ const listeners = this.listeners.get(event) ?? /* @__PURE__ */ new Map();
374
+ listeners.set(listener, wrapped);
375
+ this.listeners.set(event, listeners);
376
+ this.socket.addEventListener(event, wrapped);
377
+ }
378
+ off(event, listener) {
379
+ const listeners = this.listeners.get(event);
380
+ const wrapped = listeners?.get(listener);
381
+ if (!listeners || !wrapped) {
382
+ return;
383
+ }
384
+ this.socket.removeEventListener(event, wrapped);
385
+ listeners.delete(listener);
386
+ }
387
+ };
388
+ async function waitUntilOpen(socket, timeoutMs) {
389
+ await waitForEvent(socket, "open", timeoutMs);
390
+ }
391
+ async function waitForEvent(socket, event, timeoutMs) {
392
+ return new Promise((resolve, reject) => {
393
+ const cleanup = installSocketListeners(socket, {
394
+ timeoutMs,
395
+ onEvent(observed) {
396
+ if (observed === event) {
397
+ cleanup();
398
+ resolve();
399
+ }
400
+ },
401
+ onError(error) {
402
+ cleanup();
403
+ reject(error instanceof Error ? error : new Error("gameplay socket error"));
404
+ },
405
+ onClose() {
406
+ cleanup();
407
+ reject(new Error("gameplay socket closed"));
408
+ },
409
+ onTimeout() {
410
+ cleanup();
411
+ reject(new Error("gameplay socket timed out"));
412
+ }
413
+ });
414
+ });
415
+ }
416
+ async function waitForServerFrame(socket, predicate, timeoutMs) {
417
+ return new Promise((resolve, reject) => {
418
+ const cleanup = installSocketListeners(socket, {
419
+ timeoutMs,
420
+ onMessage(data) {
421
+ let frame;
422
+ try {
423
+ frame = ServerGameplayFrameSchema.parse(JSON.parse(messageToString(data)));
424
+ } catch (error) {
425
+ cleanup();
426
+ reject(error instanceof Error ? error : new Error("invalid gameplay authority frame"));
427
+ return;
428
+ }
429
+ if (predicate(frame)) {
430
+ cleanup();
431
+ resolve(frame);
432
+ }
433
+ },
434
+ onError(error) {
435
+ cleanup();
436
+ reject(error instanceof Error ? error : new Error("gameplay socket error"));
437
+ },
438
+ onClose() {
439
+ cleanup();
440
+ reject(new Error("gameplay socket closed"));
441
+ },
442
+ onTimeout() {
443
+ cleanup();
444
+ reject(new Error("gameplay socket timed out"));
445
+ }
446
+ });
447
+ });
448
+ }
449
+ function installSocketListeners(socket, handlers) {
450
+ const timer = setTimeout(handlers.onTimeout, handlers.timeoutMs);
451
+ const onOpen = () => handlers.onEvent?.("open");
452
+ const onMessage = (data) => handlers.onMessage?.(data);
453
+ const onError = (error) => handlers.onError(error);
454
+ const onClose = () => handlers.onClose();
455
+ socket.on("open", onOpen);
456
+ socket.on("message", onMessage);
457
+ socket.on("error", onError);
458
+ socket.on("close", onClose);
459
+ return () => {
460
+ clearTimeout(timer);
461
+ socket.off?.("open", onOpen);
462
+ socket.off?.("message", onMessage);
463
+ socket.off?.("error", onError);
464
+ socket.off?.("close", onClose);
465
+ };
466
+ }
467
+ function readServerFrames(socket, signal) {
468
+ const queue = [];
469
+ let wake = null;
470
+ let closed = false;
471
+ let error = null;
472
+ const wakeReader = () => {
473
+ wake?.();
474
+ wake = null;
475
+ };
476
+ const onMessage = (data) => {
477
+ try {
478
+ queue.push(ServerGameplayFrameSchema.parse(JSON.parse(messageToString(data))));
479
+ } catch (candidate) {
480
+ error = candidate instanceof Error ? candidate : new Error("invalid gameplay authority frame");
481
+ }
482
+ wakeReader();
483
+ };
484
+ const onError = (candidate) => {
485
+ error = candidate instanceof Error ? candidate : new Error("gameplay socket error");
486
+ wakeReader();
487
+ };
488
+ const onClose = () => {
489
+ closed = true;
490
+ wakeReader();
491
+ };
492
+ const onAbort = () => {
493
+ closed = true;
494
+ wakeReader();
495
+ };
496
+ socket.on("message", onMessage);
497
+ socket.on("error", onError);
498
+ socket.on("close", onClose);
499
+ signal?.addEventListener("abort", onAbort, { once: true });
500
+ const cleanup = () => {
501
+ socket.off?.("message", onMessage);
502
+ socket.off?.("error", onError);
503
+ socket.off?.("close", onClose);
504
+ signal?.removeEventListener("abort", onAbort);
505
+ };
506
+ const iterator = {
507
+ async next() {
508
+ while (!closed && !signal?.aborted) {
509
+ const next = queue.shift();
510
+ if (next) {
511
+ return { done: false, value: next };
512
+ }
513
+ if (error) {
514
+ cleanup();
515
+ throw error;
516
+ }
517
+ await new Promise((resolve) => {
518
+ wake = resolve;
519
+ });
520
+ }
521
+ cleanup();
522
+ if (error) {
523
+ throw error;
524
+ }
525
+ return { done: true, value: void 0 };
526
+ },
527
+ async return(value) {
528
+ closed = true;
529
+ wakeReader();
530
+ cleanup();
531
+ return { done: true, value };
532
+ },
533
+ async throw(candidate) {
534
+ closed = true;
535
+ wakeReader();
536
+ cleanup();
537
+ throw candidate;
538
+ },
539
+ [Symbol.asyncIterator]() {
540
+ return this;
541
+ },
542
+ async [Symbol.asyncDispose]() {
543
+ await this.return(void 0);
544
+ }
545
+ };
546
+ return iterator;
547
+ }
548
+ function messageToString(data) {
549
+ if (typeof data === "string") {
550
+ return data;
551
+ }
552
+ if (data instanceof ArrayBuffer) {
553
+ return new TextDecoder().decode(data);
554
+ }
555
+ if (ArrayBuffer.isView(data)) {
556
+ return new TextDecoder().decode(data);
557
+ }
558
+ return String(data);
559
+ }
560
+
561
+ // src/services/gameplay-authority-submit.ts
562
+ var CLIENT_ACTION_ID_HEADER = "X-Dreamboard-Client-Action-Id";
563
+ var DEFAULT_TIMEOUT_MS = 1e4;
564
+ async function submitGameplayAuthorityAction(options) {
565
+ try {
566
+ const requestCapability = options.capabilityRequester ?? createGameplayCapability;
567
+ const connectAuthority = options.connectAuthority ?? connectGameplayAuthority;
568
+ const capability = await requestCapability({
569
+ path: {
570
+ sessionId: options.path.sessionId,
571
+ playerId: options.path.playerId
572
+ }
573
+ });
574
+ if (capability.error || !capability.data) {
575
+ return { error: capability.error ?? new Error("Missing capability.") };
576
+ }
577
+ const clientActionId = options.headers?.[CLIENT_ACTION_ID_HEADER] ?? options.clientActionIdFactory?.() ?? randomUUID();
578
+ const client = await connectAuthority({
579
+ websocketUrl: capability.data.websocketUrl,
580
+ capabilityToken: capability.data.token,
581
+ openTimeoutMs: DEFAULT_TIMEOUT_MS,
582
+ requestTimeoutMs: DEFAULT_TIMEOUT_MS
583
+ });
584
+ try {
585
+ const frame = await client.submitCommand({
586
+ clientActionId,
587
+ expectedVersion: options.body.expectedVersion,
588
+ actionSetVersion: options.body.actionSetVersion,
589
+ interactionId: options.path.interactionId,
590
+ inputs: options.body.inputs ?? {}
591
+ });
592
+ if (frame.type === "command.rejected") {
593
+ return {
594
+ data: {
595
+ success: false,
596
+ accepted: false,
597
+ version: typeof frame.currentVersion === "number" ? frame.currentVersion : options.body.expectedVersion,
598
+ actionSetVersion: options.body.actionSetVersion,
599
+ errorCode: typeof frame.errorCode === "string" ? frame.errorCode : void 0,
600
+ message: typeof frame.message === "string" ? frame.message : void 0,
601
+ clientActionId
602
+ }
603
+ };
604
+ }
605
+ const acceptedVersion = typeof frame.version === "number" ? frame.version : void 0;
606
+ if (acceptedVersion === void 0) {
607
+ return {
608
+ error: new Error("Accepted frame did not include a numeric version.")
609
+ };
610
+ }
611
+ const update = frame.update && typeof frame.update === "object" ? frame.update : {};
612
+ const view = update.view && typeof update.view === "object" ? update.view : {};
613
+ const projectedActionSetVersion = typeof view.actionSetVersion === "string" ? view.actionSetVersion : void 0;
614
+ return {
615
+ data: {
616
+ success: true,
617
+ accepted: true,
618
+ durabilityStatus: "COMMITTED",
619
+ version: acceptedVersion,
620
+ actionSetVersion: projectedActionSetVersion ?? options.body.actionSetVersion,
621
+ clientActionId
622
+ }
623
+ };
624
+ } catch (error) {
625
+ if (error instanceof GameplayAuthorityRecoveringError) {
626
+ return {
627
+ error: new Error(error.message || "Session authority is recovering.")
628
+ };
629
+ }
630
+ throw error;
631
+ } finally {
632
+ client.close();
633
+ }
634
+ } catch (error) {
635
+ return { error };
636
+ }
637
+ }
638
+
639
+ // src/ui/playwright-runner.ts
640
+ import os from "os";
641
+ import path from "path";
642
+ function configurePlaywrightBrowsersPath() {
643
+ if (process.env.PLAYWRIGHT_BROWSERS_PATH) {
644
+ return;
645
+ }
646
+ const runtimeHome = process.env.HOME;
647
+ let realHome;
648
+ try {
649
+ realHome = os.userInfo().homedir;
650
+ } catch {
651
+ return;
652
+ }
653
+ if (!runtimeHome || path.resolve(runtimeHome) === path.resolve(realHome)) {
654
+ return;
655
+ }
656
+ const browserCachePath = process.platform === "darwin" ? path.join(realHome, "Library", "Caches", "ms-playwright") : process.platform === "win32" ? path.join(realHome, "AppData", "Local", "ms-playwright") : path.join(realHome, ".cache", "ms-playwright");
657
+ process.env.PLAYWRIGHT_BROWSERS_PATH = browserCachePath;
658
+ }
659
+ async function buildBrowserAuthInitScript(config) {
660
+ if (!config.authToken) return null;
661
+ return `(function(){localStorage.setItem('dreamboard_auth_token',${JSON.stringify(config.authToken)});})();`;
662
+ }
663
+ async function waitForGameReady(page, timeoutMs = 6e4) {
664
+ await page.waitForSelector('iframe[title="Game UI Plugin"]', {
665
+ timeout: timeoutMs
666
+ });
667
+ }
668
+
669
+ // src/services/workflows/resolve-setup-profile.ts
670
+ function resolveSetupProfileSelection(options) {
671
+ const setupProfiles = options.manifest.setupProfiles ?? [];
672
+ const requestedSetupProfileId = options.requestedSetupProfileId?.trim() || void 0;
673
+ if (requestedSetupProfileId) {
674
+ const requestedProfile = setupProfiles.find(
675
+ (profile) => profile.id === requestedSetupProfileId
676
+ );
677
+ if (!requestedProfile) {
678
+ const knownProfiles = setupProfiles.map((profile) => profile.id).join(", ");
679
+ throw new Error(
680
+ setupProfiles.length === 0 ? `Unknown setup profile '${requestedSetupProfileId}'. The manifest defines no setup profiles.` : `Unknown setup profile '${requestedSetupProfileId}'. Expected one of: ${knownProfiles}.`
681
+ );
682
+ }
683
+ return {
684
+ id: requestedProfile.id,
685
+ name: requestedProfile.name,
686
+ source: "explicit"
687
+ };
688
+ }
689
+ if (setupProfiles.length === 0) {
690
+ return {
691
+ id: null,
692
+ name: null,
693
+ source: "none"
694
+ };
695
+ }
696
+ if (setupProfiles.length === 1) {
697
+ return {
698
+ id: setupProfiles[0]?.id ?? null,
699
+ name: setupProfiles[0]?.name ?? null,
700
+ source: "implicit-single"
701
+ };
702
+ }
703
+ return {
704
+ id: setupProfiles[0]?.id ?? null,
705
+ name: setupProfiles[0]?.name ?? null,
706
+ source: "implicit-first"
707
+ };
708
+ }
709
+
710
+ // src/utils/session-game-source.ts
711
+ function projectIdFromSessionGameSource(source) {
712
+ if (source.kind === "USER_COMPILED") {
713
+ return source.projectId;
714
+ }
715
+ return `demo:${source.slug}:${source.revisionId}`;
716
+ }
717
+
718
+ // src/services/testing/reducer-native-test-harness.ts
719
+ globalThis.__DREAMBOARD_AUTHORING_WARNINGS__ = true;
720
+ var GENERATED_TESTING_TYPES_PREFIX = "// Generated by dreamboard";
721
+ var TESTING_TYPES_STUB = "export function defineScenario(scenario) { return scenario; }\n";
722
+ var DEFAULT_TIMEOUT_MS2 = 1e4;
723
+ var BASE_SUFFIX = ".base.ts";
724
+ var SCENARIO_SUFFIX = ".scenario.ts";
725
+ var SDK_UI_RUNTIME_EXTERNALS = [
726
+ "@radix-ui/react-accordion",
727
+ "@radix-ui/react-dialog",
728
+ "@radix-ui/react-label",
729
+ "@radix-ui/react-select",
730
+ "@radix-ui/react-slot",
731
+ "@radix-ui/react-tooltip",
732
+ "@use-gesture/react",
733
+ "clsx",
734
+ "framer-motion",
735
+ "lucide-react",
736
+ "react",
737
+ "react-dom",
738
+ "vaul"
739
+ ];
740
+ function formatScenarioErrorForDisplay(options) {
741
+ const message = options.error.message || "Scenario failed.";
742
+ const scenarioFrame = findScenarioStackFrame({
743
+ stack: options.error.stack,
744
+ projectRoot: options.projectRoot,
745
+ scenarioFilePath: options.scenarioFilePath
746
+ });
747
+ return scenarioFrame ? `${message}
748
+ ${scenarioFrame}` : message;
749
+ }
750
+ function findScenarioStackFrame(options) {
751
+ if (!options.stack) {
752
+ return null;
753
+ }
754
+ const absolutePath = path2.resolve(options.scenarioFilePath);
755
+ const relativePath = path2.relative(options.projectRoot, absolutePath);
756
+ const normalizedRelativePath = relativePath.split(path2.sep).join("/");
757
+ const escapedAbsolutePath = escapeRegExp(absolutePath);
758
+ const escapedRelativePath = escapeRegExp(normalizedRelativePath);
759
+ const absoluteFrame = new RegExp(`${escapedAbsolutePath}:(\\d+):(\\d+)`);
760
+ const relativeFrame = new RegExp(`${escapedRelativePath}:(\\d+):(\\d+)`);
761
+ for (const line of options.stack.split("\n")) {
762
+ const normalizedLine = line.split(path2.sep).join("/");
763
+ const match = normalizedLine.match(absoluteFrame) ?? normalizedLine.match(relativeFrame);
764
+ if (match?.[1] && match?.[2]) {
765
+ return `at ${normalizedRelativePath}:${match[1]}:${match[2]}`;
766
+ }
767
+ }
768
+ return null;
769
+ }
770
+ function escapeRegExp(value) {
771
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
772
+ }
773
+ var testingExpectApiFactoryPromise;
774
+ async function loadTestingExpectApiFactory() {
775
+ testingExpectApiFactoryPromise ??= import("@dreamboard-games/sdk/testing").then(
776
+ (module) => module.createExpectApi
777
+ );
778
+ return testingExpectApiFactoryPromise;
779
+ }
780
+ function createSubmissionError(errorCode, message, fallbackMessage) {
781
+ const error = new Error(message ?? fallbackMessage);
782
+ error.name = "SubmissionError";
783
+ error.errorCode = errorCode;
784
+ return error;
785
+ }
786
+ function parseJsonValue(value) {
787
+ if (typeof value !== "string") {
788
+ return value;
789
+ }
790
+ try {
791
+ return JSON.parse(value);
792
+ } catch {
793
+ return value;
794
+ }
795
+ }
796
+ function deepEqual(left, right) {
797
+ return isDeepStrictEqual(left, right);
798
+ }
799
+ function normalizeScenarioRunners(runners) {
800
+ return runners && runners.length > 0 ? runners : ["reducer"];
801
+ }
802
+ function shouldRefreshReducerTestingTypes(existingContent) {
803
+ if (existingContent === null || existingContent.trim().length === 0 || existingContent === TESTING_TYPES_STUB || existingContent.startsWith(GENERATED_TESTING_TYPES_PREFIX)) {
804
+ return true;
805
+ }
806
+ return false;
807
+ }
808
+ async function discoverFiles(root, suffix) {
809
+ if (!await exists(root)) {
810
+ return [];
811
+ }
812
+ const entries = await readdir(root, { withFileTypes: true });
813
+ const files = [];
814
+ for (const entry of entries) {
815
+ const entryPath = path2.join(root, entry.name);
816
+ if (entry.isDirectory()) {
817
+ files.push(...await discoverFiles(entryPath, suffix));
818
+ continue;
819
+ }
820
+ if (entry.isFile() && entry.name.endsWith(suffix)) {
821
+ files.push(entryPath);
822
+ }
823
+ }
824
+ files.sort();
825
+ return files;
826
+ }
827
+ function isFunction(value) {
828
+ return typeof value === "function";
829
+ }
830
+ function parseTypedBaseDefinition(value) {
831
+ if (typeof value !== "object" || value === null || !("id" in value) || typeof value.id !== "string" || !("setup" in value) || !isFunction(value.setup)) {
832
+ throw new Error("Invalid reducer-native base definition.");
833
+ }
834
+ const parentBaseId = "extends" in value && typeof value.extends === "string" ? value.extends : void 0;
835
+ const seed = "seed" in value && typeof value.seed === "number" ? value.seed : void 0;
836
+ const players = "players" in value && typeof value.players === "number" ? value.players : void 0;
837
+ if ((seed === void 0 || players === void 0) && !parentBaseId) {
838
+ throw new Error(
839
+ "Invalid reducer-native base definition. Base definitions without --extends must declare numeric seed and players."
840
+ );
841
+ }
842
+ const setupProfileId = "setupProfileId" in value && typeof value.setupProfileId === "string" ? value.setupProfileId : void 0;
843
+ return {
844
+ ...value,
845
+ seed,
846
+ players,
847
+ setupProfileId,
848
+ extends: parentBaseId
849
+ };
850
+ }
851
+ function parseTypedScenarioDefinition(value) {
852
+ if (typeof value !== "object" || value === null || !("id" in value) || typeof value.id !== "string" || !("from" in value) || typeof value.from !== "string" || !("when" in value) || !isFunction(value.when) || !("then" in value) || !isFunction(value.then)) {
853
+ throw new Error("Invalid reducer-native scenario definition.");
854
+ }
855
+ const phase = "phase" in value && typeof value.phase === "string" ? value.phase : void 0;
856
+ const stage = "stage" in value && typeof value.stage === "string" ? value.stage : void 0;
857
+ return {
858
+ ...value,
859
+ phase,
860
+ stage
861
+ };
862
+ }
863
+ async function isReducerNativeTestingWorkspace(projectRoot) {
864
+ return await exists(path2.join(projectRoot, "app", "game.ts")) && await exists(
865
+ path2.join(projectRoot, "shared", "generated", "ui-contract.ts")
866
+ );
867
+ }
868
+ async function ensureReducerNativeTestingFiles(projectRoot) {
869
+ const testingTypesPath = path2.join(projectRoot, "test", "testing-types.ts");
870
+ const testingContractPath = path2.join(
871
+ projectRoot,
872
+ "test",
873
+ "generated",
874
+ "testing-contract.ts"
875
+ );
876
+ const baseStatesPath = path2.join(
877
+ projectRoot,
878
+ "test",
879
+ "generated",
880
+ "base-states.generated.ts"
881
+ );
882
+ const baseStatesDtsPath = path2.join(
883
+ projectRoot,
884
+ "test",
885
+ "generated",
886
+ "base-states.generated.d.ts"
887
+ );
888
+ const scenarioManifestPath = path2.join(
889
+ projectRoot,
890
+ "test",
891
+ "generated",
892
+ "scenario-manifest.generated.ts"
893
+ );
894
+ await ensureDir(path2.dirname(testingTypesPath));
895
+ await ensureDir(path2.dirname(testingContractPath));
896
+ const existingTestingTypes = await readTextFileIfExists(testingTypesPath);
897
+ if (shouldRefreshReducerTestingTypes(existingTestingTypes)) {
898
+ await writeTextFile(
899
+ testingTypesPath,
900
+ REDUCER_TESTING_TYPES_WRAPPER_CONTENT
901
+ );
902
+ }
903
+ const rejectionCodes = await collectKnownRejectionCodes(projectRoot);
904
+ await writeTextFile(
905
+ testingContractPath,
906
+ buildReducerTestingContractContent({ rejectionCodes })
907
+ );
908
+ const header = "// Generated by dreamboard test generate. Do not edit by hand.\n";
909
+ if (!await exists(baseStatesPath)) {
910
+ await writeTextFile(
911
+ baseStatesPath,
912
+ `${header}export const BASE_STATES = {} as const;
913
+ export const BASE_STATES_CONTRACT_FINGERPRINT = undefined;
914
+ `
915
+ );
916
+ } else {
917
+ const existingBaseStates = await readTextFileIfExists(baseStatesPath);
918
+ if (existingBaseStates && !existingBaseStates.includes("BASE_STATES_CONTRACT_FINGERPRINT")) {
919
+ await writeTextFile(
920
+ baseStatesPath,
921
+ `${existingBaseStates.trimEnd()}
922
+ export const BASE_STATES_CONTRACT_FINGERPRINT = undefined;
923
+ `
924
+ );
925
+ }
926
+ }
927
+ if (!await exists(baseStatesDtsPath)) {
928
+ await writeTextFile(
929
+ baseStatesDtsPath,
930
+ `${header}export declare const BASE_STATES: Record<string, unknown>;
931
+ export declare const BASE_STATES_CONTRACT_FINGERPRINT: string | undefined;
932
+ `
933
+ );
934
+ } else {
935
+ const existingBaseStatesDts = await readTextFileIfExists(baseStatesDtsPath);
936
+ if (existingBaseStatesDts && !existingBaseStatesDts.includes("BASE_STATES_CONTRACT_FINGERPRINT")) {
937
+ await writeTextFile(
938
+ baseStatesDtsPath,
939
+ `${existingBaseStatesDts.trimEnd()}
940
+ export declare const BASE_STATES_CONTRACT_FINGERPRINT: string | undefined;
941
+ `
942
+ );
943
+ }
944
+ }
945
+ if (!await exists(scenarioManifestPath)) {
946
+ await writeTextFile(
947
+ scenarioManifestPath,
948
+ `${header}export const SCENARIO_MANIFEST = [] as const;
949
+ `
950
+ );
951
+ }
952
+ }
953
+ var DEFAULT_REJECTION_CODES = [
954
+ "NOT_YOUR_TURN",
955
+ "action-unavailable",
956
+ "invalid-action-params",
957
+ "prompt-not-owned"
958
+ ];
959
+ async function collectKnownRejectionCodes(projectRoot) {
960
+ const knownCodes = new Set(DEFAULT_REJECTION_CODES);
961
+ const gamePath = path2.join(projectRoot, "app", "game.ts");
962
+ if (!await exists(gamePath)) {
963
+ return Array.from(knownCodes).sort(
964
+ (left, right) => left.localeCompare(right)
965
+ );
966
+ }
967
+ try {
968
+ const module = await importTypeScriptModule(gamePath);
969
+ const phases = module.default?.phases ?? {};
970
+ for (const phase of Object.values(phases)) {
971
+ const interactions = phase.interactions ?? {};
972
+ for (const interaction of Object.values(interactions)) {
973
+ for (const errorCode of interaction.errorCodes ?? []) {
974
+ if (typeof errorCode === "string" && errorCode.length > 0) {
975
+ knownCodes.add(errorCode);
976
+ }
977
+ }
978
+ }
979
+ }
980
+ } catch {
981
+ }
982
+ return Array.from(knownCodes).sort(
983
+ (left, right) => left.localeCompare(right)
984
+ );
985
+ }
986
+ async function loadTypedBases(projectRoot) {
987
+ const baseFiles = await discoverFiles(
988
+ path2.join(projectRoot, "test", "bases"),
989
+ BASE_SUFFIX
990
+ );
991
+ const loaded = [];
992
+ for (const filePath of baseFiles) {
993
+ const externals = reducerNativeTestHelperExternals(projectRoot, filePath);
994
+ const [module, bundledText] = await Promise.all([
995
+ importTypeScriptModule(filePath),
996
+ bundleTypeScriptModuleText(filePath, { external: externals })
997
+ ]);
998
+ loaded.push({
999
+ filePath,
1000
+ definition: parseTypedBaseDefinition(module.default),
1001
+ bundleHash: hashContent(bundledText)
1002
+ });
1003
+ }
1004
+ return loaded;
1005
+ }
1006
+ async function loadTypedScenarios(projectRoot, options) {
1007
+ const scenarioFiles = options.scenarioPath ? [path2.resolve(projectRoot, options.scenarioPath)] : await discoverFiles(
1008
+ path2.join(projectRoot, "test", "scenarios"),
1009
+ SCENARIO_SUFFIX
1010
+ );
1011
+ const loaded = [];
1012
+ for (const filePath of scenarioFiles) {
1013
+ const externals = reducerNativeTestHelperExternals(projectRoot, filePath);
1014
+ const [module, bundledText] = await Promise.all([
1015
+ importTypeScriptModule(filePath),
1016
+ bundleTypeScriptModuleText(filePath, { external: externals })
1017
+ ]);
1018
+ loaded.push({
1019
+ filePath,
1020
+ definition: parseTypedScenarioDefinition(module.default),
1021
+ bundleHash: hashContent(bundledText)
1022
+ });
1023
+ }
1024
+ return loaded;
1025
+ }
1026
+ function reducerNativeTestHelperExternals(projectRoot, filePath) {
1027
+ const testingTypesPath = path2.join(projectRoot, "test", "testing-types");
1028
+ const relative = path2.relative(path2.dirname(filePath), testingTypesPath).replaceAll("\\", "/");
1029
+ const specifier = relative.startsWith(".") ? relative : `./${relative}`;
1030
+ return [specifier, `${specifier}.ts`, ...SDK_UI_RUNTIME_EXTERNALS];
1031
+ }
1032
+ var JS_MAX_SAFE_INTEGER = 9007199254740991n;
1033
+ var JS_MIN_SAFE_INTEGER = -JS_MAX_SAFE_INTEGER;
1034
+ var JS_MAX_SAFE_INTEGER_EXCLUSIVE = JS_MAX_SAFE_INTEGER + 1n;
1035
+ var KotlinSeededRandom = class {
1036
+ x;
1037
+ y;
1038
+ z;
1039
+ w;
1040
+ v;
1041
+ addend;
1042
+ constructor(seed) {
1043
+ const seedBig = BigInt.asIntN(64, BigInt(Math.trunc(seed)));
1044
+ const seed1 = Number(BigInt.asIntN(32, seedBig));
1045
+ const seed2 = Number(BigInt.asIntN(32, seedBig >> 32n));
1046
+ this.x = seed1 | 0;
1047
+ this.y = seed2 | 0;
1048
+ this.z = 0;
1049
+ this.w = 0;
1050
+ this.v = ~seed1;
1051
+ this.addend = seed1 << 10 ^ seed2 >>> 4 | 0;
1052
+ if ((this.x | this.y | this.z | this.w | this.v) === 0) {
1053
+ throw new Error(
1054
+ "Kotlin random seed must initialize at least one state word."
1055
+ );
1056
+ }
1057
+ for (let index = 0; index < 64; index += 1) {
1058
+ this.nextInt();
1059
+ }
1060
+ }
1061
+ nextInt() {
1062
+ let next = this.x | 0;
1063
+ next ^= next >>> 2;
1064
+ this.x = this.y;
1065
+ this.y = this.z;
1066
+ this.z = this.w;
1067
+ const currentV = this.v | 0;
1068
+ this.w = currentV;
1069
+ next = next ^ next << 1 ^ currentV ^ currentV << 4 | 0;
1070
+ this.v = next;
1071
+ this.addend = this.addend + 362437 | 0;
1072
+ return next + this.addend | 0;
1073
+ }
1074
+ nextBits(bitCount) {
1075
+ if (bitCount <= 0) {
1076
+ return 0;
1077
+ }
1078
+ return this.nextInt() >>> 32 - bitCount;
1079
+ }
1080
+ nextIntBound(bound) {
1081
+ if (bound <= 0) {
1082
+ throw new Error("bound must be positive");
1083
+ }
1084
+ if ((bound & -bound) === bound) {
1085
+ const bitCount = 31 - Math.clz32(bound);
1086
+ return this.nextBits(bitCount);
1087
+ }
1088
+ let value;
1089
+ while (true) {
1090
+ const bits = this.nextInt() >>> 1;
1091
+ value = bits % bound;
1092
+ if (bits - value + (bound - 1) >= 0) {
1093
+ return value;
1094
+ }
1095
+ }
1096
+ }
1097
+ nextLong() {
1098
+ return Number(this.nextLongBigInt());
1099
+ }
1100
+ nextLongBigInt() {
1101
+ const high = BigInt(this.nextInt());
1102
+ const low = BigInt(this.nextInt());
1103
+ return BigInt.asIntN(64, (high << 32n) + low);
1104
+ }
1105
+ nextReducerRuntimeSeed() {
1106
+ return Number(
1107
+ this.nextLongRange(JS_MIN_SAFE_INTEGER, JS_MAX_SAFE_INTEGER_EXCLUSIVE)
1108
+ );
1109
+ }
1110
+ nextLongRange(from, until) {
1111
+ if (until <= from) {
1112
+ throw new Error("until must be greater than from");
1113
+ }
1114
+ const bound = until - from;
1115
+ const mask = bound - 1n;
1116
+ if ((bound & mask) === 0n) {
1117
+ return from + (BigInt.asUintN(64, this.nextLongBigInt()) & mask);
1118
+ }
1119
+ while (true) {
1120
+ const bits = BigInt.asUintN(64, this.nextLongBigInt()) >> 1n;
1121
+ const value = bits % bound;
1122
+ if (BigInt.asIntN(64, bits + mask - value) >= 0n) {
1123
+ return from + value;
1124
+ }
1125
+ }
1126
+ }
1127
+ };
1128
+ function shuffleWithKotlinRandom(values, random) {
1129
+ const next = [...values];
1130
+ for (let index = next.length - 1; index > 0; index -= 1) {
1131
+ const swapIndex = random.nextIntBound(index + 1);
1132
+ const current = next[index];
1133
+ next[index] = next[swapIndex];
1134
+ next[swapIndex] = current;
1135
+ }
1136
+ return next;
1137
+ }
1138
+ function resolveEffectiveBaseSetup(options) {
1139
+ const inheritedSetupProfileId = options.base.setupProfileId ?? (options.base.extends && options.basesById ? resolveEffectiveBaseSetup({
1140
+ manifest: options.manifest,
1141
+ base: options.basesById.get(options.base.extends)?.definition ?? (() => {
1142
+ throw new Error(
1143
+ `Base '${options.base.id}' extends unknown parent '${options.base.extends}'.`
1144
+ );
1145
+ })(),
1146
+ basesById: options.basesById
1147
+ }).setupProfileId : void 0);
1148
+ const selection = resolveSetupProfileSelection({
1149
+ manifest: options.manifest,
1150
+ requestedSetupProfileId: inheritedSetupProfileId ?? void 0
1151
+ });
1152
+ return {
1153
+ setupProfileId: selection.id
1154
+ };
1155
+ }
1156
+ function resolveBaseDefinition(base, basesById) {
1157
+ const inherited = base.definition.extends !== void 0 ? resolveBaseDefinition(
1158
+ basesById.get(base.definition.extends) ?? (() => {
1159
+ throw new Error(
1160
+ `Base '${base.definition.id}' extends unknown parent '${base.definition.extends}'.`
1161
+ );
1162
+ })(),
1163
+ basesById
1164
+ ) : null;
1165
+ const seed = base.definition.seed ?? inherited?.seed;
1166
+ const players = base.definition.players ?? inherited?.players;
1167
+ if (seed === void 0 || players === void 0) {
1168
+ throw new Error(
1169
+ `Base '${base.definition.id}' must resolve numeric seed and players.`
1170
+ );
1171
+ }
1172
+ return {
1173
+ ...base.definition,
1174
+ seed,
1175
+ players,
1176
+ setupProfileId: base.definition.setupProfileId ?? inherited?.setupProfileId ?? void 0
1177
+ };
1178
+ }
1179
+ function summarizeTableValidationError(manifest, error) {
1180
+ const issues = error.message.split("; ").map((issue) => issue.trim()).filter((issue) => issue.length > 0);
1181
+ if (issues.length === 0 || !issues.every((issue) => issue.startsWith("table"))) {
1182
+ return error.message;
1183
+ }
1184
+ const boardBaseIdByRuntimeId = new Map(
1185
+ (manifest.boards ?? []).flatMap(
1186
+ (board) => board.scope === "perPlayer" ? Array.from(
1187
+ { length: manifest.players.maxPlayers },
1188
+ (_, index) => [`${board.id}:player-${index + 1}`, board.id]
1189
+ ) : [[board.id, board.id]]
1190
+ )
1191
+ );
1192
+ const representativeIssues = issues.slice(0, 3).map((issue) => {
1193
+ const [, runtimeBoardId = ""] = issue.match(/^table\.boards\.byId\.([^.]+)\./) ?? [];
1194
+ const boardBaseId = boardBaseIdByRuntimeId.get(runtimeBoardId);
1195
+ if (!boardBaseId) {
1196
+ return `- ${issue.replace(/^table\./, "")}`;
1197
+ }
1198
+ const boardSpecificPath = issue.replace(/^table\.boards\.byId\.[^.]+\./, "").replace(/^table\./, "");
1199
+ return `- board '${boardBaseId}' (${runtimeBoardId}): ${boardSpecificPath}`;
1200
+ });
1201
+ return [
1202
+ `Reducer-native table validation failed with ${issues.length} issue${issues.length === 1 ? "" : "s"}.`,
1203
+ "Likely root cause: the generated test table shape does not match the authored manifest topology.",
1204
+ ...representativeIssues,
1205
+ "Pass --debug for the full validation dump."
1206
+ ].join("\n");
1207
+ }
1208
+ var ShadowReducerRuntime = class {
1209
+ constructor(manifest, gameModuleDefault, createInitialTable, seed, players, setupProfileId, debug = false) {
1210
+ this.manifest = manifest;
1211
+ this.gameModuleDefault = gameModuleDefault;
1212
+ this.createInitialTable = createInitialTable;
1213
+ this.seed = seed;
1214
+ this.players = players;
1215
+ this.setupProfileId = setupProfileId;
1216
+ this.debug = debug;
1217
+ if (typeof gameModuleDefault !== "object" || gameModuleDefault === null || !("contract" in gameModuleDefault)) {
1218
+ throw new Error(
1219
+ "app/game.ts must export a reducer-native game definition."
1220
+ );
1221
+ }
1222
+ this.bundle = createReducerBundle(gameModuleDefault);
1223
+ this.runtime = this.bundle.createInProcessRuntime();
1224
+ this.playerIds = Array.from(
1225
+ { length: players },
1226
+ (_, index) => `player-${index + 1}`
1227
+ );
1228
+ }
1229
+ manifest;
1230
+ gameModuleDefault;
1231
+ createInitialTable;
1232
+ seed;
1233
+ players;
1234
+ setupProfileId;
1235
+ debug;
1236
+ bundle;
1237
+ runtime;
1238
+ playerIds;
1239
+ historyRecords = [];
1240
+ version = 0;
1241
+ started = false;
1242
+ async start() {
1243
+ if (this.started) {
1244
+ return;
1245
+ }
1246
+ const random = new KotlinSeededRandom(this.seed);
1247
+ const shuffleItems = (values) => shuffleWithKotlinRandom(values, random);
1248
+ const table = JSON.parse(
1249
+ JSON.stringify(
1250
+ this.createInitialTable?.({
1251
+ playerIds: this.playerIds,
1252
+ shuffleItems
1253
+ }) ?? materializeManifestTable({
1254
+ manifest: this.manifest,
1255
+ playerIds: this.playerIds,
1256
+ shuffleItems
1257
+ })
1258
+ )
1259
+ );
1260
+ const rngSeed = random.nextReducerRuntimeSeed();
1261
+ try {
1262
+ await this.runtime.initialize({
1263
+ table,
1264
+ playerIds: this.playerIds,
1265
+ rngSeed,
1266
+ setup: this.setupProfileId ? {
1267
+ profileId: this.setupProfileId
1268
+ } : null
1269
+ });
1270
+ } catch (error) {
1271
+ if (!this.debug && error instanceof Error) {
1272
+ throw new Error(summarizeTableValidationError(this.manifest, error));
1273
+ }
1274
+ throw error;
1275
+ }
1276
+ this.version = 0;
1277
+ this.started = true;
1278
+ }
1279
+ phase() {
1280
+ return (this.runtime.unsafeState()?.domain?.flow?.currentPhase ?? null) || "";
1281
+ }
1282
+ playerOrder() {
1283
+ return this.playerIds;
1284
+ }
1285
+ rawState() {
1286
+ return this.runtime.snapshot();
1287
+ }
1288
+ currentVersion() {
1289
+ return this.version;
1290
+ }
1291
+ hydrate(snapshot, version = 0) {
1292
+ this.runtime.hydrate({ state: structuredClone(snapshot) });
1293
+ this.version = version;
1294
+ this.started = true;
1295
+ }
1296
+ patchState(mutator) {
1297
+ const next = structuredClone(this.rawState());
1298
+ mutator(next);
1299
+ this.hydrate(next, this.version + 1);
1300
+ }
1301
+ projectAllSeats() {
1302
+ const projection = this.runtime.projectSeatsDynamic({
1303
+ playerIds: this.playerIds
1304
+ });
1305
+ const interactionsByRef = projection.interactionsByRef && typeof projection.interactionsByRef === "object" && !Array.isArray(projection.interactionsByRef) ? projection.interactionsByRef : {};
1306
+ const seats = Object.fromEntries(
1307
+ Object.entries(projection.seats ?? {}).map(([playerId, seat]) => {
1308
+ const availableInteractionRefs = Array.isArray(
1309
+ seat.availableInteractionRefs
1310
+ ) ? seat.availableInteractionRefs.filter(
1311
+ (ref) => typeof ref === "string"
1312
+ ) : [];
1313
+ const availableInteractions = seat.availableInteractions ?? availableInteractionRefs.map((ref) => interactionsByRef[ref]).filter(
1314
+ (descriptor) => !!descriptor && typeof descriptor === "object"
1315
+ );
1316
+ return [
1317
+ playerId,
1318
+ {
1319
+ view: seat.view,
1320
+ availableInteractions,
1321
+ zones: seat.zones
1322
+ }
1323
+ ];
1324
+ })
1325
+ );
1326
+ return {
1327
+ currentStage: projection.currentStage ?? null,
1328
+ stageSeats: projection.stageSeats ?? [],
1329
+ seats
1330
+ };
1331
+ }
1332
+ currentStage() {
1333
+ return this.projectAllSeats().currentStage;
1334
+ }
1335
+ view(playerId) {
1336
+ return this.runtime.projectSeatViewDynamic({
1337
+ playerId
1338
+ }) ?? null;
1339
+ }
1340
+ availableInteractionsForPlayer(playerId) {
1341
+ const projection = this.projectAllSeats();
1342
+ const seat = projection.seats[playerId];
1343
+ const descriptors = seat?.availableInteractions ?? [];
1344
+ return descriptors.filter(
1345
+ (descriptor) => !!descriptor && typeof descriptor.interactionId === "string"
1346
+ ).map((descriptor) => descriptor.interactionId);
1347
+ }
1348
+ /**
1349
+ * Phase-kind interaction descriptors (action + prompt) available to
1350
+ * `playerId` in the current phase. Surfaces the same projection the UI
1351
+ * SDK consumes, so scenarios can assert addressee authorization and
1352
+ * availability without reaching into game-specific state. Returned as
1353
+ * loose records — the typed `InteractionDescriptor` shape is narrowed
1354
+ * downstream in the workspace-generated `testing-contract.ts`.
1355
+ */
1356
+ interactionsForPlayer(playerId) {
1357
+ return this.projectAllSeats().seats[playerId]?.availableInteractions ?? [];
1358
+ }
1359
+ explain(playerId, interactionId) {
1360
+ const runtime = this.runtime;
1361
+ if (typeof runtime.explainInteraction !== "function") {
1362
+ throw new Error(
1363
+ "The installed @dreamboard-games/sdk does not expose interaction explanations. Update the workspace SDK pin."
1364
+ );
1365
+ }
1366
+ return runtime.explainInteraction({ playerId, interactionId });
1367
+ }
1368
+ clearHistory() {
1369
+ this.historyRecords.length = 0;
1370
+ }
1371
+ async applyInput(input) {
1372
+ const parsedInput = toReducerInput(input);
1373
+ const result = await this.runtime.dispatch({ input: parsedInput });
1374
+ if (result.kind === "reject") {
1375
+ const record = {
1376
+ input,
1377
+ accepted: false,
1378
+ errorCode: result.errorCode,
1379
+ message: result.message
1380
+ };
1381
+ this.historyRecords.push(record);
1382
+ throw createSubmissionError(
1383
+ result.errorCode,
1384
+ result.message,
1385
+ "Reducer input rejected."
1386
+ );
1387
+ }
1388
+ this.version += 1;
1389
+ this.historyRecords.push({ input, accepted: true });
1390
+ }
1391
+ async submitInteraction(playerId, interactionId, params) {
1392
+ await this.applyInput({
1393
+ kind: "interaction",
1394
+ playerId,
1395
+ interactionId,
1396
+ params
1397
+ });
1398
+ }
1399
+ };
1400
+ function toReducerInput(input) {
1401
+ return {
1402
+ kind: "interaction",
1403
+ playerId: input.playerId,
1404
+ interactionId: input.interactionId,
1405
+ params: input.params
1406
+ };
1407
+ }
1408
+ var RemoteGameplayTracker = class {
1409
+ constructor(sessionId, playerId) {
1410
+ this.sessionId = sessionId;
1411
+ this.playerId = playerId;
1412
+ this.state.playerId = playerId;
1413
+ }
1414
+ sessionId;
1415
+ playerId;
1416
+ state = {
1417
+ version: -1,
1418
+ actionSetVersion: "",
1419
+ currentPhase: null,
1420
+ playerId: "player-1",
1421
+ view: null,
1422
+ availableInteractions: []
1423
+ };
1424
+ liveBootstrap = false;
1425
+ async bootstrap() {
1426
+ const { data, error, response } = await getSessionSnapshot({
1427
+ path: { sessionId: this.sessionId },
1428
+ query: { playerId: this.playerId }
1429
+ });
1430
+ if (error || !data || data.type !== "gameplay") {
1431
+ throw toDreamboardApiError(
1432
+ error ?? { detail: "Gameplay snapshot was unavailable." },
1433
+ response,
1434
+ "Failed to load the remote gameplay bootstrap snapshot"
1435
+ );
1436
+ }
1437
+ if (data.gameplay.actionSetVersion === "authority") {
1438
+ return;
1439
+ }
1440
+ this.liveBootstrap = true;
1441
+ this.applyGameplay(data.gameplay);
1442
+ }
1443
+ hasLiveBootstrap() {
1444
+ return this.liveBootstrap;
1445
+ }
1446
+ snapshot() {
1447
+ return {
1448
+ ...this.state,
1449
+ availableInteractions: [...this.state.availableInteractions]
1450
+ };
1451
+ }
1452
+ applyShadow(shadow) {
1453
+ const projection = shadow.projectAllSeats();
1454
+ const playerId = this.playerId;
1455
+ const seat = projection.seats[playerId];
1456
+ this.state.version = shadow.currentVersion();
1457
+ this.state.actionSetVersion = "";
1458
+ this.state.playerId = playerId;
1459
+ this.state.currentPhase = shadow.phase();
1460
+ this.state.view = seat?.view ?? null;
1461
+ this.state.availableInteractions = shadow.availableInteractionsForPlayer(playerId);
1462
+ }
1463
+ applyGameplay(gameplay) {
1464
+ const playerId = gameplay.perspectivePlayerId;
1465
+ const seat = gameplay.seats[playerId];
1466
+ this.state.version = gameplay.version;
1467
+ this.state.actionSetVersion = gameplay.actionSetVersion;
1468
+ this.state.playerId = playerId;
1469
+ this.state.currentPhase = gameplay.shared.currentPhase;
1470
+ this.state.view = parseSeatView(seat?.view ?? "null");
1471
+ this.state.availableInteractions = (seat?.availableInteractionRefs ?? []).map((ref) => gameplay.interactionsByRef[ref]).filter(
1472
+ (interaction) => Boolean(interaction)
1473
+ ).map((interaction) => interaction.interactionId);
1474
+ }
1475
+ };
1476
+ function parseSeatView(raw) {
1477
+ return raw ? parseJsonValue(raw) : null;
1478
+ }
1479
+ function sleep(ms) {
1480
+ return new Promise((resolve) => setTimeout(resolve, ms));
1481
+ }
1482
+ function formatIssuePath(label, path3) {
1483
+ let formatted = label;
1484
+ for (const segment of path3) {
1485
+ formatted += typeof segment === "number" ? `[${segment}]` : `.${String(segment)}`;
1486
+ }
1487
+ return formatted;
1488
+ }
1489
+ function parseWirePayload(methodName, value, label, schema) {
1490
+ const parsed = schema.safeParse(value);
1491
+ if (parsed.success) {
1492
+ return parsed.data;
1493
+ }
1494
+ const firstIssue = parsed.error.issues[0];
1495
+ const formattedPath = formatIssuePath(label, firstIssue?.path ?? []);
1496
+ throw new Error(
1497
+ `Reducer bundle returned invalid payload for '${methodName}': ${formattedPath} ${firstIssue?.message ?? "failed schema validation"}.`
1498
+ );
1499
+ }
1500
+ function assertDispatchResultWireContract(result) {
1501
+ return parseWirePayload(
1502
+ "dispatch",
1503
+ result,
1504
+ "DispatchResult",
1505
+ ReducerContractZod.DispatchResultSchema
1506
+ );
1507
+ }
1508
+ async function createBrowserBridgeClient(page) {
1509
+ const bridgeExists = async () => page.evaluate(
1510
+ () => Boolean(
1511
+ window.__dreamboardTestBridge__
1512
+ )
1513
+ );
1514
+ const startedAt = Date.now();
1515
+ while (!await bridgeExists()) {
1516
+ if (Date.now() - startedAt > DEFAULT_TIMEOUT_MS2) {
1517
+ const bodyText = await page.locator("body").innerText().catch(() => "");
1518
+ const diagnostic = bodyText.trim().replace(/\s+/g, " ").slice(0, 400);
1519
+ throw new Error(
1520
+ diagnostic.length > 0 ? `Timed out waiting for browser test bridge. Page text: ${diagnostic}` : "Timed out waiting for browser test bridge."
1521
+ );
1522
+ }
1523
+ await sleep(50);
1524
+ }
1525
+ return {
1526
+ snapshot: () => page.evaluate(
1527
+ () => window.__dreamboardTestBridge__.snapshot()
1528
+ ),
1529
+ submitInteraction: (playerId, interactionId, params) => page.evaluate(
1530
+ ([nextPlayerId, nextInteractionId, nextParams]) => window.__dreamboardTestBridge__.submitInteraction(
1531
+ nextPlayerId,
1532
+ nextInteractionId,
1533
+ nextParams
1534
+ ),
1535
+ [playerId, interactionId, params]
1536
+ ),
1537
+ waitForVersionChange: async (previousVersion, timeoutMs = DEFAULT_TIMEOUT_MS2) => {
1538
+ const started = Date.now();
1539
+ while (Date.now() - started < timeoutMs) {
1540
+ const snapshot = await page.evaluate(
1541
+ () => window.__dreamboardTestBridge__.snapshot()
1542
+ );
1543
+ if (snapshot.version > previousVersion) {
1544
+ return snapshot;
1545
+ }
1546
+ await sleep(50);
1547
+ }
1548
+ throw new Error("Timed out waiting for browser gameplay version change.");
1549
+ }
1550
+ };
1551
+ }
1552
+ async function assertLiveMatchesShadow(runner, shadow, live) {
1553
+ if (runner === "reducer") {
1554
+ return;
1555
+ }
1556
+ const currentPhase = "currentPhase" in live ? live.currentPhase : shadow.phase();
1557
+ if (currentPhase !== shadow.phase()) {
1558
+ throw new Error(
1559
+ `Live phase '${String(currentPhase)}' diverged from reducer phase '${shadow.phase()}'.`
1560
+ );
1561
+ }
1562
+ const livePlayerId = "playerId" in live ? live.playerId : live.controllingPlayerId;
1563
+ const liveView = "view" in live ? live.view : null;
1564
+ if (livePlayerId && !deepEqual(liveView, await shadow.view(livePlayerId))) {
1565
+ throw new Error("Live projected views diverged from reducer shadow views.");
1566
+ }
1567
+ const liveInteractions = "availableInteractions" in live ? live.availableInteractions : null;
1568
+ if (livePlayerId && liveInteractions && !deepEqual(
1569
+ liveInteractions,
1570
+ shadow.availableInteractionsForPlayer(livePlayerId)
1571
+ )) {
1572
+ throw new Error(
1573
+ "Live available-interactions metadata diverged from reducer shadow state."
1574
+ );
1575
+ }
1576
+ }
1577
+ async function loadBrowserDriver(projectRoot) {
1578
+ const filePath = path2.join(projectRoot, "test", "browser-driver.ts");
1579
+ if (!await exists(filePath)) {
1580
+ return null;
1581
+ }
1582
+ const module = await importTypeScriptModule(filePath);
1583
+ return module.default ?? module;
1584
+ }
1585
+ async function openBrowserPage(playUrl, config) {
1586
+ configurePlaywrightBrowsersPath();
1587
+ const { chromium } = await import("playwright");
1588
+ const browser = await chromium.launch({ headless: true });
1589
+ const context = await browser.newContext({
1590
+ viewport: { width: 1440, height: 900 }
1591
+ });
1592
+ const initScript = await buildBrowserAuthInitScript(config);
1593
+ if (initScript) {
1594
+ await context.addInitScript({ content: initScript });
1595
+ }
1596
+ const page = await context.newPage();
1597
+ await page.goto(playUrl, { waitUntil: "domcontentloaded" });
1598
+ return { browser, page };
1599
+ }
1600
+ function sanitizeSnapshotSegment(value) {
1601
+ return value.replace(/[^a-zA-Z0-9._-]+/g, "-");
1602
+ }
1603
+ function createScenarioSnapshotMatcher(options) {
1604
+ return (filename, actual) => {
1605
+ const suffix = filename ? `.${sanitizeSnapshotSegment(filename)}` : "";
1606
+ const snapshotPath = path2.join(
1607
+ options.projectRoot,
1608
+ "test",
1609
+ "generated",
1610
+ "snapshots",
1611
+ `${sanitizeSnapshotSegment(options.scenarioId)}${suffix}.snapshot.json`
1612
+ );
1613
+ const wrappedValue = {
1614
+ value: actual
1615
+ };
1616
+ const serialized = `${JSON.stringify(wrappedValue, null, 2)}
1617
+ `;
1618
+ mkdirSync(path2.dirname(snapshotPath), { recursive: true });
1619
+ if (!existsSync(snapshotPath) || options.updateSnapshots) {
1620
+ writeFileSync(snapshotPath, serialized, "utf8");
1621
+ return;
1622
+ }
1623
+ const previous = JSON.parse(readFileSync(snapshotPath, "utf8"));
1624
+ if (!deepEqual(previous.value, actual)) {
1625
+ throw new Error(
1626
+ `Snapshot mismatch for scenario '${options.scenarioId}'. Re-run with --update-snapshots to refresh ${path2.relative(options.projectRoot, snapshotPath)}.`
1627
+ );
1628
+ }
1629
+ };
1630
+ }
1631
+ async function createScenarioContext(options) {
1632
+ const api = {
1633
+ start: async () => {
1634
+ await options.shadow.start();
1635
+ if (options.runner === "remote" && options.remote) {
1636
+ if (options.remote.tracker.hasLiveBootstrap()) {
1637
+ const live = options.remote.tracker.snapshot();
1638
+ await assertLiveMatchesShadow("remote", options.shadow, live);
1639
+ } else {
1640
+ options.remote.tracker.applyShadow(options.shadow);
1641
+ }
1642
+ }
1643
+ if (options.runner === "browser" && options.browser) {
1644
+ const live = await options.browser.bridge.snapshot();
1645
+ await assertLiveMatchesShadow("browser", options.shadow, live);
1646
+ }
1647
+ },
1648
+ patchState: async (mutator) => {
1649
+ await api.start();
1650
+ if (options.live || options.remote || options.browser || options.actionPlan) {
1651
+ throw new Error(
1652
+ "game.patchState is only supported for reducer snapshot scenarios. Use it to materialize a state before --from-scenario or reducer tests, not inside live replay/browser runners."
1653
+ );
1654
+ }
1655
+ options.shadow.patchState(mutator);
1656
+ },
1657
+ submit: async (playerId, interactionId, params) => {
1658
+ await api.start();
1659
+ const interactionParams = params ?? {};
1660
+ const normalizedInputs = interactionParams && typeof interactionParams === "object" && !Array.isArray(interactionParams) ? interactionParams : {};
1661
+ const previousVersion = options.live ? options.live.version : options.actionPlan ? options.shadow.currentVersion() : options.runner === "remote" ? options.remote?.tracker.snapshot().version ?? 0 : options.runner === "browser" ? (await options.browser?.bridge.snapshot())?.version ?? 0 : 0;
1662
+ if (options.live) {
1663
+ if (!options.live.actionSetVersion) {
1664
+ options.live.actionSetVersion = await fetchLiveActionSetVersion(
1665
+ options.live.sessionId,
1666
+ playerId
1667
+ );
1668
+ }
1669
+ const submittedActionSetVersion = options.live.actionSetVersion;
1670
+ const submitStartedAt = performance.now();
1671
+ const clientActionId = randomUUID2();
1672
+ const { data, error, response } = await submitGameplayAuthorityAction({
1673
+ path: {
1674
+ sessionId: options.live.sessionId,
1675
+ playerId,
1676
+ interactionId
1677
+ },
1678
+ body: {
1679
+ expectedVersion: previousVersion,
1680
+ actionSetVersion: options.live.actionSetVersion,
1681
+ inputs: normalizedInputs
1682
+ },
1683
+ headers: { "X-Dreamboard-Client-Action-Id": clientActionId }
1684
+ });
1685
+ const durationMs = performance.now() - submitStartedAt;
1686
+ options.live.diagnostics?.push({
1687
+ index: options.live.submitIndex ?? 0,
1688
+ playerId,
1689
+ interactionId,
1690
+ inputs: normalizedInputs,
1691
+ expectedVersion: previousVersion,
1692
+ actionSetVersion: submittedActionSetVersion,
1693
+ responseStatus: response?.status,
1694
+ responseBody: options.live.captureResponseBody === false ? void 0 : JSON.stringify(error ?? data ?? null),
1695
+ accepted: data?.accepted ?? void 0,
1696
+ errorCode: data?.accepted === false ? data.errorCode ?? void 0 : error && typeof error === "object" && "detail" in error ? String(error.detail) : void 0,
1697
+ backendTiming: response?.headers?.get("server-timing") ?? response?.headers?.get("x-dreamboard-timing") ?? void 0,
1698
+ durationMs
1699
+ });
1700
+ options.live.submitIndex = (options.live.submitIndex ?? 0) + 1;
1701
+ if (error) {
1702
+ throw createSubmissionError(
1703
+ "api-error",
1704
+ void 0,
1705
+ "Failed to submit interaction"
1706
+ );
1707
+ }
1708
+ if (data?.accepted === false) {
1709
+ const shadowError = await tryShadowSubmit(
1710
+ options.shadow,
1711
+ playerId,
1712
+ interactionId,
1713
+ interactionParams
1714
+ );
1715
+ if (!shadowError || shadowError.errorCode !== data.errorCode) {
1716
+ throw new Error(
1717
+ "Live session rejection diverged from reducer shadow state."
1718
+ );
1719
+ }
1720
+ throw createSubmissionError(
1721
+ data.errorCode ?? void 0,
1722
+ data.message ?? void 0,
1723
+ "Interaction rejected"
1724
+ );
1725
+ }
1726
+ options.live.version = typeof data?.version === "number" ? data.version : previousVersion + 1;
1727
+ options.live.actionSetVersion = data?.actionSetVersion ?? options.live.actionSetVersion;
1728
+ } else if (options.runner === "remote" && options.remote) {
1729
+ } else if (options.runner === "browser" && options.browser) {
1730
+ const handled = await options.browser.driver?.interaction?.(options.browser.bridge, {
1731
+ playerId,
1732
+ interactionId,
1733
+ params: interactionParams
1734
+ }) === true;
1735
+ if (!handled) {
1736
+ await options.browser.bridge.submitInteraction(
1737
+ playerId,
1738
+ interactionId,
1739
+ interactionParams
1740
+ );
1741
+ }
1742
+ }
1743
+ await options.shadow.submitInteraction(
1744
+ playerId,
1745
+ interactionId,
1746
+ interactionParams
1747
+ );
1748
+ if (options.actionPlan) {
1749
+ options.actionPlan.diagnostics.push({
1750
+ index: options.actionPlan.submitIndex ?? 0,
1751
+ playerId,
1752
+ interactionId,
1753
+ inputs: normalizedInputs,
1754
+ expectedVersion: previousVersion,
1755
+ actionSetVersion: "",
1756
+ accepted: true,
1757
+ durationMs: 0
1758
+ });
1759
+ options.actionPlan.submitIndex = (options.actionPlan.submitIndex ?? 0) + 1;
1760
+ }
1761
+ if (options.runner === "remote" && options.remote) {
1762
+ options.remote.tracker.applyShadow(options.shadow);
1763
+ await assertLiveMatchesShadow(
1764
+ "remote",
1765
+ options.shadow,
1766
+ options.remote.tracker.snapshot()
1767
+ );
1768
+ }
1769
+ if (options.runner === "browser" && options.browser) {
1770
+ const live = await options.browser.bridge.waitForVersionChange(previousVersion);
1771
+ await assertLiveMatchesShadow("browser", options.shadow, live);
1772
+ }
1773
+ }
1774
+ };
1775
+ return {
1776
+ game: api,
1777
+ players: () => options.shadow.playerOrder(),
1778
+ seat: (index) => {
1779
+ const order = options.shadow.playerOrder();
1780
+ if (!Number.isInteger(index) || index < 0 || index >= order.length) {
1781
+ throw new Error(
1782
+ `seat(${index}) is out of range; scenario has ${order.length} player(s).`
1783
+ );
1784
+ }
1785
+ return order[index];
1786
+ },
1787
+ state: () => options.shadow.phase(),
1788
+ view: (playerId) => options.shadow.view(playerId),
1789
+ interactions: (playerId) => options.shadow.interactionsForPlayer(playerId),
1790
+ explain: (playerId, interactionId) => options.shadow.explain(playerId, interactionId),
1791
+ expect: options.expect
1792
+ };
1793
+ }
1794
+ async function tryShadowSubmit(shadow, playerId, interactionId, params) {
1795
+ try {
1796
+ await shadow.submitInteraction(playerId, interactionId, params);
1797
+ throw new Error("Expected shadow interaction to reject.");
1798
+ } catch (error) {
1799
+ if (error instanceof Error && error.name === "SubmissionError") {
1800
+ return error;
1801
+ }
1802
+ throw error;
1803
+ }
1804
+ }
1805
+ async function createSessionFromScenario(options) {
1806
+ const startedAt = Date.now();
1807
+ const materialized = await materializeScenarioReducerState(options);
1808
+ const reducerHarnessMs = Date.now() - startedAt;
1809
+ const hydrateStartedAt = Date.now();
1810
+ const {
1811
+ data: snapshot,
1812
+ error,
1813
+ response
1814
+ } = await createProjectSessionFromReducerSnapshot({
1815
+ path: { projectId: options.projectId },
1816
+ body: {
1817
+ compiledResultId: options.compiledResultId,
1818
+ seed: materialized.seed,
1819
+ playerCount: materialized.playerCount,
1820
+ setupProfileId: materialized.setupProfileId ?? void 0,
1821
+ baseId: materialized.baseId,
1822
+ scenarioId: materialized.scenarioId,
1823
+ reducerState: materialized.reducerState,
1824
+ reducerStateVersion: materialized.reducerStateVersion,
1825
+ fingerprintMetadata: materialized.fingerprintMetadata
1826
+ }
1827
+ });
1828
+ const backendHydrateMs = Date.now() - hydrateStartedAt;
1829
+ if (error || !snapshot) {
1830
+ throw toDreamboardApiError(
1831
+ error,
1832
+ response,
1833
+ `Failed to materialize scenario '${options.scenarioId}' from reducer snapshot`
1834
+ );
1835
+ }
1836
+ return {
1837
+ sessionId: snapshot.context.sessionId,
1838
+ shortCode: snapshot.context.shortCode,
1839
+ gameId: projectIdFromSessionGameSource(snapshot.context.gameSource),
1840
+ seed: materialized.seed,
1841
+ playerCount: materialized.playerCount,
1842
+ setupProfileId: materialized.setupProfileId,
1843
+ scenarioId: materialized.scenarioId,
1844
+ phase: materialized.phase,
1845
+ stage: materialized.stage,
1846
+ materialization: {
1847
+ mode: "snapshot",
1848
+ totalMs: Date.now() - startedAt,
1849
+ reducerHarnessMs,
1850
+ backendHydrateMs
1851
+ }
1852
+ };
1853
+ }
1854
+ var MATERIALIZED_SCENARIO_CACHE_VERSION = 1;
1855
+ function materializedScenarioCachePath(options) {
1856
+ return path2.join(
1857
+ options.projectRoot,
1858
+ PROJECT_DIR_NAME,
1859
+ "dev",
1860
+ "scenario-cache",
1861
+ `${sanitizeSnapshotSegment(options.baseId)}.${sanitizeSnapshotSegment(
1862
+ options.scenarioId
1863
+ )}.${options.fingerprintHash}.json`
1864
+ );
1865
+ }
1866
+ async function readMaterializedScenarioCache(options) {
1867
+ const cachePath = materializedScenarioCachePath(options);
1868
+ const text = await readTextFileIfExists(cachePath);
1869
+ if (!text) return null;
1870
+ try {
1871
+ const payload = JSON.parse(
1872
+ text
1873
+ );
1874
+ if (payload.cacheVersion !== MATERIALIZED_SCENARIO_CACHE_VERSION || payload.fingerprintHash !== options.fingerprintHash || !payload.materialized) {
1875
+ return null;
1876
+ }
1877
+ return payload.materialized;
1878
+ } catch {
1879
+ return null;
1880
+ }
1881
+ }
1882
+ async function writeMaterializedScenarioCache(options) {
1883
+ const cachePath = materializedScenarioCachePath(options);
1884
+ await ensureDir(path2.dirname(cachePath));
1885
+ await writeJsonFile(cachePath, {
1886
+ cacheVersion: MATERIALIZED_SCENARIO_CACHE_VERSION,
1887
+ fingerprintHash: options.fingerprintHash,
1888
+ materialized: options.materialized
1889
+ });
1890
+ }
1891
+ async function materializeScenarioReducerState(options) {
1892
+ await ensureReducerNativeTestingFiles(options.projectRoot);
1893
+ let preloadedGeneratedBaseStates;
1894
+ if (options.trustGeneratedFingerprint === true) {
1895
+ const [generatedBaseStates2, scenarioManifest] = await Promise.all([
1896
+ loadGeneratedBaseStates(options.projectRoot),
1897
+ loadGeneratedScenarioManifest(options.projectRoot)
1898
+ ]);
1899
+ preloadedGeneratedBaseStates = generatedBaseStates2;
1900
+ const scenarioEntry = scenarioManifest.find(
1901
+ (entry) => entry.id === options.scenarioId
1902
+ );
1903
+ const generatedBase2 = scenarioEntry ? generatedBaseStates2?.[baseStateKey(scenarioEntry.base)] : null;
1904
+ if (generatedBase2 && generatedBase2.fingerprint.compiledResultId === options.compiledResultId && generatedBase2.fingerprint.gameId === options.gameId) {
1905
+ const cached2 = await readMaterializedScenarioCache({
1906
+ projectRoot: options.projectRoot,
1907
+ baseId: scenarioEntry.base,
1908
+ scenarioId: options.scenarioId,
1909
+ fingerprintHash: fingerprintHash(generatedBase2.fingerprint)
1910
+ });
1911
+ if (cached2) {
1912
+ return cached2;
1913
+ }
1914
+ }
1915
+ }
1916
+ const [bases, scenarios, generatedBaseStates, manifest] = await Promise.all([
1917
+ loadTypedBases(options.projectRoot),
1918
+ loadTypedScenarios(options.projectRoot, {}),
1919
+ preloadedGeneratedBaseStates === void 0 ? loadGeneratedBaseStates(options.projectRoot) : Promise.resolve(preloadedGeneratedBaseStates),
1920
+ loadManifest(options.projectRoot)
1921
+ ]);
1922
+ const matchingScenarios = scenarios.filter(
1923
+ (scenario2) => scenario2.definition.id === options.scenarioId
1924
+ );
1925
+ if (matchingScenarios.length === 0) {
1926
+ throw new Error(
1927
+ `Unknown scenario '${options.scenarioId}'. Available scenarios: ${scenarios.map((scenario2) => scenario2.definition.id).sort().join(", ")}`
1928
+ );
1929
+ }
1930
+ if (matchingScenarios.length > 1) {
1931
+ throw new Error(
1932
+ `Scenario id '${options.scenarioId}' is defined more than once. Keep one scenario per file and one unique id per workspace.`
1933
+ );
1934
+ }
1935
+ const scenario = matchingScenarios[0];
1936
+ const basesById = new Map(bases.map((base2) => [base2.definition.id, base2]));
1937
+ validateTypedScenarioBases([scenario], basesById);
1938
+ const base = basesById.get(scenario.definition.from);
1939
+ if (!base) {
1940
+ throw new Error(
1941
+ `Missing typed base '${scenario.definition.from}' for scenario '${scenario.definition.id}'.`
1942
+ );
1943
+ }
1944
+ const generatedBase = generatedBaseStates?.[baseStateKey(base.definition.id)];
1945
+ if (!generatedBase) {
1946
+ throw new Error(
1947
+ `Missing generated base artifact for '${base.definition.id}'. Run 'dreamboard test generate' before using --from-scenario.`
1948
+ );
1949
+ }
1950
+ if (typeof generatedBase.version !== "number") {
1951
+ throw new Error(
1952
+ `Generated base artifact for '${base.definition.id}' is stale. Run 'dreamboard test generate' before using --from-scenario.`
1953
+ );
1954
+ }
1955
+ const canTrustGeneratedFingerprint = options.trustGeneratedFingerprint === true && generatedBase.fingerprint.compiledResultId === options.compiledResultId && generatedBase.fingerprint.gameId === options.gameId;
1956
+ const current = canTrustGeneratedFingerprint ? generatedBase.fingerprint : await currentFingerprint({
1957
+ projectRoot: options.projectRoot,
1958
+ base,
1959
+ basesById,
1960
+ compiledResultId: options.compiledResultId,
1961
+ gameId: options.gameId
1962
+ });
1963
+ if (!canTrustGeneratedFingerprint) {
1964
+ validateGeneratedFingerprint({
1965
+ generated: generatedBase.fingerprint,
1966
+ current
1967
+ });
1968
+ }
1969
+ const currentHash = fingerprintHash(current);
1970
+ const cached = await readMaterializedScenarioCache({
1971
+ projectRoot: options.projectRoot,
1972
+ baseId: base.definition.id,
1973
+ scenarioId: scenario.definition.id,
1974
+ fingerprintHash: currentHash
1975
+ });
1976
+ if (cached) {
1977
+ return cached;
1978
+ }
1979
+ const [gameModule, manifestContractModule] = await Promise.all([
1980
+ importTypeScriptModule(
1981
+ path2.join(options.projectRoot, "app", "game.ts")
1982
+ ),
1983
+ importTypeScriptModule(
1984
+ path2.join(options.projectRoot, "shared", "manifest-contract.ts")
1985
+ )
1986
+ ]);
1987
+ const createInitialTable = typeof manifestContractModule.createInitialTable === "function" ? manifestContractModule.createInitialTable : null;
1988
+ const resolvedBase = resolveBaseDefinition(base, basesById);
1989
+ const effectiveSetup = resolveEffectiveBaseSetup({
1990
+ manifest,
1991
+ base: base.definition,
1992
+ basesById
1993
+ });
1994
+ const shadow = new ShadowReducerRuntime(
1995
+ manifest,
1996
+ gameModule.default,
1997
+ createInitialTable,
1998
+ resolvedBase.seed,
1999
+ resolvedBase.players,
2000
+ effectiveSetup.setupProfileId,
2001
+ options.debug ?? false
2002
+ );
2003
+ shadow.hydrate(generatedBase.snapshot, generatedBase.version);
2004
+ const context = await createScenarioContext({
2005
+ runner: "reducer",
2006
+ shadow,
2007
+ expect: (await loadTestingExpectApiFactory())()
2008
+ });
2009
+ shadow.clearHistory();
2010
+ await scenario.definition.when(context);
2011
+ if (scenario.definition.phase && shadow.phase() !== scenario.definition.phase) {
2012
+ throw new Error(
2013
+ `Scenario '${scenario.definition.id}' expected phase '${scenario.definition.phase}' but reached '${shadow.phase()}'.`
2014
+ );
2015
+ }
2016
+ if (scenario.definition.stage && shadow.currentStage() !== scenario.definition.stage) {
2017
+ throw new Error(
2018
+ `Scenario '${scenario.definition.id}' expected stage '${scenario.definition.stage}' but reached '${shadow.currentStage() ?? "null"}'.`
2019
+ );
2020
+ }
2021
+ const materialized = {
2022
+ baseId: base.definition.id,
2023
+ scenarioId: scenario.definition.id,
2024
+ seed: resolvedBase.seed,
2025
+ playerCount: resolvedBase.players,
2026
+ setupProfileId: effectiveSetup.setupProfileId,
2027
+ reducerState: shadow.rawState(),
2028
+ reducerStateVersion: shadow.currentVersion(),
2029
+ fingerprintMetadata: {
2030
+ baseFingerprintHash: fingerprintHash(generatedBase.fingerprint),
2031
+ currentFingerprintHash: fingerprintHash(current),
2032
+ baseBundleHash: generatedBase.fingerprint.baseBundleHash,
2033
+ manifestHash: generatedBase.fingerprint.manifestHash,
2034
+ appBundleHash: generatedBase.fingerprint.appBundleHash,
2035
+ uiContractHash: generatedBase.fingerprint.uiContractHash,
2036
+ baseId: base.definition.id,
2037
+ scenarioId: scenario.definition.id
2038
+ },
2039
+ phase: shadow.phase(),
2040
+ stage: shadow.currentStage()
2041
+ };
2042
+ await writeMaterializedScenarioCache({
2043
+ projectRoot: options.projectRoot,
2044
+ baseId: base.definition.id,
2045
+ scenarioId: scenario.definition.id,
2046
+ fingerprintHash: currentHash,
2047
+ materialized
2048
+ });
2049
+ return materialized;
2050
+ }
2051
+ async function replayScenarioThroughBackend(options) {
2052
+ await ensureReducerNativeTestingFiles(options.projectRoot);
2053
+ const [
2054
+ bases,
2055
+ scenarios,
2056
+ generatedBaseStates,
2057
+ manifest,
2058
+ gameModule,
2059
+ manifestContractModule
2060
+ ] = await Promise.all([
2061
+ loadTypedBases(options.projectRoot),
2062
+ loadTypedScenarios(options.projectRoot, {}),
2063
+ loadGeneratedBaseStates(options.projectRoot),
2064
+ loadManifest(options.projectRoot),
2065
+ importTypeScriptModule(
2066
+ path2.join(options.projectRoot, "app", "game.ts")
2067
+ ),
2068
+ importTypeScriptModule(
2069
+ path2.join(options.projectRoot, "shared", "manifest-contract.ts")
2070
+ )
2071
+ ]);
2072
+ const matchingScenarios = scenarios.filter(
2073
+ (scenario2) => scenario2.definition.id === options.scenarioId
2074
+ );
2075
+ if (matchingScenarios.length !== 1) {
2076
+ throw new Error(
2077
+ matchingScenarios.length === 0 ? `Unknown scenario '${options.scenarioId}'. Available scenarios: ${scenarios.map((scenario2) => scenario2.definition.id).sort().join(", ")}` : `Scenario id '${options.scenarioId}' is defined more than once. Keep one scenario per file and one unique id per workspace.`
2078
+ );
2079
+ }
2080
+ const scenario = matchingScenarios[0];
2081
+ const basesById = new Map(bases.map((base2) => [base2.definition.id, base2]));
2082
+ validateTypedScenarioBases([scenario], basesById);
2083
+ const base = basesById.get(scenario.definition.from);
2084
+ if (!base) {
2085
+ throw new Error(
2086
+ `Missing typed base '${scenario.definition.from}' for scenario '${scenario.definition.id}'.`
2087
+ );
2088
+ }
2089
+ const generatedBase = generatedBaseStates?.[baseStateKey(base.definition.id)];
2090
+ if (!generatedBase) {
2091
+ throw new Error(
2092
+ `Missing generated base artifact for '${base.definition.id}'. Run 'dreamboard test generate' before replaying a scenario.`
2093
+ );
2094
+ }
2095
+ validateGeneratedFingerprint({
2096
+ generated: generatedBase.fingerprint,
2097
+ current: await currentFingerprint({
2098
+ projectRoot: options.projectRoot,
2099
+ base,
2100
+ basesById,
2101
+ compiledResultId: options.compiledResultId,
2102
+ gameId: options.gameId
2103
+ })
2104
+ });
2105
+ const createInitialTable = typeof manifestContractModule.createInitialTable === "function" ? manifestContractModule.createInitialTable : null;
2106
+ const resolvedBase = resolveBaseDefinition(base, basesById);
2107
+ const effectiveSetup = resolveEffectiveBaseSetup({
2108
+ manifest,
2109
+ base: base.definition,
2110
+ basesById
2111
+ });
2112
+ const shadow = new ShadowReducerRuntime(
2113
+ manifest,
2114
+ gameModule.default,
2115
+ createInitialTable,
2116
+ resolvedBase.seed,
2117
+ resolvedBase.players,
2118
+ effectiveSetup.setupProfileId,
2119
+ options.debug ?? false
2120
+ );
2121
+ if (base.definition.extends) {
2122
+ const parentArtifact = generatedBaseStates?.[baseStateKey(base.definition.extends)];
2123
+ if (!parentArtifact || typeof parentArtifact.version !== "number") {
2124
+ throw new Error(
2125
+ `Base '${base.definition.id}' extends '${base.definition.extends}', but the parent artifact is missing or stale. Run 'dreamboard test generate' first.`
2126
+ );
2127
+ }
2128
+ shadow.hydrate(parentArtifact.snapshot, parentArtifact.version);
2129
+ }
2130
+ const {
2131
+ data: session,
2132
+ error: sessionError,
2133
+ response: sessionResponse
2134
+ } = await createProjectSession({
2135
+ path: { projectId: options.gameId },
2136
+ body: {
2137
+ compiledResultId: options.compiledResultId,
2138
+ seed: resolvedBase.seed,
2139
+ playerCount: resolvedBase.players,
2140
+ autoAssignSeats: true,
2141
+ setupProfileId: effectiveSetup.setupProfileId ?? void 0
2142
+ }
2143
+ });
2144
+ if (!session || sessionError) {
2145
+ throw toDreamboardApiError(
2146
+ sessionError,
2147
+ sessionResponse,
2148
+ `Failed to create replay session for '${scenario.definition.id}'`
2149
+ );
2150
+ }
2151
+ const { error: startError, response: startResponse } = await startGame({
2152
+ path: { sessionId: session.sessionId }
2153
+ });
2154
+ if (startError) {
2155
+ throw toDreamboardApiError(
2156
+ startError,
2157
+ startResponse,
2158
+ `Failed to start replay session for '${scenario.definition.id}'`
2159
+ );
2160
+ }
2161
+ const diagnostics = [];
2162
+ const context = await createScenarioContext({
2163
+ runner: "reducer",
2164
+ shadow,
2165
+ expect: (await loadTestingExpectApiFactory())(),
2166
+ live: {
2167
+ sessionId: session.sessionId,
2168
+ version: 0,
2169
+ actionSetVersion: "",
2170
+ submitIndex: 0,
2171
+ diagnostics,
2172
+ captureResponseBody: options.captureResponseBody
2173
+ }
2174
+ });
2175
+ await context.game.start();
2176
+ await base.definition.setup({
2177
+ game: context.game,
2178
+ players: context.players,
2179
+ seat: context.seat
2180
+ });
2181
+ shadow.clearHistory();
2182
+ await scenario.definition.when(context);
2183
+ if (scenario.definition.phase && shadow.phase() !== scenario.definition.phase) {
2184
+ throw new Error(
2185
+ `Scenario '${scenario.definition.id}' expected phase '${scenario.definition.phase}' but reached '${shadow.phase()}'.`
2186
+ );
2187
+ }
2188
+ if (scenario.definition.stage && shadow.currentStage() !== scenario.definition.stage) {
2189
+ throw new Error(
2190
+ `Scenario '${scenario.definition.id}' expected stage '${scenario.definition.stage}' but reached '${shadow.currentStage() ?? "null"}'.`
2191
+ );
2192
+ }
2193
+ return {
2194
+ sessionId: session.sessionId,
2195
+ shortCode: session.shortCode,
2196
+ gameId: projectIdFromSessionGameSource(session.gameSource),
2197
+ seed: resolvedBase.seed,
2198
+ playerCount: resolvedBase.players,
2199
+ setupProfileId: effectiveSetup.setupProfileId,
2200
+ scenarioId: scenario.definition.id,
2201
+ phase: shadow.phase(),
2202
+ stage: shadow.currentStage(),
2203
+ diagnostics
2204
+ };
2205
+ }
2206
+ async function createScenarioActionPlan(options) {
2207
+ await ensureReducerNativeTestingFiles(options.projectRoot);
2208
+ const [
2209
+ bases,
2210
+ scenarios,
2211
+ generatedBaseStates,
2212
+ manifest,
2213
+ gameModule,
2214
+ manifestContractModule
2215
+ ] = await Promise.all([
2216
+ loadTypedBases(options.projectRoot),
2217
+ loadTypedScenarios(options.projectRoot, {}),
2218
+ loadGeneratedBaseStates(options.projectRoot),
2219
+ loadManifest(options.projectRoot),
2220
+ importTypeScriptModule(
2221
+ path2.join(options.projectRoot, "app", "game.ts")
2222
+ ),
2223
+ importTypeScriptModule(
2224
+ path2.join(options.projectRoot, "shared", "manifest-contract.ts")
2225
+ )
2226
+ ]);
2227
+ const matchingScenarios = scenarios.filter(
2228
+ (scenario2) => scenario2.definition.id === options.scenarioId
2229
+ );
2230
+ if (matchingScenarios.length !== 1) {
2231
+ throw new Error(
2232
+ matchingScenarios.length === 0 ? `Unknown scenario '${options.scenarioId}'. Available scenarios: ${scenarios.map((scenario2) => scenario2.definition.id).sort().join(", ")}` : `Scenario id '${options.scenarioId}' is defined more than once. Keep one scenario per file and one unique id per workspace.`
2233
+ );
2234
+ }
2235
+ const scenario = matchingScenarios[0];
2236
+ const basesById = new Map(bases.map((base2) => [base2.definition.id, base2]));
2237
+ validateTypedScenarioBases([scenario], basesById);
2238
+ const base = basesById.get(scenario.definition.from);
2239
+ if (!base) {
2240
+ throw new Error(
2241
+ `Missing typed base '${scenario.definition.from}' for scenario '${scenario.definition.id}'.`
2242
+ );
2243
+ }
2244
+ const generatedBase = generatedBaseStates?.[baseStateKey(base.definition.id)];
2245
+ if (!generatedBase) {
2246
+ throw new Error(
2247
+ `Missing generated base artifact for '${base.definition.id}'. Run 'dreamboard test generate' before replaying a scenario.`
2248
+ );
2249
+ }
2250
+ validateGeneratedFingerprint({
2251
+ generated: generatedBase.fingerprint,
2252
+ current: await currentFingerprint({
2253
+ projectRoot: options.projectRoot,
2254
+ base,
2255
+ basesById,
2256
+ compiledResultId: options.compiledResultId,
2257
+ gameId: options.gameId
2258
+ })
2259
+ });
2260
+ const createInitialTable = typeof manifestContractModule.createInitialTable === "function" ? manifestContractModule.createInitialTable : null;
2261
+ const resolvedBase = resolveBaseDefinition(base, basesById);
2262
+ const effectiveSetup = resolveEffectiveBaseSetup({
2263
+ manifest,
2264
+ base: base.definition,
2265
+ basesById
2266
+ });
2267
+ const shadow = new ShadowReducerRuntime(
2268
+ manifest,
2269
+ gameModule.default,
2270
+ createInitialTable,
2271
+ resolvedBase.seed,
2272
+ resolvedBase.players,
2273
+ effectiveSetup.setupProfileId,
2274
+ options.debug ?? false
2275
+ );
2276
+ if (base.definition.extends) {
2277
+ const parentArtifact = generatedBaseStates?.[baseStateKey(base.definition.extends)];
2278
+ if (!parentArtifact || typeof parentArtifact.version !== "number") {
2279
+ throw new Error(
2280
+ `Base '${base.definition.id}' extends '${base.definition.extends}', but the parent artifact is missing or stale. Run 'dreamboard test generate' first.`
2281
+ );
2282
+ }
2283
+ shadow.hydrate(parentArtifact.snapshot, parentArtifact.version);
2284
+ }
2285
+ const diagnostics = [];
2286
+ const context = await createScenarioContext({
2287
+ runner: "reducer",
2288
+ shadow,
2289
+ expect: (await loadTestingExpectApiFactory())(),
2290
+ actionPlan: {
2291
+ submitIndex: 0,
2292
+ diagnostics
2293
+ }
2294
+ });
2295
+ await context.game.start();
2296
+ await base.definition.setup({
2297
+ game: context.game,
2298
+ players: context.players,
2299
+ seat: context.seat
2300
+ });
2301
+ shadow.clearHistory();
2302
+ await scenario.definition.when(context);
2303
+ if (scenario.definition.phase && shadow.phase() !== scenario.definition.phase) {
2304
+ throw new Error(
2305
+ `Scenario '${scenario.definition.id}' expected phase '${scenario.definition.phase}' but reached '${shadow.phase()}'.`
2306
+ );
2307
+ }
2308
+ if (scenario.definition.stage && shadow.currentStage() !== scenario.definition.stage) {
2309
+ throw new Error(
2310
+ `Scenario '${scenario.definition.id}' expected stage '${scenario.definition.stage}' but reached '${shadow.currentStage() ?? "null"}'.`
2311
+ );
2312
+ }
2313
+ return {
2314
+ gameId: options.gameId,
2315
+ seed: resolvedBase.seed,
2316
+ playerCount: resolvedBase.players,
2317
+ setupProfileId: effectiveSetup.setupProfileId,
2318
+ scenarioId: scenario.definition.id,
2319
+ phase: shadow.phase(),
2320
+ stage: shadow.currentStage(),
2321
+ diagnostics
2322
+ };
2323
+ }
2324
+ async function replayActionPlanThroughBackend(options) {
2325
+ const session = await createActionPlanReplaySession(options);
2326
+ return replayActionPlanInSession({
2327
+ session,
2328
+ actions: options.actions,
2329
+ captureResponseBody: options.captureResponseBody
2330
+ });
2331
+ }
2332
+ async function createActionPlanReplaySession(options) {
2333
+ const {
2334
+ data: session,
2335
+ error: sessionError,
2336
+ response: sessionResponse
2337
+ } = await createProjectSession({
2338
+ path: { projectId: options.gameId },
2339
+ body: {
2340
+ compiledResultId: options.compiledResultId,
2341
+ seed: options.seed,
2342
+ playerCount: options.playerCount,
2343
+ autoAssignSeats: true,
2344
+ setupProfileId: options.setupProfileId ?? void 0
2345
+ }
2346
+ });
2347
+ if (!session || sessionError) {
2348
+ throw toDreamboardApiError(
2349
+ sessionError,
2350
+ sessionResponse,
2351
+ "Failed to create replay session from action plan"
2352
+ );
2353
+ }
2354
+ const { error: startError, response: startResponse } = await startGame({
2355
+ path: { sessionId: session.sessionId }
2356
+ });
2357
+ if (startError) {
2358
+ throw toDreamboardApiError(
2359
+ startError,
2360
+ startResponse,
2361
+ "Failed to start replay session from action plan"
2362
+ );
2363
+ }
2364
+ return {
2365
+ sessionId: session.sessionId,
2366
+ shortCode: session.shortCode,
2367
+ gameId: projectIdFromSessionGameSource(session.gameSource),
2368
+ seed: options.seed,
2369
+ playerCount: options.playerCount,
2370
+ setupProfileId: options.setupProfileId ?? null,
2371
+ scenarioId: options.scenarioId ?? "action-plan",
2372
+ phase: null,
2373
+ stage: null
2374
+ };
2375
+ }
2376
+ async function replayActionPlanInSession(options) {
2377
+ let version = 0;
2378
+ let actionSetVersion = "";
2379
+ const diagnostics = [];
2380
+ for (const [index, action] of options.actions.entries()) {
2381
+ if (index > 0) {
2382
+ await waitForActionPacing({
2383
+ actionIntervalMs: options.actionIntervalMs ?? 0,
2384
+ actionJitterMs: options.actionJitterMs ?? 0
2385
+ });
2386
+ }
2387
+ if (!actionSetVersion) {
2388
+ actionSetVersion = await fetchLiveActionSetVersion(
2389
+ options.session.sessionId,
2390
+ action.playerId
2391
+ );
2392
+ }
2393
+ const submittedActionSetVersion = actionSetVersion;
2394
+ const submitStartedAt = performance.now();
2395
+ const clientActionId = randomUUID2();
2396
+ const { data, error, response } = await submitGameplayAuthorityAction({
2397
+ path: {
2398
+ sessionId: options.session.sessionId,
2399
+ playerId: action.playerId,
2400
+ interactionId: action.interactionId
2401
+ },
2402
+ body: {
2403
+ expectedVersion: version,
2404
+ actionSetVersion,
2405
+ inputs: action.inputs ?? {}
2406
+ },
2407
+ headers: { "X-Dreamboard-Client-Action-Id": clientActionId }
2408
+ });
2409
+ const durationMs = performance.now() - submitStartedAt;
2410
+ diagnostics.push({
2411
+ index,
2412
+ playerId: action.playerId,
2413
+ interactionId: action.interactionId,
2414
+ inputs: action.inputs ?? {},
2415
+ expectedVersion: version,
2416
+ actionSetVersion: submittedActionSetVersion,
2417
+ responseStatus: response?.status,
2418
+ responseBody: options.captureResponseBody === false ? void 0 : JSON.stringify(error ?? data ?? null),
2419
+ accepted: data?.accepted ?? void 0,
2420
+ errorCode: data?.accepted === false ? data.errorCode ?? void 0 : error && typeof error === "object" && "detail" in error ? String(error.detail) : void 0,
2421
+ backendTiming: response?.headers?.get("server-timing") ?? response?.headers?.get("x-dreamboard-timing") ?? void 0,
2422
+ durationMs
2423
+ });
2424
+ if (error) {
2425
+ throw createSubmissionError(
2426
+ "api-error",
2427
+ void 0,
2428
+ "Failed to submit interaction from action plan"
2429
+ );
2430
+ }
2431
+ if (data?.accepted === false) {
2432
+ throw createSubmissionError(
2433
+ data.errorCode ?? void 0,
2434
+ data.message ?? void 0,
2435
+ "Interaction rejected while replaying action plan"
2436
+ );
2437
+ }
2438
+ version = typeof data?.version === "number" ? data.version : version + 1;
2439
+ actionSetVersion = data?.actionSetVersion ?? actionSetVersion;
2440
+ }
2441
+ return {
2442
+ ...options.session,
2443
+ diagnostics
2444
+ };
2445
+ }
2446
+ async function fetchLiveActionSetVersion(sessionId, playerId) {
2447
+ const { data, error, response } = await getSessionSnapshot({
2448
+ path: { sessionId },
2449
+ query: { playerId }
2450
+ });
2451
+ if (error || !data || data.type !== "gameplay") {
2452
+ throw toDreamboardApiError(
2453
+ error ?? { detail: "Gameplay snapshot was unavailable." },
2454
+ response,
2455
+ "Failed to read live gameplay action set version"
2456
+ );
2457
+ }
2458
+ return data.gameplay.actionSetVersion;
2459
+ }
2460
+ async function waitForActionPacing(options) {
2461
+ const intervalMs = Math.max(0, options.actionIntervalMs);
2462
+ const jitterMs = Math.max(0, options.actionJitterMs);
2463
+ if (intervalMs === 0 && jitterMs === 0) {
2464
+ return;
2465
+ }
2466
+ const offsetMs = jitterMs === 0 ? 0 : Math.floor((Math.random() * 2 - 1) * jitterMs);
2467
+ const delayMs = Math.max(0, intervalMs + offsetMs);
2468
+ if (delayMs === 0) {
2469
+ return;
2470
+ }
2471
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
2472
+ }
2473
+ function baseStateKey(baseId) {
2474
+ return baseId;
2475
+ }
2476
+ function fingerprintHash(fingerprint) {
2477
+ return hashContent(JSON.stringify(fingerprint));
2478
+ }
2479
+ function sortBasesForArtifacts(bases, basesById) {
2480
+ const ordered = [];
2481
+ const visiting = /* @__PURE__ */ new Set();
2482
+ const visited = /* @__PURE__ */ new Set();
2483
+ const visit = (base) => {
2484
+ if (visited.has(base.definition.id)) {
2485
+ return;
2486
+ }
2487
+ if (visiting.has(base.definition.id)) {
2488
+ throw new Error(
2489
+ `Cyclic reducer-native base inheritance detected at '${base.definition.id}'.`
2490
+ );
2491
+ }
2492
+ visiting.add(base.definition.id);
2493
+ if (base.definition.extends) {
2494
+ const parent = basesById.get(base.definition.extends);
2495
+ if (!parent) {
2496
+ throw new Error(
2497
+ `Base '${base.definition.id}' extends unknown parent '${base.definition.extends}'.`
2498
+ );
2499
+ }
2500
+ visit(parent);
2501
+ }
2502
+ visiting.delete(base.definition.id);
2503
+ visited.add(base.definition.id);
2504
+ ordered.push(base);
2505
+ };
2506
+ for (const base of bases) {
2507
+ visit(base);
2508
+ }
2509
+ return ordered;
2510
+ }
2511
+ function writeProjectionSnapshots(options) {
2512
+ const projectionDir = path2.join(
2513
+ options.projectRoot,
2514
+ "test",
2515
+ "generated",
2516
+ "bases",
2517
+ options.baseId
2518
+ );
2519
+ rmSync(projectionDir, { recursive: true, force: true });
2520
+ mkdirSync(projectionDir, { recursive: true });
2521
+ const projection = options.shadow.projectAllSeats();
2522
+ for (const [playerId, seatProjection] of Object.entries(projection.seats)) {
2523
+ const outputPath = path2.join(projectionDir, `${playerId}.projection.json`);
2524
+ writeFileSync(
2525
+ outputPath,
2526
+ `${JSON.stringify(
2527
+ {
2528
+ currentStage: projection.currentStage,
2529
+ stageSeats: projection.stageSeats,
2530
+ ...seatProjection
2531
+ },
2532
+ null,
2533
+ 2
2534
+ )}
2535
+ `,
2536
+ "utf8"
2537
+ );
2538
+ }
2539
+ }
2540
+ function validateTypedScenarioBases(scenarios, basesById) {
2541
+ const available = new Set(basesById.keys());
2542
+ const invalid = scenarios.filter((scenario) => !available.has(scenario.definition.from)).map(
2543
+ (scenario) => path2.relative(process.cwd(), scenario.filePath) + ` -> '${scenario.definition.from}'`
2544
+ );
2545
+ if (invalid.length > 0) {
2546
+ throw new Error(
2547
+ `Unknown scenario base(s): ${invalid.join(", ")}. Available bases: ${Array.from(available).sort().join(", ")}`
2548
+ );
2549
+ }
2550
+ }
2551
+ async function writeReducerNativeGeneratedFiles(options) {
2552
+ await ensureReducerNativeTestingFiles(options.projectRoot);
2553
+ const manifest = await loadManifest(options.projectRoot);
2554
+ const manifestHash = hashContent(JSON.stringify(manifest));
2555
+ const appBundleHash = hashContent(
2556
+ await bundleTypeScriptModuleText(
2557
+ path2.join(options.projectRoot, "app", "game.ts")
2558
+ )
2559
+ );
2560
+ const uiContractHash = hashContent(
2561
+ await bundleTypeScriptModuleText(
2562
+ path2.join(options.projectRoot, "shared", "generated", "ui-contract.ts"),
2563
+ { external: SDK_UI_RUNTIME_EXTERNALS }
2564
+ )
2565
+ );
2566
+ const generatedDir = path2.join(options.projectRoot, "test", "generated");
2567
+ await ensureDir(generatedDir);
2568
+ const baseStates = {};
2569
+ const [gameModule, manifestContractModule] = await Promise.all([
2570
+ importTypeScriptModule(
2571
+ path2.join(options.projectRoot, "app", "game.ts")
2572
+ ),
2573
+ importTypeScriptModule(
2574
+ path2.join(options.projectRoot, "shared", "manifest-contract.ts")
2575
+ )
2576
+ ]);
2577
+ const createInitialTable = typeof manifestContractModule.createInitialTable === "function" ? manifestContractModule.createInitialTable : null;
2578
+ const contractFingerprintValue = contractFingerprint(
2579
+ gameModule.default
2580
+ ).value;
2581
+ const basesById = new Map(
2582
+ options.bases.map((base) => [base.definition.id, base])
2583
+ );
2584
+ for (const base of sortBasesForArtifacts(options.bases, basesById)) {
2585
+ const resolvedBase = resolveBaseDefinition(base, basesById);
2586
+ const effectiveSetup = resolveEffectiveBaseSetup({
2587
+ manifest,
2588
+ base: base.definition,
2589
+ basesById
2590
+ });
2591
+ const shadow = new ShadowReducerRuntime(
2592
+ manifest,
2593
+ gameModule.default,
2594
+ createInitialTable,
2595
+ resolvedBase.seed,
2596
+ resolvedBase.players,
2597
+ effectiveSetup.setupProfileId,
2598
+ options.debug ?? false
2599
+ );
2600
+ let parentFingerprintHash = null;
2601
+ if (base.definition.extends) {
2602
+ const parentArtifact = baseStates[baseStateKey(base.definition.extends)];
2603
+ if (!parentArtifact) {
2604
+ throw new Error(
2605
+ `Base '${base.definition.id}' extends '${base.definition.extends}', but the parent artifact does not exist yet.`
2606
+ );
2607
+ }
2608
+ if (parentArtifact.fingerprint.seed !== resolvedBase.seed) {
2609
+ throw new Error(
2610
+ `Base '${base.definition.id}' extends '${base.definition.extends}' but changes seed from ${parentArtifact.fingerprint.seed} to ${resolvedBase.seed}.`
2611
+ );
2612
+ }
2613
+ if (parentArtifact.fingerprint.players !== resolvedBase.players) {
2614
+ throw new Error(
2615
+ `Base '${base.definition.id}' extends '${base.definition.extends}' but changes players from ${parentArtifact.fingerprint.players} to ${resolvedBase.players}.`
2616
+ );
2617
+ }
2618
+ if ((parentArtifact.fingerprint.setupProfileId ?? null) !== (effectiveSetup.setupProfileId ?? null)) {
2619
+ throw new Error(
2620
+ `Base '${base.definition.id}' extends '${base.definition.extends}' but changes setup profile from ${parentArtifact.fingerprint.setupProfileId ?? "none"} to ${effectiveSetup.setupProfileId ?? "none"}.`
2621
+ );
2622
+ }
2623
+ parentFingerprintHash = fingerprintHash(parentArtifact.fingerprint);
2624
+ shadow.hydrate(parentArtifact.snapshot, parentArtifact.version);
2625
+ }
2626
+ const context = await createScenarioContext({
2627
+ runner: "reducer",
2628
+ shadow,
2629
+ expect: (await loadTestingExpectApiFactory())()
2630
+ });
2631
+ await context.game.start();
2632
+ await base.definition.setup({
2633
+ game: context.game,
2634
+ players: context.players,
2635
+ seat: context.seat
2636
+ });
2637
+ baseStates[baseStateKey(base.definition.id)] = {
2638
+ key: baseStateKey(base.definition.id),
2639
+ base: base.definition.id,
2640
+ snapshot: shadow.rawState(),
2641
+ version: shadow.currentVersion(),
2642
+ fingerprint: {
2643
+ base: base.definition.id,
2644
+ seed: resolvedBase.seed,
2645
+ players: resolvedBase.players,
2646
+ baseBundleHash: base.bundleHash,
2647
+ manifestHash,
2648
+ appBundleHash,
2649
+ uiContractHash,
2650
+ setupProfileId: effectiveSetup.setupProfileId,
2651
+ compiledResultId: options.compiledResultId,
2652
+ gameId: options.gameId,
2653
+ parentBaseId: base.definition.extends ?? null,
2654
+ parentFingerprintHash,
2655
+ contractFingerprint: contractFingerprintValue
2656
+ }
2657
+ };
2658
+ writeProjectionSnapshots({
2659
+ projectRoot: options.projectRoot,
2660
+ baseId: base.definition.id,
2661
+ shadow
2662
+ });
2663
+ }
2664
+ const header = "// Generated by dreamboard test generate. Do not edit by hand.\n";
2665
+ await writeTextFile(
2666
+ path2.join(generatedDir, "base-states.generated.ts"),
2667
+ `${header}export const BASE_STATES = ${JSON.stringify(baseStates, null, 2)} as const;
2668
+ export const BASE_STATES_CONTRACT_FINGERPRINT = ${JSON.stringify(contractFingerprintValue)};
2669
+ `
2670
+ );
2671
+ await writeTextFile(
2672
+ path2.join(generatedDir, "base-states.generated.d.ts"),
2673
+ `${header}export declare const BASE_STATES: Record<string, unknown>;
2674
+ export declare const BASE_STATES_CONTRACT_FINGERPRINT: string | undefined;
2675
+ `
2676
+ );
2677
+ await writeTextFile(
2678
+ path2.join(generatedDir, "scenario-manifest.generated.ts"),
2679
+ `${header}export const SCENARIO_MANIFEST = ${JSON.stringify(
2680
+ options.scenarios.map((scenario) => ({
2681
+ id: scenario.definition.id,
2682
+ filePath: path2.relative(generatedDir, scenario.filePath),
2683
+ base: scenario.definition.from,
2684
+ runners: normalizeScenarioRunners(scenario.definition.runners)
2685
+ })),
2686
+ null,
2687
+ 2
2688
+ )} as const;
2689
+ `
2690
+ );
2691
+ await writeJsonFile(path2.join(generatedDir, ".generation-meta.json"), {
2692
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
2693
+ contractFingerprint: contractFingerprintValue,
2694
+ scenarioCount: options.scenarios.length,
2695
+ baseStateCount: options.bases.length
2696
+ });
2697
+ }
2698
+ async function loadGeneratedBaseStates(projectRoot) {
2699
+ const filePath = path2.join(
2700
+ projectRoot,
2701
+ "test",
2702
+ "generated",
2703
+ "base-states.generated.ts"
2704
+ );
2705
+ if (!await exists(filePath)) {
2706
+ return null;
2707
+ }
2708
+ const module = await importTypeScriptModule(filePath);
2709
+ return module.BASE_STATES ?? null;
2710
+ }
2711
+ async function loadGeneratedScenarioManifest(projectRoot) {
2712
+ const filePath = path2.join(
2713
+ projectRoot,
2714
+ "test",
2715
+ "generated",
2716
+ "scenario-manifest.generated.ts"
2717
+ );
2718
+ if (!await exists(filePath)) {
2719
+ return [];
2720
+ }
2721
+ const module = await importTypeScriptModule(filePath);
2722
+ const entries = module.SCENARIO_MANIFEST;
2723
+ if (!Array.isArray(entries)) {
2724
+ return [];
2725
+ }
2726
+ return entries.flatMap((entry) => {
2727
+ if (entry && typeof entry === "object" && "id" in entry && "base" in entry && typeof entry.id === "string" && typeof entry.base === "string") {
2728
+ return [{ id: entry.id, base: entry.base }];
2729
+ }
2730
+ return [];
2731
+ });
2732
+ }
2733
+ function validateGeneratedFingerprint(options) {
2734
+ const mismatches = [];
2735
+ if (options.generated.contractFingerprint && options.current.contractFingerprint && options.generated.contractFingerprint !== options.current.contractFingerprint) {
2736
+ const error = new Error(
2737
+ `Base states were generated for contract ${options.generated.contractFingerprint} but the current contract is ${options.current.contractFingerprint}. Remedy: run \`dreamboard test generate\`, then re-run the tests.`
2738
+ );
2739
+ error.code = STALE_CONTRACT_ARTIFACT_CODE;
2740
+ throw error;
2741
+ }
2742
+ if (options.generated.baseBundleHash !== options.current.baseBundleHash) {
2743
+ mismatches.push("base module changed");
2744
+ }
2745
+ if (options.generated.manifestHash !== options.current.manifestHash) {
2746
+ mismatches.push("manifest changed");
2747
+ }
2748
+ if (options.generated.appBundleHash !== options.current.appBundleHash) {
2749
+ mismatches.push("game reducer bundle changed");
2750
+ }
2751
+ if (options.generated.uiContractHash !== options.current.uiContractHash) {
2752
+ mismatches.push("ui contract changed");
2753
+ }
2754
+ if (options.generated.seed !== options.current.seed || options.generated.players !== options.current.players) {
2755
+ mismatches.push("base seed/player count changed");
2756
+ }
2757
+ if ((options.generated.setupProfileId ?? null) !== (options.current.setupProfileId ?? null)) {
2758
+ mismatches.push("base setup profile changed");
2759
+ }
2760
+ if ((options.generated.parentBaseId ?? null) !== (options.current.parentBaseId ?? null)) {
2761
+ mismatches.push("base parent changed");
2762
+ }
2763
+ if ((options.generated.parentFingerprintHash ?? null) !== (options.current.parentFingerprintHash ?? null)) {
2764
+ mismatches.push("parent base artifact changed");
2765
+ }
2766
+ if (mismatches.length > 0) {
2767
+ throw new Error(
2768
+ `${mismatches.join("; ")}. Run 'dreamboard test generate' to refresh reducer-native base artifacts.`
2769
+ );
2770
+ }
2771
+ }
2772
+ async function currentFingerprint(options) {
2773
+ const manifest = await loadManifest(options.projectRoot);
2774
+ const gameModule = await importTypeScriptModule(
2775
+ path2.join(options.projectRoot, "app", "game.ts")
2776
+ );
2777
+ const contractFingerprintValue = contractFingerprint(
2778
+ gameModule.default
2779
+ ).value;
2780
+ const resolvedBase = resolveBaseDefinition(
2781
+ options.base,
2782
+ options.basesById ?? /* @__PURE__ */ new Map([[options.base.definition.id, options.base]])
2783
+ );
2784
+ const effectiveSetup = resolveEffectiveBaseSetup({
2785
+ manifest,
2786
+ base: options.base.definition,
2787
+ basesById: options.basesById
2788
+ });
2789
+ let parentFingerprintHash = null;
2790
+ if (options.base.definition.extends) {
2791
+ const parentBase = options.basesById?.get(options.base.definition.extends);
2792
+ if (!parentBase) {
2793
+ throw new Error(
2794
+ `Base '${options.base.definition.id}' extends unknown parent '${options.base.definition.extends}'.`
2795
+ );
2796
+ }
2797
+ parentFingerprintHash = fingerprintHash(
2798
+ await currentFingerprint({
2799
+ projectRoot: options.projectRoot,
2800
+ base: parentBase,
2801
+ basesById: options.basesById,
2802
+ compiledResultId: options.compiledResultId,
2803
+ gameId: options.gameId
2804
+ })
2805
+ );
2806
+ }
2807
+ return {
2808
+ base: options.base.definition.id,
2809
+ seed: resolvedBase.seed,
2810
+ players: resolvedBase.players,
2811
+ baseBundleHash: options.base.bundleHash,
2812
+ manifestHash: hashContent(JSON.stringify(manifest)),
2813
+ appBundleHash: hashContent(
2814
+ await bundleTypeScriptModuleText(
2815
+ path2.join(options.projectRoot, "app", "game.ts")
2816
+ )
2817
+ ),
2818
+ uiContractHash: hashContent(
2819
+ await bundleTypeScriptModuleText(
2820
+ path2.join(options.projectRoot, "shared", "generated", "ui-contract.ts"),
2821
+ { external: SDK_UI_RUNTIME_EXTERNALS }
2822
+ )
2823
+ ),
2824
+ setupProfileId: effectiveSetup.setupProfileId,
2825
+ compiledResultId: options.compiledResultId,
2826
+ gameId: options.gameId,
2827
+ parentBaseId: options.base.definition.extends ?? null,
2828
+ parentFingerprintHash,
2829
+ contractFingerprint: contractFingerprintValue
2830
+ };
2831
+ }
2832
+ async function generateReducerNativeArtifacts(options) {
2833
+ await ensureReducerNativeTestingFiles(options.projectRoot);
2834
+ const [bases, scenarios] = await Promise.all([
2835
+ loadTypedBases(options.projectRoot),
2836
+ loadTypedScenarios(options.projectRoot, {
2837
+ scenarioPath: options.scenarioPath
2838
+ })
2839
+ ]);
2840
+ const basesById = new Map(bases.map((base) => [base.definition.id, base]));
2841
+ validateTypedScenarioBases(scenarios, basesById);
2842
+ await writeReducerNativeGeneratedFiles({
2843
+ projectRoot: options.projectRoot,
2844
+ bases,
2845
+ scenarios,
2846
+ compiledResultId: options.compiledResultId,
2847
+ gameId: options.gameId,
2848
+ debug: options.debug
2849
+ });
2850
+ return { bases, scenarios };
2851
+ }
2852
+ async function runReducerNativeScenarios(options) {
2853
+ await ensureReducerNativeTestingFiles(options.projectRoot);
2854
+ const [
2855
+ bases,
2856
+ scenarios,
2857
+ initialGeneratedBaseStates,
2858
+ manifest,
2859
+ gameModule,
2860
+ manifestContractModule
2861
+ ] = await Promise.all([
2862
+ loadTypedBases(options.projectRoot),
2863
+ loadTypedScenarios(options.projectRoot, {
2864
+ scenarioPath: options.scenarioPath
2865
+ }),
2866
+ loadGeneratedBaseStates(options.projectRoot),
2867
+ loadManifest(options.projectRoot),
2868
+ importTypeScriptModule(
2869
+ path2.join(options.projectRoot, "app", "game.ts")
2870
+ ),
2871
+ importTypeScriptModule(
2872
+ path2.join(options.projectRoot, "shared", "manifest-contract.ts")
2873
+ )
2874
+ ]);
2875
+ let generatedBaseStates = initialGeneratedBaseStates;
2876
+ const createInitialTable = typeof manifestContractModule.createInitialTable === "function" ? manifestContractModule.createInitialTable : null;
2877
+ const basesById = new Map(bases.map((base) => [base.definition.id, base]));
2878
+ validateTypedScenarioBases(scenarios, basesById);
2879
+ if (options.updateSnapshots) {
2880
+ await writeReducerNativeGeneratedFiles({
2881
+ projectRoot: options.projectRoot,
2882
+ bases,
2883
+ scenarios,
2884
+ compiledResultId: options.compiledResultId,
2885
+ gameId: options.gameId,
2886
+ debug: options.debug
2887
+ });
2888
+ generatedBaseStates = await loadGeneratedBaseStates(options.projectRoot);
2889
+ }
2890
+ const runnerScenarios = scenarios.filter(
2891
+ (scenario) => normalizeScenarioRunners(scenario.definition.runners).includes(
2892
+ options.runner
2893
+ )
2894
+ );
2895
+ let browser = null;
2896
+ let page = null;
2897
+ let browserBridge = null;
2898
+ let browserDriver = null;
2899
+ if (options.runner === "browser") {
2900
+ const webBaseUrl = options.webBaseUrl ?? options.projectConfig.webBaseUrl;
2901
+ if (!webBaseUrl) {
2902
+ throw new Error(
2903
+ "Browser runner requires a local webBaseUrl. Start the local web stack and configure the project for env local."
2904
+ );
2905
+ }
2906
+ browserDriver = await loadBrowserDriver(options.projectRoot);
2907
+ const opened = await openBrowserPage(
2908
+ `${webBaseUrl}/_dev`,
2909
+ options.resolvedConfig
2910
+ );
2911
+ browser = opened.browser;
2912
+ page = opened.page;
2913
+ }
2914
+ try {
2915
+ let passed = 0;
2916
+ let failed = 0;
2917
+ const results = [];
2918
+ for (const scenario of runnerScenarios) {
2919
+ const base = basesById.get(scenario.definition.from);
2920
+ if (!base) {
2921
+ throw new Error(
2922
+ `Missing typed base '${scenario.definition.from}' for scenario '${scenario.definition.id}'.`
2923
+ );
2924
+ }
2925
+ if (generatedBaseStates?.[baseStateKey(base.definition.id)]) {
2926
+ validateGeneratedFingerprint({
2927
+ generated: generatedBaseStates[baseStateKey(base.definition.id)].fingerprint,
2928
+ current: await currentFingerprint({
2929
+ projectRoot: options.projectRoot,
2930
+ base,
2931
+ basesById,
2932
+ compiledResultId: options.compiledResultId,
2933
+ gameId: options.gameId
2934
+ })
2935
+ });
2936
+ } else if (options.runner !== "reducer") {
2937
+ throw new Error(
2938
+ "Missing reducer-native generated base artifacts. Run 'dreamboard test generate' first."
2939
+ );
2940
+ }
2941
+ try {
2942
+ const resolvedBase = resolveBaseDefinition(base, basesById);
2943
+ const effectiveSetup = resolveEffectiveBaseSetup({
2944
+ manifest,
2945
+ base: base.definition,
2946
+ basesById
2947
+ });
2948
+ const shadow = new ShadowReducerRuntime(
2949
+ manifest,
2950
+ gameModule.default,
2951
+ createInitialTable,
2952
+ resolvedBase.seed,
2953
+ resolvedBase.players,
2954
+ effectiveSetup.setupProfileId,
2955
+ options.debug ?? false
2956
+ );
2957
+ if (base.definition.extends) {
2958
+ const parentArtifact = generatedBaseStates?.[baseStateKey(base.definition.extends)];
2959
+ if (!parentArtifact) {
2960
+ throw new Error(
2961
+ `Base '${base.definition.id}' extends '${base.definition.extends}', but the parent artifact is missing. Run 'dreamboard test generate' first.`
2962
+ );
2963
+ }
2964
+ shadow.hydrate(parentArtifact.snapshot, parentArtifact.version);
2965
+ }
2966
+ let remote;
2967
+ if (options.runner === "remote") {
2968
+ const compiledResultId = options.compiledResultId ?? options.projectConfig.compile?.latestSuccessful?.resultId;
2969
+ if (!compiledResultId) {
2970
+ throw new Error(
2971
+ "Remote runner requires a compiled result. Compile the workspace first."
2972
+ );
2973
+ }
2974
+ const {
2975
+ data: session,
2976
+ error: sessionError,
2977
+ response: sessionResponse
2978
+ } = await createProjectSession({
2979
+ path: { projectId: options.projectConfig.projectId },
2980
+ body: {
2981
+ compiledResultId,
2982
+ seed: resolvedBase.seed,
2983
+ playerCount: resolvedBase.players,
2984
+ autoAssignSeats: true,
2985
+ setupProfileId: effectiveSetup.setupProfileId ?? void 0
2986
+ }
2987
+ });
2988
+ if (!session || sessionError) {
2989
+ throw toDreamboardApiError(
2990
+ sessionError,
2991
+ sessionResponse,
2992
+ "Failed to create remote-runner session."
2993
+ );
2994
+ }
2995
+ const tracker = new RemoteGameplayTracker(
2996
+ session.sessionId,
2997
+ "player-1"
2998
+ );
2999
+ const { error: startError, response: startResponse } = await startGame({
3000
+ path: { sessionId: session.sessionId }
3001
+ });
3002
+ if (startError) {
3003
+ throw toDreamboardApiError(
3004
+ startError,
3005
+ startResponse,
3006
+ "Failed to start remote-runner session."
3007
+ );
3008
+ }
3009
+ await tracker.bootstrap();
3010
+ remote = {
3011
+ sessionId: session.sessionId,
3012
+ tracker
3013
+ };
3014
+ }
3015
+ if (options.runner === "browser" && page) {
3016
+ const compiledResultId = options.compiledResultId ?? options.projectConfig.compile?.latestSuccessful?.resultId;
3017
+ if (!compiledResultId) {
3018
+ throw new Error(
3019
+ "Browser runner requires a compiled result. Compile the workspace first."
3020
+ );
3021
+ }
3022
+ const {
3023
+ data: session,
3024
+ error: sessionError,
3025
+ response: sessionResponse
3026
+ } = await createProjectSession({
3027
+ path: { projectId: options.projectConfig.projectId },
3028
+ body: {
3029
+ compiledResultId,
3030
+ seed: resolvedBase.seed,
3031
+ playerCount: resolvedBase.players,
3032
+ autoAssignSeats: true,
3033
+ setupProfileId: effectiveSetup.setupProfileId ?? void 0
3034
+ }
3035
+ });
3036
+ if (!session || sessionError) {
3037
+ throw toDreamboardApiError(
3038
+ sessionError,
3039
+ sessionResponse,
3040
+ "Failed to create browser-runner session."
3041
+ );
3042
+ }
3043
+ const { error: startError, data: started } = await startGame({
3044
+ path: { sessionId: session.sessionId }
3045
+ });
3046
+ if (startError || !started) {
3047
+ throw new Error("Failed to start browser-runner session.");
3048
+ }
3049
+ await page.goto(
3050
+ `${options.webBaseUrl ?? options.projectConfig.webBaseUrl}/_dev/play/${started.context.shortCode}`,
3051
+ { waitUntil: "domcontentloaded" }
3052
+ );
3053
+ await waitForGameReady(page);
3054
+ browserBridge = await createBrowserBridgeClient(page);
3055
+ await browserDriver?.onReady?.(browserBridge);
3056
+ }
3057
+ const context = await createScenarioContext({
3058
+ runner: options.runner,
3059
+ shadow,
3060
+ expect: (await loadTestingExpectApiFactory())({
3061
+ matchSnapshot: createScenarioSnapshotMatcher({
3062
+ projectRoot: options.projectRoot,
3063
+ scenarioId: scenario.definition.id,
3064
+ updateSnapshots: options.updateSnapshots ?? false
3065
+ })
3066
+ }),
3067
+ remote,
3068
+ browser: options.runner === "browser" && browserBridge ? {
3069
+ bridge: browserBridge,
3070
+ driver: browserDriver
3071
+ } : void 0
3072
+ });
3073
+ await context.game.start();
3074
+ await base.definition.setup({
3075
+ game: context.game,
3076
+ players: context.players,
3077
+ seat: context.seat
3078
+ });
3079
+ shadow.clearHistory();
3080
+ await scenario.definition.when(context);
3081
+ if (scenario.definition.phase && shadow.phase() !== scenario.definition.phase) {
3082
+ throw new Error(
3083
+ `Scenario '${scenario.definition.id}' expected phase '${scenario.definition.phase}' but reached '${shadow.phase()}'.`
3084
+ );
3085
+ }
3086
+ if (scenario.definition.stage && shadow.currentStage() !== scenario.definition.stage) {
3087
+ throw new Error(
3088
+ `Scenario '${scenario.definition.id}' expected stage '${scenario.definition.stage}' but reached '${shadow.currentStage() ?? "null"}'.`
3089
+ );
3090
+ }
3091
+ await scenario.definition.then(context);
3092
+ passed += 1;
3093
+ results.push({
3094
+ id: scenario.definition.id,
3095
+ success: true
3096
+ });
3097
+ } catch (error) {
3098
+ failed += 1;
3099
+ results.push({
3100
+ id: scenario.definition.id,
3101
+ success: false,
3102
+ errorCode: isStaleContractArtifactError(error) ? STALE_CONTRACT_ARTIFACT_CODE : void 0,
3103
+ error: error instanceof Error ? formatScenarioErrorForDisplay({
3104
+ error,
3105
+ projectRoot: options.projectRoot,
3106
+ scenarioFilePath: scenario.filePath
3107
+ }) : `Scenario '${scenario.definition.id}' failed.`
3108
+ });
3109
+ }
3110
+ }
3111
+ return { passed, failed, results };
3112
+ } finally {
3113
+ if (browser) {
3114
+ await browser.close();
3115
+ }
3116
+ }
3117
+ }
3118
+
3119
+ export {
3120
+ formatScenarioErrorForDisplay,
3121
+ isReducerNativeTestingWorkspace,
3122
+ ensureReducerNativeTestingFiles,
3123
+ loadTypedBases,
3124
+ loadTypedScenarios,
3125
+ assertDispatchResultWireContract,
3126
+ createSessionFromScenario,
3127
+ materializeScenarioReducerState,
3128
+ replayScenarioThroughBackend,
3129
+ createScenarioActionPlan,
3130
+ replayActionPlanThroughBackend,
3131
+ createActionPlanReplaySession,
3132
+ replayActionPlanInSession,
3133
+ writeReducerNativeGeneratedFiles,
3134
+ generateReducerNativeArtifacts,
3135
+ runReducerNativeScenarios
3136
+ };
3137
+ //# sourceMappingURL=chunk-ON62IGWK.mjs.map