-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPCFlightController.test.ts
More file actions
165 lines (140 loc) · 4.85 KB
/
Copy pathNPCFlightController.test.ts
File metadata and controls
165 lines (140 loc) · 4.85 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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2025-2026 Matthew Kissinger
import { describe, it, expect, vi, beforeEach } from 'vitest';
import * as THREE from 'three';
import { NPCFlightController, buildAirSupportMission } from './NPCFlightController';
import { FIXED_WING_CONFIGS } from '../vehicle/FixedWingConfigs';
vi.mock('../../utils/Logger', () => ({
Logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
describe('NPCFlightController', () => {
let fc: NPCFlightController;
let aircraft: THREE.Group;
const startPos = new THREE.Vector3(0, 100, -500);
const spookyConfig = FIXED_WING_CONFIGS.AC47_SPOOKY.physics;
beforeEach(() => {
aircraft = new THREE.Group();
fc = new NPCFlightController(aircraft, startPos, spookyConfig);
});
it('starts with no mission running', () => {
expect(fc.isMissionComplete()).toBe(true);
});
it('has an active mission after setMission', () => {
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
fc.setMission(mission);
expect(fc.isMissionComplete()).toBe(false);
});
it('update moves the aircraft via physics', () => {
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
fc.setMission(mission);
const posBefore = fc.getPosition().clone();
// Run several physics steps
for (let i = 0; i < 100; i++) {
fc.update(0.016, 0);
}
const posAfter = fc.getPosition();
// Aircraft should have moved
expect(posAfter.distanceTo(posBefore)).toBeGreaterThan(1);
});
it('syncs aircraft mesh to physics state', () => {
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
fc.setMission(mission);
fc.update(0.1, 0);
// Aircraft mesh position should match physics position
const physPos = fc.getPosition();
expect(aircraft.position.x).toBeCloseTo(physPos.x, 2);
expect(aircraft.position.y).toBeCloseTo(physPos.y, 2);
expect(aircraft.position.z).toBeCloseTo(physPos.z, 2);
});
it('isMissionComplete flips from true (no mission) to false (mission running)', () => {
expect(fc.isMissionComplete()).toBe(true);
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
fc.setMission(mission);
expect(fc.isMissionComplete()).toBe(false);
});
it('dispose stops any active mission', () => {
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
fc.setMission(mission);
expect(fc.isMissionComplete()).toBe(false);
fc.dispose();
expect(fc.isMissionComplete()).toBe(true);
});
});
describe('buildAirSupportMission', () => {
it('creates orbit mission with orbitPoint', () => {
const target = new THREE.Vector3(100, 0, 200);
const mission = buildAirSupportMission(
target,
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
expect(mission.waypoints).toHaveLength(1);
expect(mission.cruiseAltitude).toBe(300);
expect(mission.cruiseSpeed).toBe(40);
expect(mission.orbitPoint).toBeDefined();
expect(mission.orbitPoint!.x).toBe(100);
expect(mission.orbitRadius).toBe(200);
});
it('creates attack_run mission with attackTarget', () => {
const target = new THREE.Vector3(100, 0, 200);
const mission = buildAirSupportMission(
target,
new THREE.Vector3(0, 0, 1),
80, 60, 'attack_run',
);
expect(mission.attackTarget).toBeDefined();
expect(mission.attackTarget!.x).toBe(100);
expect(mission.orbitPoint).toBeUndefined();
});
it('creates flyover mission with no orbit or attack target', () => {
const target = new THREE.Vector3(100, 0, 200);
const mission = buildAirSupportMission(
target,
new THREE.Vector3(0, 0, 1),
200, 50, 'flyover',
);
expect(mission.orbitPoint).toBeUndefined();
expect(mission.attackTarget).toBeUndefined();
expect(mission.waypoints).toHaveLength(1);
});
it('uses custom home position when provided', () => {
const home = new THREE.Vector3(-500, 0, -500);
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
home,
);
expect(mission.homePosition.x).toBe(-500);
expect(mission.homePosition.z).toBe(-500);
});
it('defaults home position to start of approach', () => {
const mission = buildAirSupportMission(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 1),
300, 40, 'orbit',
);
// Start pos should be target + (-approach * 500)
expect(mission.homePosition.z).toBe(-500);
});
});