FERS 0.1.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
hydration.test.ts
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2026-present FERS Contributors (see AUTHORS.md).
3
4import { describe, expect, test } from 'bun:test';
5import { defaultGlobalParameters } from './defaults';
6import { buildHydratedScenarioState, parseScenarioData } from './hydration';
7import { ScenarioData, ScenarioState } from './types';
8
9function createScenarioData(): ScenarioData {
10 return {
11 globalParameters: {
12 ...defaultGlobalParameters,
13 start: 5,
14 end: 20,
15 },
16 waveforms: [],
17 timings: [],
18 antennas: [],
19 platforms: [
20 {
21 id: 'platform-1',
22 type: 'Platform',
23 name: 'Platform 1',
24 motionPath: {
25 interpolation: 'static',
26 waypoints: [
27 {
28 id: 'waypoint-1',
29 x: 0,
30 y: 0,
31 altitude: 0,
32 time: 0,
33 },
34 ],
35 },
36 rotation: {
37 type: 'fixed',
38 startAzimuth: 0,
39 startElevation: 0,
40 azimuthRate: 0,
41 elevationRate: 0,
42 },
43 components: [
44 {
45 id: 'component-1',
46 type: 'transmitter',
47 name: 'TX 1',
48 radarType: 'pulsed',
49 prf: 1000,
50 antennaId: null,
51 waveformId: null,
52 timingId: null,
53 schedule: [],
54 },
55 ],
56 },
57 ],
58 };
59}
60
61function createState(overrides: Partial<ScenarioState> = {}): ScenarioState {
62 return {
63 globalParameters: defaultGlobalParameters,
64 waveforms: [],
65 timings: [],
66 antennas: [],
67 platforms: [],
68 selectedItemId: null,
69 selectedComponentId: null,
70 isDirty: false,
71 isPlaying: false,
72 currentTime: 0,
73 targetPlaybackDuration: null,
74 isBackendSyncing: false,
75 backendVersion: 0,
76 scenarioFilePath: null,
77 outputDirectory: null,
78 antennaPreviewErrors: {},
79 notificationSnackbar: {
80 open: false,
81 message: '',
82 severity: 'error',
83 },
84 notificationQueue: [],
85 viewControlAction: { type: null, timestamp: 0 },
86 visibility: {
87 showAxes: true,
88 showPatterns: true,
89 showBoresights: true,
90 showLinks: true,
91 showLinkLabels: true,
92 showLinkMonostatic: true,
93 showLinkIlluminator: true,
94 showLinkScattered: true,
95 showLinkDirect: true,
96 showVelocities: true,
97 showPlatforms: true,
98 showPlatformLabels: true,
99 showMotionPaths: true,
100 },
101 ...overrides,
102 };
103}
104
105describe('buildHydratedScenarioState', () => {
106 test('preserves selection and clamps current time during backend recovery', () => {
107 const hydrated = buildHydratedScenarioState(
108 createState({
109 selectedItemId: 'platform-1',
110 selectedComponentId: 'component-1',
111 currentTime: 99,
112 }),
113 createScenarioData(),
114 {
115 isDirty: false,
116 preserveSelection: true,
117 preserveCurrentTime: true,
118 }
119 );
120
121 expect(hydrated.selectedItemId).toBe('platform-1');
122 expect(hydrated.selectedComponentId).toBe('component-1');
123 expect(hydrated.currentTime).toBe(20);
124 expect(hydrated.isDirty).toBe(false);
125 });
126
127 test('clears stale selection and resets time for import-style loads', () => {
128 const hydrated = buildHydratedScenarioState(
129 createState({
130 selectedItemId: 'missing-platform',
131 selectedComponentId: 'missing-component',
132 currentTime: 12,
133 }),
134 createScenarioData(),
135 {
136 isDirty: true,
137 }
138 );
139
140 expect(hydrated.selectedItemId).toBeNull();
141 expect(hydrated.selectedComponentId).toBeNull();
142 expect(hydrated.currentTime).toBe(5);
143 expect(hydrated.isDirty).toBe(true);
144 });
145});
146
147describe('parseScenarioData FMCW hydration', () => {
148 test('hydrates FMCW waveform and component mode from backend data', () => {
149 const scenario = parseScenarioData({
150 name: 'FMCW Scenario',
151 parameters: {},
152 waveforms: [
153 {
154 id: 1,
155 name: 'FMCW Down Chirp',
156 power: 50,
157 carrier_frequency: 10e9,
158 fmcw_linear_chirp: {
159 direction: 'down',
160 chirp_bandwidth: 20e6,
161 chirp_duration: 250e-6,
162 chirp_period: 300e-6,
163 start_frequency_offset: -10e6,
164 chirp_count: 8,
165 },
166 },
167 {
168 id: 4,
169 name: 'FMCW Triangle',
170 power: 50,
171 carrier_frequency: 10e9,
172 fmcw_triangle: {
173 chirp_bandwidth: 20e6,
174 chirp_duration: 250e-6,
175 start_frequency_offset: -10e6,
176 triangle_count: 8,
177 },
178 },
179 ],
180 timings: [],
181 antennas: [],
182 platforms: [
183 {
184 id: 2,
185 name: 'Radar Platform',
186 motionpath: {
187 interpolation: 'static',
188 positionwaypoints: [
189 { x: 0, y: 0, altitude: 0, time: 0 },
190 ],
191 },
192 fixedrotation: {
193 startazimuth: 0,
194 startelevation: 0,
195 azimuthrate: 0,
196 elevationrate: 0,
197 },
198 components: [
199 {
200 receiver: {
201 id: 3,
202 name: 'FMCW RX',
203 fmcw_mode: {
204 dechirp_mode: 'ideal',
205 dechirp_reference: {
206 source: 'custom',
207 waveform_name: 'FMCW Down Chirp',
208 },
209 if_sample_rate: 1e6,
210 if_filter_bandwidth: 4e5,
211 if_filter_transition_width: 1e5,
212 },
213 },
214 },
215 ],
216 },
217 ],
218 });
219
220 expect(scenario).not.toBeNull();
221 expect(scenario?.waveforms[0]).toMatchObject({
222 id: '1',
223 waveformType: 'fmcw_linear_chirp',
224 direction: 'down',
225 chirp_bandwidth: 20e6,
226 chirp_duration: 250e-6,
227 chirp_period: 300e-6,
228 start_frequency_offset: -10e6,
229 chirp_count: 8,
230 });
231 expect(scenario?.waveforms[1]).toMatchObject({
232 id: '4',
233 waveformType: 'fmcw_triangle',
234 chirp_bandwidth: 20e6,
235 chirp_duration: 250e-6,
236 start_frequency_offset: -10e6,
237 triangle_count: 8,
238 });
239 expect(scenario?.platforms[0].components[0]).toMatchObject({
240 id: '3',
241 type: 'receiver',
242 radarType: 'fmcw',
243 fmcwModeConfig: {
244 dechirp_mode: 'ideal',
245 dechirp_reference: {
246 source: 'custom',
247 waveform_name: 'FMCW Down Chirp',
248 },
249 if_sample_rate: 1e6,
250 if_filter_bandwidth: 4e5,
251 if_filter_transition_width: 1e5,
252 },
253 });
254 });
255
256 test('hydrates receiver and monostatic dechirp references by backend names', () => {
257 const scenario = parseScenarioData({
258 name: 'FMCW Dechirp Scenario',
259 parameters: {},
260 waveforms: [
261 {
262 id: 10,
263 name: 'FMCW LO',
264 power: 50,
265 carrier_frequency: 10e9,
266 fmcw_linear_chirp: {
267 direction: 'up',
268 chirp_bandwidth: 20e6,
269 chirp_duration: 250e-6,
270 chirp_period: 300e-6,
271 },
272 },
273 ],
274 timings: [],
275 antennas: [],
276 platforms: [
277 {
278 id: 20,
279 name: 'Radar Platform',
280 motionpath: {
281 interpolation: 'static',
282 positionwaypoints: [
283 { x: 0, y: 0, altitude: 0, time: 0 },
284 ],
285 },
286 fixedrotation: {
287 startazimuth: 0,
288 startelevation: 0,
289 azimuthrate: 0,
290 elevationrate: 0,
291 },
292 components: [
293 {
294 transmitter: {
295 id: 21,
296 name: 'Reference TX',
297 waveform: 10,
298 fmcw_mode: {},
299 },
300 },
301 {
302 receiver: {
303 id: 22,
304 name: 'FMCW RX',
305 fmcw_mode: {
306 dechirp_mode: 'physical',
307 dechirp_reference: {
308 source: 'transmitter',
309 transmitter_name: 'Reference TX',
310 },
311 },
312 },
313 },
314 {
315 monostatic: {
316 id: 23,
317 tx_id: 24,
318 rx_id: 25,
319 name: 'FMCW Mono',
320 waveform: 10,
321 fmcw_mode: {
322 dechirp_mode: 'ideal',
323 dechirp_reference: {
324 source: 'attached',
325 },
326 },
327 },
328 },
329 ],
330 },
331 ],
332 });
333
334 expect(scenario).not.toBeNull();
335 expect(scenario?.platforms[0].components[1]).toMatchObject({
336 id: '22',
337 type: 'receiver',
338 radarType: 'fmcw',
339 fmcwModeConfig: {
340 dechirp_mode: 'physical',
341 dechirp_reference: {
342 source: 'transmitter',
343 transmitter_name: 'Reference TX',
344 },
345 },
346 });
347 expect(scenario?.platforms[0].components[2]).toMatchObject({
348 id: '23',
349 type: 'monostatic',
350 txId: '24',
351 rxId: '25',
352 radarType: 'fmcw',
353 fmcwModeConfig: {
354 dechirp_mode: 'ideal',
355 dechirp_reference: {
356 source: 'attached',
357 },
358 },
359 });
360 });
361});