|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { projectStore } from './project.svelte.js'; |
| 3 | +import { settingsStore } from './settings.svelte.js'; |
| 4 | +import { decodeState } from '$lib/serialization/decode.js'; |
| 5 | +import { encodeState } from '$lib/serialization/encode.js'; |
| 6 | +import { |
| 7 | + SCHEMA_VERSION, |
| 8 | + defaultVisualSettingsV2, |
| 9 | + type AppStateV2, |
| 10 | + type LineV2 |
| 11 | +} from '$lib/serialization/schema.js'; |
| 12 | + |
| 13 | +function line(id: string, rawText: string): LineV2 { |
| 14 | + return { |
| 15 | + id, |
| 16 | + rawText, |
| 17 | + font: { family: 'Inter', source: 'google' }, |
| 18 | + textSizePx: 36, |
| 19 | + gapWordPx: 14 |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +/** Middle line only splits into three tokens with the custom `-` separator. */ |
| 24 | +function stateWithCustomSeparator(): AppStateV2 { |
| 25 | + return { |
| 26 | + v: SCHEMA_VERSION, |
| 27 | + settings: { ...defaultVisualSettingsV2(), tokenSplitChars: '|-' }, |
| 28 | + project: { |
| 29 | + lines: [line('s', 'a b c'), line('t', 'x-y-z'), line('u', 'd e f')], |
| 30 | + connections: [ |
| 31 | + { id: 'c1', upperTokenId: 's-0', lowerTokenId: 't-0' }, |
| 32 | + { id: 'c2', upperTokenId: 's-1', lowerTokenId: 't-1' }, |
| 33 | + { id: 'c3', upperTokenId: 's-2', lowerTokenId: 't-2' }, |
| 34 | + { id: 'c4', upperTokenId: 't-0', lowerTokenId: 'u-2' }, |
| 35 | + { id: 'c5', upperTokenId: 't-1', lowerTokenId: 'u-1' }, |
| 36 | + { id: 'c6', upperTokenId: 't-2', lowerTokenId: 'u-0' } |
| 37 | + ], |
| 38 | + pairControls: [], |
| 39 | + linePairGaps: [] |
| 40 | + } |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +describe('project store: loadState', () => { |
| 45 | + beforeEach(() => { |
| 46 | + settingsStore.reset(); |
| 47 | + projectStore.loadSnapshotV2({ |
| 48 | + lines: [line('s', 'a'), line('t', 'b')], |
| 49 | + connections: [], |
| 50 | + pairControls: [], |
| 51 | + linePairGaps: [] |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keeps links on tokens produced by the loaded tokenization settings', () => { |
| 56 | + const shared = decodeState(encodeState(stateWithCustomSeparator())); |
| 57 | + expect(shared.project.connections).toHaveLength(6); |
| 58 | + |
| 59 | + projectStore.loadState(shared); |
| 60 | + |
| 61 | + expect(settingsStore.settings.tokenSplitChars).toBe('|-'); |
| 62 | + expect(projectStore.tokensOnLine('t').map((t) => t.text)).toEqual(['x', 'y', 'z']); |
| 63 | + expect(projectStore.connections).toHaveLength(6); |
| 64 | + }); |
| 65 | +}); |
0 commit comments