import { describe, it, type TestContext } from 'node:test'; import { withFetchMock } from '#/test'; import { Notification } from './notification.ts'; import { AssertionError } from './util/mod.ts'; describe('notification', () => { it('should send ticket with click', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:1', market: 'se', session: () => ({ customerKey: 'c1', sessionKey: 's1' }) }); await notify.click('ticket:1') satisfies void; f.assertSnapshot(1); }); it('should send page and list on impression', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:1', market: 'se', session: () => ({ customerKey: 'c1', sessionKey: 's1' }) }); await notify.adImpression('ticket:1') satisfies void; f.assertSnapshot(1); }); it('should send ticket with add-to-cart', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:2', market: 'us', session: () => ({ customerKey: 'c2', sessionKey: 's2' }) }); await notify.addToCart('ticket:2') satisfies void; f.assertSnapshot(1); }); it('should send add favorite notification with product-key', async (t: TestContext) => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:3', market: 'no', session: () => ({ customerKey: 'c3', sessionKey: 's3' }) }); await notify.addFavorite('pk:3') satisfies void; t.assert.strictEqual(f.mock.callCount(), 1); await notify.addFavorite({ productKey: 'pk:3-2' }) satisfies void; f.assertSnapshot(2); }); it('should send remove favorite notification with product-key', async (t: TestContext) => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:4', market: 'dk', session: () => ({ customerKey: 'c4', sessionKey: 's4' }) }); await notify.removeFavorite('pk:4') satisfies void; t.assert.strictEqual(f.mock.callCount(), 1); await notify.removeFavorite({ productKey: 'pk:4-2' }) satisfies void; f.assertSnapshot(2); }); it('should send add favorite notification with variant-key', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:5', market: 'fi', session: () => ({ customerKey: 'c5', sessionKey: 's5' }) }); await notify.addFavorite({ variantKey: 'vk:5' }) satisfies void; f.assertSnapshot(1); }); it('should send remove favorite notification with variant-key', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:6', market: 'de', session: () => ({ customerKey: 'c6', sessionKey: 's6' }) }); await notify.removeFavorite({ variantKey: 'vk:6' }) satisfies void; f.assertSnapshot(1); }); it('should send remove recent search notification with correct payload', async (t: TestContext) => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:7', market: 'it', session: () => ({ customerKey: 'c7', sessionKey: 's7' }) }); await notify.removeRecentSearches(['foo', 'bar', 'baz']) satisfies void; t.assert.strictEqual(f.mock.callCount(), 1); await notify.removeRecentSearches('removeAll') satisfies void; f.assertSnapshot(2); }); it('should send remove recently viewed notification with correct payload', async (t: TestContext) => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:9', market: 'cr', session: () => ({ customerKey: 'c9', sessionKey: 's9' }) }); await notify.removeRecentlyViewed(['๐Ÿ™Œ', '๐Ÿคจ']) satisfies void; t.assert.strictEqual(f.mock.callCount(), 1); await notify.removeRecentlyViewed('removeAll') satisfies void; f.assertSnapshot(2); }); it('should send end notification', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:8', market: 'fr', session: () => ({ customerKey: 'c8', sessionKey: 's8' }) }); await notify.end() satisfies void; f.assertSnapshot(1); }); it('should reject promise if response is not "ok"', async (t: TestContext) => { using f = withFetchMock(() => new Response('', { status: 400 })); const notify = new Notification({ clusterUrl: 'http://localhost:82', market: 'fr', session: () => ({ customerKey: 'c8a', sessionKey: 's8a' }) }); // TODO(@csvn): Replace `AssertionError` with `ResponseError` in next major version await t.assert.rejects(notify.end(), new AssertionError('')); // Two calls due to retry with keepalive:false f.assertSnapshot(2); }); it('should retry once with keepalive:false if fetch fails', async () => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:9', market: 'cr', session: () => ({ customerKey: 'c9', sessionKey: 's9' }) }); const responses = [ Promise.resolve(new Response('', { status: 200 })), Promise.reject(new TypeError('Failed to fetch')) ]; f.respondWith(() => responses.pop()!); await notify.click('--ticket--') satisfies void; f.assertSnapshot(2); }); it('should throw on notify when both fetch and retry (with keepalive:false) fails', async (t: TestContext) => { using f = withFetchMock(); const notify = new Notification({ clusterUrl: 'http://localhost:10', market: 'ch', session: () => ({ customerKey: 'c10', sessionKey: 's10' }) }); // TODO(@csvn): Catch network errors and wrap in `ResponseError` in next major version const fetchError = new TypeError('Failed to fetch'); f.respondWith(() => Promise.reject(fetchError)); await t.assert.rejects(notify.click('--ticket--'), fetchError); f.assertSnapshot(2); }); });