|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { resolveDate, toMomentFormat } from '../../journal/template-engine'; |
| 3 | + |
| 4 | +const KNOWN_DATE = new Date(2024, 2, 5, 14, 30, 0); // 2024-03-05 14:30 |
| 5 | + |
| 6 | +suite('TemplateEngine', () => { |
| 7 | + |
| 8 | + suite('resolveDate — named variables', () => { |
| 9 | + test('replaces ${year}', () => { |
| 10 | + assert.strictEqual(resolveDate('${year}', KNOWN_DATE), '2024'); |
| 11 | + }); |
| 12 | + test('replaces ${month}', () => { |
| 13 | + assert.strictEqual(resolveDate('${month}', KNOWN_DATE), '03'); |
| 14 | + }); |
| 15 | + test('replaces ${day}', () => { |
| 16 | + assert.strictEqual(resolveDate('${day}', KNOWN_DATE), '05'); |
| 17 | + }); |
| 18 | + test('replaces ${weekday}', () => { |
| 19 | + assert.strictEqual(resolveDate('${weekday}', KNOWN_DATE), 'Tuesday'); |
| 20 | + }); |
| 21 | + test('replaces ${week}', () => { |
| 22 | + assert.strictEqual(resolveDate('${week}', KNOWN_DATE), '10'); |
| 23 | + }); |
| 24 | + test('replaces multiple variables in one pass', () => { |
| 25 | + assert.strictEqual(resolveDate('${year}/${month}/${day}', KNOWN_DATE), '2024/03/05'); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + suite('resolveDate — custom format', () => { |
| 30 | + test('replaces ${d:YY} with two-digit year', () => { |
| 31 | + assert.strictEqual(resolveDate('year: ${d:YY}', KNOWN_DATE), 'year: 24'); |
| 32 | + }); |
| 33 | + test('replaces ${d:dddd} with full weekday name', () => { |
| 34 | + assert.strictEqual(resolveDate('${d:dddd}', KNOWN_DATE), 'Tuesday'); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + suite('resolveDate — edge cases', () => { |
| 39 | + test('returns template unchanged when no variables', () => { |
| 40 | + assert.strictEqual(resolveDate('no variables here', KNOWN_DATE), 'no variables here'); |
| 41 | + }); |
| 42 | + test('locale isolation: two calls with different locales do not bleed', () => { |
| 43 | + const de = resolveDate('${weekday}', KNOWN_DATE, 'de'); |
| 44 | + const en = resolveDate('${weekday}', KNOWN_DATE, 'en'); |
| 45 | + assert.strictEqual(de, 'Dienstag'); |
| 46 | + assert.strictEqual(en, 'Tuesday'); |
| 47 | + // third call without locale should not inherit 'de' |
| 48 | + const neutral = resolveDate('${weekday}', KNOWN_DATE); |
| 49 | + assert.strictEqual(neutral, en); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + suite('toMomentFormat — named variables', () => { |
| 54 | + test('maps ${year} to YYYY', () => { |
| 55 | + assert.strictEqual(toMomentFormat('${year}'), 'YYYY'); |
| 56 | + }); |
| 57 | + test('maps ${month} to MM', () => { |
| 58 | + assert.strictEqual(toMomentFormat('${month}'), 'MM'); |
| 59 | + }); |
| 60 | + test('maps ${day} to DD', () => { |
| 61 | + assert.strictEqual(toMomentFormat('${day}'), 'DD'); |
| 62 | + }); |
| 63 | + test('maps ${localTime} to LT', () => { |
| 64 | + assert.strictEqual(toMomentFormat('${localTime}'), 'LT'); |
| 65 | + }); |
| 66 | + test('maps ${localDate} to LL', () => { |
| 67 | + assert.strictEqual(toMomentFormat('${localDate}'), 'LL'); |
| 68 | + }); |
| 69 | + test('maps ${weekday} to dddd', () => { |
| 70 | + assert.strictEqual(toMomentFormat('${weekday}'), 'dddd'); |
| 71 | + }); |
| 72 | + test('maps ${week} to w — fixes the prior gap', () => { |
| 73 | + assert.strictEqual(toMomentFormat('${week}'), 'w'); |
| 74 | + }); |
| 75 | + test('handles mixed template', () => { |
| 76 | + assert.strictEqual(toMomentFormat('${year}/${month}/${day}'), 'YYYY/MM/DD'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + suite('toMomentFormat — custom format passthrough', () => { |
| 81 | + test('passes ${d:YY} through as YY', () => { |
| 82 | + assert.strictEqual(toMomentFormat('${d:YY}'), 'YY'); |
| 83 | + }); |
| 84 | + test('passes ${d:dddd} through as dddd', () => { |
| 85 | + assert.strictEqual(toMomentFormat('${d:dddd}'), 'dddd'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + suite('toMomentFormat — edge cases', () => { |
| 90 | + test('returns template unchanged when no variables', () => { |
| 91 | + assert.strictEqual(toMomentFormat('no variables'), 'no variables'); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments