-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerVehicleAdapterFactory.test.ts
More file actions
502 lines (411 loc) · 19 KB
/
Copy pathPlayerVehicleAdapterFactory.test.ts
File metadata and controls
502 lines (411 loc) · 19 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2025-2026 Matthew Kissinger
import { describe, expect, it, vi } from 'vitest';
import * as THREE from 'three';
import { Emplacement } from './Emplacement';
import { GroundVehicle } from './GroundVehicle';
import { GroundVehicleProximityChecker } from './GroundVehicleProximityChecker';
import { PBR } from './PBR';
import {
PlayerVehicleAdapterFactory,
resolveAdapterFamily,
type PlayerVehicleAdapterFactoryDeps,
} from './PlayerVehicleAdapterFactory';
import { Sampan } from './Sampan';
import { Tank } from './Tank';
import { VehicleManager } from './VehicleManager';
import { VehicleSessionController } from './VehicleSessionController';
import type { PlayerState } from '../../types';
import type { IHUDSystem } from '../../types/SystemInterfaces';
import { Faction } from '../combat/types';
vi.mock('../../utils/Logger', () => ({
Logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
/**
* Behavior tests for the player vehicle adapter factory.
*
* Split A of `vekhikl-board-controller-factory` — the factory module +
* tests only; the PlayerController handler and composer wire live in
* split B. Tests assert observable behavior at the factory's public
* surface (`tryBoardNearest`, `tryExit`, `resolveAdapterFamily`) and the
* `vehicleType` that flows into the session controller — not any internal
* caching or adapter construction order.
*/
// ───────────────────────────── Test fixtures ─────────────────────────────
function createPlayerState(spawn: THREE.Vector3): PlayerState {
return {
position: spawn.clone(),
velocity: new THREE.Vector3(),
speed: 10,
runSpeed: 20,
isRunning: false,
isGrounded: true,
isJumping: false,
jumpForce: 12,
gravity: -25,
isCrouching: false,
isInHelicopter: false,
helicopterId: null,
isInFixedWing: false,
fixedWingId: null,
};
}
function createPlayerInput() {
return {
setInHelicopter: vi.fn(),
setFlightVehicleMode: vi.fn(),
setInputContext: vi.fn(),
isKeyPressed: vi.fn(() => false),
getMouseMovement: vi.fn(() => ({ x: 0, y: 0 })),
clearMouseMovement: vi.fn(),
getIsPointerLocked: vi.fn(() => false),
getTouchControls: vi.fn(() => null),
getTouchMovementVector: vi.fn(() => ({ x: 0, z: 0 })),
relockPointer: vi.fn(),
clearTransientInputState: vi.fn(),
};
}
function createCameraController() {
return {
saveInfantryAngles: vi.fn(),
restoreInfantryAngles: vi.fn(),
};
}
function createHud(): IHUDSystem {
return {
showInteractionPrompt: vi.fn(),
hideInteractionPrompt: vi.fn(),
setVehicleContext: vi.fn(),
updateElevation: vi.fn(),
showMessage: vi.fn(),
} as unknown as IHUDSystem;
}
/**
* Set the proximity-prompt cache to `vehicleId` by running a single
* proximity check with the vehicle placed inside the prompt radius. This
* is the same path the production wire takes (proximity checker → HUD
* prompt → factory reads the prompted id).
*/
function primePrompt(
checker: GroundVehicleProximityChecker,
playerPos: THREE.Vector3,
vehiclePos: THREE.Vector3,
): void {
playerPos.copy(vehiclePos).add(new THREE.Vector3(1, 0, 0));
checker.checkPlayerProximity();
}
interface Harness {
factory: PlayerVehicleAdapterFactory;
vehicleManager: VehicleManager;
session: VehicleSessionController;
proximityChecker: GroundVehicleProximityChecker;
playerState: PlayerState;
hud: IHUDSystem;
enterSpy: ReturnType<typeof vi.spyOn>;
exitSpy: ReturnType<typeof vi.spyOn>;
}
async function buildHarness(
vehicles: Array<{ vehicle: any; position: THREE.Vector3 }>,
options: { playerFaction?: Faction } = {},
): Promise<Harness> {
const vehicleManager = new VehicleManager();
await vehicleManager.init();
for (const { vehicle } of vehicles) {
vehicleManager.register(vehicle);
}
const session = new VehicleSessionController();
const playerState = createPlayerState(new THREE.Vector3(0, 0, 0));
const hud = createHud();
const proximityChecker = new GroundVehicleProximityChecker(
vehicleManager,
() => playerState.position,
() => session.isInVehicle(),
options.playerFaction
? { getPlayerFaction: () => options.playerFaction ?? null }
: {},
);
proximityChecker.setHUDSystem(hud);
const deps: PlayerVehicleAdapterFactoryDeps = {
vehicleManager,
vehicleSessionController: session,
proximityChecker,
playerState,
input: createPlayerInput() as any,
cameraController: createCameraController() as any,
hudSystem: hud,
};
const factory = new PlayerVehicleAdapterFactory(deps);
const enterSpy = vi.spyOn(session, 'enterVehicle');
const exitSpy = vi.spyOn(session, 'exitVehicle');
return {
factory,
vehicleManager,
session,
proximityChecker,
playerState,
hud,
enterSpy,
exitSpy,
};
}
function makeObject(position: THREE.Vector3): THREE.Group {
const obj = new THREE.Group();
obj.position.copy(position);
return obj;
}
function makeJeep(id: string, position: THREE.Vector3): GroundVehicle {
return new GroundVehicle(id, makeObject(position), Faction.US);
}
function makeTank(id: string, position: THREE.Vector3): Tank {
return new Tank(id, makeObject(position), Faction.US);
}
function makeSampan(id: string, position: THREE.Vector3): Sampan {
return new Sampan(id, makeObject(position), Faction.NVA);
}
function makePBR(id: string, position: THREE.Vector3): PBR {
return new PBR(id, makeObject(position), Faction.US);
}
function makeEmplacement(id: string, position: THREE.Vector3): Emplacement {
return new Emplacement(id, makeObject(position), Faction.US);
}
// ───────────────────────────── Tests ─────────────────────────────
describe('PlayerVehicleAdapterFactory.tryBoardNearest', () => {
it('returns false when no proximity prompt is showing', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(50, 0, 50));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
// No prompt primed → the proximity checker's last-shown id is null.
expect(h.proximityChecker.getLastShownVehicleId()).toBeNull();
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(false);
expect(h.enterSpy).not.toHaveBeenCalled();
expect(h.session.isInVehicle()).toBe(false);
});
it('dispatches an M151 prompt through the ground adapter', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
expect(h.proximityChecker.getLastShownVehicleId()).toBe('motor_pool_small_m151');
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(true);
expect(h.enterSpy).toHaveBeenCalledTimes(1);
const [vehicleType, vehicleId] = h.enterSpy.mock.calls[0];
expect(vehicleType).toBe('ground');
expect(vehicleId).toBe('motor_pool_small_m151');
expect(h.session.getVehicleType()).toBe('ground');
expect(jeep.getPilotId()).toBe('player');
});
it('dispatches an M48 prompt through the tank adapter', async () => {
const tank = makeTank('m48_tank_of_us_fob', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: tank, position: tank.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, tank.getPosition());
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(true);
const [vehicleType, vehicleId] = h.enterSpy.mock.calls[0];
expect(vehicleType).toBe('tank');
expect(vehicleId).toBe('m48_tank_of_us_fob');
expect(h.session.getVehicleType()).toBe('tank');
});
it('does not board an enemy-owned tank from a non-boardable prompt', async () => {
const tank = new Tank('m48_tank_of_nva_fob', makeObject(new THREE.Vector3(10, 0, 10)), Faction.NVA);
const h = await buildHarness(
[{ vehicle: tank, position: tank.getPosition() }],
{ playerFaction: Faction.US },
);
primePrompt(h.proximityChecker, h.playerState.position, tank.getPosition());
expect(h.hud.showInteractionPrompt).toHaveBeenCalledWith('Enemy M48 Patton tank - cannot board');
expect(h.proximityChecker.getLastShownVehicleId()).toBeNull();
expect(h.factory.tryBoardNearest()).toBe(false);
expect(h.enterSpy).not.toHaveBeenCalled();
expect(tank.getPilotId()).toBeNull();
});
it('dispatches a Sampan prompt through the watercraft adapter', async () => {
const sampan = makeSampan('sampan_open_frontier_river', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: sampan, position: sampan.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, sampan.getPosition());
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(true);
const [vehicleType, vehicleId] = h.enterSpy.mock.calls[0];
expect(vehicleType).toBe('watercraft');
expect(vehicleId).toBe('sampan_open_frontier_river');
});
it('dispatches a PBR prompt through the watercraft adapter', async () => {
const pbr = makePBR('pbr_us_open_frontier', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: pbr, position: pbr.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, pbr.getPosition());
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(true);
const [vehicleType, vehicleId] = h.enterSpy.mock.calls[0];
expect(vehicleType).toBe('watercraft');
expect(vehicleId).toBe('pbr_us_open_frontier');
});
it('dispatches an M2HB prompt through the emplacement adapter', async () => {
const emp = makeEmplacement('m2hb_emp_of_us_fob', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: emp, position: emp.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, emp.getPosition());
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(true);
const [vehicleType, vehicleId] = h.enterSpy.mock.calls[0];
expect(vehicleType).toBe('emplacement');
expect(vehicleId).toBe('m2hb_emp_of_us_fob');
});
it('returns false when the prompted vehicle was unregistered between prompt and board', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
// Pull the jeep out of the manager while the prompt id is still cached.
h.vehicleManager.unregister('motor_pool_small_m151');
const boarded = h.factory.tryBoardNearest();
expect(boarded).toBe(false);
expect(h.enterSpy).not.toHaveBeenCalled();
});
});
describe('PlayerVehicleAdapterFactory.tryExit', () => {
it('returns false when the player is not seated', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
const exited = h.factory.tryExit();
expect(exited).toBe(false);
expect(h.exitSpy).not.toHaveBeenCalled();
});
it('fires a voluntary exit through the session controller while seated', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
h.factory.tryBoardNearest();
expect(h.session.isInVehicle()).toBe(true);
const exited = h.factory.tryExit();
expect(exited).toBe(true);
expect(h.session.isInVehicle()).toBe(false);
expect(h.exitSpy).toHaveBeenCalledTimes(1);
// Sanity-check the exit options carry the voluntary `'input'` reason
// — the helicopter handler shape this is mirroring uses the same tag,
// and the session controller surfaces it to adapters via the exit ctx.
const exitOptions = h.exitSpy.mock.calls[0][1];
expect(exitOptions?.reason).toBe('input');
// The seat is released so an NPC can mount it after the player walks
// off (regression guard for cycle-vekhikl-4 NPC re-mount).
expect(jeep.getPilotId()).toBeNull();
});
it('boards a tank, then exits cleanly leaving the chassis empty', async () => {
const tank = makeTank('m48_tank_of_us_fob', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: tank, position: tank.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, tank.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
const exited = h.factory.tryExit();
expect(exited).toBe(true);
expect(h.session.isInVehicle()).toBe(false);
// The tank's pilot seat is released — sibling NPC-driver task can
// re-occupy it without colliding with a stale 'player' lock.
expect(tank.getPilotId()).toBeNull();
});
});
describe('PlayerVehicleAdapterFactory.trySwapSeat', () => {
it('swaps the active tank adapter between driver and gunner seats', async () => {
const tank = makeTank('m48_tank_of_us_fob', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: tank, position: tank.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, tank.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
expect(tank.getPilotId()).toBe('player');
expect(h.factory.trySwapSeat()).toBe(true);
expect(tank.getPilotId()).toBeNull();
expect(tank.getSeats().find((seat) => seat.role === 'gunner')?.occupantId).toBe('player');
});
it('does not consume the input for a non-swappable active vehicle', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
expect(h.factory.trySwapSeat()).toBe(false);
});
});
describe('seat lifecycle: exit paths that bypass factory.tryExit (Escape / requestVehicleExit)', () => {
/**
* Repro for the seat-ghost bug: `handleEscape` / `requestVehicleExit` end
* the player's session by calling `session.exitVehicle(...)` directly —
* they never route through `factory.tryExit()`. On master that path left
* the IVehicle seat locked to `'player'`, so `getPilotId()` lied (seat
* ghost) and re-boarding into the same seat failed.
*
* This exercises the production chokepoint (the shared session controller)
* rather than the factory wrapper, which is exactly the path the
* Escape / requestVehicleExit handlers take.
*/
function buildTransitionCtx(h: Harness) {
return {
playerState: h.playerState,
vehicleId: h.session.getVehicleId() ?? '',
position: h.playerState.position.clone(),
setPosition: (p: THREE.Vector3) => h.playerState.position.copy(p),
input: createPlayerInput() as any,
cameraController: createCameraController() as any,
hudSystem: h.hud,
};
}
it('frees the seat when the session exits directly (Escape path), not just via factory.tryExit', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
expect(jeep.getPilotId()).toBe('player');
// Simulate handleEscape -> requestVehicleExit: ends the session WITHOUT
// calling factory.tryExit(). This is the path the bug lived on.
const result = h.session.exitVehicle(buildTransitionCtx(h), { reason: 'escape' });
expect(result.exited).toBe(true);
expect(h.session.isInVehicle()).toBe(false);
// The seat must be free — no ghost occupant blocking re-board / NPC mount.
expect(jeep.getPilotId()).toBeNull();
expect(jeep.getOccupant(0)).toBeNull();
});
it('re-board into the same seat succeeds after an Escape-style exit', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
h.session.exitVehicle(buildTransitionCtx(h), { reason: 'escape' });
expect(jeep.getPilotId()).toBeNull();
// Re-board: with a freed seat the player gets the pilot seat back.
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
expect(h.session.isInVehicle()).toBe(true);
expect(jeep.getPilotId()).toBe('player');
});
it('releases the occupied seat exactly once (an NPC passenger keeps its seat)', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(10, 0, 10));
const h = await buildHarness([{ vehicle: jeep, position: jeep.getPosition() }]);
primePrompt(h.proximityChecker, h.playerState.position, jeep.getPosition());
expect(h.factory.tryBoardNearest()).toBe(true);
// An NPC mounts a passenger seat while the player drives.
jeep.enterVehicle('npc_grunt', 'passenger');
// Player exits via the Escape path (session-direct).
h.session.exitVehicle(buildTransitionCtx(h), { reason: 'escape' });
// Only the player's pilot seat is freed; the NPC keeps its seat.
expect(jeep.getPilotId()).toBeNull();
expect(jeep.getSeats().find((s) => s.occupantId === 'npc_grunt')).toBeTruthy();
});
});
describe('resolveAdapterFamily', () => {
it('returns the correct family for each drivable category', async () => {
const jeep = makeJeep('motor_pool_small_m151', new THREE.Vector3(0, 0, 0));
const tank = makeTank('m48_tank_of_us_fob', new THREE.Vector3(0, 0, 0));
const sampan = makeSampan('sampan_open_frontier_river', new THREE.Vector3(0, 0, 0));
const pbr = makePBR('pbr_us_open_frontier', new THREE.Vector3(0, 0, 0));
const emp = makeEmplacement('m2hb_emp_of_us_fob', new THREE.Vector3(0, 0, 0));
expect(resolveAdapterFamily(jeep)).toBe('ground');
expect(resolveAdapterFamily(tank)).toBe('tank');
expect(resolveAdapterFamily(sampan)).toBe('watercraft');
expect(resolveAdapterFamily(pbr)).toBe('watercraft');
expect(resolveAdapterFamily(emp)).toBe('emplacement');
});
it('returns null for aircraft (those have dedicated boarding paths)', () => {
const helicopter = {
vehicleId: 'uh1_heli',
category: 'helicopter' as const,
} as any;
const plane = {
vehicleId: 'ac47_plane',
category: 'fixed_wing' as const,
} as any;
expect(resolveAdapterFamily(helicopter)).toBeNull();
expect(resolveAdapterFamily(plane)).toBeNull();
});
});