@flare-engine/testing 0.2.1 → 0.3.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 +201 -21
- package/NOTICE +2 -0
- package/README.md +5 -3
- package/dist/index.d.mts +87 -83
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +87 -83
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +221 -229
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +211 -193
- package/dist/index.mjs.map +1 -1
- package/package.json +20 -12
package/dist/index.js
CHANGED
|
@@ -1,243 +1,235 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const id = nextId++;
|
|
45
|
-
pending.set(id, callback);
|
|
46
|
-
return id;
|
|
47
|
-
},
|
|
48
|
-
cancelFrame(id) {
|
|
49
|
-
pending.delete(id);
|
|
50
|
-
},
|
|
51
|
-
advance(seconds) {
|
|
52
|
-
if (seconds < 0) throw new Error("[@flare-engine/testing] cannot advance backwards");
|
|
53
|
-
nowMs += seconds * 1e3;
|
|
54
|
-
const snapshot = [...pending.entries()];
|
|
55
|
-
pending.clear();
|
|
56
|
-
for (const [, cb] of snapshot) cb(nowMs);
|
|
57
|
-
},
|
|
58
|
-
reset() {
|
|
59
|
-
nowMs = 0;
|
|
60
|
-
pending.clear();
|
|
61
|
-
nextId = 1;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
return clock;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _flare_engine_math = require("@flare-engine/math");
|
|
3
|
+
let _flare_engine_input = require("@flare-engine/input");
|
|
4
|
+
let _flare_engine_core = require("@flare-engine/core");
|
|
5
|
+
let _flare_engine_ecs = require("@flare-engine/ecs");
|
|
6
|
+
//#region src/bench-session.ts
|
|
7
|
+
/**
|
|
8
|
+
* Creates a deterministic bench session. Installs a SeededRNG-backed Math.random.
|
|
9
|
+
*
|
|
10
|
+
* Sessions must be ended LIFO if nested. Callers should wrap usage in try/finally
|
|
11
|
+
* so a throw between install and end() cannot leak the patched Math.random.
|
|
12
|
+
*/
|
|
13
|
+
function createBenchSession(opts = {}) {
|
|
14
|
+
const rng = new _flare_engine_math.SeededRNG(opts.seed ?? 1);
|
|
15
|
+
const originalRandom = Math.random;
|
|
16
|
+
const patched = () => rng.next();
|
|
17
|
+
Math.random = patched;
|
|
18
|
+
const flags = {};
|
|
19
|
+
let resolve = null;
|
|
20
|
+
const whenDone = new Promise((res) => {
|
|
21
|
+
resolve = res;
|
|
22
|
+
});
|
|
23
|
+
let doneResolved = false;
|
|
24
|
+
return {
|
|
25
|
+
rng,
|
|
26
|
+
setFlag(name, value) {
|
|
27
|
+
flags[name] = value;
|
|
28
|
+
},
|
|
29
|
+
getFlag(name) {
|
|
30
|
+
return flags[name] ?? false;
|
|
31
|
+
},
|
|
32
|
+
done() {
|
|
33
|
+
if (!doneResolved) {
|
|
34
|
+
doneResolved = true;
|
|
35
|
+
resolve?.();
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
whenDone,
|
|
39
|
+
end() {
|
|
40
|
+
if (Math.random === patched) Math.random = originalRandom;
|
|
41
|
+
else console.warn("[BenchSession] end() called out of order: Math.random is no longer this session's patch. Sessions must be ended in LIFO order; a seeded Math.random may have leaked into later tests.");
|
|
42
|
+
}
|
|
43
|
+
};
|
|
65
44
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/define-snapshot.ts
|
|
47
|
+
function defineSnapshot(schema) {
|
|
48
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
49
|
+
const keys = Object.keys(schema);
|
|
50
|
+
return (world) => {
|
|
51
|
+
let queries = cache.get(world);
|
|
52
|
+
if (queries === void 0) {
|
|
53
|
+
queries = keys.map((key) => {
|
|
54
|
+
const projector = schema[key];
|
|
55
|
+
return world.query(...projector.query);
|
|
56
|
+
});
|
|
57
|
+
cache.set(world, queries);
|
|
58
|
+
}
|
|
59
|
+
const result = {};
|
|
60
|
+
for (let i = 0; i < keys.length; i++) {
|
|
61
|
+
const key = keys[i];
|
|
62
|
+
const projector = schema[key];
|
|
63
|
+
const q = queries[i];
|
|
64
|
+
const ids = [];
|
|
65
|
+
for (const id of q) ids.push(id);
|
|
66
|
+
ids.sort((a, b) => a - b);
|
|
67
|
+
result[key] = ids.map((id) => projector.project(world, id));
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
};
|
|
87
71
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/fake-clock.ts
|
|
74
|
+
function createFakeClock() {
|
|
75
|
+
let nowMs = 0;
|
|
76
|
+
let nextId = 1;
|
|
77
|
+
const pending = /* @__PURE__ */ new Map();
|
|
78
|
+
return {
|
|
79
|
+
get now() {
|
|
80
|
+
return nowMs;
|
|
81
|
+
},
|
|
82
|
+
requestFrame(callback) {
|
|
83
|
+
const id = nextId++;
|
|
84
|
+
pending.set(id, callback);
|
|
85
|
+
return id;
|
|
86
|
+
},
|
|
87
|
+
cancelFrame(id) {
|
|
88
|
+
pending.delete(id);
|
|
89
|
+
},
|
|
90
|
+
advance(seconds) {
|
|
91
|
+
if (seconds < 0) throw new Error("[@flare-engine/testing] cannot advance backwards");
|
|
92
|
+
nowMs += seconds * 1e3;
|
|
93
|
+
const snapshot = [...pending.entries()];
|
|
94
|
+
pending.clear();
|
|
95
|
+
for (const [, cb] of snapshot) cb(nowMs);
|
|
96
|
+
},
|
|
97
|
+
reset() {
|
|
98
|
+
nowMs = 0;
|
|
99
|
+
pending.clear();
|
|
100
|
+
nextId = 1;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/mock-input.ts
|
|
106
|
+
/**
|
|
107
|
+
* `InputManager` subclass with scripted helpers for tests. Drives the same
|
|
108
|
+
* internal state the real manager uses, so any system reading
|
|
109
|
+
* `.pointer` / `.touches` sees identical data.
|
|
110
|
+
*/
|
|
111
|
+
var MockInputManager = class extends _flare_engine_input.InputManager {
|
|
112
|
+
/** Press a pointer at `(x, y)`. `id` defaults to 0 (primary). */
|
|
113
|
+
press(x, y, id = 0) {
|
|
114
|
+
this.onPointerDown(id, x, y);
|
|
115
|
+
}
|
|
116
|
+
/** Move a pointer to `(x, y)`. */
|
|
117
|
+
move(x, y, id = 0) {
|
|
118
|
+
this.onPointerMove(id, x, y);
|
|
119
|
+
}
|
|
120
|
+
/** Release a pointer at `(x, y)`. */
|
|
121
|
+
release(x, y, id = 0) {
|
|
122
|
+
this.onPointerUp(id, x, y);
|
|
123
|
+
}
|
|
124
|
+
/** Full tap: press → release in one call. */
|
|
125
|
+
tap(x, y, id = 0) {
|
|
126
|
+
this.onPointerDown(id, x, y);
|
|
127
|
+
this.onPointerUp(id, x, y);
|
|
128
|
+
}
|
|
109
129
|
};
|
|
110
130
|
function createMockInputManager() {
|
|
111
|
-
|
|
131
|
+
return new MockInputManager();
|
|
112
132
|
}
|
|
113
|
-
|
|
114
|
-
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/scene-snapshot.ts
|
|
135
|
+
const singletonQueries = /* @__PURE__ */ new WeakMap();
|
|
136
|
+
function singletonQuery(world, def) {
|
|
137
|
+
let byComponent = singletonQueries.get(world);
|
|
138
|
+
if (byComponent === void 0) {
|
|
139
|
+
byComponent = /* @__PURE__ */ new Map();
|
|
140
|
+
singletonQueries.set(world, byComponent);
|
|
141
|
+
}
|
|
142
|
+
let q = byComponent.get(def.id);
|
|
143
|
+
if (q === void 0) {
|
|
144
|
+
q = world.query(def);
|
|
145
|
+
byComponent.set(def.id, q);
|
|
146
|
+
}
|
|
147
|
+
return q;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Serialize every entity that has at least one of the supplied components into
|
|
151
|
+
* a stable, deterministic snapshot. Entities are sorted by id; component keys
|
|
152
|
+
* are inserted in the order the components are passed.
|
|
153
|
+
*/
|
|
115
154
|
function serializeScene(world, components) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
entities.push({ id, components: comps });
|
|
131
|
-
}
|
|
132
|
-
return { entities };
|
|
155
|
+
const ids = /* @__PURE__ */ new Set();
|
|
156
|
+
for (const def of components) for (const id of singletonQuery(world, def)) ids.add(id);
|
|
157
|
+
const sortedIds = [...ids].sort((a, b) => a - b);
|
|
158
|
+
const entities = [];
|
|
159
|
+
for (const id of sortedIds) {
|
|
160
|
+
const comps = {};
|
|
161
|
+
for (const def of components) if (world.has(id, def)) comps[def.name] = world.get(id, def);
|
|
162
|
+
entities.push({
|
|
163
|
+
id,
|
|
164
|
+
components: comps
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return { entities };
|
|
133
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Deep-equal comparison good enough for snapshot assertions. Compares plain
|
|
171
|
+
* objects, arrays, and primitives — no Date, Map, Set, or cyclic graphs.
|
|
172
|
+
*/
|
|
134
173
|
function snapshotsEqual(a, b) {
|
|
135
|
-
|
|
174
|
+
return deepEqual(a, b);
|
|
136
175
|
}
|
|
137
176
|
function deepEqual(a, b) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
return false;
|
|
177
|
+
if (Object.is(a, b)) return true;
|
|
178
|
+
if (typeof a !== typeof b) return false;
|
|
179
|
+
if (a === null || b === null) return false;
|
|
180
|
+
if (Array.isArray(a)) {
|
|
181
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
182
|
+
for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
186
|
+
const ao = a;
|
|
187
|
+
const bo = b;
|
|
188
|
+
const ak = Object.keys(ao);
|
|
189
|
+
const bk = Object.keys(bo);
|
|
190
|
+
if (ak.length !== bk.length) return false;
|
|
191
|
+
for (const k of ak) {
|
|
192
|
+
if (!Object.hasOwn(bo, k)) return false;
|
|
193
|
+
if (!deepEqual(ao[k], bo[k])) return false;
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
161
198
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
doneResolved = true;
|
|
188
|
-
resolve?.();
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
whenDone,
|
|
192
|
-
end() {
|
|
193
|
-
if (Math.random === patched) {
|
|
194
|
-
Math.random = originalRandom;
|
|
195
|
-
} else {
|
|
196
|
-
console.warn(
|
|
197
|
-
"[BenchSession] end() called out of order: Math.random is no longer this session's patch. Sessions must be ended in LIFO order; a seeded Math.random may have leaked into later tests."
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
};
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/test-world.ts
|
|
201
|
+
/**
|
|
202
|
+
* Create a World + GameLoop + FakeClock wired together for deterministic
|
|
203
|
+
* testing. `step()` calls the loop's `step()` directly so fixed and variable
|
|
204
|
+
* updates fire for the exact dt you pass, without needing to start the rAF
|
|
205
|
+
* loop.
|
|
206
|
+
*/
|
|
207
|
+
function createTestWorld(options = {}) {
|
|
208
|
+
const world = new _flare_engine_ecs.World();
|
|
209
|
+
const loop = new _flare_engine_core.GameLoop(options);
|
|
210
|
+
const clock = createFakeClock();
|
|
211
|
+
loop.tick = clock.requestFrame;
|
|
212
|
+
loop.cancelTick = clock.cancelFrame;
|
|
213
|
+
return {
|
|
214
|
+
world,
|
|
215
|
+
loop,
|
|
216
|
+
clock,
|
|
217
|
+
step(dt) {
|
|
218
|
+
loop.step(dt);
|
|
219
|
+
},
|
|
220
|
+
stepFrames(frames, dt = 1 / 60) {
|
|
221
|
+
for (let i = 0; i < frames; i++) loop.step(dt);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
202
224
|
}
|
|
225
|
+
//#endregion
|
|
226
|
+
exports.MockInputManager = MockInputManager;
|
|
227
|
+
exports.createBenchSession = createBenchSession;
|
|
228
|
+
exports.createFakeClock = createFakeClock;
|
|
229
|
+
exports.createMockInputManager = createMockInputManager;
|
|
230
|
+
exports.createTestWorld = createTestWorld;
|
|
231
|
+
exports.defineSnapshot = defineSnapshot;
|
|
232
|
+
exports.serializeScene = serializeScene;
|
|
233
|
+
exports.snapshotsEqual = snapshotsEqual;
|
|
203
234
|
|
|
204
|
-
// src/define-snapshot.ts
|
|
205
|
-
function defineSnapshot(schema) {
|
|
206
|
-
const cache = /* @__PURE__ */ new WeakMap();
|
|
207
|
-
const keys = Object.keys(schema);
|
|
208
|
-
return (world) => {
|
|
209
|
-
let queries = cache.get(world);
|
|
210
|
-
if (queries === void 0) {
|
|
211
|
-
queries = keys.map((key) => {
|
|
212
|
-
const projector = schema[key];
|
|
213
|
-
return world.query(...projector.query);
|
|
214
|
-
});
|
|
215
|
-
cache.set(world, queries);
|
|
216
|
-
}
|
|
217
|
-
const result = {};
|
|
218
|
-
for (let i = 0; i < keys.length; i++) {
|
|
219
|
-
const key = keys[i];
|
|
220
|
-
const projector = schema[key];
|
|
221
|
-
const q = queries[i];
|
|
222
|
-
const ids = [];
|
|
223
|
-
for (const id of q) {
|
|
224
|
-
ids.push(id);
|
|
225
|
-
}
|
|
226
|
-
ids.sort((a, b) => a - b);
|
|
227
|
-
result[key] = ids.map((id) => projector.project(world, id));
|
|
228
|
-
}
|
|
229
|
-
return result;
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
233
|
-
0 && (module.exports = {
|
|
234
|
-
MockInputManager,
|
|
235
|
-
createBenchSession,
|
|
236
|
-
createFakeClock,
|
|
237
|
-
createMockInputManager,
|
|
238
|
-
createTestWorld,
|
|
239
|
-
defineSnapshot,
|
|
240
|
-
serializeScene,
|
|
241
|
-
snapshotsEqual
|
|
242
|
-
});
|
|
243
235
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/fake-clock.ts","../src/test-world.ts","../src/mock-input.ts","../src/scene-snapshot.ts","../src/bench-session.ts","../src/define-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';\nexport { createBenchSession } from './bench-session.js';\nexport type { BenchSession, BenchSessionOptions } from './bench-session.js';\nexport { defineSnapshot } from './define-snapshot.js';\nexport type { SnapshotProjector, SnapshotSchema } from './define-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","import { SeededRNG } from '@flare-engine/math';\nimport type { SeededRNG as SeededRNGType } from '@flare-engine/math';\n\nexport interface BenchSessionOptions {\n seed?: number;\n}\n\nexport interface BenchSession {\n readonly rng: SeededRNGType;\n setFlag(name: string, value: boolean): void;\n getFlag(name: string): boolean;\n /** Resolves whenDone. Idempotent — safe to call multiple times. */\n done(): void;\n readonly whenDone: Promise<void>;\n /** Restores Math.random. Idempotent. */\n end(): void;\n}\n\n/**\n * Creates a deterministic bench session. Installs a SeededRNG-backed Math.random.\n *\n * Sessions must be ended LIFO if nested. Callers should wrap usage in try/finally\n * so a throw between install and end() cannot leak the patched Math.random.\n */\nexport function createBenchSession(opts: BenchSessionOptions = {}): BenchSession {\n const seed = opts.seed ?? 1;\n const rng = new SeededRNG(seed);\n\n const originalRandom = Math.random;\n const patched = () => rng.next();\n Math.random = patched;\n\n const flags: Record<string, boolean> = {};\n\n let resolve: (() => void) | null = null;\n const whenDone = new Promise<void>((res) => {\n resolve = res;\n });\n\n let doneResolved = false;\n\n return {\n rng,\n setFlag(name: string, value: boolean): void {\n flags[name] = value;\n },\n getFlag(name: string): boolean {\n return flags[name] ?? false;\n },\n done(): void {\n if (!doneResolved) {\n doneResolved = true;\n resolve?.();\n }\n },\n whenDone,\n end(): void {\n // Only restore when our own patch is still active — guards against\n // out-of-order end() clobbering a newer session's patch.\n if (Math.random === patched) {\n Math.random = originalRandom;\n } else {\n console.warn(\n \"[BenchSession] end() called out of order: Math.random is no longer this session's patch. \" +\n 'Sessions must be ended in LIFO order; a seeded Math.random may have leaked into later tests.',\n );\n }\n },\n };\n}\n","import type { ComponentDef, Query, World } from '@flare-engine/ecs';\n\nexport interface SnapshotProjector {\n readonly query: readonly ComponentDef<unknown>[];\n project(world: World, id: number): unknown;\n}\n\nexport interface SnapshotSchema {\n readonly [key: string]: SnapshotProjector;\n}\n\nexport function defineSnapshot<S extends SnapshotSchema>(\n schema: S,\n): (world: World) => { readonly [K in keyof S]: readonly unknown[] } {\n // One Query[] per world; queries auto-update so reuse is correct.\n const cache = new WeakMap<World, Query[]>();\n const keys = Object.keys(schema);\n\n return (world: World) => {\n let queries = cache.get(world);\n if (queries === undefined) {\n queries = keys.map((key) => {\n const projector = schema[key]!;\n return world.query(...(projector.query as ComponentDef<unknown>[]));\n });\n cache.set(world, queries);\n }\n\n const result: Record<string, readonly unknown[]> = {};\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i]!;\n const projector = schema[key]!;\n const q = queries[i]!;\n const ids: number[] = [];\n for (const id of q) {\n ids.push(id);\n }\n ids.sort((a, b) => a - b);\n result[key] = ids.map((id) => projector.project(world, id));\n }\n return result as { readonly [K in keyof S]: readonly unknown[] };\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACuBO,SAAS,kBAA6B;AAC3C,MAAI,QAAQ;AACZ,MAAI,SAAS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;AAAA,IACF;AAAA,IACA;AAAA,IACA,MAAY;AAGV,UAAI,KAAK,WAAW,SAAS;AAC3B,aAAK,SAAS;AAAA,MAChB,OAAO;AACL,gBAAQ;AAAA,UACN;AAAA,QAEF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;AC1DO,SAAS,eACd,QACmE;AAEnE,QAAM,QAAQ,oBAAI,QAAwB;AAC1C,QAAM,OAAO,OAAO,KAAK,MAAM;AAE/B,SAAO,CAAC,UAAiB;AACvB,QAAI,UAAU,MAAM,IAAI,KAAK;AAC7B,QAAI,YAAY,QAAW;AACzB,gBAAU,KAAK,IAAI,CAAC,QAAQ;AAC1B,cAAM,YAAY,OAAO,GAAG;AAC5B,eAAO,MAAM,MAAM,GAAI,UAAU,KAAiC;AAAA,MACpE,CAAC;AACD,YAAM,IAAI,OAAO,OAAO;AAAA,IAC1B;AAEA,UAAM,SAA6C,CAAC;AACpD,aAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,YAAM,MAAM,KAAK,CAAC;AAClB,YAAM,YAAY,OAAO,GAAG;AAC5B,YAAM,IAAI,QAAQ,CAAC;AACnB,YAAM,MAAgB,CAAC;AACvB,iBAAW,MAAM,GAAG;AAClB,YAAI,KAAK,EAAE;AAAA,MACb;AACA,UAAI,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AACxB,aAAO,GAAG,IAAI,IAAI,IAAI,CAAC,OAAO,UAAU,QAAQ,OAAO,EAAE,CAAC;AAAA,IAC5D;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["SeededRNG","InputManager","World","GameLoop"],"sources":["../src/bench-session.ts","../src/define-snapshot.ts","../src/fake-clock.ts","../src/mock-input.ts","../src/scene-snapshot.ts","../src/test-world.ts"],"sourcesContent":["import type { SeededRNG as SeededRNGType } from '@flare-engine/math';\nimport { SeededRNG } from '@flare-engine/math';\n\nexport interface BenchSessionOptions {\n seed?: number;\n}\n\nexport interface BenchSession {\n readonly rng: SeededRNGType;\n setFlag(name: string, value: boolean): void;\n getFlag(name: string): boolean;\n /** Resolves whenDone. Idempotent — safe to call multiple times. */\n done(): void;\n readonly whenDone: Promise<void>;\n /** Restores Math.random. Idempotent. */\n end(): void;\n}\n\n/**\n * Creates a deterministic bench session. Installs a SeededRNG-backed Math.random.\n *\n * Sessions must be ended LIFO if nested. Callers should wrap usage in try/finally\n * so a throw between install and end() cannot leak the patched Math.random.\n */\nexport function createBenchSession(opts: BenchSessionOptions = {}): BenchSession {\n const seed = opts.seed ?? 1;\n const rng = new SeededRNG(seed);\n\n const originalRandom = Math.random;\n const patched = () => rng.next();\n Math.random = patched;\n\n const flags: Record<string, boolean> = {};\n\n let resolve: (() => void) | null = null;\n const whenDone = new Promise<void>((res) => {\n resolve = res;\n });\n\n let doneResolved = false;\n\n return {\n rng,\n setFlag(name: string, value: boolean): void {\n flags[name] = value;\n },\n getFlag(name: string): boolean {\n return flags[name] ?? false;\n },\n done(): void {\n if (!doneResolved) {\n doneResolved = true;\n resolve?.();\n }\n },\n whenDone,\n end(): void {\n // Only restore when our own patch is still active — guards against\n // out-of-order end() clobbering a newer session's patch.\n if (Math.random === patched) {\n Math.random = originalRandom;\n } else {\n console.warn(\n \"[BenchSession] end() called out of order: Math.random is no longer this session's patch. \" +\n 'Sessions must be ended in LIFO order; a seeded Math.random may have leaked into later tests.',\n );\n }\n },\n };\n}\n","import type { ComponentDef, Query, World } from '@flare-engine/ecs';\n\nexport interface SnapshotProjector {\n readonly query: readonly ComponentDef<unknown>[];\n project(world: World, id: number): unknown;\n}\n\nexport interface SnapshotSchema {\n readonly [key: string]: SnapshotProjector;\n}\n\nexport function defineSnapshot<S extends SnapshotSchema>(\n schema: S,\n): (world: World) => { readonly [K in keyof S]: readonly unknown[] } {\n // One Query[] per world; queries auto-update so reuse is correct.\n const cache = new WeakMap<World, Query[]>();\n const keys = Object.keys(schema);\n\n return (world: World) => {\n let queries = cache.get(world);\n if (queries === undefined) {\n queries = keys.map((key) => {\n const projector = schema[key]!;\n return world.query(...(projector.query as ComponentDef<unknown>[]));\n });\n cache.set(world, queries);\n }\n\n const result: Record<string, readonly unknown[]> = {};\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i]!;\n const projector = schema[key]!;\n const q = queries[i]!;\n const ids: number[] = [];\n for (const id of q) {\n ids.push(id);\n }\n ids.sort((a, b) => a - b);\n result[key] = ids.map((id) => projector.project(world, id));\n }\n return result as { readonly [K in keyof S]: readonly unknown[] };\n };\n}\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 { 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, Query, World } from '@flare-engine/ecs';\n\n// One singleton Query per (world, component), memoized so repeat serializeScene\n// calls reuse them instead of registering a fresh (leaked) query each call.\n// Queries auto-update, so reuse always reflects current world state. Bounded: at\n// most one query per distinct component per world, all released when the world is\n// garbage-collected (the WeakMap holds no strong ref beyond the world's lifetime).\nconst singletonQueries = new WeakMap<World, Map<number, Query>>();\n\nfunction singletonQuery(world: World, def: ComponentDef<unknown>): Query {\n let byComponent = singletonQueries.get(world);\n if (byComponent === undefined) {\n byComponent = new Map();\n singletonQueries.set(world, byComponent);\n }\n let q = byComponent.get(def.id);\n if (q === undefined) {\n q = world.query(def);\n byComponent.set(def.id, q);\n }\n return q;\n}\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 // Collect every entity that has ANY of the supplied components (union of the\n // per-component queries). A separate ALL query would be redundant — anything\n // matching ALL already matches each singleton.\n const ids = new Set<number>();\n for (const def of components) {\n for (const id of singletonQuery(world, def)) 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","import { GameLoop } from '@flare-engine/core';\nimport { World } from '@flare-engine/ecs';\nimport { createFakeClock, type FakeClock } 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"],"mappings":";;;;;;;;;;;;AAwBA,SAAgB,mBAAmB,OAA4B,CAAC,GAAiB;CAE/E,MAAM,MAAM,IAAIA,mBAAAA,UADH,KAAK,QAAQ,CACI;CAE9B,MAAM,iBAAiB,KAAK;CAC5B,MAAM,gBAAgB,IAAI,KAAK;CAC/B,KAAK,SAAS;CAEd,MAAM,QAAiC,CAAC;CAExC,IAAI,UAA+B;CACnC,MAAM,WAAW,IAAI,SAAe,QAAQ;EAC1C,UAAU;CACZ,CAAC;CAED,IAAI,eAAe;CAEnB,OAAO;EACL;EACA,QAAQ,MAAc,OAAsB;GAC1C,MAAM,QAAQ;EAChB;EACA,QAAQ,MAAuB;GAC7B,OAAO,MAAM,SAAS;EACxB;EACA,OAAa;GACX,IAAI,CAAC,cAAc;IACjB,eAAe;IACf,UAAU;GACZ;EACF;EACA;EACA,MAAY;GAGV,IAAI,KAAK,WAAW,SAClB,KAAK,SAAS;QAEd,QAAQ,KACN,uLAEF;EAEJ;CACF;AACF;;;AC1DA,SAAgB,eACd,QACmE;CAEnE,MAAM,wBAAQ,IAAI,QAAwB;CAC1C,MAAM,OAAO,OAAO,KAAK,MAAM;CAE/B,QAAQ,UAAiB;EACvB,IAAI,UAAU,MAAM,IAAI,KAAK;EAC7B,IAAI,YAAY,KAA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