import { describe, it, type TestContext } from 'node:test'; import { delay } from '#/test'; import { createBaseUrl, addUrlParams, facetsToParams } from './url.ts'; describe('createBaseUrl()', () => { it('should return a URL', async (t: TestContext) => { const url = await createBaseUrl('queries/', { market: '', clusterUrl: 'https://example.com', session: { customerKey: '', sessionKey: '' } }); t.assert.ok(url instanceof URL); }); it('should not add any query params when market + session is missing', async (t: TestContext) => { const clusterUrl = 'https://example.com'; // @ts-expect-error expected error const a = await createBaseUrl('queries/', { clusterUrl, session: {} }); // @ts-expect-error expected error const b = await createBaseUrl('queries/', { clusterUrl, session: delay(50, {}) }); // @ts-expect-error expected error const c = await createBaseUrl('queries/', { clusterUrl, session: () => ({}) }); // @ts-expect-error expected error const d = await createBaseUrl('queries/', { clusterUrl, session: () => delay(50, {}) }); t.assert.strictEqual(a.search, ''); t.assert.strictEqual(b.search, ''); t.assert.strictEqual(c.search, ''); t.assert.strictEqual(d.search, ''); }); it('should create correct paths for different endpoints', async (t: TestContext) => { const config = { market: '', clusterUrl: 'https://example.com', session: { customerKey: '', sessionKey: '' } }; const { pathname: queryPath } = await createBaseUrl('queries/endpoint-1', config); const { pathname: notifyPath } = await createBaseUrl('notifications/endpoint-2', config); t.assert.strictEqual(queryPath, '/api/storefront/v3/queries/endpoint-1'); t.assert.strictEqual(notifyPath, '/api/storefront/v3/notifications/endpoint-2'); }); it('should support all variants of session config', async (t: TestContext) => { const config = { market: '', clusterUrl: 'https://example.com' }; const sync = { customerKey: 'a_c', sessionKey: 'a_s' }; const promise = Promise.resolve({ customerKey: 'b_c', sessionKey: 'b_s' }); const lazy = () => ({ customerKey: 'c_c', sessionKey: 'c_s' }); const lazyAsync = () => Promise.resolve({ customerKey: 'd_c', sessionKey: 'd_s' }); const a = await createBaseUrl('queries/a', { ...config, session: sync }); const b = await createBaseUrl('queries/b', { ...config, session: promise }); const c = await createBaseUrl('queries/c', { ...config, session: lazy }); const d = await createBaseUrl('queries/d', { ...config, session: lazyAsync }); t.assert.strictEqual(a.search, '?market=&customerKey=a_c&sessionKey=a_s'); t.assert.strictEqual(b.search, '?market=&customerKey=b_c&sessionKey=b_s'); t.assert.strictEqual(c.search, '?market=&customerKey=c_c&sessionKey=c_s'); t.assert.strictEqual(d.search, '?market=&customerKey=d_c&sessionKey=d_s'); }); it('should pass `userContext` through as query param', async (t: TestContext) => { const market = undefined as unknown as string; const sessionKey = undefined as unknown as string; const customerKey = undefined as unknown as string; const clusterUrl = 'https://example.com'; const a = await createBaseUrl('queries/', { clusterUrl, market, session: { userContext: 'PERSONALIZED', sessionKey, customerKey } }); const b = await createBaseUrl('queries/', { clusterUrl, market, session: delay(50, { userContext: 'UNPERSONALIZED', sessionKey }) }); const c = await createBaseUrl('queries/', { clusterUrl, market, session: () => ({ userContext: 'SYNTHETIC' }) }); const d = await createBaseUrl('queries/', { clusterUrl, market, session: () => delay(50, { userContext: 'ANONYMOUS'}) }); t.assert.strictEqual(a.search, '?userContext=PERSONALIZED'); t.assert.strictEqual(b.search, '?userContext=UNPERSONALIZED'); t.assert.strictEqual(c.search, '?userContext=SYNTHETIC'); t.assert.strictEqual(d.search, '?userContext=ANONYMOUS'); }); it('should handle "keyless" context and create `userContext` query', async (t: TestContext) => { const market = undefined as unknown as string; const clusterUrl = 'https://example.com'; const a = await createBaseUrl('queries/', { market, clusterUrl, session: () => delay(50, 'SYNTHETIC') }); const b = await createBaseUrl('queries/', { market, clusterUrl, session: 'ANONYMOUS' }); t.assert.strictEqual(a.search, '?userContext=SYNTHETIC'); t.assert.strictEqual(b.search, '?userContext=ANONYMOUS'); }); it('should produce correct full URL href', async (t: TestContext) => { const { href } = await createBaseUrl('queries/page', { market: 'se', clusterUrl: 'https://w01234567.elevate-api.cloud', session: { customerKey: '9', sessionKey: 'z', userContext: 'PERSONALIZED' } }); t.assert.strictEqual( href, 'https://w01234567.elevate-api.cloud/api/storefront/v3/queries/page?market=se&customerKey=9&sessionKey=z&userContext=PERSONALIZED' ); }); }); describe('addUrlParams()', () => { let url: URL; it.beforeEach(() => { url = new URL('https://apptus.com/'); }); it('should remove null/undefined values', (t: TestContext) => { addUrlParams(url, { sure: '1', nope: undefined, niet: null, yes: '2' }); t.assert.strictEqual(url.href, 'https://apptus.com/?sure=1&yes=2'); }); it('should convert primitives to strings', (t: TestContext) => { addUrlParams(url, { name: 'John', age: 42, isWick: true }); t.assert.strictEqual(url.href, 'https://apptus.com/?name=John&age=42&isWick=true'); }); it('should join array values with "|"', (t: TestContext) => { addUrlParams(url, { cart: ['pk1', 'pk2', 'pk3'], bools: [false, false, true], nums: [1, 2, 3, 9001] }); const p = encodeURIComponent('|'); t.assert.strictEqual(url.href, `https://apptus.com/?cart=pk1${p}pk2${p}pk3&bools=false${p}false${p}true&nums=1${p}2${p}3${p}9001`); }); }); describe('facetsToParams()', () => { it('should prefix all facets with `f.` in their parameter names', (t: TestContext) => { const params = facetsToParams({ foo: ['a'], bar: ['b'] }); t.assert.deepStrictEqual(Object.keys(params), ['f.foo', 'f.bar']); }); it('should pass regular param values unchanged', (t: TestContext) => { const items = ['one', 'two', 'three']; const params = facetsToParams({ items }); t.assert.strictEqual(params['f.items'], items); }); it('should convert min & max range values into multiple parameters', (t: TestContext) => { t.assert.deepStrictEqual(facetsToParams({ price: { min: 15, max: 32 } }), { 'f.price.min': 15, 'f.price.max': 32 }); }); it('should convert min or max range values into a single parameter', (t: TestContext) => { t.assert.deepStrictEqual(facetsToParams({ price: { max: 99 } }), { 'f.price.max': 99 }); t.assert.deepStrictEqual(facetsToParams({ coolness: { min: 9001 } }), { 'f.coolness.min': 9001 }); }); it('should give the correct result for kitchen sink example', (t: TestContext) => { const params = facetsToParams({ brand: ['nike', 'adidas'], price: { max: 500 } }); t.assert.deepStrictEqual(params, { 'f.brand': ['nike', 'adidas'], 'f.price.max': 500 }); }); });