@moku-labs/game 0.0.2 → 0.0.3

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 (35) hide show
  1. package/README.md +89 -21
  2. package/dist/assets.d.mts +58 -6
  3. package/dist/assets.mjs +1630 -234
  4. package/dist/commands-BOxqlv7w.d.mts +99 -0
  5. package/dist/control.d.mts +3 -92
  6. package/dist/control.mjs +3 -757
  7. package/dist/define-BMo0pHe_.d.mts +51 -0
  8. package/dist/{registry-DlpRCibU.mjs → dev-CcwFrwok.mjs} +30 -1
  9. package/dist/index.d.mts +217 -29
  10. package/dist/index.mjs +6179 -727
  11. package/dist/inspect.d.mts +10 -8
  12. package/dist/inspect.mjs +2 -518
  13. package/dist/jsx-dev-runtime.d.mts +1 -1
  14. package/dist/jsx-runtime.d.mts +1 -1
  15. package/dist/{rolldown-runtime-D7D4PA-g.mjs → rolldown-runtime-8H4AJuhK.mjs} +1 -0
  16. package/dist/run-tlcn1w7y.mjs +944 -0
  17. package/dist/{runtime-DiOTkDZz.d.mts → runtime-BstIkrhH.d.mts} +1 -1
  18. package/dist/sources-BooL2MsX.mjs +545 -0
  19. package/dist/testing.d.mts +305 -3
  20. package/dist/testing.mjs +2056 -2
  21. package/dist/types-BTycq8_Z.d.mts +1196 -0
  22. package/dist/{types-yg_ywtT-.d.mts → types-C0SLQsx0.d.mts} +230 -31
  23. package/dist/types-Cjf3ywai.d.mts +5982 -0
  24. package/dist/{define-sFoO3y6X.d.mts → types-D0uYTTC3.d.mts} +180 -51
  25. package/dist/{types-DWILGrPn.d.mts → types-DgwbzDLP.d.mts} +87 -19
  26. package/dist/types-KlE_GL1z.d.mts +1444 -0
  27. package/dist/{visual-BDUHSRvf.mjs → visual-NPrDkvUk.mjs} +19 -13
  28. package/package.json +17 -2
  29. package/dist/headless-CvamCUcR.mjs +0 -186
  30. package/dist/headless-KaSWcd0s.d.mts +0 -181
  31. package/dist/types-BxkNNYul.d.mts +0 -2840
  32. package/dist/types-CTPS9GBu.d.mts +0 -742
  33. package/dist/types-DD-QrG_z.d.mts +0 -588
  34. package/dist/types-DYLnSgMI.d.mts +0 -570
  35. package/dist/types-JNc_UQBo.d.mts +0 -2896
@@ -1,5 +1,8 @@
1
- import { H as PlayerStateProvider, L as Json, U as ProviderCall, W as SaveDoc, it as FakeClock } from "./types-JNc_UQBo.mjs";
2
- import { a as createHeadless, i as ReproResult, n as HeadlessGame, o as runRepro, r as Repro, s as stepFrames, t as HeadlessApp } from "./headless-KaSWcd0s.mjs";
1
+ import { At as Json, Dt as Api$2, Ft as PlayerStateProvider, It as ProviderCall, Lt as SaveDoc, Ut as Api$3, Zt as FakeClock, h as Api$1, t as Api } from "./types-Cjf3ywai.mjs";
2
+ import { _ as stepFrames, a as InputOf, d as HeadlessApp, f as HeadlessGame, g as runRepro, h as createHeadless, m as ReproResult, p as Repro, r as ControlApp } from "./types-D0uYTTC3.mjs";
3
+ import { H as InputApi } from "./types-C0SLQsx0.mjs";
4
+ import { b as UiApi, t as AnimApi } from "./types-KlE_GL1z.mjs";
5
+ import { t as commands } from "./commands-BOxqlv7w.mjs";
3
6
 
4
7
  //#region src/plugins/clock/fake.d.ts
5
8
  /**
@@ -70,4 +73,303 @@ declare function memory(fixture?: {
70
73
  */
71
74
  declare function saveOf(player: Json, seed?: number): SaveDoc;
72
75
  //#endregion
73
- export { type HeadlessApp, type HeadlessGame, type Repro, type ReproResult, createHeadless, fakeClock, memory, runRepro, saveOf, stepFrames };
76
+ //#region src/visual/types.d.ts
77
+ /** The base commands of the `/control` door, keyed by their short names. */
78
+ type Catalogue = typeof commands;
79
+ /**
80
+ * The commands a step may name: the catalogue of `/control` minus `bookmark` (it only reads),
81
+ * `capture` (the runner captures by itself) and `debug` (debug drawing would enter a baseline). A
82
+ * command the catalogue gains is a step with no change to the runner.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const name: StepCommand = "tap"; // also "answer", "walk", "step", "reducedMotion", ...
87
+ * ```
88
+ */
89
+ type StepCommand = Exclude<keyof Catalogue, "bookmark" | "capture" | "debug">;
90
+ /**
91
+ * Where a visual test starts: what `game.restore` loads, a repro without its route. The player
92
+ * is required; the session, the rng and the checkpoint are optional, as in a repro.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * // A save with a plank ready for the first order, entered at Home.
97
+ * const start: VisualStart = { player: { coins: 40, orders: [] }, checkpoint: "home" };
98
+ * ```
99
+ */
100
+ type VisualStart = Omit<Repro, "route">;
101
+ /**
102
+ * One step of a visual test: a `/control` command by its short name with that command's input,
103
+ * typed from the catalogue, or a checkpoint by its name.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * // Tap Play, answer the order on the board, then compare the screen with its baseline.
108
+ * const steps: VisualStep[] = [
109
+ * { tap: { key: "play" } },
110
+ * { answer: { intent: "deliver", payload: { orderId: "o1" } } },
111
+ * { checkpoint: "open" }
112
+ * ];
113
+ * ```
114
+ */
115
+ type VisualStep = {
116
+ checkpoint: string;
117
+ } | { [K in StepCommand]: { [P in K]: InputOf<Catalogue[K]["input"]> } }[StepCommand];
118
+ /**
119
+ * A visual test as `defineVisualTest` answers it: its name, where it starts and its steps.
120
+ * Frozen data; nothing runs at definition.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * const test: VisualTest = defineVisualTest("reward-popup", {
125
+ * start: { player: { coins: 40 }, checkpoint: "home" },
126
+ * steps: [{ tap: { key: "play" } }, { checkpoint: "board" }]
127
+ * });
128
+ * test.name; // "reward-popup"
129
+ * Object.isFrozen(test.steps); // true
130
+ * ```
131
+ */
132
+ type VisualTest = {
133
+ readonly name: string;
134
+ readonly start: Readonly<VisualStart>;
135
+ readonly steps: readonly VisualStep[];
136
+ };
137
+ /**
138
+ * What the runner needs of an app: the control app, the plugins the base commands act on and
139
+ * the plugins the readers read. Every app composed with the screen set fits.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * const app: VisualApp = createApp({ plugins: [...screen, boardFeature], pluginConfigs });
144
+ * ```
145
+ */
146
+ type VisualApp = ControlApp & {
147
+ readonly anim: AnimApi;
148
+ readonly ui: UiApi;
149
+ readonly renderer: Api;
150
+ readonly world: Api$1;
151
+ readonly model: Api$2;
152
+ readonly input: InputApi;
153
+ readonly lifecycle: Api$3;
154
+ };
155
+ /**
156
+ * The dev page the browser leg opens: its URL, the viewport width in CSS px (390 by default), the
157
+ * device scale (2 by default) and which Chrome to launch. The page height follows the inert frame
158
+ * of the renderer: 520 for a 390 wide portrait game at the default 4:3. Without `browser` the leg
159
+ * launches Playwright's own Chromium, and the system Chrome when that one is not installed.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * // The fixture's dev server, a 390 px wide phone at 2x, the system Chrome.
164
+ * const page: VisualPage = { url: "http://localhost:3000/", browser: { channel: "chrome" } };
165
+ * ```
166
+ */
167
+ type VisualPage = {
168
+ url: string;
169
+ width?: number;
170
+ deviceScaleFactor?: number;
171
+ browser?: {
172
+ channel?: string;
173
+ executablePath?: string;
174
+ };
175
+ };
176
+ /**
177
+ * How the runner builds a game: a fresh app per test, not started, with the screen set
178
+ * composed; the renderer stays inert in Bun. `page` is the dev page of the pixel leg.
179
+ *
180
+ * @example
181
+ * ```ts
182
+ * // tests/visual/run.ts of a game.
183
+ * const setup: VisualSetup = { app: () => createScreenGame().app, page: { url: "http://localhost:3000/" } };
184
+ * ```
185
+ */
186
+ type VisualSetup = {
187
+ app: () => VisualApp;
188
+ page?: VisualPage;
189
+ };
190
+ /**
191
+ * How far a pixel may differ: `threshold` is the largest channel delta that still counts as the
192
+ * same pixel, `ratio` the share of differing pixels a checkpoint tolerates.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * const tolerance: VisualTolerance = { ratio: 0.001, threshold: 24 }; // the defaults
197
+ * ```
198
+ */
199
+ type VisualTolerance = {
200
+ ratio: number;
201
+ threshold: number;
202
+ };
203
+ /**
204
+ * The options of a run. Each one left out comes from the command line (`argv`), then from its
205
+ * default: `dir` `"tests/visual"`, `update` false, `pixels` true with a `page` on a Mac,
206
+ * `settleFrames` 600, `tolerance` `{ ratio: 0.001, threshold: 24 }`, `argv`
207
+ * `process.argv.slice(2)`. An option given here wins over a flag.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * // The same as `bun tests/visual/run.ts --only reward-popup --no-pixels`.
212
+ * const options: VisualOptions = { only: ["reward-popup"], pixels: false };
213
+ * ```
214
+ */
215
+ type VisualOptions = {
216
+ dir?: string;
217
+ update?: boolean;
218
+ pixels?: boolean;
219
+ only?: readonly string[];
220
+ settleFrames?: number;
221
+ tolerance?: VisualTolerance;
222
+ argv?: readonly string[];
223
+ };
224
+ /**
225
+ * What a comparison with a baseline file found: the same content, a different one, or no
226
+ * baseline (or `--update`), so the file was written.
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * const outcome: Outcome = "written"; // the first run of a new checkpoint
231
+ * ```
232
+ */
233
+ type Outcome = "same" | "different" | "written";
234
+ /**
235
+ * The outcome of one checkpoint. `first` is the JSON path of the first difference, the state's
236
+ * before the describe's. `pixels` is `"skipped"` when the pixel leg did not run; `pixelRatio`
237
+ * and `verdict` come with a pixel difference.
238
+ *
239
+ * @example
240
+ * ```ts
241
+ * // The player's coins differ from the baseline; the layout is the same.
242
+ * const result: CheckpointResult = {
243
+ * name: "claimed", state: "different", describe: "same", pixels: "skipped", first: "player.coins"
244
+ * };
245
+ * ```
246
+ */
247
+ type CheckpointResult = {
248
+ name: string;
249
+ state: Outcome;
250
+ describe: Outcome;
251
+ pixels: Outcome | "skipped";
252
+ pixelRatio?: number;
253
+ first?: string;
254
+ verdict?: "rendering" | "behaviour";
255
+ };
256
+ /**
257
+ * The outcome of one visual test: every checkpoint it reached, and the error that ended it early.
258
+ *
259
+ * @example
260
+ * ```ts
261
+ * const result: VisualTestResult = {
262
+ * name: "reward-popup",
263
+ * checkpoints: [{ name: "open", state: "same", describe: "same", pixels: "skipped" }]
264
+ * };
265
+ * ```
266
+ */
267
+ type VisualTestResult = {
268
+ name: string;
269
+ checkpoints: readonly CheckpointResult[];
270
+ error?: string;
271
+ };
272
+ /**
273
+ * What `runVisualTests` answers: `ok` is true when no test failed and no outcome is
274
+ * `"different"`.
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * const report: VisualReport = await runVisualTests(setup, [rewardPopup], { argv: [] });
279
+ * process.exitCode = report.ok ? 0 : 1;
280
+ * ```
281
+ */
282
+ type VisualReport = {
283
+ ok: boolean;
284
+ tests: readonly VisualTestResult[];
285
+ };
286
+ /** What the command line asked for: the flags it carried, nothing else. */
287
+ type VisualFlags = {
288
+ dir?: string;
289
+ update?: boolean;
290
+ pixels?: boolean;
291
+ only?: readonly string[];
292
+ };
293
+ //#endregion
294
+ //#region src/visual/define.d.ts
295
+ /**
296
+ * Defines a visual test: where it starts, what `game.restore` loads, and its steps. A step is a
297
+ * `/control` command by its short name with that command's input, or `{ checkpoint: name }`,
298
+ * where the runner saves `state.json` and `describe.json` next to the test. `walk` runs fast: a
299
+ * popup its last node opens never mounts, so the step before a checkpoint that shows a node's
300
+ * result is `answer` or `tap`. The answer is a frozen copy; nothing runs here.
301
+ *
302
+ * @param name - The test name, also its folder: lowercase letters, digits and dashes.
303
+ * @param test - Where it starts and its steps.
304
+ * @param test.start - The player, and optionally the session, the rng and the checkpoint.
305
+ * @param test.steps - The commands and the checkpoints, in order.
306
+ * @returns The test, frozen.
307
+ * @throws {Error} When the name or a checkpoint name is not a file name, a checkpoint name is
308
+ * used twice, or a step is not one step command.
309
+ * @example
310
+ * ```ts
311
+ * // tests/visual/reward-popup.visual.ts: Play, deliver the first order, claim the reward.
312
+ * export const rewardPopup = defineVisualTest("reward-popup", {
313
+ * start: { player: fixtures.ready, checkpoint: "home" },
314
+ * steps: [{ tap: { key: "play" } }, { answer: { intent: "deliver", payload: { orderId: "o1" } } },
315
+ * { checkpoint: "open" }, { tap: { key: "claim" } }, { checkpoint: "claimed" }]
316
+ * });
317
+ * ```
318
+ */
319
+ declare function defineVisualTest(name: string, test: {
320
+ start: VisualStart;
321
+ steps: readonly VisualStep[];
322
+ }): VisualTest;
323
+ //#endregion
324
+ //#region src/visual/run.d.ts
325
+ /**
326
+ * Reads the flags of the visual tests from a command line: `--update`, `--no-pixels`,
327
+ * `--only <name>` (repeatable) and `--dir <path>`. An argument it does not know is left to the
328
+ * script that got it, so a test runner's own arguments pass through.
329
+ *
330
+ * @param argv - The arguments after the script name.
331
+ * @returns The flags that were given, and nothing else.
332
+ * @throws {Error} When `--only` or `--dir` has no value.
333
+ * @example
334
+ * ```ts
335
+ * // `bun tests/visual/run.ts --update --only reward-popup`
336
+ * parseVisualArgv(["--update", "--only", "reward-popup"]); // { update: true, only: ["reward-popup"] }
337
+ * parseVisualArgv(["--no-pixels", "--dir", "shots"]); // { pixels: false, dir: "shots" }
338
+ * ```
339
+ */
340
+ declare function parseVisualArgv(argv: readonly string[]): VisualFlags;
341
+ /**
342
+ * Runs visual tests: the headless leg over every selected test, then the browser leg over the
343
+ * same tests when `pixels` is on (by default only with a `page` on a Mac). A checkpoint saves
344
+ * `state.json` and `describe.json` in `<dir>/<test>/<checkpoint>/`, and the browser leg
345
+ * `screen.webp`, lossless: a missing file is written, `--update` rewrites every file of the tests
346
+ * run, a JSON file is compared exactly. The browser leg opens the dev page in Chrome with WebGPU
347
+ * through `playwright-core`, pauses its game, plays each test there on stepped frames of 1000/60
348
+ * ms, so two runs see the same game time, checks the page's state against `state.json` and
349
+ * compares the picture in the page: a pixel differs above `tolerance.threshold`, and the screen
350
+ * differs when more than `tolerance.ratio` of its pixels do; `screen.actual.webp` and
351
+ * `screen.diff.webp` are written beside the baseline then. A pixel difference over the same state
352
+ * and describe is a rendering regression (`verdict: "rendering"`). One line per checkpoint goes
353
+ * to the branded console; the script sets the exit code from `ok`.
354
+ *
355
+ * @param setup - How to build a game: a fresh, unstarted app per test, and the dev page.
356
+ * @param tests - Every visual test of the game.
357
+ * @param options - Overrides of the flags of `argv` and of the defaults.
358
+ * @returns The report: `ok` when no test failed and nothing is `"different"`.
359
+ * @throws {Error} When a test is not well formed, two tests share a name, `only` names no test,
360
+ * a flag misses its value, or the browser leg fails as a whole: no `page`, no
361
+ * `playwright-core`, no Chrome, or a page that does not open, expose `game` and `doors`, start,
362
+ * draw with WebGPU or stop loading.
363
+ * @example
364
+ * ```ts
365
+ * // tests/visual/run.ts of a game, `bun tests/visual/run.ts --update` on a Mac
366
+ * const report = await runVisualTests(
367
+ * { app: () => createScreenGame().app, page: { url: "http://localhost:3000/" } },
368
+ * [rewardPopup]
369
+ * );
370
+ * report.tests[0]?.checkpoints[0]; // { name: "open", state: "written", describe: "written", pixels: "written" }
371
+ * ```
372
+ */
373
+ declare function runVisualTests(setup: VisualSetup, tests: readonly VisualTest[], options?: VisualOptions): Promise<VisualReport>;
374
+ //#endregion
375
+ export { type CheckpointResult, type HeadlessApp, type HeadlessGame, type Repro, type ReproResult, type VisualApp, type VisualOptions, type VisualPage, type VisualReport, type VisualSetup, type VisualStart, type VisualStep, type VisualTest, type VisualTestResult, type VisualTolerance, createHeadless, defineVisualTest, fakeClock, memory, parseVisualArgv, runRepro, runVisualTests, saveOf, stepFrames };