@flare-engine/testing 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/index.d.mts +94 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +169 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +137 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Grzegorz Otto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @flare-engine/testing
|
|
2
|
+
|
|
3
|
+
Deterministic test helpers for ECS games. Pure dev-time package — never bundled into apps. `createFakeClock` is a deterministic `requestFrame` / `cancelFrame` replacement; `createTestWorld` wires a `World` + `GameLoop` + fake clock together; `MockInputManager` extends the real `InputManager` with scriptable pointer sequences; `serializeScene` / `snapshotsEqual` enable regression-style snapshot tests.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add -d @flare-engine/testing
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createTestWorld, MockInputManager, serializeScene } from '@flare-engine/testing';
|
|
15
|
+
import { Position, Velocity } from '@flare-engine/ecs';
|
|
16
|
+
|
|
17
|
+
const { world, step, stepFrames } = createTestWorld({ fixedDt: 1 / 60 });
|
|
18
|
+
const e = world.spawn();
|
|
19
|
+
world.add(e, Position, { x: 0, y: 0 });
|
|
20
|
+
world.add(e, Velocity, { x: 1, y: 0 });
|
|
21
|
+
|
|
22
|
+
stepFrames(60, 1 / 60);
|
|
23
|
+
|
|
24
|
+
const snap = serializeScene(world, [Position]);
|
|
25
|
+
expect(snap.entities[0].components.Position.x).toBe(60);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
See [API spec](../../docs/design/api-specs.md#flare-enginetesting--4kb-raw) and [skill reference](../../.github/skills/flare-testing/SKILL.md).
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { GameLoop } from '@flare-engine/core';
|
|
2
|
+
import { World, ComponentDef } from '@flare-engine/ecs';
|
|
3
|
+
import { InputManager } from '@flare-engine/input';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fake monotonic clock for driving `GameLoop` deterministically in tests.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* const fake = createFakeClock();
|
|
10
|
+
* loop.tick = fake.requestFrame;
|
|
11
|
+
* loop.cancelTick = fake.cancelFrame;
|
|
12
|
+
* loop.start();
|
|
13
|
+
* fake.advance(1 / 60); // runs one frame
|
|
14
|
+
*/
|
|
15
|
+
interface FakeClock {
|
|
16
|
+
/** Current fake timestamp in milliseconds. */
|
|
17
|
+
readonly now: number;
|
|
18
|
+
/** rAF-compatible request — stores the callback for the next `advance`. */
|
|
19
|
+
requestFrame(callback: (time: number) => void): number;
|
|
20
|
+
/** rAF-compatible cancel. */
|
|
21
|
+
cancelFrame(id: number): void;
|
|
22
|
+
/** Advance time by `seconds` and fire every pending callback once. */
|
|
23
|
+
advance(seconds: number): void;
|
|
24
|
+
/** Reset time and cancel all pending callbacks. */
|
|
25
|
+
reset(): void;
|
|
26
|
+
}
|
|
27
|
+
declare function createFakeClock(): FakeClock;
|
|
28
|
+
|
|
29
|
+
interface TestWorld {
|
|
30
|
+
/** ECS world. */
|
|
31
|
+
readonly world: World;
|
|
32
|
+
/** Game loop wired to a fake clock. */
|
|
33
|
+
readonly loop: GameLoop;
|
|
34
|
+
/** Fake clock driving the loop. */
|
|
35
|
+
readonly clock: FakeClock;
|
|
36
|
+
/** Advance one frame of `dt` seconds. Runs the loop's fixed + variable update. */
|
|
37
|
+
step(dt: number): void;
|
|
38
|
+
/** Advance `frames` frames of `dt` seconds each. */
|
|
39
|
+
stepFrames(frames: number, dt?: number): void;
|
|
40
|
+
}
|
|
41
|
+
interface CreateTestWorldOptions {
|
|
42
|
+
/** Fixed timestep passed to `GameLoop`. Defaults to 1/60. */
|
|
43
|
+
fixedDt?: number;
|
|
44
|
+
/** Maximum raw dt cap passed to `GameLoop`. Defaults to 0.25s. */
|
|
45
|
+
maxDt?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a World + GameLoop + FakeClock wired together for deterministic
|
|
49
|
+
* testing. `step()` calls the loop's `step()` directly so fixed and variable
|
|
50
|
+
* updates fire for the exact dt you pass, without needing to start the rAF
|
|
51
|
+
* loop.
|
|
52
|
+
*/
|
|
53
|
+
declare function createTestWorld(options?: CreateTestWorldOptions): TestWorld;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `InputManager` subclass with scripted helpers for tests. Drives the same
|
|
57
|
+
* internal state the real manager uses, so any system reading
|
|
58
|
+
* `.pointer` / `.touches` sees identical data.
|
|
59
|
+
*/
|
|
60
|
+
declare class MockInputManager extends InputManager {
|
|
61
|
+
/** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */
|
|
62
|
+
press(x: number, y: number, id?: number): void;
|
|
63
|
+
/** Move a pointer to `(x, y)`. */
|
|
64
|
+
move(x: number, y: number, id?: number): void;
|
|
65
|
+
/** Release a pointer at `(x, y)`. */
|
|
66
|
+
release(x: number, y: number, id?: number): void;
|
|
67
|
+
/** Full tap: press → release in one call. */
|
|
68
|
+
tap(x: number, y: number, id?: number): void;
|
|
69
|
+
}
|
|
70
|
+
declare function createMockInputManager(): MockInputManager;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A single entity in a snapshot. Component names come from `ComponentDef.name`.
|
|
74
|
+
*/
|
|
75
|
+
interface SceneSnapshotEntity {
|
|
76
|
+
id: number;
|
|
77
|
+
components: Record<string, unknown>;
|
|
78
|
+
}
|
|
79
|
+
interface SceneSnapshot {
|
|
80
|
+
entities: SceneSnapshotEntity[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Serialize every entity that has at least one of the supplied components into
|
|
84
|
+
* a stable, deterministic snapshot. Entities are sorted by id; component keys
|
|
85
|
+
* are inserted in the order the components are passed.
|
|
86
|
+
*/
|
|
87
|
+
declare function serializeScene(world: World, components: readonly ComponentDef<unknown>[]): SceneSnapshot;
|
|
88
|
+
/**
|
|
89
|
+
* Deep-equal comparison good enough for snapshot assertions. Compares plain
|
|
90
|
+
* objects, arrays, and primitives — no Date, Map, Set, or cyclic graphs.
|
|
91
|
+
*/
|
|
92
|
+
declare function snapshotsEqual(a: SceneSnapshot, b: SceneSnapshot): boolean;
|
|
93
|
+
|
|
94
|
+
export { type CreateTestWorldOptions, type FakeClock, MockInputManager, type SceneSnapshot, type SceneSnapshotEntity, type TestWorld, createFakeClock, createMockInputManager, createTestWorld, serializeScene, snapshotsEqual };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { GameLoop } from '@flare-engine/core';
|
|
2
|
+
import { World, ComponentDef } from '@flare-engine/ecs';
|
|
3
|
+
import { InputManager } from '@flare-engine/input';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fake monotonic clock for driving `GameLoop` deterministically in tests.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* const fake = createFakeClock();
|
|
10
|
+
* loop.tick = fake.requestFrame;
|
|
11
|
+
* loop.cancelTick = fake.cancelFrame;
|
|
12
|
+
* loop.start();
|
|
13
|
+
* fake.advance(1 / 60); // runs one frame
|
|
14
|
+
*/
|
|
15
|
+
interface FakeClock {
|
|
16
|
+
/** Current fake timestamp in milliseconds. */
|
|
17
|
+
readonly now: number;
|
|
18
|
+
/** rAF-compatible request — stores the callback for the next `advance`. */
|
|
19
|
+
requestFrame(callback: (time: number) => void): number;
|
|
20
|
+
/** rAF-compatible cancel. */
|
|
21
|
+
cancelFrame(id: number): void;
|
|
22
|
+
/** Advance time by `seconds` and fire every pending callback once. */
|
|
23
|
+
advance(seconds: number): void;
|
|
24
|
+
/** Reset time and cancel all pending callbacks. */
|
|
25
|
+
reset(): void;
|
|
26
|
+
}
|
|
27
|
+
declare function createFakeClock(): FakeClock;
|
|
28
|
+
|
|
29
|
+
interface TestWorld {
|
|
30
|
+
/** ECS world. */
|
|
31
|
+
readonly world: World;
|
|
32
|
+
/** Game loop wired to a fake clock. */
|
|
33
|
+
readonly loop: GameLoop;
|
|
34
|
+
/** Fake clock driving the loop. */
|
|
35
|
+
readonly clock: FakeClock;
|
|
36
|
+
/** Advance one frame of `dt` seconds. Runs the loop's fixed + variable update. */
|
|
37
|
+
step(dt: number): void;
|
|
38
|
+
/** Advance `frames` frames of `dt` seconds each. */
|
|
39
|
+
stepFrames(frames: number, dt?: number): void;
|
|
40
|
+
}
|
|
41
|
+
interface CreateTestWorldOptions {
|
|
42
|
+
/** Fixed timestep passed to `GameLoop`. Defaults to 1/60. */
|
|
43
|
+
fixedDt?: number;
|
|
44
|
+
/** Maximum raw dt cap passed to `GameLoop`. Defaults to 0.25s. */
|
|
45
|
+
maxDt?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a World + GameLoop + FakeClock wired together for deterministic
|
|
49
|
+
* testing. `step()` calls the loop's `step()` directly so fixed and variable
|
|
50
|
+
* updates fire for the exact dt you pass, without needing to start the rAF
|
|
51
|
+
* loop.
|
|
52
|
+
*/
|
|
53
|
+
declare function createTestWorld(options?: CreateTestWorldOptions): TestWorld;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `InputManager` subclass with scripted helpers for tests. Drives the same
|
|
57
|
+
* internal state the real manager uses, so any system reading
|
|
58
|
+
* `.pointer` / `.touches` sees identical data.
|
|
59
|
+
*/
|
|
60
|
+
declare class MockInputManager extends InputManager {
|
|
61
|
+
/** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */
|
|
62
|
+
press(x: number, y: number, id?: number): void;
|
|
63
|
+
/** Move a pointer to `(x, y)`. */
|
|
64
|
+
move(x: number, y: number, id?: number): void;
|
|
65
|
+
/** Release a pointer at `(x, y)`. */
|
|
66
|
+
release(x: number, y: number, id?: number): void;
|
|
67
|
+
/** Full tap: press → release in one call. */
|
|
68
|
+
tap(x: number, y: number, id?: number): void;
|
|
69
|
+
}
|
|
70
|
+
declare function createMockInputManager(): MockInputManager;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A single entity in a snapshot. Component names come from `ComponentDef.name`.
|
|
74
|
+
*/
|
|
75
|
+
interface SceneSnapshotEntity {
|
|
76
|
+
id: number;
|
|
77
|
+
components: Record<string, unknown>;
|
|
78
|
+
}
|
|
79
|
+
interface SceneSnapshot {
|
|
80
|
+
entities: SceneSnapshotEntity[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Serialize every entity that has at least one of the supplied components into
|
|
84
|
+
* a stable, deterministic snapshot. Entities are sorted by id; component keys
|
|
85
|
+
* are inserted in the order the components are passed.
|
|
86
|
+
*/
|
|
87
|
+
declare function serializeScene(world: World, components: readonly ComponentDef<unknown>[]): SceneSnapshot;
|
|
88
|
+
/**
|
|
89
|
+
* Deep-equal comparison good enough for snapshot assertions. Compares plain
|
|
90
|
+
* objects, arrays, and primitives — no Date, Map, Set, or cyclic graphs.
|
|
91
|
+
*/
|
|
92
|
+
declare function snapshotsEqual(a: SceneSnapshot, b: SceneSnapshot): boolean;
|
|
93
|
+
|
|
94
|
+
export { type CreateTestWorldOptions, type FakeClock, MockInputManager, type SceneSnapshot, type SceneSnapshotEntity, type TestWorld, createFakeClock, createMockInputManager, createTestWorld, serializeScene, snapshotsEqual };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
MockInputManager: () => MockInputManager,
|
|
24
|
+
createFakeClock: () => createFakeClock,
|
|
25
|
+
createMockInputManager: () => createMockInputManager,
|
|
26
|
+
createTestWorld: () => createTestWorld,
|
|
27
|
+
serializeScene: () => serializeScene,
|
|
28
|
+
snapshotsEqual: () => snapshotsEqual
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/fake-clock.ts
|
|
33
|
+
function createFakeClock() {
|
|
34
|
+
let nowMs = 0;
|
|
35
|
+
let nextId = 1;
|
|
36
|
+
const pending = /* @__PURE__ */ new Map();
|
|
37
|
+
const clock = {
|
|
38
|
+
get now() {
|
|
39
|
+
return nowMs;
|
|
40
|
+
},
|
|
41
|
+
requestFrame(callback) {
|
|
42
|
+
const id = nextId++;
|
|
43
|
+
pending.set(id, callback);
|
|
44
|
+
return id;
|
|
45
|
+
},
|
|
46
|
+
cancelFrame(id) {
|
|
47
|
+
pending.delete(id);
|
|
48
|
+
},
|
|
49
|
+
advance(seconds) {
|
|
50
|
+
if (seconds < 0) throw new Error("[@flare-engine/testing] cannot advance backwards");
|
|
51
|
+
nowMs += seconds * 1e3;
|
|
52
|
+
const snapshot = [...pending.entries()];
|
|
53
|
+
pending.clear();
|
|
54
|
+
for (const [, cb] of snapshot) cb(nowMs);
|
|
55
|
+
},
|
|
56
|
+
reset() {
|
|
57
|
+
nowMs = 0;
|
|
58
|
+
pending.clear();
|
|
59
|
+
nextId = 1;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return clock;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/test-world.ts
|
|
66
|
+
var import_core = require("@flare-engine/core");
|
|
67
|
+
var import_ecs = require("@flare-engine/ecs");
|
|
68
|
+
function createTestWorld(options = {}) {
|
|
69
|
+
const world = new import_ecs.World();
|
|
70
|
+
const loop = new import_core.GameLoop(options);
|
|
71
|
+
const clock = createFakeClock();
|
|
72
|
+
loop.tick = clock.requestFrame;
|
|
73
|
+
loop.cancelTick = clock.cancelFrame;
|
|
74
|
+
return {
|
|
75
|
+
world,
|
|
76
|
+
loop,
|
|
77
|
+
clock,
|
|
78
|
+
step(dt) {
|
|
79
|
+
loop.step(dt);
|
|
80
|
+
},
|
|
81
|
+
stepFrames(frames, dt = 1 / 60) {
|
|
82
|
+
for (let i = 0; i < frames; i++) loop.step(dt);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/mock-input.ts
|
|
88
|
+
var import_input = require("@flare-engine/input");
|
|
89
|
+
var MockInputManager = class extends import_input.InputManager {
|
|
90
|
+
/** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */
|
|
91
|
+
press(x, y, id = 0) {
|
|
92
|
+
this.onPointerDown(id, x, y);
|
|
93
|
+
}
|
|
94
|
+
/** Move a pointer to `(x, y)`. */
|
|
95
|
+
move(x, y, id = 0) {
|
|
96
|
+
this.onPointerMove(id, x, y);
|
|
97
|
+
}
|
|
98
|
+
/** Release a pointer at `(x, y)`. */
|
|
99
|
+
release(x, y, id = 0) {
|
|
100
|
+
this.onPointerUp(id, x, y);
|
|
101
|
+
}
|
|
102
|
+
/** Full tap: press → release in one call. */
|
|
103
|
+
tap(x, y, id = 0) {
|
|
104
|
+
this.onPointerDown(id, x, y);
|
|
105
|
+
this.onPointerUp(id, x, y);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
function createMockInputManager() {
|
|
109
|
+
return new MockInputManager();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/scene-snapshot.ts
|
|
113
|
+
function serializeScene(world, components) {
|
|
114
|
+
const ids = /* @__PURE__ */ new Set();
|
|
115
|
+
const q = world.query(...components);
|
|
116
|
+
for (const id of q) ids.add(id);
|
|
117
|
+
for (const def of components) {
|
|
118
|
+
const singletonQ = world.query(def);
|
|
119
|
+
for (const id of singletonQ) ids.add(id);
|
|
120
|
+
}
|
|
121
|
+
const sortedIds = [...ids].sort((a, b) => a - b);
|
|
122
|
+
const entities = [];
|
|
123
|
+
for (const id of sortedIds) {
|
|
124
|
+
const comps = {};
|
|
125
|
+
for (const def of components) {
|
|
126
|
+
if (world.has(id, def)) comps[def.name] = world.get(id, def);
|
|
127
|
+
}
|
|
128
|
+
entities.push({ id, components: comps });
|
|
129
|
+
}
|
|
130
|
+
return { entities };
|
|
131
|
+
}
|
|
132
|
+
function snapshotsEqual(a, b) {
|
|
133
|
+
return deepEqual(a, b);
|
|
134
|
+
}
|
|
135
|
+
function deepEqual(a, b) {
|
|
136
|
+
if (Object.is(a, b)) return true;
|
|
137
|
+
if (typeof a !== typeof b) return false;
|
|
138
|
+
if (a === null || b === null) return false;
|
|
139
|
+
if (Array.isArray(a)) {
|
|
140
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
141
|
+
for (let i = 0; i < a.length; i++) {
|
|
142
|
+
if (!deepEqual(a[i], b[i])) return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
147
|
+
const ao = a;
|
|
148
|
+
const bo = b;
|
|
149
|
+
const ak = Object.keys(ao);
|
|
150
|
+
const bk = Object.keys(bo);
|
|
151
|
+
if (ak.length !== bk.length) return false;
|
|
152
|
+
for (const k of ak) {
|
|
153
|
+
if (!Object.hasOwn(bo, k)) return false;
|
|
154
|
+
if (!deepEqual(ao[k], bo[k])) return false;
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
161
|
+
0 && (module.exports = {
|
|
162
|
+
MockInputManager,
|
|
163
|
+
createFakeClock,
|
|
164
|
+
createMockInputManager,
|
|
165
|
+
createTestWorld,
|
|
166
|
+
serializeScene,
|
|
167
|
+
snapshotsEqual
|
|
168
|
+
});
|
|
169
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/fake-clock.ts","../src/test-world.ts","../src/mock-input.ts","../src/scene-snapshot.ts"],"sourcesContent":["export { createFakeClock } from './fake-clock.js';\nexport type { FakeClock } from './fake-clock.js';\nexport { createTestWorld } from './test-world.js';\nexport type { CreateTestWorldOptions, TestWorld } from './test-world.js';\nexport { MockInputManager, createMockInputManager } from './mock-input.js';\nexport { serializeScene, snapshotsEqual } from './scene-snapshot.js';\nexport type { SceneSnapshot, SceneSnapshotEntity } from './scene-snapshot.js';\n","/**\n * Fake monotonic clock for driving `GameLoop` deterministically in tests.\n *\n * Usage:\n * const fake = createFakeClock();\n * loop.tick = fake.requestFrame;\n * loop.cancelTick = fake.cancelFrame;\n * loop.start();\n * fake.advance(1 / 60); // runs one frame\n */\nexport interface FakeClock {\n /** Current fake timestamp in milliseconds. */\n readonly now: number;\n /** rAF-compatible request — stores the callback for the next `advance`. */\n requestFrame(callback: (time: number) => void): number;\n /** rAF-compatible cancel. */\n cancelFrame(id: number): void;\n /** Advance time by `seconds` and fire every pending callback once. */\n advance(seconds: number): void;\n /** Reset time and cancel all pending callbacks. */\n reset(): void;\n}\n\nexport function createFakeClock(): FakeClock {\n let nowMs = 0;\n let nextId = 1;\n const pending = new Map<number, (time: number) => void>();\n\n const clock: FakeClock = {\n get now() {\n return nowMs;\n },\n requestFrame(callback) {\n const id = nextId++;\n pending.set(id, callback);\n return id;\n },\n cancelFrame(id) {\n pending.delete(id);\n },\n advance(seconds) {\n if (seconds < 0) throw new Error('[@flare-engine/testing] cannot advance backwards');\n nowMs += seconds * 1000;\n // Snapshot callbacks so any newly scheduled ones via requestFrame during\n // this flush are deferred to the next advance — mirrors rAF semantics.\n const snapshot = [...pending.entries()];\n pending.clear();\n for (const [, cb] of snapshot) cb(nowMs);\n },\n reset() {\n nowMs = 0;\n pending.clear();\n nextId = 1;\n },\n };\n return clock;\n}\n","import { GameLoop } from '@flare-engine/core';\nimport { World } from '@flare-engine/ecs';\nimport { type FakeClock, createFakeClock } from './fake-clock.js';\n\nexport interface TestWorld {\n /** ECS world. */\n readonly world: World;\n /** Game loop wired to a fake clock. */\n readonly loop: GameLoop;\n /** Fake clock driving the loop. */\n readonly clock: FakeClock;\n /** Advance one frame of `dt` seconds. Runs the loop's fixed + variable update. */\n step(dt: number): void;\n /** Advance `frames` frames of `dt` seconds each. */\n stepFrames(frames: number, dt?: number): void;\n}\n\nexport interface CreateTestWorldOptions {\n /** Fixed timestep passed to `GameLoop`. Defaults to 1/60. */\n fixedDt?: number;\n /** Maximum raw dt cap passed to `GameLoop`. Defaults to 0.25s. */\n maxDt?: number;\n}\n\n/**\n * Create a World + GameLoop + FakeClock wired together for deterministic\n * testing. `step()` calls the loop's `step()` directly so fixed and variable\n * updates fire for the exact dt you pass, without needing to start the rAF\n * loop.\n */\nexport function createTestWorld(options: CreateTestWorldOptions = {}): TestWorld {\n const world = new World();\n const loop = new GameLoop(options);\n const clock = createFakeClock();\n loop.tick = clock.requestFrame;\n loop.cancelTick = clock.cancelFrame;\n\n return {\n world,\n loop,\n clock,\n step(dt) {\n loop.step(dt);\n },\n stepFrames(frames, dt = 1 / 60) {\n for (let i = 0; i < frames; i++) loop.step(dt);\n },\n };\n}\n","import { InputManager } from '@flare-engine/input';\n\n/**\n * `InputManager` subclass with scripted helpers for tests. Drives the same\n * internal state the real manager uses, so any system reading\n * `.pointer` / `.touches` sees identical data.\n */\nexport class MockInputManager extends InputManager {\n /** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */\n press(x: number, y: number, id = 0): void {\n this.onPointerDown(id, x, y);\n }\n /** Move a pointer to `(x, y)`. */\n move(x: number, y: number, id = 0): void {\n this.onPointerMove(id, x, y);\n }\n /** Release a pointer at `(x, y)`. */\n release(x: number, y: number, id = 0): void {\n this.onPointerUp(id, x, y);\n }\n /** Full tap: press → release in one call. */\n tap(x: number, y: number, id = 0): void {\n this.onPointerDown(id, x, y);\n this.onPointerUp(id, x, y);\n }\n}\n\nexport function createMockInputManager(): MockInputManager {\n return new MockInputManager();\n}\n","import type { ComponentDef, World } from '@flare-engine/ecs';\n\n/**\n * A single entity in a snapshot. Component names come from `ComponentDef.name`.\n */\nexport interface SceneSnapshotEntity {\n id: number;\n components: Record<string, unknown>;\n}\n\nexport interface SceneSnapshot {\n entities: SceneSnapshotEntity[];\n}\n\n/**\n * Serialize every entity that has at least one of the supplied components into\n * a stable, deterministic snapshot. Entities are sorted by id; component keys\n * are inserted in the order the components are passed.\n */\nexport function serializeScene(\n world: World,\n components: readonly ComponentDef<unknown>[],\n): SceneSnapshot {\n const ids = new Set<number>();\n const q = world.query(...components);\n for (const id of q) ids.add(id);\n // Also include entities that have any subset — the query above requires ALL.\n // For snapshots we want \"any of\". Iterate each component individually.\n for (const def of components) {\n const singletonQ = world.query(def);\n for (const id of singletonQ) ids.add(id);\n }\n\n const sortedIds = [...ids].sort((a, b) => a - b);\n const entities: SceneSnapshotEntity[] = [];\n for (const id of sortedIds) {\n const comps: Record<string, unknown> = {};\n for (const def of components) {\n if (world.has(id, def)) comps[def.name] = world.get(id, def);\n }\n entities.push({ id, components: comps });\n }\n return { entities };\n}\n\n/**\n * Deep-equal comparison good enough for snapshot assertions. Compares plain\n * objects, arrays, and primitives — no Date, Map, Set, or cyclic graphs.\n */\nexport function snapshotsEqual(a: SceneSnapshot, b: SceneSnapshot): boolean {\n return deepEqual(a, b);\n}\n\nfunction deepEqual(a: unknown, b: unknown): boolean {\n if (Object.is(a, b)) return true;\n if (typeof a !== typeof b) return false;\n if (a === null || b === null) return false;\n if (Array.isArray(a)) {\n if (!Array.isArray(b) || a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (!deepEqual(a[i], b[i])) return false;\n }\n return true;\n }\n if (typeof a === 'object' && typeof b === 'object') {\n const ao = a as Record<string, unknown>;\n const bo = b as Record<string, unknown>;\n const ak = Object.keys(ao);\n const bk = Object.keys(bo);\n if (ak.length !== bk.length) return false;\n for (const k of ak) {\n if (!Object.hasOwn(bo, k)) return false;\n if (!deepEqual(ao[k], bo[k])) return false;\n }\n return true;\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACuBO,SAAS,kBAA6B;AAC3C,MAAI,QAAQ;AACZ,MAAI,SAAS;AACb,QAAM,UAAU,oBAAI,IAAoC;AAExD,QAAM,QAAmB;AAAA,IACvB,IAAI,MAAM;AACR,aAAO;AAAA,IACT;AAAA,IACA,aAAa,UAAU;AACrB,YAAM,KAAK;AACX,cAAQ,IAAI,IAAI,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,YAAY,IAAI;AACd,cAAQ,OAAO,EAAE;AAAA,IACnB;AAAA,IACA,QAAQ,SAAS;AACf,UAAI,UAAU,EAAG,OAAM,IAAI,MAAM,kDAAkD;AACnF,eAAS,UAAU;AAGnB,YAAM,WAAW,CAAC,GAAG,QAAQ,QAAQ,CAAC;AACtC,cAAQ,MAAM;AACd,iBAAW,CAAC,EAAE,EAAE,KAAK,SAAU,IAAG,KAAK;AAAA,IACzC;AAAA,IACA,QAAQ;AACN,cAAQ;AACR,cAAQ,MAAM;AACd,eAAS;AAAA,IACX;AAAA,EACF;AACA,SAAO;AACT;;;ACxDA,kBAAyB;AACzB,iBAAsB;AA6Bf,SAAS,gBAAgB,UAAkC,CAAC,GAAc;AAC/E,QAAM,QAAQ,IAAI,iBAAM;AACxB,QAAM,OAAO,IAAI,qBAAS,OAAO;AACjC,QAAM,QAAQ,gBAAgB;AAC9B,OAAK,OAAO,MAAM;AAClB,OAAK,aAA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1;
|
|
5
|
+
const pending = /* @__PURE__ */ new Map();
|
|
6
|
+
const clock = {
|
|
7
|
+
get now() {
|
|
8
|
+
return nowMs;
|
|
9
|
+
},
|
|
10
|
+
requestFrame(callback) {
|
|
11
|
+
const id = nextId++;
|
|
12
|
+
pending.set(id, callback);
|
|
13
|
+
return id;
|
|
14
|
+
},
|
|
15
|
+
cancelFrame(id) {
|
|
16
|
+
pending.delete(id);
|
|
17
|
+
},
|
|
18
|
+
advance(seconds) {
|
|
19
|
+
if (seconds < 0) throw new Error("[@flare-engine/testing] cannot advance backwards");
|
|
20
|
+
nowMs += seconds * 1e3;
|
|
21
|
+
const snapshot = [...pending.entries()];
|
|
22
|
+
pending.clear();
|
|
23
|
+
for (const [, cb] of snapshot) cb(nowMs);
|
|
24
|
+
},
|
|
25
|
+
reset() {
|
|
26
|
+
nowMs = 0;
|
|
27
|
+
pending.clear();
|
|
28
|
+
nextId = 1;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return clock;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/test-world.ts
|
|
35
|
+
import { GameLoop } from "@flare-engine/core";
|
|
36
|
+
import { World } from "@flare-engine/ecs";
|
|
37
|
+
function createTestWorld(options = {}) {
|
|
38
|
+
const world = new World();
|
|
39
|
+
const loop = new GameLoop(options);
|
|
40
|
+
const clock = createFakeClock();
|
|
41
|
+
loop.tick = clock.requestFrame;
|
|
42
|
+
loop.cancelTick = clock.cancelFrame;
|
|
43
|
+
return {
|
|
44
|
+
world,
|
|
45
|
+
loop,
|
|
46
|
+
clock,
|
|
47
|
+
step(dt) {
|
|
48
|
+
loop.step(dt);
|
|
49
|
+
},
|
|
50
|
+
stepFrames(frames, dt = 1 / 60) {
|
|
51
|
+
for (let i = 0; i < frames; i++) loop.step(dt);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/mock-input.ts
|
|
57
|
+
import { InputManager } from "@flare-engine/input";
|
|
58
|
+
var MockInputManager = class extends InputManager {
|
|
59
|
+
/** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */
|
|
60
|
+
press(x, y, id = 0) {
|
|
61
|
+
this.onPointerDown(id, x, y);
|
|
62
|
+
}
|
|
63
|
+
/** Move a pointer to `(x, y)`. */
|
|
64
|
+
move(x, y, id = 0) {
|
|
65
|
+
this.onPointerMove(id, x, y);
|
|
66
|
+
}
|
|
67
|
+
/** Release a pointer at `(x, y)`. */
|
|
68
|
+
release(x, y, id = 0) {
|
|
69
|
+
this.onPointerUp(id, x, y);
|
|
70
|
+
}
|
|
71
|
+
/** Full tap: press → release in one call. */
|
|
72
|
+
tap(x, y, id = 0) {
|
|
73
|
+
this.onPointerDown(id, x, y);
|
|
74
|
+
this.onPointerUp(id, x, y);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function createMockInputManager() {
|
|
78
|
+
return new MockInputManager();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/scene-snapshot.ts
|
|
82
|
+
function serializeScene(world, components) {
|
|
83
|
+
const ids = /* @__PURE__ */ new Set();
|
|
84
|
+
const q = world.query(...components);
|
|
85
|
+
for (const id of q) ids.add(id);
|
|
86
|
+
for (const def of components) {
|
|
87
|
+
const singletonQ = world.query(def);
|
|
88
|
+
for (const id of singletonQ) ids.add(id);
|
|
89
|
+
}
|
|
90
|
+
const sortedIds = [...ids].sort((a, b) => a - b);
|
|
91
|
+
const entities = [];
|
|
92
|
+
for (const id of sortedIds) {
|
|
93
|
+
const comps = {};
|
|
94
|
+
for (const def of components) {
|
|
95
|
+
if (world.has(id, def)) comps[def.name] = world.get(id, def);
|
|
96
|
+
}
|
|
97
|
+
entities.push({ id, components: comps });
|
|
98
|
+
}
|
|
99
|
+
return { entities };
|
|
100
|
+
}
|
|
101
|
+
function snapshotsEqual(a, b) {
|
|
102
|
+
return deepEqual(a, b);
|
|
103
|
+
}
|
|
104
|
+
function deepEqual(a, b) {
|
|
105
|
+
if (Object.is(a, b)) return true;
|
|
106
|
+
if (typeof a !== typeof b) return false;
|
|
107
|
+
if (a === null || b === null) return false;
|
|
108
|
+
if (Array.isArray(a)) {
|
|
109
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
110
|
+
for (let i = 0; i < a.length; i++) {
|
|
111
|
+
if (!deepEqual(a[i], b[i])) return false;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
116
|
+
const ao = a;
|
|
117
|
+
const bo = b;
|
|
118
|
+
const ak = Object.keys(ao);
|
|
119
|
+
const bk = Object.keys(bo);
|
|
120
|
+
if (ak.length !== bk.length) return false;
|
|
121
|
+
for (const k of ak) {
|
|
122
|
+
if (!Object.hasOwn(bo, k)) return false;
|
|
123
|
+
if (!deepEqual(ao[k], bo[k])) return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
MockInputManager,
|
|
131
|
+
createFakeClock,
|
|
132
|
+
createMockInputManager,
|
|
133
|
+
createTestWorld,
|
|
134
|
+
serializeScene,
|
|
135
|
+
snapshotsEqual
|
|
136
|
+
};
|
|
137
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fake-clock.ts","../src/test-world.ts","../src/mock-input.ts","../src/scene-snapshot.ts"],"sourcesContent":["/**\n * Fake monotonic clock for driving `GameLoop` deterministically in tests.\n *\n * Usage:\n * const fake = createFakeClock();\n * loop.tick = fake.requestFrame;\n * loop.cancelTick = fake.cancelFrame;\n * loop.start();\n * fake.advance(1 / 60); // runs one frame\n */\nexport interface FakeClock {\n /** Current fake timestamp in milliseconds. */\n readonly now: number;\n /** rAF-compatible request — stores the callback for the next `advance`. */\n requestFrame(callback: (time: number) => void): number;\n /** rAF-compatible cancel. */\n cancelFrame(id: number): void;\n /** Advance time by `seconds` and fire every pending callback once. */\n advance(seconds: number): void;\n /** Reset time and cancel all pending callbacks. */\n reset(): void;\n}\n\nexport function createFakeClock(): FakeClock {\n let nowMs = 0;\n let nextId = 1;\n const pending = new Map<number, (time: number) => void>();\n\n const clock: FakeClock = {\n get now() {\n return nowMs;\n },\n requestFrame(callback) {\n const id = nextId++;\n pending.set(id, callback);\n return id;\n },\n cancelFrame(id) {\n pending.delete(id);\n },\n advance(seconds) {\n if (seconds < 0) throw new Error('[@flare-engine/testing] cannot advance backwards');\n nowMs += seconds * 1000;\n // Snapshot callbacks so any newly scheduled ones via requestFrame during\n // this flush are deferred to the next advance — mirrors rAF semantics.\n const snapshot = [...pending.entries()];\n pending.clear();\n for (const [, cb] of snapshot) cb(nowMs);\n },\n reset() {\n nowMs = 0;\n pending.clear();\n nextId = 1;\n },\n };\n return clock;\n}\n","import { GameLoop } from '@flare-engine/core';\nimport { World } from '@flare-engine/ecs';\nimport { type FakeClock, createFakeClock } from './fake-clock.js';\n\nexport interface TestWorld {\n /** ECS world. */\n readonly world: World;\n /** Game loop wired to a fake clock. */\n readonly loop: GameLoop;\n /** Fake clock driving the loop. */\n readonly clock: FakeClock;\n /** Advance one frame of `dt` seconds. Runs the loop's fixed + variable update. */\n step(dt: number): void;\n /** Advance `frames` frames of `dt` seconds each. */\n stepFrames(frames: number, dt?: number): void;\n}\n\nexport interface CreateTestWorldOptions {\n /** Fixed timestep passed to `GameLoop`. Defaults to 1/60. */\n fixedDt?: number;\n /** Maximum raw dt cap passed to `GameLoop`. Defaults to 0.25s. */\n maxDt?: number;\n}\n\n/**\n * Create a World + GameLoop + FakeClock wired together for deterministic\n * testing. `step()` calls the loop's `step()` directly so fixed and variable\n * updates fire for the exact dt you pass, without needing to start the rAF\n * loop.\n */\nexport function createTestWorld(options: CreateTestWorldOptions = {}): TestWorld {\n const world = new World();\n const loop = new GameLoop(options);\n const clock = createFakeClock();\n loop.tick = clock.requestFrame;\n loop.cancelTick = clock.cancelFrame;\n\n return {\n world,\n loop,\n clock,\n step(dt) {\n loop.step(dt);\n },\n stepFrames(frames, dt = 1 / 60) {\n for (let i = 0; i < frames; i++) loop.step(dt);\n },\n };\n}\n","import { InputManager } from '@flare-engine/input';\n\n/**\n * `InputManager` subclass with scripted helpers for tests. Drives the same\n * internal state the real manager uses, so any system reading\n * `.pointer` / `.touches` sees identical data.\n */\nexport class MockInputManager extends InputManager {\n /** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */\n press(x: number, y: number, id = 0): void {\n this.onPointerDown(id, x, y);\n }\n /** Move a pointer to `(x, y)`. */\n move(x: number, y: number, id = 0): void {\n this.onPointerMove(id, x, y);\n }\n /** Release a pointer at `(x, y)`. */\n release(x: number, y: number, id = 0): void {\n this.onPointerUp(id, x, y);\n }\n /** Full tap: press → release in one call. */\n tap(x: number, y: number, id = 0): void {\n this.onPointerDown(id, x, y);\n this.onPointerUp(id, x, y);\n }\n}\n\nexport function createMockInputManager(): MockInputManager {\n return new MockInputManager();\n}\n","import type { ComponentDef, World } from '@flare-engine/ecs';\n\n/**\n * A single entity in a snapshot. Component names come from `ComponentDef.name`.\n */\nexport interface SceneSnapshotEntity {\n id: number;\n components: Record<string, unknown>;\n}\n\nexport interface SceneSnapshot {\n entities: SceneSnapshotEntity[];\n}\n\n/**\n * Serialize every entity that has at least one of the supplied components into\n * a stable, deterministic snapshot. Entities are sorted by id; component keys\n * are inserted in the order the components are passed.\n */\nexport function serializeScene(\n world: World,\n components: readonly ComponentDef<unknown>[],\n): SceneSnapshot {\n const ids = new Set<number>();\n const q = world.query(...components);\n for (const id of q) ids.add(id);\n // Also include entities that have any subset — the query above requires ALL.\n // For snapshots we want \"any of\". Iterate each component individually.\n for (const def of components) {\n const singletonQ = world.query(def);\n for (const id of singletonQ) ids.add(id);\n }\n\n const sortedIds = [...ids].sort((a, b) => a - b);\n const entities: SceneSnapshotEntity[] = [];\n for (const id of sortedIds) {\n const comps: Record<string, unknown> = {};\n for (const def of components) {\n if (world.has(id, def)) comps[def.name] = world.get(id, def);\n }\n entities.push({ id, components: comps });\n }\n return { entities };\n}\n\n/**\n * Deep-equal comparison good enough for snapshot assertions. Compares plain\n * objects, arrays, and primitives — no Date, Map, Set, or cyclic graphs.\n */\nexport function snapshotsEqual(a: SceneSnapshot, b: SceneSnapshot): boolean {\n return deepEqual(a, b);\n}\n\nfunction deepEqual(a: unknown, b: unknown): boolean {\n if (Object.is(a, b)) return true;\n if (typeof a !== typeof b) return false;\n if (a === null || b === null) return false;\n if (Array.isArray(a)) {\n if (!Array.isArray(b) || a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (!deepEqual(a[i], b[i])) return false;\n }\n return true;\n }\n if (typeof a === 'object' && typeof b === 'object') {\n const ao = a as Record<string, unknown>;\n const bo = b as Record<string, unknown>;\n const ak = Object.keys(ao);\n const bk = Object.keys(bo);\n if (ak.length !== bk.length) return false;\n for (const k of ak) {\n if (!Object.hasOwn(bo, k)) return false;\n if (!deepEqual(ao[k], bo[k])) return false;\n }\n return true;\n }\n return false;\n}\n"],"mappings":";AAuBO,SAAS,kBAA6B;AAC3C,MAAI,QAAQ;AACZ,MAAI,SAAS;AACb,QAAM,UAAU,oBAAI,IAAoC;AAExD,QAAM,QAAmB;AAAA,IACvB,IAAI,MAAM;AACR,aAAO;AAAA,IACT;AAAA,IACA,aAAa,UAAU;AACrB,YAAM,KAAK;AACX,cAAQ,IAAI,IAAI,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,YAAY,IAAI;AACd,cAAQ,OAAO,EAAE;AAAA,IACnB;AAAA,IACA,QAAQ,SAAS;AACf,UAAI,UAAU,EAAG,OAAM,IAAI,MAAM,kDAAkD;AACnF,eAAS,UAAU;AAGnB,YAAM,WAAW,CAAC,GAAG,QAAQ,QAAQ,CAAC;AACtC,cAAQ,MAAM;AACd,iBAAW,CAAC,EAAE,EAAE,KAAK,SAAU,IAAG,KAAK;AAAA,IACzC;AAAA,IACA,QAAQ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,OAAO,OAAO,IAAI,CAAC,EAAG,QAAO;AAClC,UAAI,CAAC,UAAU,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,EAAG,QAAO;AAAA,IACvC;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flare-engine/testing",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Deterministic World factory, fake Clock, mock InputManager, and scene snapshot helpers",
|
|
5
|
+
"keywords": ["flare-engine", "testing", "ecs", "fake-clock", "snapshot"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Grzegorz Otto",
|
|
8
|
+
"homepage": "https://github.com/grzott/engine#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/grzott/flare-engine/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/grzott/flare-engine.git",
|
|
15
|
+
"directory": "packages/testing"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"module": "dist/index.mjs",
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.mjs",
|
|
24
|
+
"require": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"lint": "biome check src/",
|
|
32
|
+
"typecheck": "tsc --noEmit"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@flare-engine/core": "^0.1.0",
|
|
36
|
+
"@flare-engine/ecs": "^0.1.0",
|
|
37
|
+
"@flare-engine/input": "^0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "restricted",
|
|
41
|
+
"registry": "https://registry.npmjs.org/"
|
|
42
|
+
},
|
|
43
|
+
"sideEffects": false
|
|
44
|
+
}
|