@rpgjs/testing 5.0.0-alpha.25 → 5.0.0-alpha.27
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/dist/index.d.ts +20 -62
- package/dist/index.js +163 -102
- package/dist/index.js.map +1 -1
- package/dist/setup.js +3 -0
- package/dist/setup.js.map +1 -1
- package/package.json +4 -3
- package/src/index.ts +552 -426
- package/src/setup.ts +5 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Provider } from '@signe/di';
|
|
2
|
-
import { RpgClientEngine, RpgClient } from '@rpgjs/client';
|
|
2
|
+
import { AbstractWebsocket, RpgClientEngine, RpgClient } from '@rpgjs/client';
|
|
3
3
|
import { RpgServer, RpgPlayer } from '@rpgjs/server';
|
|
4
4
|
/**
|
|
5
5
|
* Provides a default map loader for testing environments
|
|
@@ -22,8 +22,24 @@ import { RpgServer, RpgPlayer } from '@rpgjs/server';
|
|
|
22
22
|
*/
|
|
23
23
|
export declare function provideTestingLoadMap(): {
|
|
24
24
|
provide: string;
|
|
25
|
-
useFactory: (context: import('@
|
|
25
|
+
useFactory: (context: import('@rpgjs/client').Context) => void;
|
|
26
26
|
}[];
|
|
27
|
+
export interface TestingFixture {
|
|
28
|
+
createClient(): Promise<{
|
|
29
|
+
socket: AbstractWebsocket;
|
|
30
|
+
client: RpgClientEngine;
|
|
31
|
+
playerId: string;
|
|
32
|
+
player: RpgPlayer;
|
|
33
|
+
waitForMapChange(expectedMapId: string, timeout?: number): Promise<RpgPlayer>;
|
|
34
|
+
}>;
|
|
35
|
+
server: RpgServer;
|
|
36
|
+
clear(): Promise<void>;
|
|
37
|
+
applySyncToClient(): Promise<void>;
|
|
38
|
+
nextTick(timestamp?: number): Promise<void>;
|
|
39
|
+
nextTickTimes(times: number, timestamp?: number): Promise<void>;
|
|
40
|
+
wait(ms: number): Promise<void>;
|
|
41
|
+
waitUntil(promise: Promise<any>): Promise<void>;
|
|
42
|
+
}
|
|
27
43
|
/**
|
|
28
44
|
* Testing utility function to set up server and client instances for unit testing
|
|
29
45
|
*
|
|
@@ -55,65 +71,7 @@ export declare function provideTestingLoadMap(): {
|
|
|
55
71
|
export declare function testing(modules?: ({
|
|
56
72
|
server?: RpgServer;
|
|
57
73
|
client?: RpgClient;
|
|
58
|
-
} | Provider)[], clientConfig?: any, serverConfig?: any): Promise<
|
|
59
|
-
createClient(): Promise<{
|
|
60
|
-
readonly server: any;
|
|
61
|
-
socket: any;
|
|
62
|
-
client: RpgClientEngine<any>;
|
|
63
|
-
playerId: string;
|
|
64
|
-
readonly player: RpgPlayer;
|
|
65
|
-
/**
|
|
66
|
-
* Wait for player to be on a specific map
|
|
67
|
-
*
|
|
68
|
-
* This utility function polls the player's current map until it matches
|
|
69
|
-
* the expected map ID, or throws an error if the timeout is exceeded.
|
|
70
|
-
*
|
|
71
|
-
* @param expectedMapId - The expected map ID (without 'map-' prefix, e.g. 'map1')
|
|
72
|
-
* @param timeout - Maximum time to wait in milliseconds (default: 5000)
|
|
73
|
-
* @returns Promise that resolves when player is on the expected map
|
|
74
|
-
* @throws Error if timeout is exceeded
|
|
75
|
-
* @example
|
|
76
|
-
* ```ts
|
|
77
|
-
* const client = await fixture.createClient()
|
|
78
|
-
* await client.waitForMapChange('map1')
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
waitForMapChange(expectedMapId: string, timeout?: number): Promise<RpgPlayer>;
|
|
82
|
-
/**
|
|
83
|
-
* Manually trigger a game tick for processing inputs and physics
|
|
84
|
-
*
|
|
85
|
-
* This method is a convenience wrapper around the exported nextTick() function.
|
|
86
|
-
*
|
|
87
|
-
* @param timestamp - Optional timestamp to use for the tick (default: Date.now())
|
|
88
|
-
* @returns Promise that resolves when the tick is complete
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* const client = await fixture.createClient()
|
|
93
|
-
*
|
|
94
|
-
* // Manually advance the game by one tick
|
|
95
|
-
* await client.nextTick()
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
nextTick(timestamp?: number): Promise<void>;
|
|
99
|
-
}>;
|
|
100
|
-
/**
|
|
101
|
-
* Clear all server, client instances and reset the DOM
|
|
102
|
-
*
|
|
103
|
-
* This method should be called in afterEach to clean up test state.
|
|
104
|
-
* It destroys all created client instances, clears the server, and resets the DOM.
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
* ```ts
|
|
108
|
-
* const fixture = await testing([myModule])
|
|
109
|
-
*
|
|
110
|
-
* afterEach(() => {
|
|
111
|
-
* fixture.clear()
|
|
112
|
-
* })
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
clear(): void;
|
|
116
|
-
}>;
|
|
74
|
+
} | Provider)[], clientConfig?: any, serverConfig?: any): Promise<TestingFixture>;
|
|
117
75
|
/**
|
|
118
76
|
* Clear all caches and reset test state
|
|
119
77
|
*
|
|
@@ -136,7 +94,7 @@ export declare function testing(modules?: ({
|
|
|
136
94
|
* })
|
|
137
95
|
* ```
|
|
138
96
|
*/
|
|
139
|
-
export declare function clear(): void
|
|
97
|
+
export declare function clear(): Promise<void>;
|
|
140
98
|
/**
|
|
141
99
|
* Manually trigger a game tick for processing inputs and physics
|
|
142
100
|
*
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { mergeConfig } from './node_modules/.pnpm/@signe_di@2.6.0/node_modules/@
|
|
|
2
2
|
import { provideLoadMap, LoadMapToken, startGame, provideRpg, provideClientGlobalConfig, provideClientModules, inject, WebSocketToken, RpgClientEngine, clearInject } from '@rpgjs/client';
|
|
3
3
|
import { createServer, provideServerModules, clearInject as clearInject$1 } from '@rpgjs/server';
|
|
4
4
|
import { h, Container } from 'canvasengine';
|
|
5
|
-
import { combineLatest, filter, take, firstValueFrom } from 'rxjs';
|
|
5
|
+
import { Subject, combineLatest, filter, take, firstValueFrom, map, timer, switchMap, throwError, race } from 'rxjs';
|
|
6
6
|
|
|
7
7
|
function provideTestingLoadMap() {
|
|
8
8
|
return provideLoadMap((id) => {
|
|
@@ -66,75 +66,83 @@ function normalizeModules(modules) {
|
|
|
66
66
|
const globalFixtures = [];
|
|
67
67
|
async function testing(modules = [], clientConfig = {}, serverConfig = {}) {
|
|
68
68
|
const { serverModules, clientModules } = normalizeModules(modules);
|
|
69
|
+
const mapChangeSubject = new Subject();
|
|
69
70
|
const serverClass = createServer({
|
|
70
71
|
...serverConfig,
|
|
71
72
|
providers: [
|
|
72
|
-
provideServerModules(
|
|
73
|
+
provideServerModules([
|
|
74
|
+
...serverModules,
|
|
75
|
+
{
|
|
76
|
+
player: {
|
|
77
|
+
onJoinMap(player, map2) {
|
|
78
|
+
const mapId = map2?.id;
|
|
79
|
+
if (mapId) {
|
|
80
|
+
mapChangeSubject.next({ mapId, player });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
]),
|
|
73
86
|
...serverConfig.providers || []
|
|
74
87
|
]
|
|
75
88
|
});
|
|
89
|
+
const hasLoadMap = clientConfig.providers?.some((provider) => {
|
|
90
|
+
if (Array.isArray(provider)) {
|
|
91
|
+
return provider.some((p) => p?.provide === LoadMapToken);
|
|
92
|
+
}
|
|
93
|
+
return provider?.provide === LoadMapToken;
|
|
94
|
+
}) || false;
|
|
95
|
+
await startGame(
|
|
96
|
+
mergeConfig(
|
|
97
|
+
{
|
|
98
|
+
...clientConfig,
|
|
99
|
+
providers: [
|
|
100
|
+
provideClientGlobalConfig({
|
|
101
|
+
// TODO
|
|
102
|
+
// bootstrapCanvasOptions: {
|
|
103
|
+
// components: mockComponents,
|
|
104
|
+
// autoRegister: false,
|
|
105
|
+
// },
|
|
106
|
+
}),
|
|
107
|
+
...hasLoadMap ? [] : [provideTestingLoadMap()],
|
|
108
|
+
// Add only if not already provided
|
|
109
|
+
provideClientModules(clientModules),
|
|
110
|
+
...clientConfig.providers || []
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
providers: [provideRpg(serverClass, { env: { TEST: "true" } })]
|
|
115
|
+
}
|
|
116
|
+
)
|
|
117
|
+
);
|
|
118
|
+
const websocket = inject(WebSocketToken);
|
|
119
|
+
const clientEngine = inject(RpgClientEngine);
|
|
76
120
|
return {
|
|
77
121
|
async createClient() {
|
|
78
|
-
const
|
|
79
|
-
if (Array.isArray(provider)) {
|
|
80
|
-
return provider.some((p) => p?.provide === LoadMapToken);
|
|
81
|
-
}
|
|
82
|
-
return provider?.provide === LoadMapToken;
|
|
83
|
-
}) || false;
|
|
84
|
-
const context = await startGame(
|
|
85
|
-
mergeConfig({
|
|
86
|
-
...clientConfig,
|
|
87
|
-
providers: [
|
|
88
|
-
provideClientGlobalConfig({}),
|
|
89
|
-
...hasLoadMap ? [] : [provideTestingLoadMap()],
|
|
90
|
-
// Add only if not already provided
|
|
91
|
-
provideClientModules(clientModules),
|
|
92
|
-
...clientConfig.providers || []
|
|
93
|
-
]
|
|
94
|
-
}, {
|
|
95
|
-
providers: [provideRpg(serverClass)]
|
|
96
|
-
})
|
|
97
|
-
);
|
|
98
|
-
const websocket = inject(WebSocketToken);
|
|
99
|
-
const clientEngine = inject(RpgClientEngine);
|
|
100
|
-
const playerId = clientEngine.playerId;
|
|
101
|
-
if (!playerId) {
|
|
102
|
-
throw new Error("Player ID is not available");
|
|
103
|
-
}
|
|
104
|
-
const playerIdString = playerId;
|
|
105
|
-
const server = websocket.getServer();
|
|
106
|
-
setTimeout(() => {
|
|
107
|
-
try {
|
|
108
|
-
const serverMap = server?.subRoom;
|
|
109
|
-
if (serverMap && typeof serverMap.setAutoTick === "function") {
|
|
110
|
-
serverMap.setAutoTick(false);
|
|
111
|
-
}
|
|
112
|
-
} catch (error) {
|
|
113
|
-
}
|
|
114
|
-
}, 0);
|
|
115
|
-
const clientInstance = {
|
|
116
|
-
context,
|
|
117
|
-
clientEngine,
|
|
118
|
-
websocket,
|
|
119
|
-
server
|
|
120
|
-
};
|
|
121
|
-
globalFixtures.push(clientInstance);
|
|
122
|
-
return {
|
|
123
|
-
get server() {
|
|
124
|
-
return websocket.getServer();
|
|
125
|
-
},
|
|
122
|
+
const clientObj = {
|
|
126
123
|
socket: websocket.getSocket(),
|
|
127
124
|
client: clientEngine,
|
|
128
|
-
playerId
|
|
125
|
+
get playerId() {
|
|
126
|
+
return Object.keys(websocket.getServer().subRoom.players())[0];
|
|
127
|
+
},
|
|
129
128
|
get player() {
|
|
130
|
-
return
|
|
129
|
+
return websocket.getServer().subRoom.players()[clientObj.playerId];
|
|
131
130
|
},
|
|
132
131
|
/**
|
|
133
132
|
* Wait for player to be on a specific map
|
|
134
|
-
*
|
|
135
|
-
* This utility function
|
|
136
|
-
* the expected map
|
|
137
|
-
*
|
|
133
|
+
*
|
|
134
|
+
* This utility function waits for the `onJoinMap` hook to be triggered
|
|
135
|
+
* when the player joins the expected map, or throws an error if the timeout is exceeded.
|
|
136
|
+
*
|
|
137
|
+
* ## Design
|
|
138
|
+
*
|
|
139
|
+
* Uses RxJS to listen for map change events emitted by `onJoinMap`. The function:
|
|
140
|
+
* 1. Checks if the player is already on the expected map
|
|
141
|
+
* 2. Subscribes to the `mapChangeSubject` observable
|
|
142
|
+
* 3. Filters events to match the expected map ID
|
|
143
|
+
* 4. Uses `race` operator with a timer to implement timeout handling
|
|
144
|
+
* 5. Resolves with the player when the map change event is received
|
|
145
|
+
*
|
|
138
146
|
* @param expectedMapId - The expected map ID (without 'map-' prefix, e.g. 'map1')
|
|
139
147
|
* @param timeout - Maximum time to wait in milliseconds (default: 5000)
|
|
140
148
|
* @returns Promise that resolves when player is on the expected map
|
|
@@ -146,61 +154,115 @@ async function testing(modules = [], clientConfig = {}, serverConfig = {}) {
|
|
|
146
154
|
* ```
|
|
147
155
|
*/
|
|
148
156
|
async waitForMapChange(expectedMapId, timeout = 5e3) {
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (currentMap2?.id === expectedMapId) {
|
|
153
|
-
return this.player;
|
|
154
|
-
}
|
|
155
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
157
|
+
const currentMap = clientObj.player.getCurrentMap();
|
|
158
|
+
if (currentMap?.id === expectedMapId) {
|
|
159
|
+
return clientObj.player;
|
|
156
160
|
}
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
161
|
+
const mapChange$ = mapChangeSubject.pipe(
|
|
162
|
+
filter((event) => event.mapId === expectedMapId),
|
|
163
|
+
take(1),
|
|
164
|
+
map((event) => event.player)
|
|
160
165
|
);
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
166
|
+
const timeout$ = timer(timeout).pipe(
|
|
167
|
+
take(1),
|
|
168
|
+
switchMap(() => {
|
|
169
|
+
const currentMap2 = clientObj.player.getCurrentMap();
|
|
170
|
+
return throwError(() => new Error(
|
|
171
|
+
`Timeout: Player did not reach map ${expectedMapId} within ${timeout}ms. Current map: ${currentMap2?.id || "null"}`
|
|
172
|
+
));
|
|
173
|
+
})
|
|
174
|
+
);
|
|
175
|
+
try {
|
|
176
|
+
const result = await firstValueFrom(race([mapChange$, timeout$]));
|
|
177
|
+
return result;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error instanceof Error) {
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
const currentMap2 = clientObj.player.getCurrentMap();
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Timeout: Player did not reach map ${expectedMapId} within ${timeout}ms. Current map: ${currentMap2?.id || "null"}`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
180
187
|
}
|
|
181
188
|
};
|
|
189
|
+
return clientObj;
|
|
190
|
+
},
|
|
191
|
+
get server() {
|
|
192
|
+
return websocket.getServer();
|
|
182
193
|
},
|
|
183
194
|
/**
|
|
184
195
|
* Clear all server, client instances and reset the DOM
|
|
185
|
-
*
|
|
196
|
+
*
|
|
186
197
|
* This method should be called in afterEach to clean up test state.
|
|
187
198
|
* It destroys all created client instances, clears the server, and resets the DOM.
|
|
188
|
-
*
|
|
199
|
+
*
|
|
189
200
|
* @example
|
|
190
201
|
* ```ts
|
|
191
202
|
* const fixture = await testing([myModule])
|
|
192
|
-
*
|
|
203
|
+
*
|
|
193
204
|
* afterEach(() => {
|
|
194
205
|
* fixture.clear()
|
|
195
206
|
* })
|
|
196
207
|
* ```
|
|
197
208
|
*/
|
|
198
209
|
clear() {
|
|
199
|
-
clear();
|
|
210
|
+
return clear();
|
|
211
|
+
},
|
|
212
|
+
async applySyncToClient() {
|
|
213
|
+
websocket.getServer().subRoom.applySyncToClient();
|
|
214
|
+
await waitForSync(clientEngine);
|
|
215
|
+
},
|
|
216
|
+
/**
|
|
217
|
+
* Manually trigger a game tick for processing inputs and physics
|
|
218
|
+
*
|
|
219
|
+
* This method is a convenience wrapper around the exported nextTick() function.
|
|
220
|
+
* It uses the clientEngine from the fixture, so no client parameter is needed.
|
|
221
|
+
*
|
|
222
|
+
* @param timestamp - Optional timestamp to use for the tick (default: Date.now())
|
|
223
|
+
* @returns Promise that resolves when the tick is complete
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* const fixture = await testing([myModule])
|
|
228
|
+
*
|
|
229
|
+
* // Manually advance the game by one tick
|
|
230
|
+
* await fixture.nextTick()
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
async nextTick(timestamp) {
|
|
234
|
+
return nextTick(clientEngine);
|
|
235
|
+
},
|
|
236
|
+
async nextTickTimes(times, timestamp) {
|
|
237
|
+
for (let i = 0; i < times; i++) {
|
|
238
|
+
await nextTick(clientEngine);
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
async wait(ms) {
|
|
242
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
243
|
+
},
|
|
244
|
+
async waitUntil(promise) {
|
|
245
|
+
let finish = false;
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
promise.then((value) => {
|
|
248
|
+
finish = true;
|
|
249
|
+
resolve(value);
|
|
250
|
+
}).catch(reject);
|
|
251
|
+
const timeout = () => {
|
|
252
|
+
setTimeout(() => {
|
|
253
|
+
if (!finish) {
|
|
254
|
+
nextTick(clientEngine);
|
|
255
|
+
timeout();
|
|
256
|
+
}
|
|
257
|
+
}, 0);
|
|
258
|
+
};
|
|
259
|
+
timeout();
|
|
260
|
+
});
|
|
200
261
|
}
|
|
201
262
|
};
|
|
202
263
|
}
|
|
203
|
-
function clear() {
|
|
264
|
+
async function clear() {
|
|
265
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
204
266
|
for (const client of globalFixtures) {
|
|
205
267
|
try {
|
|
206
268
|
if (client.clientEngine && typeof client.clientEngine.clear === "function") {
|
|
@@ -251,9 +313,7 @@ async function nextTick(client, timestamp) {
|
|
|
251
313
|
await serverMap.processInput(player.id);
|
|
252
314
|
}
|
|
253
315
|
}
|
|
254
|
-
|
|
255
|
-
serverMap.runFixedTicks(delta);
|
|
256
|
-
}
|
|
316
|
+
serverMap.nextTick(delta);
|
|
257
317
|
for (const player of serverMap.getPlayers()) {
|
|
258
318
|
if (player && typeof player.syncChanges === "function") {
|
|
259
319
|
player.syncChanges();
|
|
@@ -272,7 +332,9 @@ async function waitForSync(client, timeout = 1e3) {
|
|
|
272
332
|
const playersReceived$ = client.playersReceived$;
|
|
273
333
|
const eventsReceived$ = client.eventsReceived$;
|
|
274
334
|
if (!playersReceived$ || !eventsReceived$) {
|
|
275
|
-
throw new Error(
|
|
335
|
+
throw new Error(
|
|
336
|
+
"waitForSync: playersReceived$ or eventsReceived$ not found in client"
|
|
337
|
+
);
|
|
276
338
|
}
|
|
277
339
|
const playersAlreadyTrue = playersReceived$.getValue ? playersReceived$.getValue() === true : false;
|
|
278
340
|
const eventsAlreadyTrue = eventsReceived$.getValue ? eventsReceived$.getValue() === true : false;
|
|
@@ -284,18 +346,17 @@ async function waitForSync(client, timeout = 1e3) {
|
|
|
284
346
|
const syncComplete$ = combineLatest([
|
|
285
347
|
playersReceived$.pipe(filter((received) => received === true)),
|
|
286
348
|
eventsReceived$.pipe(filter((received) => received === true))
|
|
287
|
-
]).pipe(
|
|
288
|
-
take(1)
|
|
289
|
-
);
|
|
349
|
+
]).pipe(take(1));
|
|
290
350
|
const timeoutPromise = new Promise((_, reject) => {
|
|
291
351
|
setTimeout(() => {
|
|
292
|
-
reject(
|
|
352
|
+
reject(
|
|
353
|
+
new Error(
|
|
354
|
+
`waitForSync: Timeout after ${timeout}ms. Synchronization did not complete.`
|
|
355
|
+
)
|
|
356
|
+
);
|
|
293
357
|
}, timeout);
|
|
294
358
|
});
|
|
295
|
-
await Promise.race([
|
|
296
|
-
firstValueFrom(syncComplete$),
|
|
297
|
-
timeoutPromise
|
|
298
|
-
]);
|
|
359
|
+
await Promise.race([firstValueFrom(syncComplete$), timeoutPromise]);
|
|
299
360
|
}
|
|
300
361
|
async function waitForSyncComplete(player, client, timeout = 1e3) {
|
|
301
362
|
if (!client) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { mergeConfig, Provider } from \"@signe/di\";\nimport { provideRpg, startGame, provideClientModules, provideLoadMap, provideClientGlobalConfig, inject, WebSocketToken, AbstractWebsocket, RpgClientEngine, RpgClient, LoadMapToken } from \"@rpgjs/client\";\nimport { createServer, provideServerModules, RpgServer, RpgPlayer } from \"@rpgjs/server\";\nimport { h, Container } from \"canvasengine\";\nimport { clearInject as clearClientInject } from \"@rpgjs/client\";\nimport { clearInject as clearServerInject } from \"@rpgjs/server\";\nimport { combineLatest, filter, take, race, timer, firstValueFrom } from \"rxjs\";\n\n/**\n * Provides a default map loader for testing environments\n * \n * This function returns a `provideLoadMap` provider that creates mock maps\n * with default dimensions (1024x768) and a minimal component. It's automatically\n * used by `testing()` if no custom `provideLoadMap` is provided in `clientConfig.providers`.\n * \n * @returns A provider function that can be used with `provideLoadMap`\n * @example\n * ```ts\n * // Used automatically by testing()\n * const fixture = await testing([myModule])\n * \n * // Or use directly in clientConfig\n * const fixture = await testing([myModule], {\n * providers: [provideTestingLoadMap()]\n * })\n * ```\n */\nexport function provideTestingLoadMap() {\n return provideLoadMap((id: string) => {\n return {\n id,\n data: {\n width: 1024,\n height: 768,\n hitboxes: [],\n params: {}\n },\n component: h(Container),\n width: 1024,\n height: 768,\n }\n })\n}\n\n/**\n * Normalizes modules input to extract server/client modules from createModule providers or direct module objects\n * \n * @param modules - Array of modules that can be either:\n * - Direct module objects: { server: RpgServer, client: RpgClient }\n * - Providers returned by createModule(): Provider[] with meta.server/client and useValue\n * @returns Object with separate arrays for server and client modules\n * @example\n * ```ts\n * // Direct modules\n * normalizeModules([{ server: serverModule, client: clientModule }])\n * \n * // createModule providers\n * const providers = createModule('MyModule', [{ server: serverModule, client: clientModule }])\n * normalizeModules(providers)\n * ```\n */\nfunction normalizeModules(modules: any[]): { serverModules: RpgServer[], clientModules: RpgClient[] } {\n if (!modules || modules.length === 0) {\n return { serverModules: [], clientModules: [] }\n }\n\n // Check if first item is a provider (has meta and useValue properties)\n const isProviderArray = modules.some((item: any) => \n item && typeof item === 'object' && 'meta' in item && 'useValue' in item\n )\n\n const serverModules: RpgServer[] = []\n const clientModules: RpgClient[] = []\n\n if (!isProviderArray) {\n // Direct module objects, extract server and client separately\n modules.forEach((module: any) => {\n if (module && typeof module === 'object') {\n if (module.server) {\n serverModules.push(module.server)\n }\n if (module.client) {\n clientModules.push(module.client)\n }\n }\n })\n return { serverModules, clientModules }\n }\n\n // Extract modules from createModule providers\n // createModule returns providers where useValue contains the original { server, client } object\n // We need to group providers by their useValue to reconstruct the original modules\n const seenUseValues = new Set<any>()\n\n modules.forEach((provider: any) => {\n if (!provider || typeof provider !== 'object' || !('meta' in provider) || !('useValue' in provider)) {\n return\n }\n\n const { useValue } = provider\n \n // Skip if we've already processed this useValue (same module, different provider for server/client)\n if (seenUseValues.has(useValue)) {\n return\n }\n\n // Check if useValue has server or client properties (it's a module object)\n if (useValue && typeof useValue === 'object' && ('server' in useValue || 'client' in useValue)) {\n seenUseValues.add(useValue)\n if (useValue.server) {\n serverModules.push(useValue.server)\n }\n if (useValue.client) {\n clientModules.push(useValue.client)\n }\n }\n })\n\n return { serverModules, clientModules }\n}\n\n// Global storage for all created fixtures and clients (for clear() function)\nconst globalFixtures: Array<{\n context: any;\n clientEngine: RpgClientEngine;\n websocket: AbstractWebsocket;\n server?: any;\n}> = []\n\n/**\n * Testing utility function to set up server and client instances for unit testing\n * \n * This function creates a test environment with both server and client instances,\n * allowing you to test player interactions, server hooks, and game mechanics.\n * \n * @param modules - Array of modules that can be either:\n * - Direct module objects: { server: RpgServer, client: RpgClient }\n * - Providers returned by createModule(): Provider[] with meta.server/client and useValue\n * @param clientConfig - Optional client configuration\n * @param serverConfig - Optional server configuration\n * @returns Testing fixture with createClient method\n * @example\n * ```ts\n * // Using direct modules\n * const fixture = await testing([{\n * server: serverModule,\n * client: clientModule\n * }])\n * \n * // Using createModule\n * const myModule = createModule('MyModule', [{\n * server: serverModule,\n * client: clientModule\n * }])\n * const fixture = await testing(myModule)\n * ```\n */\nexport async function testing(\n modules: ({ server?: RpgServer, client?: RpgClient } | Provider)[] = [], \n clientConfig: any = {}, \n serverConfig: any = {}\n) {\n // Normalize modules to extract server/client from providers if needed\n const { serverModules, clientModules } = normalizeModules(modules as any[])\n \n const serverClass = createServer({\n ...serverConfig,\n providers: [\n provideServerModules(serverModules),\n ...(serverConfig.providers || [])\n ]\n })\n \n // Store created instances for cleanup\n const createdClients: Array<{\n context: any;\n clientEngine: RpgClientEngine;\n websocket: AbstractWebsocket;\n server?: any;\n }> = []\n \n return {\n async createClient() {\n // Check if LoadMapToken is already provided in clientConfig.providers\n // (provideLoadMap returns an array with LoadMapToken)\n const hasLoadMap = clientConfig.providers?.some((provider: any) => {\n if (Array.isArray(provider)) {\n return provider.some((p: any) => p?.provide === LoadMapToken)\n }\n return provider?.provide === LoadMapToken\n }) || false\n \n const context = await startGame(\n mergeConfig({\n ...clientConfig,\n providers: [\n provideClientGlobalConfig({}),\n ...(hasLoadMap ? [] : [provideTestingLoadMap()]), // Add only if not already provided\n provideClientModules(clientModules),\n ...(clientConfig.providers || [])\n ]\n }, {\n providers: [provideRpg(serverClass)],\n })\n )\n const websocket = inject<AbstractWebsocket>(WebSocketToken) as any\n const clientEngine = inject<RpgClientEngine>(RpgClientEngine)\n const playerId = clientEngine.playerId\n if (!playerId) {\n throw new Error('Player ID is not available')\n }\n const playerIdString: string = playerId\n \n // Get server instance\n const server = websocket.getServer()\n \n // Disable auto tick for unit tests (manual control via nextTick)\n // The map will be available after the player connects, so we disable it asynchronously\n setTimeout(() => {\n try {\n const serverMap = server?.subRoom as any\n if (serverMap && typeof serverMap.setAutoTick === 'function') {\n serverMap.setAutoTick(false)\n }\n } catch (error) {\n // Ignore errors if map is not ready yet\n }\n }, 0)\n \n // Store instance for cleanup (both locally and globally)\n const clientInstance = {\n context,\n clientEngine,\n websocket,\n server\n }\n createdClients.push(clientInstance)\n globalFixtures.push(clientInstance)\n \n return {\n get server() {\n return websocket.getServer()\n },\n socket: websocket.getSocket(),\n client: clientEngine,\n playerId: playerIdString,\n get player(): RpgPlayer {\n return this.server.subRoom.players()[playerIdString] as RpgPlayer\n },\n /**\n * Wait for player to be on a specific map\n * \n * This utility function polls the player's current map until it matches\n * the expected map ID, or throws an error if the timeout is exceeded.\n * \n * @param expectedMapId - The expected map ID (without 'map-' prefix, e.g. 'map1')\n * @param timeout - Maximum time to wait in milliseconds (default: 5000)\n * @returns Promise that resolves when player is on the expected map\n * @throws Error if timeout is exceeded\n * @example\n * ```ts\n * const client = await fixture.createClient()\n * await client.waitForMapChange('map1')\n * ```\n */\n async waitForMapChange(expectedMapId: string, timeout = 5000): Promise<RpgPlayer> {\n const startTime = Date.now()\n\n while (Date.now() - startTime < timeout) {\n const currentMap = this.player.getCurrentMap()\n if (currentMap?.id === expectedMapId) {\n return this.player\n }\n await new Promise(resolve => setTimeout(resolve, 50)) // Wait 50ms before next check\n }\n \n const currentMap = this.player.getCurrentMap()\n throw new Error(\n `Timeout: Player did not reach map ${expectedMapId} within ${timeout}ms. ` +\n `Current map: ${currentMap?.id || 'null'}`\n )\n },\n /**\n * Manually trigger a game tick for processing inputs and physics\n * \n * This method is a convenience wrapper around the exported nextTick() function.\n * \n * @param timestamp - Optional timestamp to use for the tick (default: Date.now())\n * @returns Promise that resolves when the tick is complete\n * \n * @example\n * ```ts\n * const client = await fixture.createClient()\n * \n * // Manually advance the game by one tick\n * await client.nextTick()\n * ```\n */\n async nextTick(timestamp?: number): Promise<void> {\n return nextTick(this.client, timestamp)\n }\n }\n },\n /**\n * Clear all server, client instances and reset the DOM\n * \n * This method should be called in afterEach to clean up test state.\n * It destroys all created client instances, clears the server, and resets the DOM.\n * \n * @example\n * ```ts\n * const fixture = await testing([myModule])\n * \n * afterEach(() => {\n * fixture.clear()\n * })\n * ```\n */\n clear() {\n // Use the global clear function\n clear()\n }\n }\n}\n\n/**\n * Clear all caches and reset test state\n * \n * This function should be called after the end of each test to clean up\n * all server and client instances, clear caches, and reset the DOM.\n * \n * ## Design\n * \n * Cleans up all created fixtures, client engines, server instances, and resets\n * the DOM to a clean state. This ensures no state leaks between tests.\n * \n * @returns void\n * \n * @example\n * ```ts\n * import { clear } from '@rpgjs/testing'\n * \n * afterEach(() => {\n * clear()\n * })\n * ```\n */\nexport function clear(): void {\n // Clean up all created client and server instances from all fixtures\n for (const client of globalFixtures) {\n try {\n // Clear client engine\n if (client.clientEngine && typeof (client.clientEngine as any).clear === 'function') {\n (client.clientEngine as any).clear()\n }\n \n // Clear server map (subRoom)\n const serverMap = client.server?.subRoom as any\n if (serverMap && typeof serverMap.clear === 'function') {\n serverMap.clear()\n }\n } catch (error) {\n // Silently ignore cleanup errors\n console.warn('Error during cleanup:', error)\n }\n }\n \n // Clear the global fixtures array\n globalFixtures.length = 0\n \n // Clear client context injection\n try {\n clearClientInject()\n } catch (error) {\n console.warn('Error clearing client inject:', error)\n }\n \n // Clear server context injection\n try {\n clearServerInject()\n } catch (error) {\n console.warn('Error clearing server inject:', error)\n }\n \n // Reset DOM\n if (typeof window !== 'undefined' && window.document) {\n window.document.body.innerHTML = `<div id=\"rpg\"></div>`\n }\n}\n\n/**\n * Manually trigger a game tick for processing inputs and physics\n * \n * This function allows you to manually advance the game by one tick.\n * It performs the following operations:\n * 1. On server: processes pending inputs and advances physics\n * 2. Server sends data to client\n * 3. Client retrieves data and performs inputs (move, etc.) and server reconciliation\n * 4. A tick is performed on the client\n * 5. A tick is performed on VueJS (if Vue is used)\n * \n * @param client - The RpgClientEngine instance\n * @param timestamp - Optional timestamp to use for the tick (default: Date.now())\n * @returns Promise that resolves when the tick is complete\n * \n * @example\n * ```ts\n * import { nextTick } from '@rpgjs/testing'\n * \n * const client = await fixture.createClient()\n * \n * // Manually advance the game by one tick\n * await nextTick(client.client, Date.now())\n * ```\n */\nexport async function nextTick(client: RpgClientEngine, timestamp?: number): Promise<void> {\n if (!client) {\n throw new Error('nextTick: client parameter is required')\n }\n\n const tickTimestamp = timestamp ?? Date.now()\n const delta = 16 // 16ms for 60fps\n\n // Get server instance from client context\n const websocket = (client as any).webSocket\n if (!websocket) {\n throw new Error('nextTick: websocket not found in client')\n }\n\n const server = websocket.getServer()\n if (!server) {\n throw new Error('nextTick: server not found')\n }\n\n // Get server map (subRoom)\n const serverMap = server.subRoom as any\n if (!serverMap) {\n return\n }\n\n // 1. On server: Process inputs for all players\n for (const player of serverMap.getPlayers()) {\n if (player.pendingInputs && player.pendingInputs.length > 0) {\n await serverMap.processInput(player.id)\n }\n }\n\n // 2. Run physics tick on server map\n if (typeof serverMap.runFixedTicks === 'function') {\n serverMap.runFixedTicks(delta)\n }\n\n // 3. Server sends data to client - trigger sync for all players\n // The sync is triggered by calling syncChanges() on each player\n for (const player of serverMap.getPlayers()) {\n if (player && typeof (player as any).syncChanges === 'function') {\n (player as any).syncChanges()\n }\n }\n\n // 4. Client retrieves data and performs reconciliation\n // The sync data will be received by the client through the websocket\n // We need to wait a bit for the sync data to be processed\n await new Promise(resolve => setTimeout(resolve, 0))\n\n // 5. Run physics tick on client map (performs client-side prediction)\n const sceneMap = (client as any).sceneMap\n if (sceneMap && typeof sceneMap.stepPredictionTick === 'function') {\n sceneMap.stepPredictionTick()\n }\n\n // 6. Trigger VueJS tick if Vue is used (handled by CanvasEngine internally)\n // CanvasEngine handles this automatically through its tick system\n}\n\n/**\n * Wait for synchronization to complete on the client\n * \n * This function waits for the client to receive and process synchronization data\n * from the server. It monitors the `playersReceived$` and `eventsReceived$` observables\n * in the RpgClientEngine to determine when synchronization is complete.\n * \n * ## Design\n * \n * - Uses `combineLatest` to wait for both `playersReceived$` and `eventsReceived$` to be `true`\n * - Filters to only proceed when both are `true`\n * - Includes a timeout to prevent waiting indefinitely\n * - Resets the observables to `false` before waiting to ensure we catch the next sync\n * \n * @param client - The RpgClientEngine instance\n * @param timeout - Maximum time to wait in milliseconds (default: 1000ms)\n * @returns Promise that resolves when synchronization is complete\n * @throws Error if timeout is exceeded\n * \n * @example\n * ```ts\n * import { waitForSync } from '@rpgjs/testing'\n * \n * const client = await fixture.createClient()\n * \n * // Wait for sync to complete\n * await waitForSync(client.client)\n * \n * // Now you can safely test client-side state\n * expect(client.client.sceneMap.players()).toBeDefined()\n * ```\n */\nexport async function waitForSync(client: RpgClientEngine, timeout: number = 1000): Promise<void> {\n if (!client) {\n throw new Error('waitForSync: client parameter is required')\n }\n\n // Access private observables via type assertion\n const playersReceived$ = (client as any).playersReceived$ as any\n const eventsReceived$ = (client as any).eventsReceived$ as any\n\n if (!playersReceived$ || !eventsReceived$) {\n throw new Error('waitForSync: playersReceived$ or eventsReceived$ not found in client')\n }\n\n // Check if observables are already true - if so, sync has already arrived, don't reset\n const playersAlreadyTrue = playersReceived$.getValue ? playersReceived$.getValue() === true : false;\n const eventsAlreadyTrue = eventsReceived$.getValue ? eventsReceived$.getValue() === true : false;\n\n // If both observables are already true, sync has already completed - return immediately\n if (playersAlreadyTrue && eventsAlreadyTrue) {\n return;\n }\n\n // Reset observables to false to ensure we catch the next sync\n // Note: This is only needed when waitForSync is called standalone.\n // When called from waitForSyncComplete, observables are already reset before nextTick\n playersReceived$.next(false)\n eventsReceived$.next(false)\n\n // Wait for both observables to be true\n const syncComplete$ = combineLatest([\n playersReceived$.pipe(filter(received => received === true)),\n eventsReceived$.pipe(filter(received => received === true))\n ]).pipe(\n take(1)\n )\n\n // Create a timeout promise\n const timeoutPromise = new Promise<never>((_, reject) => {\n setTimeout(() => {\n reject(new Error(`waitForSync: Timeout after ${timeout}ms. Synchronization did not complete.`))\n }, timeout)\n })\n\n // Race between sync completion and timeout\n await Promise.race([\n firstValueFrom(syncComplete$),\n timeoutPromise\n ])\n}\n\n/**\n * Wait for complete synchronization cycle (server sync + client receive)\n * \n * This function performs a complete synchronization cycle:\n * 1. Triggers a game tick using `nextTick()` which calls `syncChanges()` on all players\n * 2. Waits for the client to receive and process the synchronization data\n * \n * This is useful when you need to ensure that server-side changes are fully\n * synchronized to the client before testing client-side state.\n * \n * ## Design\n * \n * - Calls `nextTick()` to trigger server-side sync\n * - Waits for client to receive sync data using `waitForSync()`\n * - Ensures complete synchronization cycle is finished\n * \n * @param player - The RpgPlayer instance (optional, will sync all players if not provided)\n * @param client - The RpgClientEngine instance\n * @param timeout - Maximum time to wait in milliseconds (default: 1000ms)\n * @returns Promise that resolves when synchronization is complete\n * @throws Error if timeout is exceeded\n * \n * @example\n * ```ts\n * import { waitForSyncComplete } from '@rpgjs/testing'\n * \n * const client = await fixture.createClient()\n * const player = client.player\n * \n * // Make a server-side change\n * player.addItem('potion', 5)\n * \n * // Wait for sync to complete\n * await waitForSyncComplete(player, client.client)\n * \n * // Now you can safely test client-side state\n * const clientPlayer = client.client.sceneMap.players()[player.id]\n * expect(clientPlayer.items()).toBeDefined()\n * ```\n */\nexport async function waitForSyncComplete(player: RpgPlayer | null, client: RpgClientEngine, timeout: number = 1000): Promise<void> {\n if (!client) {\n throw new Error('waitForSyncComplete: client parameter is required')\n }\n\n // Reset observables BEFORE calling nextTick to ensure we catch the sync that will be sent\n // This prevents race condition where sync arrives before we start waiting\n const playersReceived$ = (client as any).playersReceived$ as any\n const eventsReceived$ = (client as any).eventsReceived$ as any\n if (playersReceived$ && eventsReceived$) {\n playersReceived$.next(false)\n eventsReceived$.next(false)\n }\n\n // Trigger sync by calling nextTick (which calls syncChanges on all players)\n await nextTick(client)\n\n // Wait for client to receive and process the sync\n await waitForSync(client, timeout)\n}"],"names":["currentMap","clearClientInject","clearServerInject"],"mappings":";;;;;;AA2BO,SAAS,qBAAA,GAAwB;AACpC,EAAA,OAAO,cAAA,CAAe,CAAC,EAAA,KAAe;AAClC,IAAA,OAAO;AAAA,MACH,EAAA;AAAA,MACA,IAAA,EAAM;AAAA,QACF,KAAA,EAAO,IAAA;AAAA,QACP,MAAA,EAAQ,GAAA;AAAA,QACR,UAAU,EAAC;AAAA,QACX,QAAQ;AAAC,OACb;AAAA,MACA,SAAA,EAAW,EAAE,SAAS,CAAA;AAAA,MACtB,KAAA,EAAO,IAAA;AAAA,MACP,MAAA,EAAQ;AAAA,KACZ;AAAA,EACJ,CAAC,CAAA;AACL;AAmBA,SAAS,iBAAiB,OAAA,EAA4E;AAClG,EAAA,IAAI,CAAC,OAAA,IAAW,OAAA,CAAQ,MAAA,KAAW,CAAA,EAAG;AAClC,IAAA,OAAO,EAAE,aAAA,EAAe,EAAC,EAAG,aAAA,EAAe,EAAC,EAAE;AAAA,EAClD;AAGA,EAAA,MAAM,kBAAkB,OAAA,CAAQ,IAAA;AAAA,IAAK,CAAC,SAClC,IAAA,IAAQ,O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A0B,MAAA,EAAyB,OAAA,GAAkB,GAAA,EAAqB;AAChI,EAAA,IAAI,CAAC,MAAA,EAAQ;AACT,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,EACvE;AAIA,EAAA,MAAM,mBAAoB,MAAA,CAAe,gBAAA;AACzC,EAAA,MAAM,kBAAmB,MAAA,CAAe,eAAA;AACxC,EAAA,IAAI,oBAAoB,eAAA,EAAiB;AACrC,IAAA,gBAAA,CAAiB,KAAK,KAAK,CAAA;AAC3B,IAAA,eAAA,CAAgB,KAAK,KAAK,CAAA;AAAA,EAC9B;AAGA,EAAA,MAAM,SAAS,MAAM,CAAA;AAGrB,EAAA,MAAM,WAAA,CAAY,QAAQ,OAAO,CAAA;AACrC;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { mergeConfig, Provider } from \"@signe/di\";\nimport {\n provideRpg,\n startGame,\n provideClientModules,\n provideLoadMap,\n provideClientGlobalConfig,\n inject,\n WebSocketToken,\n AbstractWebsocket,\n RpgClientEngine,\n RpgClient,\n LoadMapToken,\n} from \"@rpgjs/client\";\nimport {\n createServer,\n provideServerModules,\n RpgServer,\n RpgPlayer,\n} from \"@rpgjs/server\";\nimport { h, Container } from \"canvasengine\";\nimport { mockComponents } from \"@canvasengine/testing\";\nimport { clearInject as clearClientInject } from \"@rpgjs/client\";\nimport { clearInject as clearServerInject } from \"@rpgjs/server\";\nimport { combineLatest, filter, take, firstValueFrom, Subject, map, throwError, race, timer, switchMap } from \"rxjs\";\n\n/**\n * Provides a default map loader for testing environments\n *\n * This function returns a `provideLoadMap` provider that creates mock maps\n * with default dimensions (1024x768) and a minimal component. It's automatically\n * used by `testing()` if no custom `provideLoadMap` is provided in `clientConfig.providers`.\n *\n * @returns A provider function that can be used with `provideLoadMap`\n * @example\n * ```ts\n * // Used automatically by testing()\n * const fixture = await testing([myModule])\n *\n * // Or use directly in clientConfig\n * const fixture = await testing([myModule], {\n * providers: [provideTestingLoadMap()]\n * })\n * ```\n */\nexport function provideTestingLoadMap() {\n return provideLoadMap((id: string) => {\n return {\n id,\n data: {\n width: 1024,\n height: 768,\n hitboxes: [],\n params: {},\n },\n component: h(Container),\n width: 1024,\n height: 768,\n };\n });\n}\n\n/**\n * Normalizes modules input to extract server/client modules from createModule providers or direct module objects\n *\n * @param modules - Array of modules that can be either:\n * - Direct module objects: { server: RpgServer, client: RpgClient }\n * - Providers returned by createModule(): Provider[] with meta.server/client and useValue\n * @returns Object with separate arrays for server and client modules\n * @example\n * ```ts\n * // Direct modules\n * normalizeModules([{ server: serverModule, client: clientModule }])\n *\n * // createModule providers\n * const providers = createModule('MyModule', [{ server: serverModule, client: clientModule }])\n * normalizeModules(providers)\n * ```\n */\nfunction normalizeModules(modules: any[]): {\n serverModules: RpgServer[];\n clientModules: RpgClient[];\n} {\n if (!modules || modules.length === 0) {\n return { serverModules: [], clientModules: [] };\n }\n\n // Check if first item is a provider (has meta and useValue properties)\n const isProviderArray = modules.some(\n (item: any) =>\n item && typeof item === \"object\" && \"meta\" in item && \"useValue\" in item\n );\n\n const serverModules: RpgServer[] = [];\n const clientModules: RpgClient[] = [];\n\n if (!isProviderArray) {\n // Direct module objects, extract server and client separately\n modules.forEach((module: any) => {\n if (module && typeof module === \"object\") {\n if (module.server) {\n serverModules.push(module.server);\n }\n if (module.client) {\n clientModules.push(module.client);\n }\n }\n });\n return { serverModules, clientModules };\n }\n\n // Extract modules from createModule providers\n // createModule returns providers where useValue contains the original { server, client } object\n // We need to group providers by their useValue to reconstruct the original modules\n const seenUseValues = new Set<any>();\n\n modules.forEach((provider: any) => {\n if (\n !provider ||\n typeof provider !== \"object\" ||\n !(\"meta\" in provider) ||\n !(\"useValue\" in provider)\n ) {\n return;\n }\n\n const { useValue } = provider;\n\n // Skip if we've already processed this useValue (same module, different provider for server/client)\n if (seenUseValues.has(useValue)) {\n return;\n }\n\n // Check if useValue has server or client properties (it's a module object)\n if (\n useValue &&\n typeof useValue === \"object\" &&\n (\"server\" in useValue || \"client\" in useValue)\n ) {\n seenUseValues.add(useValue);\n if (useValue.server) {\n serverModules.push(useValue.server);\n }\n if (useValue.client) {\n clientModules.push(useValue.client);\n }\n }\n });\n\n return { serverModules, clientModules };\n}\n\n// Global storage for all created fixtures and clients (for clear() function)\nconst globalFixtures: Array<{\n context: any;\n clientEngine: RpgClientEngine;\n websocket: AbstractWebsocket;\n server?: any;\n}> = [];\n\nexport interface TestingFixture {\n createClient(): Promise<{\n socket: AbstractWebsocket;\n client: RpgClientEngine;\n playerId: string;\n player: RpgPlayer;\n waitForMapChange(expectedMapId: string, timeout?: number): Promise<RpgPlayer>;\n }>;\n server: RpgServer;\n clear(): Promise<void>;\n applySyncToClient(): Promise<void>;\n nextTick(timestamp?: number): Promise<void>;\n nextTickTimes(times: number, timestamp?: number): Promise<void>;\n wait(ms: number): Promise<void>;\n waitUntil(promise: Promise<any>): Promise<void>;\n}\n\n/**\n * Testing utility function to set up server and client instances for unit testing\n *\n * This function creates a test environment with both server and client instances,\n * allowing you to test player interactions, server hooks, and game mechanics.\n *\n * @param modules - Array of modules that can be either:\n * - Direct module objects: { server: RpgServer, client: RpgClient }\n * - Providers returned by createModule(): Provider[] with meta.server/client and useValue\n * @param clientConfig - Optional client configuration\n * @param serverConfig - Optional server configuration\n * @returns Testing fixture with createClient method\n * @example\n * ```ts\n * // Using direct modules\n * const fixture = await testing([{\n * server: serverModule,\n * client: clientModule\n * }])\n *\n * // Using createModule\n * const myModule = createModule('MyModule', [{\n * server: serverModule,\n * client: clientModule\n * }])\n * const fixture = await testing(myModule)\n * ```\n */\nexport async function testing(\n modules: ({ server?: RpgServer; client?: RpgClient } | Provider)[] = [],\n clientConfig: any = {},\n serverConfig: any = {}\n): Promise<TestingFixture> {\n // Normalize modules to extract server/client from providers if needed\n const { serverModules, clientModules } = normalizeModules(modules as any[]);\n\n // Subject to emit map change events when onJoinMap is triggered\n const mapChangeSubject = new Subject<{ mapId: string; player: RpgPlayer }>();\n\n const serverClass = createServer({\n ...serverConfig,\n providers: [\n provideServerModules([\n ...serverModules,\n {\n player: {\n onJoinMap(player: RpgPlayer, map: any) {\n // Emit map change event to RxJS Subject\n const mapId = map?.id;\n if (mapId) {\n mapChangeSubject.next({ mapId, player });\n }\n }\n }\n },\n ]),\n ...(serverConfig.providers || []),\n ],\n });\n\n // Check if LoadMapToken is already provided in clientConfig.providers\n // (provideLoadMap returns an array with LoadMapToken)\n const hasLoadMap =\n clientConfig.providers?.some((provider: any) => {\n if (Array.isArray(provider)) {\n return provider.some((p: any) => p?.provide === LoadMapToken);\n }\n return provider?.provide === LoadMapToken;\n }) || false;\n\n await startGame(\n mergeConfig(\n {\n ...clientConfig,\n providers: [\n provideClientGlobalConfig({\n // TODO\n // bootstrapCanvasOptions: {\n // components: mockComponents,\n // autoRegister: false,\n // },\n }),\n ...(hasLoadMap ? [] : [provideTestingLoadMap()]), // Add only if not already provided\n provideClientModules(clientModules),\n ...(clientConfig.providers || []),\n ],\n },\n {\n providers: [provideRpg(serverClass, { env: { TEST: \"true\" } })],\n }\n )\n );\n const websocket = inject<AbstractWebsocket>(WebSocketToken) as any;\n const clientEngine = inject<RpgClientEngine>(RpgClientEngine);\n\n return {\n async createClient() {\n const clientObj = {\n socket: websocket.getSocket(),\n client: clientEngine,\n get playerId() {\n return Object.keys(websocket.getServer().subRoom.players())[0];\n },\n get player(): RpgPlayer {\n return websocket.getServer().subRoom.players()[clientObj.playerId] as RpgPlayer;\n },\n /**\n * Wait for player to be on a specific map\n *\n * This utility function waits for the `onJoinMap` hook to be triggered\n * when the player joins the expected map, or throws an error if the timeout is exceeded.\n *\n * ## Design\n *\n * Uses RxJS to listen for map change events emitted by `onJoinMap`. The function:\n * 1. Checks if the player is already on the expected map\n * 2. Subscribes to the `mapChangeSubject` observable\n * 3. Filters events to match the expected map ID\n * 4. Uses `race` operator with a timer to implement timeout handling\n * 5. Resolves with the player when the map change event is received\n *\n * @param expectedMapId - The expected map ID (without 'map-' prefix, e.g. 'map1')\n * @param timeout - Maximum time to wait in milliseconds (default: 5000)\n * @returns Promise that resolves when player is on the expected map\n * @throws Error if timeout is exceeded\n * @example\n * ```ts\n * const client = await fixture.createClient()\n * await client.waitForMapChange('map1')\n * ```\n */\n async waitForMapChange(\n expectedMapId: string,\n timeout = 5000\n ): Promise<RpgPlayer> {\n // Check if already on the expected map\n const currentMap = clientObj.player.getCurrentMap();\n if (currentMap?.id === expectedMapId) {\n return clientObj.player;\n }\n\n // Create observable that filters map changes for the expected map ID\n const mapChange$ = mapChangeSubject.pipe(\n filter((event) => event.mapId === expectedMapId),\n take(1),\n map((event) => event.player)\n );\n\n // Create timeout observable that throws an error\n const timeout$ = timer(timeout).pipe(\n take(1),\n switchMap(() => {\n const currentMap = clientObj.player.getCurrentMap();\n return throwError(() => new Error(\n `Timeout: Player did not reach map ${expectedMapId} within ${timeout}ms. ` +\n `Current map: ${currentMap?.id || \"null\"}`\n ));\n })\n );\n\n // Race between map change and timeout\n try {\n const result = await firstValueFrom(race([mapChange$, timeout$]));\n return result as RpgPlayer;\n } catch (error) {\n if (error instanceof Error) {\n throw error;\n }\n const currentMap = clientObj.player.getCurrentMap();\n throw new Error(\n `Timeout: Player did not reach map ${expectedMapId} within ${timeout}ms. ` +\n `Current map: ${currentMap?.id || \"null\"}`\n );\n }\n },\n \n };\n return clientObj;\n },\n get server() {\n return websocket.getServer();\n },\n /**\n * Clear all server, client instances and reset the DOM\n *\n * This method should be called in afterEach to clean up test state.\n * It destroys all created client instances, clears the server, and resets the DOM.\n *\n * @example\n * ```ts\n * const fixture = await testing([myModule])\n *\n * afterEach(() => {\n * fixture.clear()\n * })\n * ```\n */\n clear() {\n return clear();\n },\n async applySyncToClient() {\n websocket.getServer().subRoom.applySyncToClient();\n await waitForSync(clientEngine);\n },\n /**\n * Manually trigger a game tick for processing inputs and physics\n *\n * This method is a convenience wrapper around the exported nextTick() function.\n * It uses the clientEngine from the fixture, so no client parameter is needed.\n *\n * @param timestamp - Optional timestamp to use for the tick (default: Date.now())\n * @returns Promise that resolves when the tick is complete\n *\n * @example\n * ```ts\n * const fixture = await testing([myModule])\n *\n * // Manually advance the game by one tick\n * await fixture.nextTick()\n * ```\n */\n async nextTick(timestamp?: number): Promise<void> {\n return nextTick(clientEngine, timestamp);\n },\n async nextTickTimes(times: number, timestamp?: number): Promise<void> {\n for (let i = 0; i < times; i++) {\n await nextTick(clientEngine, timestamp);\n }\n },\n async wait(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n },\n async waitUntil<T = any>(promise: Promise<T>): Promise<T> {\n let tick = 0\n let finish = false\n return new Promise<T>((resolve: (value: T) => void, reject: (reason?: any) => void) => {\n promise.then((value: T) => { \n finish = true\n resolve(value)\n }).catch(reject)\n const timeout = () => {\n setTimeout(() => {\n if (!finish) {\n tick++\n nextTick(clientEngine, Date.now() + tick * 16)\n timeout()\n }\n }, 0)\n }\n timeout()\n })\n },\n };\n}\n\n/**\n * Clear all caches and reset test state\n *\n * This function should be called after the end of each test to clean up\n * all server and client instances, clear caches, and reset the DOM.\n *\n * ## Design\n *\n * Cleans up all created fixtures, client engines, server instances, and resets\n * the DOM to a clean state. This ensures no state leaks between tests.\n *\n * @returns void\n *\n * @example\n * ```ts\n * import { clear } from '@rpgjs/testing'\n *\n * afterEach(() => {\n * clear()\n * })\n * ```\n */\nexport async function clear(): Promise<void> {\n\n // Wait for the next tick to ensure all promises are resolved\n await new Promise((resolve) => setTimeout(resolve, 0));\n\n // Clean up all created client and server instances from all fixtures\n for (const client of globalFixtures) {\n try {\n // Clear client engine\n if (\n client.clientEngine &&\n typeof (client.clientEngine as any).clear === \"function\"\n ) {\n (client.clientEngine as any).clear();\n }\n\n // Clear server map (subRoom)\n const serverMap = client.server?.subRoom as any;\n if (serverMap && typeof serverMap.clear === \"function\") {\n serverMap.clear();\n }\n } catch (error) {\n // Silently ignore cleanup errors\n console.warn(\"Error during cleanup:\", error);\n }\n }\n\n // Clear the global fixtures array\n globalFixtures.length = 0;\n\n // Clear client context injection\n try {\n clearClientInject();\n } catch (error) {\n console.warn(\"Error clearing client inject:\", error);\n }\n\n // Clear server context injection\n try {\n clearServerInject();\n } catch (error) {\n console.warn(\"Error clearing server inject:\", error);\n }\n\n // Reset DOM\n if (typeof window !== \"undefined\" && window.document) {\n window.document.body.innerHTML = `<div id=\"rpg\"></div>`;\n }\n}\n\n/**\n * Manually trigger a game tick for processing inputs and physics\n *\n * This function allows you to manually advance the game by one tick.\n * It performs the following operations:\n * 1. On server: processes pending inputs and advances physics\n * 2. Server sends data to client\n * 3. Client retrieves data and performs inputs (move, etc.) and server reconciliation\n * 4. A tick is performed on the client\n * 5. A tick is performed on VueJS (if Vue is used)\n *\n * @param client - The RpgClientEngine instance\n * @param timestamp - Optional timestamp to use for the tick (default: Date.now())\n * @returns Promise that resolves when the tick is complete\n *\n * @example\n * ```ts\n * import { nextTick } from '@rpgjs/testing'\n *\n * const client = await fixture.createClient()\n *\n * // Manually advance the game by one tick\n * await nextTick(client.client, Date.now())\n * ```\n */\nexport async function nextTick(\n client: RpgClientEngine,\n timestamp?: number\n): Promise<void> {\n if (!client) {\n throw new Error(\"nextTick: client parameter is required\");\n }\n\n const tickTimestamp = timestamp ?? Date.now();\n const delta = 16; // 16ms for 60fps\n\n // Get server instance from client context\n const websocket = (client as any).webSocket;\n if (!websocket) {\n throw new Error(\"nextTick: websocket not found in client\");\n }\n\n const server = websocket.getServer();\n if (!server) {\n throw new Error(\"nextTick: server not found\");\n }\n\n // Get server map (subRoom)\n const serverMap = server.subRoom as any;\n if (!serverMap) {\n return;\n }\n\n // 1. On server: Process inputs for all players\n for (const player of serverMap.getPlayers()) {\n if (player.pendingInputs && player.pendingInputs.length > 0) {\n await serverMap.processInput(player.id);\n }\n }\n\n serverMap.nextTick(delta);\n\n // 3. Server sends data to client - trigger sync for all players\n // The sync is triggered by calling syncChanges() on each player\n for (const player of serverMap.getPlayers()) {\n if (player && typeof (player as any).syncChanges === \"function\") {\n (player as any).syncChanges();\n }\n }\n\n // 4. Client retrieves data and performs reconciliation\n // The sync data will be received by the client through the websocket\n // We need to wait a bit for the sync data to be processed\n await new Promise((resolve) => setTimeout(resolve, 0));\n\n // 5. Run physics tick on client map (performs client-side prediction)\n const sceneMap = (client as any).sceneMap;\n if (sceneMap && typeof sceneMap.stepPredictionTick === \"function\") {\n sceneMap.stepPredictionTick();\n }\n\n // 6. Trigger VueJS tick if Vue is used (handled by CanvasEngine internally)\n // CanvasEngine handles this automatically through its tick system\n}\n\n/**\n * Wait for synchronization to complete on the client\n *\n * This function waits for the client to receive and process synchronization data\n * from the server. It monitors the `playersReceived$` and `eventsReceived$` observables\n * in the RpgClientEngine to determine when synchronization is complete.\n *\n * ## Design\n *\n * - Uses `combineLatest` to wait for both `playersReceived$` and `eventsReceived$` to be `true`\n * - Filters to only proceed when both are `true`\n * - Includes a timeout to prevent waiting indefinitely\n * - Resets the observables to `false` before waiting to ensure we catch the next sync\n *\n * @param client - The RpgClientEngine instance\n * @param timeout - Maximum time to wait in milliseconds (default: 1000ms)\n * @returns Promise that resolves when synchronization is complete\n * @throws Error if timeout is exceeded\n *\n * @example\n * ```ts\n * import { waitForSync } from '@rpgjs/testing'\n *\n * const client = await fixture.createClient()\n *\n * // Wait for sync to complete\n * await waitForSync(client.client)\n *\n * // Now you can safely test client-side state\n * expect(client.client.sceneMap.players()).toBeDefined()\n * ```\n */\nexport async function waitForSync(\n client: RpgClientEngine,\n timeout: number = 1000\n): Promise<void> {\n if (!client) {\n throw new Error(\"waitForSync: client parameter is required\");\n }\n\n // Access private observables via type assertion\n const playersReceived$ = (client as any).playersReceived$ as any;\n const eventsReceived$ = (client as any).eventsReceived$ as any;\n\n if (!playersReceived$ || !eventsReceived$) {\n throw new Error(\n \"waitForSync: playersReceived$ or eventsReceived$ not found in client\"\n );\n }\n\n // Check if observables are already true - if so, sync has already arrived, don't reset\n const playersAlreadyTrue = playersReceived$.getValue\n ? playersReceived$.getValue() === true\n : false;\n const eventsAlreadyTrue = eventsReceived$.getValue\n ? eventsReceived$.getValue() === true\n : false;\n\n // If both observables are already true, sync has already completed - return immediately\n if (playersAlreadyTrue && eventsAlreadyTrue) {\n return;\n }\n\n // Reset observables to false to ensure we catch the next sync\n // Note: This is only needed when waitForSync is called standalone.\n // When called from waitForSyncComplete, observables are already reset before nextTick\n playersReceived$.next(false);\n eventsReceived$.next(false);\n\n // Wait for both observables to be true\n const syncComplete$ = combineLatest([\n playersReceived$.pipe(filter((received) => received === true)),\n eventsReceived$.pipe(filter((received) => received === true)),\n ]).pipe(take(1));\n\n // Create a timeout promise\n const timeoutPromise = new Promise<never>((_, reject) => {\n setTimeout(() => {\n reject(\n new Error(\n `waitForSync: Timeout after ${timeout}ms. Synchronization did not complete.`\n )\n );\n }, timeout);\n });\n\n // Race between sync completion and timeout\n await Promise.race([firstValueFrom(syncComplete$), timeoutPromise]);\n}\n\n/**\n * Wait for complete synchronization cycle (server sync + client receive)\n *\n * This function performs a complete synchronization cycle:\n * 1. Triggers a game tick using `nextTick()` which calls `syncChanges()` on all players\n * 2. Waits for the client to receive and process the synchronization data\n *\n * This is useful when you need to ensure that server-side changes are fully\n * synchronized to the client before testing client-side state.\n *\n * ## Design\n *\n * - Calls `nextTick()` to trigger server-side sync\n * - Waits for client to receive sync data using `waitForSync()`\n * - Ensures complete synchronization cycle is finished\n *\n * @param player - The RpgPlayer instance (optional, will sync all players if not provided)\n * @param client - The RpgClientEngine instance\n * @param timeout - Maximum time to wait in milliseconds (default: 1000ms)\n * @returns Promise that resolves when synchronization is complete\n * @throws Error if timeout is exceeded\n *\n * @example\n * ```ts\n * import { waitForSyncComplete } from '@rpgjs/testing'\n *\n * const client = await fixture.createClient()\n * const player = client.player\n *\n * // Make a server-side change\n * player.addItem('potion', 5)\n *\n * // Wait for sync to complete\n * await waitForSyncComplete(player, client.client)\n *\n * // Now you can safely test client-side state\n * const clientPlayer = client.client.sceneMap.players()[player.id]\n * expect(clientPlayer.items()).toBeDefined()\n * ```\n */\nexport async function waitForSyncComplete(\n player: RpgPlayer | null,\n client: RpgClientEngine,\n timeout: number = 1000\n): Promise<void> {\n if (!client) {\n throw new Error(\"waitForSyncComplete: client parameter is required\");\n }\n\n // Reset observables BEFORE calling nextTick to ensure we catch the sync that will be sent\n // This prevents race condition where sync arrives before we start waiting\n const playersReceived$ = (client as any).playersReceived$ as any;\n const eventsReceived$ = (client as any).eventsReceived$ as any;\n if (playersReceived$ && eventsReceived$) {\n playersReceived$.next(false);\n eventsReceived$.next(false);\n }\n\n // Trigger sync by calling nextTick (which calls syncChanges on all players)\n await nextTick(client);\n\n // Wait for client to receive and process the sync\n await waitForSync(client, timeout);\n}\n"],"names":["map","currentMap","clearClientInject","clearServerInject"],"mappings":";;;;;;AA6CO,SAAS,qBAAA,GAAwB;AACtC,EAAA,OAAO,cAAA,CAAe,CAAC,EAAA,KAAe;AACpC,IAAA,OAAO;AAAA,MACL,EAAA;AAAA,MACA,IAAA,EAAM;AAAA,QACJ,KAAA,EAAO,IAAA;AAAA,QACP,MAAA,EAAQ,GAAA;AAAA,QACR,UAAU,EAAC;AAAA,QACX,QAAQ;AAAC,OACX;AAAA,MACA,SAAA,EAAW,EAAE,SAAS,CAAA;AAAA,MACtB,KAAA,EAAO,IAAA;AAAA,MACP,MAAA,EAAQ;AAAA,KACV;AAAA,EACF,CAAC,CAAA;AACH;AAmBA,SAAS,iBAAiB,OAAA,EAGxB;AACA,EAAA,IAAI,CAAC,OAAA,IAAW,OAAA,CAAQ,MAAA,KAAW,CAAA,EAAG;AACpC,IAAA,OAAO,EAAE,aAAA,EAAe,EAAC,EAAG,aAAA,EAAe,EAAC,EAAE;AAAA,EAChD;AAGA,EAAA,MAAM,kBAAkB,OAAA,CAAQ,IAAA;AAAA,IAC9B,CAAC,SACC,IAAA,IAAQ,OAAO,SAAS,QAAA,IAAY,MAAA,IAAU,QAAQ,UAAA,IAAc;AAAA,GACxE;AAEA,EAAA,MAAM,gBAA6B,EAAC;AACpC,EAAA,MA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AAM,cAAA,GAAiB,IAAI,OAAA,CAAe,CAAC,GAAG,MAAA,KAAW;AACvD,IAAA,UAAA,CAAW,MAAM;AACf,MAAA,MAAA;AAAA,QACE,IAAI,KAAA;AAAA,UACF,8BAA8B,OAAO,CAAA,qCAAA;AAAA;AACvC,OACF;AAAA,IACF,GAAG,OAAO,CAAA;AAAA,EACZ,CAAC,CAAA;AAGD,EAAA,MAAM,QAAQ,IAAA,CAAK,CAAC,eAAe,aAAa,CAAA,EAAG,cAAc,CAAC,CAAA;AACpE;AA0CA,eAAsB,mBAAA,CACpB,MAAA,EACA,MAAA,EACA,OAAA,GAAkB,GAAA,EACH;AACf,EAAA,IAAI,CAAC,MAAA,EAAQ;AACX,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAAA,EACrE;AAIA,EAAA,MAAM,mBAAoB,MAAA,CAAe,gBAAA;AACzC,EAAA,MAAM,kBAAmB,MAAA,CAAe,eAAA;AACxC,EAAA,IAAI,oBAAoB,eAAA,EAAiB;AACvC,IAAA,gBAAA,CAAiB,KAAK,KAAK,CAAA;AAC3B,IAAA,eAAA,CAAgB,KAAK,KAAK,CAAA;AAAA,EAC5B;AAGA,EAAA,MAAM,SAAS,MAAM,CAAA;AAGrB,EAAA,MAAM,WAAA,CAAY,QAAQ,OAAO,CAAA;AACnC;;;;"}
|
package/dist/setup.js
CHANGED
|
@@ -27,6 +27,9 @@ Object.defineProperty(global.window.HTMLMediaElement.prototype, "load", {
|
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
29
|
window.document.body.innerHTML = `<div id="rpg"></div>`;
|
|
30
|
+
if (typeof window !== "undefined") {
|
|
31
|
+
window.__RPGJS_TEST__ = true;
|
|
32
|
+
}
|
|
30
33
|
console.error = () => {
|
|
31
34
|
};
|
|
32
35
|
//# sourceMappingURL=setup.js.map
|
package/dist/setup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.js","sources":["../src/setup.ts"],"sourcesContent":["import 'vitest-webgl-canvas-mock'\n\nconst LOAD_FAILURE_SRC = 'LOAD_FAILURE_SRC';\n\n// mock image loading\nObject.defineProperty(global.Image.prototype, 'src', {\n set(src) {\n if (src === LOAD_FAILURE_SRC) {\n setTimeout(() => this.onerror(new Error('mocked error')));\n } else if (src.startsWith('data')) {\n setTimeout(() => this.dispatchEvent(new Event(\"load\")));\n }\n },\n});\n\nObject.defineProperty(global.window.HTMLMediaElement.prototype, 'play', {\n configurable: true,\n get() {\n setTimeout(() => (this.onloadeddata && this.onloadeddata()))\n return () => { }\n }\n})\n\nObject.defineProperty(global.window.HTMLMediaElement.prototype, 'load', {\n configurable: true,\n get() {\n setTimeout(() => (this.onloadeddata && this.onloadeddata()))\n return () => { }\n }\n})\n\nwindow.document.body.innerHTML = `<div id=\"rpg\"></div>`\n\nconsole.error = () => {}"],"names":[],"mappings":";;AAEA,MAAM,gBAAA,GAAmB,kBAAA;AAGzB,MAAA,CAAO,cAAA,CAAe,MAAA,CAAO,KAAA,CAAM,SAAA,EAAW,KAAA,EAAO;AAAA,EACjD,IAAI,GAAA,EAAK;AACL,IAAA,IAAI,QAAQ,gBAAA,EAAkB;AAC1B,MAAA,UAAA,CAAW,MAAM,IAAA,CAAK,OAAA,CAAQ,IAAI,KAAA,CAAM,cAAc,CAAC,CAAC,CAAA;AAAA,IAC5D,CAAA,MAAA,IAAW,GAAA,CAAI,UAAA,CAAW,MAAM,CAAA,EAAG;AAC/B,MAAA,UAAA,CAAW,MAAM,IAAA,CAAK,aAAA,CAAc,IAAI,KAAA,CAAM,MAAM,CAAC,CAAC,CAAA;AAAA,IAC1D;AAAA,EACJ;AACJ,CAAC,CAAA;AAED,MAAA,CAAO,cAAA,CAAe,MAAA,CAAO,MAAA,CAAO,gBAAA,CAAiB,WAAW,MAAA,EAAQ;AAAA,EACpE,YAAA,EAAc,IAAA;AAAA,EACd,GAAA,GAAM;AACF,IAAA,UAAA,CAAW,MAAO,IAAA,CAAK,YAAA,IAAgB,IAAA,CAAK,cAAe,CAAA;AAC3D,IAAA,OAAO,MAAM;AAAA,IAAE,CAAA;AAAA,EACnB;AACJ,CAAC,CAAA;AAED,MAAA,CAAO,cAAA,CAAe,MAAA,CAAO,MAAA,CAAO,gBAAA,CAAiB,WAAW,MAAA,EAAQ;AAAA,EACpE,YAAA,EAAc,IAAA;AAAA,EACd,GAAA,GAAM;AACF,IAAA,UAAA,CAAW,MAAO,IAAA,CAAK,YAAA,IAAgB,IAAA,CAAK,cAAe,CAAA;AAC3D,IAAA,OAAO,MAAM;AAAA,IAAE,CAAA;AAAA,EACnB;AACJ,CAAC,CAAA;AAED,MAAA,CAAO,QAAA,CAAS,KAAK,SAAA,GAAY,CAAA,oBAAA,CAAA;
|
|
1
|
+
{"version":3,"file":"setup.js","sources":["../src/setup.ts"],"sourcesContent":["import 'vitest-webgl-canvas-mock'\n\nconst LOAD_FAILURE_SRC = 'LOAD_FAILURE_SRC';\n\n// mock image loading\nObject.defineProperty(global.Image.prototype, 'src', {\n set(src) {\n if (src === LOAD_FAILURE_SRC) {\n setTimeout(() => this.onerror(new Error('mocked error')));\n } else if (src.startsWith('data')) {\n setTimeout(() => this.dispatchEvent(new Event(\"load\")));\n }\n },\n});\n\nObject.defineProperty(global.window.HTMLMediaElement.prototype, 'play', {\n configurable: true,\n get() {\n setTimeout(() => (this.onloadeddata && this.onloadeddata()))\n return () => { }\n }\n})\n\nObject.defineProperty(global.window.HTMLMediaElement.prototype, 'load', {\n configurable: true,\n get() {\n setTimeout(() => (this.onloadeddata && this.onloadeddata()))\n return () => { }\n }\n})\n\nwindow.document.body.innerHTML = `<div id=\"rpg\"></div>`\n\n// Définir une variable globale pour que le client puisse détecter l'environnement de test\nif (typeof window !== 'undefined') {\n (window as any).__RPGJS_TEST__ = true;\n}\n\nconsole.error = () => {}"],"names":[],"mappings":";;AAEA,MAAM,gBAAA,GAAmB,kBAAA;AAGzB,MAAA,CAAO,cAAA,CAAe,MAAA,CAAO,KAAA,CAAM,SAAA,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