|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { slugifySlideId, isValidSlideId, uniqueSlideId, assignSlideIds } from './ids.ts'; |
| 3 | +import type { TLTimeline } from '../types.ts'; |
| 4 | + |
| 5 | +describe('slugifySlideId', () => { |
| 6 | + it('slugs a headline', () => { |
| 7 | + expect(slugifySlideId('The Moon Landing')).toBe('the-moon-landing'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('strips HTML and punctuation', () => { |
| 11 | + expect(slugifySlideId('<b>Apollo 11:</b> "one small step"')).toBe('apollo-11-one-small-step'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('folds accents rather than dropping the word', () => { |
| 15 | + expect(slugifySlideId('Año Nuevo en México')).toBe('ano-nuevo-en-mexico'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('keeps a leading number by prefixing instead of truncating', () => { |
| 19 | + expect(slugifySlideId('1969 Moon Landing')).toBe('slide-1969-moon-landing'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns empty when nothing usable survives', () => { |
| 23 | + expect(slugifySlideId('???')).toBe(''); |
| 24 | + expect(slugifySlideId('東京')).toBe(''); |
| 25 | + }); |
| 26 | + |
| 27 | + it('produces legal ids', () => { |
| 28 | + for (const h of ['The Moon Landing', '1969 Moon Landing', '<b>Apollo 11:</b>']) { |
| 29 | + expect(isValidSlideId(slugifySlideId(h))).toBe(true); |
| 30 | + } |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('assignSlideIds', () => { |
| 35 | + it('builds ids from headlines', () => { |
| 36 | + const tl: TLTimeline = { |
| 37 | + events: [ |
| 38 | + { start_date: { year: 2000 }, text: { headline: 'First Thing' } }, |
| 39 | + { start_date: { year: 2001 }, text: { headline: 'Second Thing' } }, |
| 40 | + ], |
| 41 | + }; |
| 42 | + assignSlideIds(tl); |
| 43 | + expect(tl.events.map(e => e.unique_id)).toEqual(['first-thing', 'second-thing']); |
| 44 | + }); |
| 45 | + |
| 46 | + it('replaces illegal ids, including the numeric ones legacy imports produced', () => { |
| 47 | + const tl: TLTimeline = { |
| 48 | + events: [ |
| 49 | + { start_date: { year: 2000 }, unique_id: '1', text: { headline: 'First Thing' } }, |
| 50 | + { start_date: { year: 2001 }, unique_id: '2', text: { headline: 'Second Thing' } }, |
| 51 | + ], |
| 52 | + }; |
| 53 | + assignSlideIds(tl); |
| 54 | + expect(tl.events.map(e => e.unique_id)).toEqual(['first-thing', 'second-thing']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('keeps ids that are already legal', () => { |
| 58 | + const tl: TLTimeline = { |
| 59 | + events: [{ start_date: { year: 2000 }, unique_id: 'my-slide', text: { headline: 'First Thing' } }], |
| 60 | + }; |
| 61 | + assignSlideIds(tl); |
| 62 | + expect(tl.events[0].unique_id).toBe('my-slide'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('names the title slide "title"', () => { |
| 66 | + const tl: TLTimeline = { title: { unique_id: '1', text: { headline: 'My Timeline' } }, events: [] }; |
| 67 | + assignSlideIds(tl); |
| 68 | + expect(tl.title?.unique_id).toBe('title'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('disambiguates repeated headlines', () => { |
| 72 | + const tl: TLTimeline = { |
| 73 | + events: [ |
| 74 | + { start_date: { year: 2000 }, text: { headline: 'Same' } }, |
| 75 | + { start_date: { year: 2001 }, text: { headline: 'Same' } }, |
| 76 | + { start_date: { year: 2002 }, text: { headline: 'Same' } }, |
| 77 | + ], |
| 78 | + }; |
| 79 | + assignSlideIds(tl); |
| 80 | + expect(tl.events.map(e => e.unique_id)).toEqual(['same', 'same-2', 'same-3']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not let a regenerated id collide with a preserved one', () => { |
| 84 | + const tl: TLTimeline = { |
| 85 | + events: [ |
| 86 | + { start_date: { year: 2000 }, unique_id: '1', text: { headline: 'Same' } }, |
| 87 | + { start_date: { year: 2001 }, unique_id: 'same', text: { headline: 'Same' } }, |
| 88 | + ], |
| 89 | + }; |
| 90 | + assignSlideIds(tl); |
| 91 | + expect(tl.events.map(e => e.unique_id)).toEqual(['same-2', 'same']); |
| 92 | + }); |
| 93 | + |
| 94 | + it('falls back to a positional id when a headline yields nothing', () => { |
| 95 | + const tl: TLTimeline = { |
| 96 | + events: [ |
| 97 | + { start_date: { year: 2000 } }, |
| 98 | + { start_date: { year: 2001 }, text: { headline: '東京' } }, |
| 99 | + ], |
| 100 | + }; |
| 101 | + assignSlideIds(tl); |
| 102 | + expect(tl.events.map(e => e.unique_id)).toEqual(['slide-1', 'slide-2']); |
| 103 | + for (const e of tl.events) expect(isValidSlideId(e.unique_id!)).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('gives every event an id even with duplicate illegal ids in the source', () => { |
| 107 | + const tl: TLTimeline = { |
| 108 | + events: [ |
| 109 | + { start_date: { year: 2000 }, unique_id: 'dup', text: { headline: 'A' } }, |
| 110 | + { start_date: { year: 2001 }, unique_id: 'dup', text: { headline: 'B' } }, |
| 111 | + ], |
| 112 | + }; |
| 113 | + assignSlideIds(tl); |
| 114 | + expect(tl.events.map(e => e.unique_id)).toEqual(['dup', 'b']); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('uniqueSlideId', () => { |
| 119 | + it('returns the base when it is free', () => { |
| 120 | + expect(uniqueSlideId('intro', new Set(['other']))).toBe('intro'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('suffixes past every taken variant', () => { |
| 124 | + expect(uniqueSlideId('intro', new Set(['intro', 'intro-2']))).toBe('intro-3'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('does not mutate the set it is given', () => { |
| 128 | + const taken = new Set(['intro']); |
| 129 | + uniqueSlideId('intro', taken); |
| 130 | + expect([...taken]).toEqual(['intro']); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('slugifySlideId with entities', () => { |
| 135 | + it('drops HTML entities rather than spelling them out', () => { |
| 136 | + expect(slugifySlideId('Rock & Roll')).toBe('rock-roll'); |
| 137 | + }); |
| 138 | +}); |
0 commit comments