-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransforms.test.ts
More file actions
615 lines (518 loc) · 24.2 KB
/
transforms.test.ts
File metadata and controls
615 lines (518 loc) · 24.2 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Copyright 2026 bburda
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
unwrapItems,
transformFault,
transformFaultsResponse,
transformOperationsResponse,
transformDataResponse,
transformConfigurationsResponse,
transformBulkDataDescriptor,
} from './transforms';
// =============================================================================
// unwrapItems
// =============================================================================
describe('unwrapItems', () => {
it('passes through an array directly', () => {
const arr = [1, 2, 3];
expect(unwrapItems<number>(arr)).toEqual([1, 2, 3]);
});
it('unwraps {items: [...]} wrapper object', () => {
const wrapped = { items: ['a', 'b'] };
expect(unwrapItems<string>(wrapped)).toEqual(['a', 'b']);
});
it('returns empty array when items key is missing', () => {
expect(unwrapItems<string>({})).toEqual([]);
});
it('returns empty array for null input', () => {
expect(unwrapItems<string>(null)).toEqual([]);
});
it('returns empty array for undefined input', () => {
expect(unwrapItems<string>(undefined)).toEqual([]);
});
it('returns empty array when items is undefined', () => {
const wrapped = { items: undefined };
expect(unwrapItems<string>(wrapped)).toEqual([]);
});
});
// =============================================================================
// transformFault
// =============================================================================
describe('transformFault', () => {
const makeFaultInput = (overrides: Record<string, unknown> = {}) => ({
fault_code: 'ENGINE_OVERHEAT',
description: 'Engine temperature exceeded threshold',
severity: 2,
severity_label: 'error',
status: 'CONFIRMED',
first_occurred: 1700000000,
last_occurred: 1700001000,
occurrence_count: 3,
reporting_sources: ['/powertrain/engine_monitor'],
...overrides,
});
it('maps fault_code to code', () => {
const result = transformFault(makeFaultInput());
expect(result.code).toBe('ENGINE_OVERHEAT');
});
it('maps description to message', () => {
const result = transformFault(makeFaultInput());
expect(result.message).toBe('Engine temperature exceeded threshold');
});
it('maps first_occurred unix timestamp to ISO 8601 string', () => {
const result = transformFault(makeFaultInput({ first_occurred: 1700000000 }));
expect(result.timestamp).toBe(new Date(1700000000 * 1000).toISOString());
});
describe('timestamp defensive parsing', () => {
// All fallback branches must return an ISO string close to "now".
// Asserting recency (not just "doesn't throw") actually verifies the
// fallback fired and produced a sane value.
const expectRecent = (iso: string) => {
const ts = new Date(iso).getTime();
const now = Date.now();
expect(ts).toBeGreaterThan(now - 5000);
expect(ts).toBeLessThanOrEqual(now);
};
// Suppress the dev-tools breadcrumb console.warn in fallback cases.
let warnSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
warnSpy.mockRestore();
});
it('falls back to current time when first_occurred is 0', () => {
const result = transformFault(makeFaultInput({ first_occurred: 0 }));
expectRecent(result.timestamp);
expect(warnSpy).toHaveBeenCalledTimes(1);
});
it('falls back to current time when first_occurred is negative', () => {
const result = transformFault(makeFaultInput({ first_occurred: -1 }));
expectRecent(result.timestamp);
expect(warnSpy).toHaveBeenCalledTimes(1);
});
it('parses ISO 8601 string first_occurred', () => {
const iso = '2026-04-13T10:00:00.000Z';
const result = transformFault(makeFaultInput({ first_occurred: iso }));
expect(result.timestamp).toBe(iso);
expect(warnSpy).not.toHaveBeenCalled();
});
it('falls back to current time when first_occurred is an invalid string', () => {
const result = transformFault(makeFaultInput({ first_occurred: 'not-a-date' }));
expectRecent(result.timestamp);
expect(warnSpy).toHaveBeenCalledTimes(1);
});
it('falls back to current time when first_occurred is missing', () => {
const result = transformFault(makeFaultInput({ first_occurred: undefined }));
expectRecent(result.timestamp);
expect(warnSpy).toHaveBeenCalledTimes(1);
});
});
it('defaults entity_type to "app" when not in raw data', () => {
const result = transformFault(makeFaultInput());
expect(result.entity_type).toBe('app');
});
it('uses entity_type from raw data when provided', () => {
const result = transformFault(makeFaultInput({ entity_type: 'component' }));
expect(result.entity_type).toBe('component');
});
it('uses entity_type "area" from raw data', () => {
const result = transformFault(makeFaultInput({ entity_type: 'area' }));
expect(result.entity_type).toBe('area');
});
it('falls back to "app" when entity_type is empty string', () => {
const result = transformFault(makeFaultInput({ entity_type: '' }));
expect(result.entity_type).toBe('app');
});
describe('entity_id extraction from reporting_sources', () => {
it('extracts last segment of node path', () => {
const result = transformFault(makeFaultInput({ reporting_sources: ['/powertrain/engine_monitor'] }));
expect(result.entity_id).toBe('engine_monitor');
});
it('preserves underscores in entity_id', () => {
const result = transformFault(makeFaultInput({ reporting_sources: ['/ns/my_node_name'] }));
expect(result.entity_id).toBe('my_node_name');
});
it('uses "unknown" when reporting_sources is empty', () => {
const result = transformFault(makeFaultInput({ reporting_sources: [] }));
expect(result.entity_id).toBe('unknown');
});
it('uses "unknown" when reporting_sources is missing', () => {
const result = transformFault(makeFaultInput({ reporting_sources: undefined }));
expect(result.entity_id).toBe('unknown');
});
});
describe('severity mapping', () => {
it('maps severity_label "critical" to critical', () => {
const result = transformFault(makeFaultInput({ severity: 0, severity_label: 'critical' }));
expect(result.severity).toBe('critical');
});
it('maps severity_label "error" to error', () => {
const result = transformFault(makeFaultInput({ severity: 0, severity_label: 'error' }));
expect(result.severity).toBe('error');
});
it('maps severity_label "warning" to warning', () => {
const result = transformFault(makeFaultInput({ severity: 0, severity_label: 'warning' }));
expect(result.severity).toBe('warning');
});
it('maps severity_label "warn" to warning', () => {
const result = transformFault(makeFaultInput({ severity: 0, severity_label: 'warn' }));
expect(result.severity).toBe('warning');
});
it('maps severity >= 3 to critical regardless of label', () => {
const result = transformFault(makeFaultInput({ severity: 3, severity_label: '' }));
expect(result.severity).toBe('critical');
});
it('maps severity === 2 to error when label is absent', () => {
const result = transformFault(makeFaultInput({ severity: 2, severity_label: '' }));
expect(result.severity).toBe('error');
});
it('maps severity === 1 to warning when label is absent', () => {
const result = transformFault(makeFaultInput({ severity: 1, severity_label: '' }));
expect(result.severity).toBe('warning');
});
it('defaults to info when severity is 0 and label is empty', () => {
const result = transformFault(makeFaultInput({ severity: 0, severity_label: '' }));
expect(result.severity).toBe('info');
});
});
describe('status mapping', () => {
it('maps CONFIRMED to active', () => {
const result = transformFault(makeFaultInput({ status: 'CONFIRMED' }));
expect(result.status).toBe('active');
});
it('maps ACTIVE to active', () => {
const result = transformFault(makeFaultInput({ status: 'ACTIVE' }));
expect(result.status).toBe('active');
});
it('maps PENDING to pending', () => {
const result = transformFault(makeFaultInput({ status: 'PENDING' }));
expect(result.status).toBe('pending');
});
it('maps PREFAILED to pending', () => {
const result = transformFault(makeFaultInput({ status: 'PREFAILED' }));
expect(result.status).toBe('pending');
});
it('maps CLEARED to cleared', () => {
const result = transformFault(makeFaultInput({ status: 'CLEARED' }));
expect(result.status).toBe('cleared');
});
it('maps RESOLVED to cleared', () => {
const result = transformFault(makeFaultInput({ status: 'RESOLVED' }));
expect(result.status).toBe('cleared');
});
it('maps HEALED to healed', () => {
const result = transformFault(makeFaultInput({ status: 'HEALED' }));
expect(result.status).toBe('healed');
});
it('maps PREPASSED to healed', () => {
const result = transformFault(makeFaultInput({ status: 'PREPASSED' }));
expect(result.status).toBe('healed');
});
it('defaults to active for unknown status', () => {
const result = transformFault(makeFaultInput({ status: 'UNKNOWN_STATUS' }));
expect(result.status).toBe('active');
});
});
it('includes occurrence metadata in parameters', () => {
const result = transformFault(makeFaultInput({ occurrence_count: 5, last_occurred: 1700002000 }));
expect(result.parameters?.occurrence_count).toBe(5);
expect(result.parameters?.last_occurred).toBe(1700002000);
expect(result.parameters?.reporting_sources).toEqual(['/powertrain/engine_monitor']);
});
});
// =============================================================================
// transformFaultsResponse
// =============================================================================
describe('transformFaultsResponse', () => {
const makeFaultItem = (overrides: Record<string, unknown> = {}) => ({
fault_code: 'TEST_FAULT',
description: 'A test fault',
severity: 2,
severity_label: 'error',
status: 'CONFIRMED',
first_occurred: 1700000000,
reporting_sources: ['/test/node'],
...overrides,
});
it('returns an empty items array for empty response', () => {
const result = transformFaultsResponse({ items: [] });
expect(result.items).toEqual([]);
expect(result.count).toBe(0);
});
it('transforms each fault item', () => {
const result = transformFaultsResponse({ items: [makeFaultItem()] });
expect(result.items).toHaveLength(1);
expect(result.items[0]?.code).toBe('TEST_FAULT');
expect(result.items[0]?.message).toBe('A test fault');
});
it('uses x-medkit count when provided', () => {
const result = transformFaultsResponse({ items: [makeFaultItem()], 'x-medkit': { count: 42 } });
expect(result.count).toBe(42);
});
it('falls back to items.length when x-medkit count is absent', () => {
const result = transformFaultsResponse({ items: [makeFaultItem(), makeFaultItem()] });
expect(result.count).toBe(2);
});
it('handles missing items array gracefully', () => {
const result = transformFaultsResponse({});
expect(result.items).toEqual([]);
expect(result.count).toBe(0);
});
});
// =============================================================================
// transformOperationsResponse
// =============================================================================
describe('transformOperationsResponse', () => {
const makeRawOp = (overrides: Record<string, unknown> = {}) => ({
id: 'calibrate',
name: 'calibrate',
'x-medkit': {
ros2: {
kind: 'service' as const,
service: '/engine/calibrate',
type: 'std_srvs/srv/Trigger',
},
},
...overrides,
});
it('extracts kind from x-medkit.ros2.kind', () => {
const result = transformOperationsResponse({ items: [makeRawOp()] });
expect(result[0]?.kind).toBe('service');
});
it('uses action kind when x-medkit.ros2.kind is action', () => {
const raw = makeRawOp({ 'x-medkit': { ros2: { kind: 'action', action: '/engine/drive', type: 'pkg/Drive' } } });
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.kind).toBe('action');
});
it('infers action kind from asynchronous_execution flag when x-medkit kind absent', () => {
const raw = { id: 'op', name: 'op', asynchronous_execution: true };
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.kind).toBe('action');
});
it('defaults to service kind when no explicit kind info', () => {
const raw = { id: 'op', name: 'op' };
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.kind).toBe('service');
});
it('extracts path from ros2.service for service ops', () => {
const result = transformOperationsResponse({ items: [makeRawOp()] });
expect(result[0]?.path).toBe('/engine/calibrate');
});
it('extracts path from ros2.action for action ops', () => {
const raw = makeRawOp({ 'x-medkit': { ros2: { kind: 'action', action: '/engine/drive', type: 'pkg/Drive' } } });
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.path).toBe('/engine/drive');
});
it('falls back path to /name when ros2 path absent', () => {
const raw = { id: 'op', name: 'my_op' };
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.path).toBe('/my_op');
});
it('extracts type from x-medkit.ros2.type', () => {
const result = transformOperationsResponse({ items: [makeRawOp()] });
expect(result[0]?.type).toBe('std_srvs/srv/Trigger');
});
it('returns empty string for type when absent', () => {
const raw = { id: 'op', name: 'op' };
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.type).toBe('');
});
it('returns empty array for empty response', () => {
const result = transformOperationsResponse({ items: [] });
expect(result).toEqual([]);
});
it('uses name as fallback when id is the only identifier', () => {
const raw = { id: 'my_op', name: 'my_op' };
const result = transformOperationsResponse({ items: [raw] });
expect(result[0]?.name).toBe('my_op');
});
});
// =============================================================================
// transformDataResponse
// =============================================================================
describe('transformDataResponse', () => {
const makeDataItem = (overrides: Record<string, unknown> = {}) => ({
id: '/engine/temperature',
name: '/engine/temperature',
'x-medkit': {
ros2: {
topic: '/engine/temperature',
type: 'sensor_msgs/msg/Temperature',
direction: 'publish',
},
},
...overrides,
});
it('extracts topic name from item name', () => {
const result = transformDataResponse({ items: [makeDataItem()] });
expect(result[0]?.topic).toBe('/engine/temperature');
});
it('falls back topic name to x-medkit.ros2.topic', () => {
const raw = { id: 'tmp', 'x-medkit': { ros2: { topic: '/fallback/topic', type: 'std_msgs/msg/Float32' } } };
const result = transformDataResponse({ items: [raw] });
expect(result[0]?.topic).toBe('/fallback/topic');
});
it('falls back topic name to id when name and x-medkit topic absent', () => {
const raw = { id: 'my_topic_id' };
const result = transformDataResponse({ items: [raw] });
expect(result[0]?.topic).toBe('my_topic_id');
});
it('extracts type from x-medkit.ros2.type', () => {
const result = transformDataResponse({ items: [makeDataItem()] });
expect(result[0]?.type).toBe('sensor_msgs/msg/Temperature');
});
it('sets status to metadata_only', () => {
const result = transformDataResponse({ items: [makeDataItem()] });
expect(result[0]?.status).toBe('metadata_only');
});
it('maps direction "publish" to isPublisher=true, isSubscriber=false', () => {
const result = transformDataResponse({
items: [makeDataItem({ 'x-medkit': { ros2: { direction: 'publish' } } })],
});
expect(result[0]?.isPublisher).toBe(true);
expect(result[0]?.isSubscriber).toBe(false);
});
it('maps direction "subscribe" to isPublisher=false, isSubscriber=true', () => {
const raw = makeDataItem({ 'x-medkit': { ros2: { direction: 'subscribe' } } });
const result = transformDataResponse({ items: [raw] });
expect(result[0]?.isPublisher).toBe(false);
expect(result[0]?.isSubscriber).toBe(true);
});
it('maps direction "both" to isPublisher=true, isSubscriber=true', () => {
const raw = makeDataItem({ 'x-medkit': { ros2: { direction: 'both' } } });
const result = transformDataResponse({ items: [raw] });
expect(result[0]?.isPublisher).toBe(true);
expect(result[0]?.isSubscriber).toBe(true);
});
it('uses topic:direction as uniqueKey when direction present', () => {
const result = transformDataResponse({ items: [makeDataItem()] });
expect(result[0]?.uniqueKey).toBe('/engine/temperature:publish');
});
it('uses topic as uniqueKey when direction absent', () => {
const raw = { id: 'my_topic', name: 'my_topic' };
const result = transformDataResponse({ items: [raw] });
expect(result[0]?.uniqueKey).toBe('my_topic');
});
it('returns empty array for empty response', () => {
const result = transformDataResponse({ items: [] });
expect(result).toEqual([]);
});
});
// =============================================================================
// transformConfigurationsResponse
// =============================================================================
describe('transformConfigurationsResponse', () => {
const makeConfigResponse = (overrides: Record<string, unknown> = {}) => ({
'x-medkit': {
entity_id: 'engine-controller',
ros2: { node: '/powertrain/engine_controller' },
parameters: [
{ name: 'max_rpm', value: 8000, type: 'int' },
{ name: 'enabled', value: true, type: 'bool' },
],
},
...overrides,
});
it('extracts component_id from x-medkit.entity_id', () => {
const result = transformConfigurationsResponse(makeConfigResponse(), 'fallback-id');
expect(result.component_id).toBe('engine-controller');
});
it('falls back component_id to entityId parameter', () => {
const result = transformConfigurationsResponse({ 'x-medkit': {} }, 'fallback-id');
expect(result.component_id).toBe('fallback-id');
});
it('extracts node_name from x-medkit.ros2.node', () => {
const result = transformConfigurationsResponse(makeConfigResponse(), 'fallback-id');
expect(result.node_name).toBe('/powertrain/engine_controller');
});
it('falls back node_name to entityId when ros2.node absent', () => {
const result = transformConfigurationsResponse({ 'x-medkit': {} }, 'fallback-id');
expect(result.node_name).toBe('fallback-id');
});
it('extracts parameters array', () => {
const result = transformConfigurationsResponse(makeConfigResponse(), 'fallback-id');
expect(result.parameters).toHaveLength(2);
expect(result.parameters[0]?.name).toBe('max_rpm');
});
it('returns empty parameters array when absent', () => {
const result = transformConfigurationsResponse({ 'x-medkit': {} }, 'fallback-id');
expect(result.parameters).toEqual([]);
});
it('handles missing x-medkit entirely', () => {
const result = transformConfigurationsResponse({}, 'fallback-id');
expect(result.component_id).toBe('fallback-id');
expect(result.node_name).toBe('fallback-id');
expect(result.parameters).toEqual([]);
});
});
// =============================================================================
// transformBulkDataDescriptor
// =============================================================================
describe('transformBulkDataDescriptor', () => {
const makeRawDescriptor = (overrides: Record<string, unknown> = {}) => ({
id: 'abc-123',
name: 'rosbag_2026_03_22.mcap',
content_type: 'application/x-mcap',
size: 1024 * 1024,
created_at: '2026-03-22T10:00:00Z',
'x-medkit': {
fault_code: 'ENGINE_OVERHEAT',
duration_sec: 5.0,
format: 'mcap',
},
...overrides,
});
it('renames content_type to mimetype', () => {
const result = transformBulkDataDescriptor(makeRawDescriptor());
expect(result.mimetype).toBe('application/x-mcap');
expect(result).not.toHaveProperty('content_type');
});
it('renames created_at to creation_date', () => {
const result = transformBulkDataDescriptor(makeRawDescriptor());
expect(result.creation_date).toBe('2026-03-22T10:00:00Z');
expect(result).not.toHaveProperty('created_at');
});
it('preserves id, name, and size', () => {
const result = transformBulkDataDescriptor(makeRawDescriptor());
expect(result.id).toBe('abc-123');
expect(result.name).toBe('rosbag_2026_03_22.mcap');
expect(result.size).toBe(1024 * 1024);
});
it('preserves x-medkit extension fields', () => {
const result = transformBulkDataDescriptor(makeRawDescriptor());
expect(result['x-medkit']?.fault_code).toBe('ENGINE_OVERHEAT');
expect(result['x-medkit']?.duration_sec).toBe(5.0);
expect(result['x-medkit']?.format).toBe('mcap');
});
it('handles missing x-medkit gracefully', () => {
const raw = makeRawDescriptor();
delete (raw as Record<string, unknown>)['x-medkit'];
const result = transformBulkDataDescriptor(raw);
expect(result['x-medkit']).toBeUndefined();
});
it('handles missing content_type gracefully', () => {
const raw = makeRawDescriptor();
delete (raw as Record<string, unknown>)['content_type'];
const result = transformBulkDataDescriptor(raw);
expect(result.mimetype).toBe('');
});
it('handles missing created_at gracefully', () => {
const raw = makeRawDescriptor();
delete (raw as Record<string, unknown>)['created_at'];
const result = transformBulkDataDescriptor(raw);
expect(result.creation_date).toBe('');
});
});