|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 2 | +import { createPinia, setActivePinia } from 'pinia'; |
| 3 | +import type { ScriptLine } from '@/types/api/script'; |
| 4 | +import type { Cue } from '@/types/api/cues'; |
| 5 | +import { useScriptStore } from './script'; |
| 6 | + |
| 7 | +function makeLine(id: number): ScriptLine { |
| 8 | + return { |
| 9 | + id, |
| 10 | + act_id: 1, |
| 11 | + scene_id: 1, |
| 12 | + page: 1, |
| 13 | + line_type: 1, |
| 14 | + stage_direction_style_id: null, |
| 15 | + line_parts: [], |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function makeCue(id: number, cueTypeId: number, linePosition: number | null = 0): Cue { |
| 20 | + return { |
| 21 | + id, |
| 22 | + cue_type_id: cueTypeId, |
| 23 | + ident: String(id), |
| 24 | + group_id: null, |
| 25 | + sort_order: null, |
| 26 | + line_position: linePosition, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe('script store cue tracking getters', () => { |
| 31 | + beforeEach(() => { |
| 32 | + setActivePinia(createPinia()); |
| 33 | + }); |
| 34 | + |
| 35 | + it('builds a line order index across pages', () => { |
| 36 | + const store = useScriptStore(); |
| 37 | + store.script = { |
| 38 | + '1': [makeLine(10), makeLine(11)], |
| 39 | + '2': [makeLine(20)], |
| 40 | + }; |
| 41 | + |
| 42 | + const index = store.lineOrderIndex; |
| 43 | + |
| 44 | + expect(index.get(10)).toEqual({ page: 1, index: 0 }); |
| 45 | + expect(index.get(11)).toEqual({ page: 1, index: 1 }); |
| 46 | + expect(index.get(20)).toEqual({ page: 2, index: 0 }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns the last cue per cue type at or before the given position', () => { |
| 50 | + const store = useScriptStore(); |
| 51 | + store.script = { |
| 52 | + '1': [makeLine(10), makeLine(11)], |
| 53 | + '2': [makeLine(20), makeLine(21)], |
| 54 | + }; |
| 55 | + store.cues = { |
| 56 | + '10': [makeCue(1, 100), makeCue(2, 200)], |
| 57 | + '11': [makeCue(3, 100)], |
| 58 | + '20': [makeCue(4, 200)], |
| 59 | + '21': [makeCue(5, 100)], |
| 60 | + }; |
| 61 | + |
| 62 | + // Position at page 1, line index 1 (line 11): cue type 100 -> cue 3, type 200 -> cue 2 |
| 63 | + const atLine11 = store.lastCuePerTypeAt(1, 1); |
| 64 | + expect(atLine11[100].id).toBe(3); |
| 65 | + expect(atLine11[200].id).toBe(2); |
| 66 | + |
| 67 | + // Position at page 2, line index 0 (line 20): type 100 still cue 3 (last passed), type 200 -> cue 4 |
| 68 | + const atLine20 = store.lastCuePerTypeAt(2, 0); |
| 69 | + expect(atLine20[100].id).toBe(3); |
| 70 | + expect(atLine20[200].id).toBe(4); |
| 71 | + |
| 72 | + // Position at page 2, line index 1 (line 21): type 100 -> cue 5 (overtakes cue 3) |
| 73 | + const atLine21 = store.lastCuePerTypeAt(2, 1); |
| 74 | + expect(atLine21[100].id).toBe(5); |
| 75 | + expect(atLine21[200].id).toBe(4); |
| 76 | + }); |
| 77 | + |
| 78 | + it('ignores cues on lines not yet in the line order index', () => { |
| 79 | + const store = useScriptStore(); |
| 80 | + store.script = { '1': [makeLine(10)] }; |
| 81 | + store.cues = { '999': [makeCue(1, 100)] }; |
| 82 | + |
| 83 | + const result = store.lastCuePerTypeAt(1, 0); |
| 84 | + |
| 85 | + expect(result).toEqual({}); |
| 86 | + }); |
| 87 | + |
| 88 | + it('breaks ties on the same line using line_position', () => { |
| 89 | + const store = useScriptStore(); |
| 90 | + store.script = { '1': [makeLine(10)] }; |
| 91 | + store.cues = { |
| 92 | + '10': [makeCue(1, 100, 0), makeCue(2, 100, 5)], |
| 93 | + }; |
| 94 | + |
| 95 | + const result = store.lastCuePerTypeAt(1, 0); |
| 96 | + |
| 97 | + expect(result[100].id).toBe(2); |
| 98 | + }); |
| 99 | +}); |
0 commit comments