|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +/** |
| 5 | + * (c) 2023-2025, Center for Computational Thinking and Design at Aarhus University and contributors |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: MIT |
| 8 | + */ |
| 9 | + |
| 10 | +import { writable } from 'svelte/store'; |
| 11 | +import type { RecordingData } from '../../lib/domain/RecordingData'; |
| 12 | +import Gesture from '../../lib/domain/stores/gesture/Gesture'; |
| 13 | +import type { PersistedGestureData } from '../../lib/domain/stores/gesture/Gestures'; |
| 14 | +import type GestureConfidence from '../../lib/domain/stores/gesture/GestureConfidence'; |
| 15 | +import { |
| 16 | + serializeGestureRecordingsToCSV, |
| 17 | + serializeRecordingToCsvWithoutGestureName, |
| 18 | +} from '../../lib/utils/CSVUtils'; |
| 19 | + |
| 20 | +describe('CSV Test', () => { |
| 21 | + // A crude way to enforce direction of dependencies, inspired by ArchUnit for java |
| 22 | + test('Convert recording', () => { |
| 23 | + const input: RecordingData = { |
| 24 | + ID: 123, |
| 25 | + labels: ['x', 'y', 'z'], |
| 26 | + samples: [ |
| 27 | + { |
| 28 | + vector: [1, 2, 3], |
| 29 | + }, |
| 30 | + { |
| 31 | + vector: [4, 5, 6], |
| 32 | + }, |
| 33 | + { |
| 34 | + vector: [7, 8, 9], |
| 35 | + }, |
| 36 | + ], |
| 37 | + }; |
| 38 | + const data = writable({ |
| 39 | + recordings: [input], |
| 40 | + name: 'Test;Gesture', |
| 41 | + } as PersistedGestureData); |
| 42 | + const confidence = writable({}) as unknown as GestureConfidence; |
| 43 | + const gesture: Gesture = new Gesture(data, confidence, () => void 0); |
| 44 | + const result = serializeGestureRecordingsToCSV([gesture]); |
| 45 | + expect(result).toBe( |
| 46 | + 'gesture;sample;x;y;z\nTest\\;Gesture;0;1;2;3\nTest\\;Gesture;1;4;5;6\nTest\\;Gesture;2;7;8;9', |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Convert multiple gestures', () => { |
| 51 | + const input1: RecordingData = { |
| 52 | + ID: 123, |
| 53 | + labels: ['x', 'y', 'z'], |
| 54 | + samples: [ |
| 55 | + { |
| 56 | + vector: [1, 2, 3], |
| 57 | + }, |
| 58 | + { |
| 59 | + vector: [4, 5, 6], |
| 60 | + }, |
| 61 | + ], |
| 62 | + }; |
| 63 | + const input2: RecordingData = { |
| 64 | + ID: 456, |
| 65 | + labels: ['x', 'y', 'z'], |
| 66 | + samples: [ |
| 67 | + { |
| 68 | + vector: [7, 8, 9], |
| 69 | + }, |
| 70 | + { |
| 71 | + vector: [10, 11, 12], |
| 72 | + }, |
| 73 | + ], |
| 74 | + }; |
| 75 | + const data1 = writable({ |
| 76 | + recordings: [input1], |
| 77 | + name: 'Gesture1', |
| 78 | + } as PersistedGestureData); |
| 79 | + const data2 = writable({ |
| 80 | + recordings: [input2], |
| 81 | + name: 'Gesture2', |
| 82 | + } as PersistedGestureData); |
| 83 | + const confidence = writable({}) as unknown as GestureConfidence; |
| 84 | + const gesture1: Gesture = new Gesture(data1, confidence, () => void 0); |
| 85 | + const gesture2: Gesture = new Gesture(data2, confidence, () => void 0); |
| 86 | + const result = serializeGestureRecordingsToCSV([gesture1, gesture2]); |
| 87 | + expect(result).toBe( |
| 88 | + 'gesture;sample;x;y;z\n' + |
| 89 | + 'Gesture1;0;1;2;3\n' + |
| 90 | + 'Gesture1;1;4;5;6\n' + |
| 91 | + 'Gesture2;0;7;8;9\n' + |
| 92 | + 'Gesture2;1;10;11;12', |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + test('Serialize recording without gesture name (with headers)', () => { |
| 97 | + const input: RecordingData = { |
| 98 | + ID: 123, |
| 99 | + labels: ['x', 'y', 'z'], |
| 100 | + samples: [ |
| 101 | + { |
| 102 | + vector: [1, 2, 3], |
| 103 | + }, |
| 104 | + { |
| 105 | + vector: [4, 5, 6], |
| 106 | + }, |
| 107 | + { |
| 108 | + vector: [7, 8, 9], |
| 109 | + }, |
| 110 | + ], |
| 111 | + }; |
| 112 | + |
| 113 | + const result = serializeRecordingToCsvWithoutGestureName(input); |
| 114 | + expect(result).toBe('sample;x;y;z\n' + '0;1;2;3\n' + '1;4;5;6\n' + '2;7;8;9'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments