-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadSegmentIdentity.test.ts
More file actions
381 lines (308 loc) · 11.8 KB
/
Copy pathadSegmentIdentity.test.ts
File metadata and controls
381 lines (308 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* Unit Tests for Episode Identity Resolution
*
* Tests the episode key generation logic:
* - Primary: RSS <guid>
* - Fallback: hash(feedUrl + enclosureUrl + pubDate)
*/
import {
generateEpisodeKey,
isHashBasedKey,
batchGenerateEpisodeKeys,
} from '~/services/adSegments/adSegmentIdentity'
import { EpisodeIdentityInput } from '~/services/adSegments/adSegmentIdentity'
describe('Generate Episode Key - GUID-based', () => {
test('uses GUID when available', () => {
const input: EpisodeIdentityInput = {
guid: 'https://example.com/podcast/episode-123',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode-123.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toBe('https://example.com/podcast/episode-123')
expect(isHashBasedKey(key)).toBe(false)
})
test('trims whitespace from GUID', () => {
const input: EpisodeIdentityInput = {
guid: ' https://example.com/podcast/episode-123 ',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode-123.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toBe('https://example.com/podcast/episode-123')
})
test('strips CDATA wrapper from GUID', () => {
const input: EpisodeIdentityInput = {
guid: '<![CDATA[ b47746b8-daa3-11f0-b08a-233a79f7667c ]]>',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toBe('b47746b8-daa3-11f0-b08a-233a79f7667c')
expect(isHashBasedKey(key)).toBe(false)
})
test('handles CDATA with extra whitespace', () => {
const input: EpisodeIdentityInput = {
guid: '<![CDATA[ ec8b4655-cb61-4d69-87ee-dbdba77c0a62 ]]>',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toBe('ec8b4655-cb61-4d69-87ee-dbdba77c0a62')
})
test('handles real-world test episode GUIDs', () => {
// Test 1: Plain UUID (What Bitcoin Did)
const test1: EpisodeIdentityInput = {
guid: 'ec8b4655-cb61-4d69-87ee-dbdba77c0a62',
feedUrl: 'https://feeds.fountain.fm/UZSKQcrOnhqYS1JopxGg',
enclosureUrl: 'https://feeds.fountain.fm/UZSKQcrOnhqYS1JopxGg/items/RpJavE7EEgZNW5hpgUDJ/files/AUDIO---DEFAULT---dcfdc6d6-489e-4ac7-b9a4-5629cc632395.mp3',
}
const key1 = generateEpisodeKey(test1)
expect(key1).toBe('ec8b4655-cb61-4d69-87ee-dbdba77c0a62')
// Test 2: Plain UUID (TFTC)
const test2: EpisodeIdentityInput = {
guid: '2d4217bb-799b-4192-a559-348e82e2ea0e',
feedUrl: 'https://feeds.fountain.fm/ZwwaDULvAj0yZvJ5kdB9/',
enclosureUrl: 'https://feeds.fountain.fm/ZwwaDULvAj0yZvJ5kdB9/items/oQBhreJIa5P98Zh0AUmh/files/AUDIO---DEFAULT---34eb3a45-d135-453e-a790-58a744da35ba.mp3',
}
const key2 = generateEpisodeKey(test2)
expect(key2).toBe('2d4217bb-799b-4192-a559-348e82e2ea0e')
// Test 3: CDATA wrapped UUID (TIP)
const test3: EpisodeIdentityInput = {
guid: '<![CDATA[ b47746b8-daa3-11f0-b08a-233a79f7667c ]]>',
feedUrl: 'https://feeds.megaphone.fm/PPLLC8974708240',
enclosureUrl: 'https://www.podtrac.com/pts/redirect.mp3/pdst.fm/e/traffic.megaphone.fm/PPLLC1896786790.mp3?updated=1765909572',
}
const key3 = generateEpisodeKey(test3)
expect(key3).toBe('b47746b8-daa3-11f0-b08a-233a79f7667c')
})
test('handles GUID with special characters', () => {
const input: EpisodeIdentityInput = {
guid: 'urn:uuid:12345678-1234-1234-1234-123456789abc',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toBe('urn:uuid:12345678-1234-1234-1234-123456789abc')
})
})
describe('Generate Episode Key - Hash-based Fallback', () => {
test('generates hash when GUID is missing', () => {
const input: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toMatch(/^hash:[0-9a-f]{16}$/)
expect(isHashBasedKey(key)).toBe(true)
})
test('generates hash when GUID is empty string', () => {
const input: EpisodeIdentityInput = {
guid: '',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toMatch(/^hash:[0-9a-f]{16}$/)
expect(isHashBasedKey(key)).toBe(true)
})
test('generates hash when GUID is only whitespace', () => {
const input: EpisodeIdentityInput = {
guid: ' ',
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key = generateEpisodeKey(input)
expect(key).toMatch(/^hash:[0-9a-f]{16}$/)
expect(isHashBasedKey(key)).toBe(true)
})
test('hash is deterministic (same input = same hash)', () => {
const input1: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const input2: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key1 = generateEpisodeKey(input1)
const key2 = generateEpisodeKey(input2)
expect(key1).toBe(key2)
})
test('hash is unique (different input = different hash)', () => {
const input1: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode1.mp3',
}
const input2: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode2.mp3',
}
const key1 = generateEpisodeKey(input1)
const key2 = generateEpisodeKey(input2)
expect(key1).not.toBe(key2)
})
test('includes pubDate in hash when available', () => {
const inputWithPubDate: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
pubDate: '2025-01-01T00:00:00Z',
}
const inputWithoutPubDate: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const keyWith = generateEpisodeKey(inputWithPubDate)
const keyWithout = generateEpisodeKey(inputWithoutPubDate)
// Different pubDate should result in different hash
expect(keyWith).not.toBe(keyWithout)
})
test('normalizes URLs (case-insensitive)', () => {
const input1: EpisodeIdentityInput = {
guid: null,
feedUrl: 'HTTPS://EXAMPLE.COM/PODCAST/FEED.XML',
enclosureUrl: 'HTTPS://EXAMPLE.COM/PODCAST/EPISODE.MP3',
}
const input2: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key1 = generateEpisodeKey(input1)
const key2 = generateEpisodeKey(input2)
// Should generate same hash despite case differences
expect(key1).toBe(key2)
})
test('normalizes trailing slashes', () => {
const input1: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml/',
enclosureUrl: 'https://example.com/podcast/episode.mp3/',
}
const input2: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
const key1 = generateEpisodeKey(input1)
const key2 = generateEpisodeKey(input2)
// Should generate same hash despite trailing slashes
expect(key1).toBe(key2)
})
})
describe('Generate Episode Key - Error Cases', () => {
test('throws error when GUID is missing and feedUrl is missing', () => {
const input: EpisodeIdentityInput = {
guid: null,
feedUrl: null,
enclosureUrl: 'https://example.com/podcast/episode.mp3',
}
expect(() => generateEpisodeKey(input)).toThrow()
})
test('throws error when GUID is missing and enclosureUrl is missing', () => {
const input: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/podcast/feed.xml',
enclosureUrl: null,
}
expect(() => generateEpisodeKey(input)).toThrow()
})
test('throws error when GUID is missing and both feedUrl and enclosureUrl are missing', () => {
const input: EpisodeIdentityInput = {
guid: null,
feedUrl: null,
enclosureUrl: null,
}
expect(() => generateEpisodeKey(input)).toThrow()
})
})
describe('Batch Generate Episode Keys', () => {
test('generates keys for multiple episodes', () => {
const inputs: EpisodeIdentityInput[] = [
{
guid: 'https://example.com/episode/1',
feedUrl: 'https://example.com/feed.xml',
enclosureUrl: 'https://example.com/episode1.mp3',
},
{
guid: null,
feedUrl: 'https://example.com/feed.xml',
enclosureUrl: 'https://example.com/episode2.mp3',
},
{
guid: 'urn:uuid:12345',
feedUrl: 'https://example.com/feed.xml',
enclosureUrl: 'https://example.com/episode3.mp3',
},
]
const keys = batchGenerateEpisodeKeys(inputs)
expect(keys).toHaveLength(3)
expect(keys[0]).toBe('https://example.com/episode/1')
expect(keys[1]).toMatch(/^hash:[0-9a-f]{16}$/)
expect(keys[2]).toBe('urn:uuid:12345')
})
test('handles failed key generation gracefully', () => {
const inputs: EpisodeIdentityInput[] = [
{
guid: 'valid-guid',
feedUrl: 'https://example.com/feed.xml',
enclosureUrl: 'https://example.com/episode.mp3',
},
{
guid: null,
feedUrl: null,
enclosureUrl: null, // This will fail
},
]
const keys = batchGenerateEpisodeKeys(inputs)
expect(keys).toHaveLength(2)
expect(keys[0]).toBe('valid-guid')
expect(keys[1]).toBe('') // Failed keys return empty string
})
})
describe('Is Hash-Based Key', () => {
test('identifies hash-based keys', () => {
expect(isHashBasedKey('hash:abc123def456')).toBe(true)
expect(isHashBasedKey('hash:0000000000000000')).toBe(true)
})
test('identifies GUID-based keys', () => {
expect(isHashBasedKey('https://example.com/episode/1')).toBe(false)
expect(isHashBasedKey('urn:uuid:12345')).toBe(false)
expect(isHashBasedKey('episode-guid-123')).toBe(false)
})
})
describe('Alternative Field Names', () => {
test('accepts mediaUrl as alternative to enclosureUrl', () => {
const input: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/feed.xml',
mediaUrl: 'https://example.com/episode.mp3', // Using mediaUrl instead of enclosureUrl
}
const key = generateEpisodeKey(input)
expect(key).toMatch(/^hash:[0-9a-f]{16}$/)
})
test('prefers enclosureUrl over mediaUrl when both are present', () => {
const input: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/feed.xml',
enclosureUrl: 'https://example.com/episode1.mp3',
mediaUrl: 'https://example.com/episode2.mp3',
}
const key1 = generateEpisodeKey(input)
const inputOnlyEnclosure: EpisodeIdentityInput = {
guid: null,
feedUrl: 'https://example.com/feed.xml',
enclosureUrl: 'https://example.com/episode1.mp3',
}
const key2 = generateEpisodeKey(inputOnlyEnclosure)
// Should use enclosureUrl, so keys should match
expect(key1).toBe(key2)
})
})