@dreamboard-games/sdk 0.4.0-alpha.6 → 0.4.0-alpha.8

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 (61) hide show
  1. package/REFERENCE.md +561 -59
  2. package/dist/authoring/index.d.ts +5 -4
  3. package/dist/authoring/index.js +4 -4
  4. package/dist/authoring/index.js.map +1 -1
  5. package/dist/{chunk-52DH7VQF.js → chunk-2TAKLJJD.js} +5 -2
  6. package/dist/{chunk-52DH7VQF.js.map → chunk-2TAKLJJD.js.map} +1 -1
  7. package/dist/{chunk-TGMXBRKY.js → chunk-3BLN5POZ.js} +5 -3
  8. package/dist/chunk-3BLN5POZ.js.map +1 -0
  9. package/dist/{chunk-OV6JC2BM.js → chunk-44N3T2A5.js} +19 -15
  10. package/dist/chunk-44N3T2A5.js.map +1 -0
  11. package/dist/chunk-5CQIJHGF.js +663 -0
  12. package/dist/chunk-5CQIJHGF.js.map +1 -0
  13. package/dist/chunk-IYX5OO2K.js +565 -0
  14. package/dist/chunk-IYX5OO2K.js.map +1 -0
  15. package/dist/{chunk-SOBNI363.js → chunk-VTK4TIXC.js} +11 -6
  16. package/dist/{chunk-SOBNI363.js.map → chunk-VTK4TIXC.js.map} +1 -1
  17. package/dist/{chunk-HK3WN4U7.js → chunk-VTNEWMWJ.js} +2 -2
  18. package/dist/{chunk-HK3WN4U7.js.map → chunk-VTNEWMWJ.js.map} +1 -1
  19. package/dist/{chunk-PAPIAMYX.js → chunk-XV27IQHA.js} +6 -4
  20. package/dist/{chunk-PAPIAMYX.js.map → chunk-XV27IQHA.js.map} +1 -1
  21. package/dist/{chunk-DDST4U2P.js → chunk-YGQTL63W.js} +2 -2
  22. package/dist/chunk-ZYONCTZ2.js +564 -0
  23. package/dist/chunk-ZYONCTZ2.js.map +1 -0
  24. package/dist/codegen.js +1 -1
  25. package/dist/{stale-contract-artifact-error-BelRiIDR.d.ts → diagnostics-1BWjRo6-.d.ts} +1 -18
  26. package/dist/{index.d-DMASeF6M.d.ts → index.d-C0PKOVbh.d.ts} +25 -4
  27. package/dist/index.js +2 -2
  28. package/dist/package-set.d.ts +2 -2
  29. package/dist/package-set.js +2 -2
  30. package/dist/{protocol-835hEGne.d.ts → protocol-nS2ojsGr.d.ts} +5 -0
  31. package/dist/reducer/advanced.d.ts +2 -2
  32. package/dist/reducer-contract.d.ts +1 -1
  33. package/dist/reducer-contract.js +2 -2
  34. package/dist/reducer.d.ts +26 -22
  35. package/dist/reducer.js +136 -89
  36. package/dist/reducer.js.map +1 -1
  37. package/dist/reference-games/index.d.ts +223 -0
  38. package/dist/reference-games/index.js +230 -0
  39. package/dist/reference-games/index.js.map +1 -0
  40. package/dist/runtime/primitives.js +4 -3
  41. package/dist/runtime/workspace-contract.js +5 -4
  42. package/dist/runtime.d.ts +3 -3
  43. package/dist/runtime.js +15 -205
  44. package/dist/runtime.js.map +1 -1
  45. package/dist/stale-contract-artifact-error-C5AaZPJ8.d.ts +18 -0
  46. package/dist/testing-runtime.d.ts +42 -0
  47. package/dist/testing-runtime.js +153 -0
  48. package/dist/testing-runtime.js.map +1 -0
  49. package/dist/testing.d.ts +8 -194
  50. package/dist/testing.js +22 -640
  51. package/dist/testing.js.map +1 -1
  52. package/dist/types-B_3pObUV.d.ts +192 -0
  53. package/dist/{types-aI4V9CWe.d.ts → types-CTeN_QAk.d.ts} +1 -1
  54. package/dist/types.d.ts +4 -0
  55. package/dist/{views-32rSsfeU.d.ts → views-Qx-EHBP8.d.ts} +27 -10
  56. package/package.json +13 -3
  57. package/dist/chunk-OV6JC2BM.js.map +0 -1
  58. package/dist/chunk-TGMXBRKY.js.map +0 -1
  59. package/dist/chunk-UUQNOGZV.js +0 -1266
  60. package/dist/chunk-UUQNOGZV.js.map +0 -1
  61. /package/dist/{chunk-DDST4U2P.js.map → chunk-YGQTL63W.js.map} +0 -0
package/dist/testing.js CHANGED
@@ -1,11 +1,24 @@
1
+ import {
2
+ PluginRuntimeBoundary,
3
+ createPluginRuntimeClient
4
+ } from "./chunk-XV27IQHA.js";
5
+ import "./chunk-ZYONCTZ2.js";
6
+ import {
7
+ BROWSER_INTERACTION_ACTUATOR_KINDS,
8
+ DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_VERSION,
9
+ browserInteractionEffectSchema
10
+ } from "./chunk-WHR5UW3F.js";
11
+ import {
12
+ RuntimeJsonSchema
13
+ } from "./chunk-PF7L4BMG.js";
14
+ import {
15
+ createExpectApi,
16
+ createTestRuntime
17
+ } from "./chunk-5CQIJHGF.js";
1
18
  import {
2
19
  StaleContractArtifactError,
3
20
  isStaleContractArtifactError
4
21
  } from "./chunk-3OZMHZK3.js";
5
- import {
6
- PluginRuntimeBoundary,
7
- createPluginRuntimeClient
8
- } from "./chunk-PAPIAMYX.js";
9
22
  import {
10
23
  DREAMBOARD_PLUGIN_PROTOCOL,
11
24
  DREAMBOARD_PLUGIN_PROTOCOL_VERSION,
@@ -14,15 +27,7 @@ import {
14
27
  PluginProtocolTapeSchema,
15
28
  computePluginActionSetVersion,
16
29
  materializePluginGameplayFrame
17
- } from "./chunk-UUQNOGZV.js";
18
- import {
19
- BROWSER_INTERACTION_ACTUATOR_KINDS,
20
- DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_VERSION,
21
- browserInteractionEffectSchema
22
- } from "./chunk-WHR5UW3F.js";
23
- import {
24
- RuntimeJsonSchema
25
- } from "./chunk-PF7L4BMG.js";
30
+ } from "./chunk-IYX5OO2K.js";
26
31
  import "./chunk-PZ5AY32C.js";
27
32
 
28
33
  // src/testing/definitions.ts
@@ -33,629 +38,6 @@ function defineScenario(definition) {
33
38
  return definition;
34
39
  }
35
40
 
36
- // src/testing/create-expect-api.ts
37
- import { isDeepStrictEqual } from "util";
38
- function isRecord(value) {
39
- return typeof value === "object" && value !== null && !Array.isArray(value);
40
- }
41
- function matchPartial(actual, expected, path = "value") {
42
- if (!isRecord(expected)) {
43
- return isDeepStrictEqual(actual, expected) ? null : `${path} does not match`;
44
- }
45
- if (!isRecord(actual)) {
46
- return `${path} is not an object`;
47
- }
48
- for (const [key, expectedValue] of Object.entries(expected)) {
49
- const actualValue = actual[key];
50
- if (!(key in actual)) {
51
- return `${path}.${key} is missing`;
52
- }
53
- const mismatch2 = matchPartial(actualValue, expectedValue, `${path}.${key}`);
54
- if (mismatch2) {
55
- return mismatch2;
56
- }
57
- }
58
- return null;
59
- }
60
- function asDescriptorList(actual) {
61
- if (!Array.isArray(actual)) {
62
- throw new Error("Expected interaction descriptor array.");
63
- }
64
- return actual;
65
- }
66
- function findInteraction(descriptors, interactionId) {
67
- return descriptors.find(
68
- (descriptor) => descriptor.interactionId === interactionId
69
- ) ?? null;
70
- }
71
- function assertDescriptorMatches(descriptor, opts) {
72
- if (!opts) {
73
- return;
74
- }
75
- const mismatch2 = matchPartial(descriptor, opts, "interaction");
76
- if (mismatch2) {
77
- throw new Error(mismatch2);
78
- }
79
- }
80
- function createSubmissionErrorMatcher(actual, expected, options) {
81
- const resolvePromise = () => {
82
- if (typeof actual === "function") {
83
- return Promise.resolve().then(() => actual());
84
- }
85
- return Promise.resolve(actual);
86
- };
87
- return resolvePromise().then(() => {
88
- throw new Error("Expected promise to reject.");
89
- }).catch((error) => {
90
- if (!(error instanceof Error)) {
91
- throw new Error("Expected rejection to be an Error.");
92
- }
93
- if (expected.errorCode !== void 0 && error.errorCode !== expected.errorCode) {
94
- throw new Error(
95
- `Expected rejection errorCode '${expected.errorCode}', received '${error.errorCode ?? "undefined"}'.${formatLastDiagnosticRejection(options)}`
96
- );
97
- }
98
- if (typeof expected.message === "string" && error.message !== expected.message) {
99
- throw new Error(
100
- `Expected rejection message '${expected.message}', received '${error.message}'.${formatLastDiagnosticRejection(
101
- options
102
- )}`
103
- );
104
- }
105
- if (expected.message instanceof RegExp && !expected.message.test(error.message)) {
106
- throw new Error(
107
- `Expected rejection message '${error.message}' to match ${String(
108
- expected.message
109
- )}.${formatLastDiagnosticRejection(options)}`
110
- );
111
- }
112
- });
113
- }
114
- function formatLastDiagnosticRejection(options) {
115
- const rejection = options.lastDiagnosticRejection?.();
116
- if (!rejection) return "";
117
- const rule = rejection.ruleId ? ` ruleId=${rejection.ruleId}` : "";
118
- const message = rejection.message ? ` message=${rejection.message}` : "";
119
- return `
120
- last rejection: errorCode=${rejection.errorCode}${rule}${message}`;
121
- }
122
- function assertLength(actual, expected) {
123
- if (actual === null || actual === void 0 || typeof actual.length !== "number") {
124
- throw new Error("toHaveLength expects a value with a numeric length.");
125
- }
126
- const length = actual.length;
127
- if (length !== expected) {
128
- throw new Error(`Expected length ${expected}, received ${length}.`);
129
- }
130
- }
131
- function formatInteractionExplanation(explanation) {
132
- if (!explanation) {
133
- return "";
134
- }
135
- const lines = [
136
- `availability: ${explanation.availability}`,
137
- ...explanation.rules.map((rule) => {
138
- const code = rule.errorCode ? ` (${rule.errorCode})` : "";
139
- const message = rule.message ? `: ${rule.message}` : "";
140
- return `rule ${rule.ruleId} ${rule.outcome}${code}${message}`;
141
- }),
142
- ...explanation.inputs.map(
143
- (input) => `input ${input.key} eligibleCount=${input.eligibleCount}`
144
- )
145
- ];
146
- return `
147
- ${lines.join("\n")}`;
148
- }
149
- function assertThrown(actual, predicate) {
150
- if (typeof actual !== "function") {
151
- throw new Error("toThrow expects a function.");
152
- }
153
- try {
154
- actual();
155
- } catch (error) {
156
- if (!(error instanceof Error)) {
157
- throw new Error("Thrown value is not an Error.");
158
- }
159
- if (predicate === void 0) {
160
- return;
161
- }
162
- if (typeof predicate === "string" && error.message !== predicate) {
163
- throw new Error(
164
- `Expected thrown message '${predicate}', received '${error.message}'.`
165
- );
166
- }
167
- if (predicate instanceof RegExp && !predicate.test(error.message)) {
168
- throw new Error(
169
- `Expected thrown message '${error.message}' to match ${String(predicate)}.`
170
- );
171
- }
172
- if (typeof predicate === "function" && !predicate(error)) {
173
- throw new Error("Thrown error did not satisfy predicate.");
174
- }
175
- return;
176
- }
177
- throw new Error("Expected function to throw.");
178
- }
179
- function createExpectApi(options = {}) {
180
- const buildMatchers = (actual) => ({
181
- toBe: (expected) => {
182
- if (actual !== expected) {
183
- throw new Error(
184
- `Expected '${String(actual)}' to be '${String(expected)}'.`
185
- );
186
- }
187
- },
188
- toEqual: (expected) => {
189
- if (!isDeepStrictEqual(actual, expected)) {
190
- throw new Error("Expected values to be deeply equal.");
191
- }
192
- },
193
- toMatchObject: (expected) => {
194
- const mismatch2 = matchPartial(actual, expected);
195
- if (mismatch2) {
196
- throw new Error(mismatch2);
197
- }
198
- },
199
- toBeDefined: () => {
200
- if (actual === void 0) {
201
- throw new Error("Expected value to be defined.");
202
- }
203
- },
204
- toBeUndefined: () => {
205
- if (actual !== void 0) {
206
- throw new Error(
207
- `Expected value to be undefined, but received '${String(actual)}'.`
208
- );
209
- }
210
- },
211
- toBeNull: () => {
212
- if (actual !== null) {
213
- throw new Error(
214
- `Expected value to be null, but received '${String(actual)}'.`
215
- );
216
- }
217
- },
218
- toContain: (expected) => {
219
- if (Array.isArray(actual)) {
220
- if (!actual.includes(expected)) {
221
- throw new Error("Expected array to contain value.");
222
- }
223
- return;
224
- }
225
- if (typeof actual === "string") {
226
- if (!actual.includes(String(expected))) {
227
- throw new Error("Expected string to contain value.");
228
- }
229
- return;
230
- }
231
- throw new Error("toContain expects an array or string actual value.");
232
- },
233
- toContainEqual: (expected) => {
234
- if (!Array.isArray(actual)) {
235
- throw new Error("toContainEqual expects an array actual value.");
236
- }
237
- if (!actual.some((value) => isDeepStrictEqual(value, expected))) {
238
- throw new Error("Expected array to contain an equal value.");
239
- }
240
- },
241
- toHaveLength: (expected) => {
242
- assertLength(actual, expected);
243
- },
244
- toBeGreaterThan: (expected) => {
245
- if (typeof actual !== "number") {
246
- throw new Error("toBeGreaterThan expects a number actual value.");
247
- }
248
- if (actual <= expected) {
249
- throw new Error(`Expected ${actual} to be > ${expected}.`);
250
- }
251
- },
252
- toBeGreaterThanOrEqual: (expected) => {
253
- if (typeof actual !== "number") {
254
- throw new Error(
255
- "toBeGreaterThanOrEqual expects a number actual value."
256
- );
257
- }
258
- if (actual < expected) {
259
- throw new Error(`Expected ${actual} to be >= ${expected}.`);
260
- }
261
- },
262
- toThrow: (predicate) => {
263
- assertThrown(actual, predicate);
264
- },
265
- toMatchSnapshot: (filename) => {
266
- if (!options.matchSnapshot) {
267
- throw new Error(
268
- "Snapshot matching is not configured for this expect API."
269
- );
270
- }
271
- options.matchSnapshot(filename, actual);
272
- },
273
- toRejectWith: (expected) => createSubmissionErrorMatcher(actual, expected, options),
274
- toHaveInteraction: (interactionId, opts) => {
275
- const descriptors = asDescriptorList(actual);
276
- const descriptor = findInteraction(descriptors, interactionId);
277
- if (!descriptor) {
278
- throw new Error(`Expected interaction '${interactionId}' to exist.`);
279
- }
280
- assertDescriptorMatches(descriptor, opts);
281
- },
282
- toBeGatedBy: (reason, opts) => {
283
- const descriptor = Array.isArray(actual) ? (() => {
284
- if (!opts?.interactionId) {
285
- throw new Error(
286
- "toBeGatedBy on a descriptor array requires opts.interactionId."
287
- );
288
- }
289
- return findInteraction(
290
- asDescriptorList(actual),
291
- opts.interactionId
292
- );
293
- })() : actual;
294
- if (!descriptor) {
295
- throw new Error("Expected interaction descriptor to exist.");
296
- }
297
- if (descriptor.availability?.status === "available") {
298
- throw new Error("Expected interaction to be unavailable.");
299
- }
300
- const actualReason = descriptor.availability?.status === "notYourTurn" || descriptor.availability?.status === "insufficientResources" || descriptor.availability?.status === "blocked" ? descriptor.availability.reason : void 0;
301
- if (actualReason !== reason) {
302
- throw new Error(
303
- `Expected unavailable reason '${reason}', received '${actualReason ?? "undefined"}'.`
304
- );
305
- }
306
- },
307
- toBeAvailable: (explanation) => {
308
- const descriptor = Array.isArray(actual) ? (() => {
309
- if (!explanation?.interactionId) {
310
- throw new Error(
311
- "toBeAvailable on a descriptor array requires an explanation."
312
- );
313
- }
314
- return findInteraction(
315
- asDescriptorList(actual),
316
- explanation.interactionId
317
- );
318
- })() : actual;
319
- if (!descriptor) {
320
- throw new Error(
321
- `Expected interaction descriptor to exist.${formatInteractionExplanation(
322
- explanation
323
- )}`
324
- );
325
- }
326
- if (descriptor.availability?.status !== "available") {
327
- throw new Error(
328
- `Expected interaction '${descriptor.interactionId ?? "unknown"}' to be available.${formatInteractionExplanation(
329
- explanation
330
- )}`
331
- );
332
- }
333
- },
334
- toBeActiveFor: (playerId, opts) => {
335
- const descriptor = Array.isArray(actual) ? (() => {
336
- if (!opts?.interactionId) {
337
- throw new Error(
338
- "toBeActiveFor on a descriptor array requires opts.interactionId."
339
- );
340
- }
341
- return findInteraction(
342
- asDescriptorList(actual),
343
- opts.interactionId
344
- );
345
- })() : actual;
346
- if (!descriptor) {
347
- throw new Error("Expected interaction descriptor to exist.");
348
- }
349
- if (descriptor.context?.to !== playerId) {
350
- throw new Error(
351
- `Expected interaction to target '${playerId}', received '${descriptor.context?.to ?? "undefined"}'.`
352
- );
353
- }
354
- if (descriptor.availability?.status !== "available") {
355
- throw new Error("Expected interaction to be available.");
356
- }
357
- },
358
- not: {
359
- toHaveInteraction: (interactionId) => {
360
- const descriptors = asDescriptorList(actual);
361
- if (findInteraction(descriptors, interactionId)) {
362
- throw new Error(
363
- `Expected interaction '${interactionId}' to be absent.`
364
- );
365
- }
366
- }
367
- }
368
- });
369
- return (actual) => buildMatchers(actual);
370
- }
371
-
372
- // src/testing/create-test-runtime.ts
373
- function cloneState(value) {
374
- return structuredClone(value);
375
- }
376
- function createSubmissionError(errorCode, message) {
377
- const error = new Error(message ?? "Interaction rejected");
378
- error.name = "SubmissionError";
379
- error.errorCode = errorCode;
380
- return error;
381
- }
382
- function summarizeWireTrace(trace) {
383
- return (trace ?? []).flatMap((entry) => {
384
- if (typeof entry !== "object" || entry === null) return [];
385
- const record = entry;
386
- switch (record.kind) {
387
- case "acceptedClientInput": {
388
- const input = typeof record.input === "object" && record.input !== null ? record.input : {};
389
- return [
390
- {
391
- kind: "acceptedClientInput",
392
- interactionId: String(input.interactionId ?? ""),
393
- playerId: String(input.playerId ?? "")
394
- }
395
- ];
396
- }
397
- case "appliedEffect": {
398
- const effect = typeof record.effect === "object" && record.effect !== null ? record.effect : {};
399
- return [
400
- {
401
- kind: "appliedInstruction",
402
- instruction: String(effect.kind ?? effect.type ?? "effect")
403
- }
404
- ];
405
- }
406
- case "rngConsumption":
407
- return [
408
- {
409
- kind: "rngConsumption",
410
- operation: String(record.operation ?? ""),
411
- traceEntry: String(record.traceEntry ?? "")
412
- }
413
- ];
414
- default:
415
- return [];
416
- }
417
- });
418
- }
419
- function readFlowState(state) {
420
- const flow = state.domain?.flow ?? {};
421
- return {
422
- currentPhase: flow.currentPhase ?? null,
423
- activePlayers: Array.isArray(flow.activePlayers) ? flow.activePlayers : []
424
- };
425
- }
426
- function resolvePlayerIds(options) {
427
- if (options.explicitPlayerIds && options.explicitPlayerIds.length > 0) {
428
- return [...options.explicitPlayerIds];
429
- }
430
- return Array.from(
431
- { length: options.baseState.fingerprint.players },
432
- (_, index) => `player-${index + 1}`
433
- );
434
- }
435
- function isWireZoneHandles(value) {
436
- return typeof value === "object" && value !== null;
437
- }
438
- function buildPluginFrame(options) {
439
- const projection = options.bundle.projectSeatsDynamic({
440
- state: options.state,
441
- playerIds: [options.playerId]
442
- });
443
- const flow = readFlowState(options.state);
444
- if (options.expectedPhase && flow.currentPhase !== options.expectedPhase) {
445
- throw new Error(
446
- `Expected base '${options.baseId}' to be in phase '${options.expectedPhase}', received '${flow.currentPhase ?? "null"}'.`
447
- );
448
- }
449
- const interactionsByRef = projection.interactionsByRef ?? {};
450
- const hydrateRefs = (refs) => (refs ?? []).map((ref) => interactionsByRef[ref]).filter(
451
- (descriptor) => Boolean(descriptor)
452
- );
453
- const seat = projection.seats?.[options.playerId];
454
- const zones = Object.fromEntries(
455
- Object.entries(
456
- seat?.zones ?? {}
457
- ).map(([zoneId, zoneValue]) => {
458
- const zone = isWireZoneHandles(zoneValue) ? zoneValue : {};
459
- return [
460
- zoneId,
461
- {
462
- cardIds: [...zone.cardIds ?? []],
463
- cardViewsById: { ...zone.cardViewsById ?? {} },
464
- playableByCardId: Object.fromEntries(
465
- Object.entries(zone.playableByCardId ?? {}).map(
466
- ([cardId, refs]) => [cardId, hydrateRefs(refs)]
467
- )
468
- )
469
- }
470
- ];
471
- })
472
- );
473
- const availableInteractions = hydrateRefs(
474
- seat?.availableInteractionRefs
475
- );
476
- return {
477
- gameVersion: options.version,
478
- actionSetVersion: computePluginActionSetVersion({
479
- gameVersion: options.version,
480
- availableInteractions
481
- }),
482
- perspectivePlayerId: options.playerId || null,
483
- view: seat?.view ?? null,
484
- flow: {
485
- currentPhase: flow.currentPhase,
486
- currentStage: projection.currentStage ?? null,
487
- activePlayers: flow.activePlayers,
488
- simultaneousPhase: projection.simultaneousPhase ?? null
489
- },
490
- availableInteractions,
491
- recentEvents: [],
492
- zones
493
- };
494
- }
495
- function createTestRuntime(options) {
496
- const baseState = options.baseStates[options.baseId];
497
- if (!baseState) {
498
- throw new Error(`Unknown test base '${options.baseId}'.`);
499
- }
500
- const expectedBaseStateFingerprint = options.expectedBaseStateFingerprint ?? baseState.fingerprint.contractFingerprint;
501
- if (options.contractFingerprint && expectedBaseStateFingerprint && options.contractFingerprint !== expectedBaseStateFingerprint) {
502
- throw new StaleContractArtifactError({
503
- artifact: "base-states",
504
- expected: options.contractFingerprint,
505
- found: expectedBaseStateFingerprint
506
- });
507
- }
508
- let currentState = cloneState(baseState.snapshot);
509
- const playerIds = resolvePlayerIds({
510
- baseState,
511
- explicitPlayerIds: options.playerIds
512
- });
513
- const sessionId = options.sessionId ?? "test-session";
514
- const sessionDescriptor = {
515
- sessionId,
516
- players: playerIds.map((playerId) => ({
517
- playerId,
518
- displayName: options.displayNameByPlayerId?.[playerId] ?? playerId
519
- }))
520
- };
521
- let version = 0;
522
- let submissionCounter = 0;
523
- const currentPlayerId = playerIds[0] ?? "";
524
- const diagnosticEvents = [];
525
- let lastDispatch = null;
526
- const toReadySessionState = (frame) => ({
527
- status: "ready",
528
- sessionId,
529
- controllingPlayerId: frame.perspectivePlayerId
530
- });
531
- let lastPluginFrame;
532
- let lastSessionState;
533
- const applyCurrentState = () => {
534
- version += 1;
535
- lastPluginFrame = buildPluginFrame({
536
- state: currentState,
537
- bundle: options.bundle,
538
- playerId: currentPlayerId,
539
- version,
540
- expectedPhase: version === 1 ? options.phase : void 0,
541
- baseId: options.baseId
542
- });
543
- lastSessionState = toReadySessionState(lastPluginFrame);
544
- };
545
- applyCurrentState();
546
- const validate = async (playerId, interactionId, params = {}) => {
547
- const result = await options.bundle.validateInput({
548
- state: currentState,
549
- input: {
550
- kind: "interaction",
551
- playerId,
552
- interactionId,
553
- params
554
- }
555
- });
556
- return {
557
- valid: result.valid,
558
- errorCode: result.errorCode,
559
- message: result.message
560
- };
561
- };
562
- const submit2 = async (playerId, interactionId, params = {}) => {
563
- submissionCounter += 1;
564
- const submissionId = `sub-${submissionCounter}`;
565
- diagnosticEvents.push({
566
- type: "submitReceived",
567
- submissionId,
568
- playerId,
569
- interactionId,
570
- phase: readFlowState(currentState).currentPhase ?? ""
571
- });
572
- const validation = await validate(playerId, interactionId, params);
573
- if (!validation.valid) {
574
- diagnosticEvents.push({
575
- type: "submitRejected",
576
- submissionId,
577
- errorCode: validation.errorCode ?? "invalid-action",
578
- ...validation.message ? { message: validation.message } : {}
579
- });
580
- throw createSubmissionError(validation.errorCode, validation.message);
581
- }
582
- const result = await options.bundle.dispatch({
583
- state: currentState,
584
- input: {
585
- kind: "interaction",
586
- playerId,
587
- interactionId,
588
- params
589
- }
590
- });
591
- if (result.kind === "reject") {
592
- diagnosticEvents.push({
593
- type: "submitRejected",
594
- submissionId,
595
- errorCode: result.errorCode,
596
- ...result.message ? { message: result.message } : {}
597
- });
598
- throw createSubmissionError(result.errorCode, result.message);
599
- }
600
- const trace = summarizeWireTrace(result.trace);
601
- lastDispatch = { submissionId, trace };
602
- diagnosticEvents.push({
603
- type: "submitAccepted",
604
- submissionId,
605
- trace
606
- });
607
- currentState = cloneState(result.state);
608
- applyCurrentState();
609
- };
610
- const explain = (playerId, interactionId) => {
611
- if (!options.bundle.explainInteraction) {
612
- throw new Error(
613
- "This reducer bundle does not expose explainInteraction()."
614
- );
615
- }
616
- return options.bundle.explainInteraction({
617
- state: currentState,
618
- playerId,
619
- interactionId
620
- });
621
- };
622
- const runtime = {
623
- validateInteraction: (interactionId, params) => validate(currentPlayerId, interactionId, params),
624
- submitInteraction: (interactionId, params) => submit2(currentPlayerId, interactionId, params),
625
- getSessionState: () => lastSessionState,
626
- disconnect: () => void 0
627
- };
628
- return {
629
- runtime,
630
- getFrame: () => lastPluginFrame,
631
- getSessionDescriptor: () => sessionDescriptor,
632
- players: () => [...playerIds],
633
- seat: (index) => {
634
- if (!Number.isInteger(index) || index < 0 || index >= playerIds.length) {
635
- throw new Error(
636
- `seat(${index}) is out of range; base '${options.baseId}' has ${playerIds.length} player(s).`
637
- );
638
- }
639
- return playerIds[index];
640
- },
641
- submit: submit2,
642
- validate,
643
- explain,
644
- diagnostics: {
645
- get events() {
646
- return diagnosticEvents;
647
- },
648
- get lastDispatch() {
649
- return lastDispatch;
650
- },
651
- clear() {
652
- diagnosticEvents.length = 0;
653
- lastDispatch = null;
654
- }
655
- }
656
- };
657
- }
658
-
659
41
  // src/testing/ui-fixture/canonical.ts
660
42
  import { createHash } from "crypto";
661
43
  var sha256DigestPattern = /^sha256:[a-f0-9]{64}$/;
@@ -751,7 +133,7 @@ function createReducerScenarioRunner(options) {
751
133
  playerIds: [options.viewer.playerId]
752
134
  });
753
135
  const staticProjection = options.bundle.projectStatic?.() ?? null;
754
- const flow = readFlowState2(state);
136
+ const flow = readFlowState(state);
755
137
  const materialized = materializePluginGameplayFrame({
756
138
  currentPhase: flow.currentPhase,
757
139
  activePlayers: flow.activePlayers,
@@ -851,7 +233,7 @@ function createReducerScenarioRunner(options) {
851
233
  }
852
234
  };
853
235
  }
854
- function readFlowState2(state) {
236
+ function readFlowState(state) {
855
237
  return {
856
238
  currentPhase: state.domain?.flow?.currentPhase ?? null,
857
239
  activePlayers: [...state.domain?.flow?.activePlayers ?? []]
@@ -1525,7 +907,7 @@ function compilePluginProtocolTape(options) {
1525
907
  };
1526
908
  }
1527
909
  function materializeTraceFrame(frame, perspectivePlayerId) {
1528
- const flow = readFlowState3(frame.reducerState);
910
+ const flow = readFlowState2(frame.reducerState);
1529
911
  return {
1530
912
  id: frame.id,
1531
913
  frame: materializePluginGameplayFrame({
@@ -1552,7 +934,7 @@ function digestRuntimeCommand(exchange, frame) {
1552
934
  payload: exchange.input.params
1553
935
  });
1554
936
  }
1555
- function readFlowState3(state) {
937
+ function readFlowState2(state) {
1556
938
  return {
1557
939
  currentPhase: state.domain?.flow?.currentPhase ?? null,
1558
940
  activePlayers: [...state.domain?.flow?.activePlayers ?? []]