|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const { dedupeCalendarEvents, normalizeTitle, titleSimilarity } = require('../utils/calendarDedup'); |
| 4 | + |
| 5 | +const BASE = new Date('2026-07-10T15:00:00.000Z').getTime(); |
| 6 | + |
| 7 | +function ev(id, sourceId, sourceName, title, startOffsetMin = 0, durationMin = 60) { |
| 8 | + const start = new Date(BASE + startOffsetMin * 60000); |
| 9 | + const end = new Date(start.getTime() + durationMin * 60000); |
| 10 | + return { id, source_id: sourceId, source_name: sourceName, title, start, end }; |
| 11 | +} |
| 12 | + |
| 13 | +test('normalizeTitle strips calendar prefix, punctuation, and case', () => { |
| 14 | + assert.equal(normalizeTitle('[Family] Soccer Practice!'), 'soccer practice'); |
| 15 | + assert.equal(normalizeTitle('(Work) Stand-up'), 'stand up'); |
| 16 | + assert.equal(normalizeTitle(null), ''); |
| 17 | +}); |
| 18 | + |
| 19 | +test('titleSimilarity: containment and exact match score 1', () => { |
| 20 | + assert.equal(titleSimilarity('Soccer', 'Soccer practice'), 1); |
| 21 | + assert.equal(titleSimilarity('Dentist', 'dentist'), 1); |
| 22 | + assert.ok(titleSimilarity('Dentist', 'Soccer') < 0.3); |
| 23 | +}); |
| 24 | + |
| 25 | +test('merges the same event across two different sources', () => { |
| 26 | + const events = [ |
| 27 | + ev('a1', 1, 'Family ICS', 'Soccer practice'), |
| 28 | + ev('a2', 2, 'Google', 'Soccer Practice - City Fields', 2), |
| 29 | + ]; |
| 30 | + const out = dedupeCalendarEvents(events); |
| 31 | + assert.equal(out.length, 1); |
| 32 | + assert.equal(out[0].source_id, 1, 'lowest source_id survives'); |
| 33 | + assert.equal(out[0].title, 'Soccer practice'); |
| 34 | + assert.deepEqual(out[0].merged_from, [{ source_id: 2, source_name: 'Google' }]); |
| 35 | +}); |
| 36 | + |
| 37 | +test('never merges two events from the same source', () => { |
| 38 | + const events = [ |
| 39 | + ev('d1', 1, 'Family ICS', 'Dishes'), |
| 40 | + ev('d2', 1, 'Family ICS', 'Dishes'), |
| 41 | + ]; |
| 42 | + const out = dedupeCalendarEvents(events); |
| 43 | + assert.equal(out.length, 2); |
| 44 | + assert.ok(!out.some((e) => e.merged_from)); |
| 45 | +}); |
| 46 | + |
| 47 | +test('does not merge same title at clearly different times', () => { |
| 48 | + const events = [ |
| 49 | + ev('b1', 1, 'Family ICS', 'Dentist', 0), |
| 50 | + ev('c1', 2, 'Google', 'Dentist', 120), // 2 hours later |
| 51 | + ]; |
| 52 | + const out = dedupeCalendarEvents(events); |
| 53 | + assert.equal(out.length, 2); |
| 54 | +}); |
| 55 | + |
| 56 | +test('does not merge different events at the same time', () => { |
| 57 | + const events = [ |
| 58 | + ev('x1', 1, 'Family ICS', 'Piano lesson'), |
| 59 | + ev('y1', 2, 'Google', 'Grocery run'), |
| 60 | + ]; |
| 61 | + const out = dedupeCalendarEvents(events); |
| 62 | + assert.equal(out.length, 2); |
| 63 | +}); |
| 64 | + |
| 65 | +test('merges a 3-source cluster into one survivor listing both others', () => { |
| 66 | + const events = [ |
| 67 | + ev('t3', 3, 'Apple', 'Team meeting', 1), |
| 68 | + ev('t1', 1, 'Family ICS', 'Team meeting'), |
| 69 | + ev('t2', 2, 'Google', 'Team Meeting', 3), |
| 70 | + ]; |
| 71 | + const out = dedupeCalendarEvents(events); |
| 72 | + assert.equal(out.length, 1); |
| 73 | + assert.equal(out[0].source_id, 1); |
| 74 | + assert.deepEqual( |
| 75 | + out[0].merged_from.map((m) => m.source_id), |
| 76 | + [2, 3] |
| 77 | + ); |
| 78 | +}); |
| 79 | + |
| 80 | +test('respects the time tolerance boundary', () => { |
| 81 | + const withinTol = [ev('w1', 1, 'A', 'Event'), ev('w2', 2, 'B', 'Event', 4)]; |
| 82 | + assert.equal(dedupeCalendarEvents(withinTol).length, 1, '4 min apart merges'); |
| 83 | + |
| 84 | + const outsideTol = [ev('o1', 1, 'A', 'Event'), ev('o2', 2, 'B', 'Event', 10)]; |
| 85 | + assert.equal(dedupeCalendarEvents(outsideTol).length, 2, '10 min apart does not'); |
| 86 | +}); |
| 87 | + |
| 88 | +test('output stays sorted by start time and does not mutate input', () => { |
| 89 | + const events = [ |
| 90 | + ev('late', 1, 'A', 'Late thing', 120), |
| 91 | + ev('early', 2, 'B', 'Early thing', 0), |
| 92 | + ]; |
| 93 | + const snapshot = JSON.stringify(events); |
| 94 | + const out = dedupeCalendarEvents(events); |
| 95 | + assert.equal(out[0].id, 'early'); |
| 96 | + assert.equal(out[1].id, 'late'); |
| 97 | + assert.equal(JSON.stringify(events), snapshot, 'input array/objects unchanged'); |
| 98 | +}); |
| 99 | + |
| 100 | +test('passes through arrays of 0 or 1 event unchanged', () => { |
| 101 | + assert.deepEqual(dedupeCalendarEvents([]), []); |
| 102 | + const one = [ev('solo', 1, 'A', 'Solo')]; |
| 103 | + assert.equal(dedupeCalendarEvents(one).length, 1); |
| 104 | +}); |
0 commit comments