// deno-lint-ignore-file import { describe, it, type TestContext } from 'node:test'; import { withFetchMock } from '#/test'; import { Query } from './query.ts'; import { ResponseError } from './util/mod.ts'; import type * as m from './models/mod.ts'; import type { AddToCartPopupParams, AddToCartPopupBody } from './models/add-to-cart-popup.ts'; import type { AutocompleteParams, AutocompleteBody } from './models/autocomplete.ts'; import type { NavigationTreeParams } from './models/navigation-tree.ts'; import type { SearchPageParams, SearchPageBody } from './models/search-page.ts'; describe('query', () => { // TODO(@csvn): Use a single method for checking `ProductFilter` type and values? it.todo('should allow all valid productFilter values for page bodies'); describe('general', () => { // Using `text/plain` helps avoid CORS preflight requests for POST in browsers. // All POST endpoints funnel through the same request helper, so asserting a // couple of representative paths (non-empty and empty body) is enough. it('should use text/plain header for POST requests', async (t: TestContext) => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'https://w00000000.elevate-api.cloud', market: 'g', locale: 'g', touchpoint: 'desktop', session: 'ANONYMOUS' }); await query.addToCartPopup({ variantKey: '' }, []); await query.searchPage({ q: '' }, {}); t.assert.strictEqual(f.requestForCall(0).headers.get('Content-Type'), 'text/plain'); t.assert.strictEqual(f.requestForCall(1).headers.get('Content-Type'), 'text/plain'); f.assertSnapshot(2); }); it('should throw `ResponseError` for non-2xx responses', async (t: TestContext) => { using f = withFetchMock(() => new Response('{ "message": "fail", "details": "incorrect" }', { status: 400 })); const query = new Query({ clusterUrl: 'https://w00000000.elevate-api.cloud', market: 'g', locale: 'g', touchpoint: 'desktop', session: 'SYNTHETIC' }); await t.assert.rejects(query.landingPage(), err => { t.assert.ok(err instanceof ResponseError); // TODO(@csvn): Should use `ResponseError` to match instance type // See: https://github.com/voyado/esales-api/issues/53 t.assert.strictEqual(err.name, 'Error'); t.assert.strictEqual(err.message, 'fail'); t.assert.deepStrictEqual(err.details, { message: 'fail', details: 'incorrect' }); return true; }); f.assertSnapshot(1); }); }); describe('addToCartPopup()', () => { it('should pass on correct endpoint, params and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:13', market: 'fr', locale: 'fr-FR', touchpoint: 'mobile', session: () => ({ customerKey: 'c13', sessionKey: 's13' }) }); const params = Object.freeze({ variantKey: 'some_special_key' }); const body = Object.freeze([{ id: '1313', algorithm: 'ADD_TO_CART_RECS' }]); await query.addToCartPopup(params, body as AddToCartPopupBody); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:13', market: 'fr', locale: 'fr-FR', touchpoint: 'mobile', session: () => ({ customerKey: 'c13', sessionKey: 's13' }) }); const result = await query.addToCartPopup({ variantKey: 'very_special_key', cart: ['cart-one'], channels: 'ONLINE|STORE', notify: true, presentCustom: ['something', 'anything'], presentPrices: ['low', 'high'], priceId: 'highPrice', stores: 'store-one|store-two', templateId: 'tpl-2', viewId: 'preview', }, [{ id: '1313', algorithm: 'ADD_TO_CART_RECS', label: 'Do you have everything you need?', limit: 9, params: { cart: ['cart-one'], productKey: 'pk1234' }, productRules: 'rule incl newness 10d ', showMoreLink: '/add-to-cart-popup', visualization: 'GRID' }] ); result satisfies m.RecommendationListPage; result.recommendationLists satisfies m.RecommendationList[]; f.assertSnapshot(1); }); }); describe('autocomplete()', () => { it('should use GET for requests without body', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:19', market: 'se9', locale: 'sv-SE', touchpoint: 'desktop', session: () => ({ customerKey: 'c19', sessionKey: 's19' }) }); const params = Object.freeze({ q: 'shipping❓' }); await query.autocomplete(params); f.assertSnapshot(1); }); it('should pass on correct endpoint, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:10', market: 'se', locale: 'sv-SE', touchpoint: 'desktop', session: () => ({ customerKey: 'c10', sessionKey: 's10' }) }); const params = Object.freeze({ q: 'foo', notify: false, viewId: 'production', priceId: 'default', channels: 'ONLINE', stores: 'lund', presentCustom: ['material'] }); const body = Object.freeze({ contentLists: [{ id: '1', }], productFilter: { size: ['XL'] } }); await query.autocomplete(params, body); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:11', market: 'se2', locale: 'sv-SE2', touchpoint: 'desktop', session: () => ({ customerKey: 'c11', sessionKey: 's11' }) }); const result = await query.autocomplete({ q: 'robe', notify: false, viewId: 'production', priceId: 'default', channels: 'ONLINE', stores: 'storeA|storeB', templateId: 'my-data-template', presentCustom: ['foo', 'bar'], presentPrices: ['discount'] }, { contentLists: [{ id: 'asd', algorithm: 'NEWEST_CONTENT', limit: 2, contentFilter: { type: 'article' } }], productFilter: { brand: 'Nike', color: ['RED', 'GREEN'], price: { min: 10, max: 100 } }, productRules: 'rule incl newness 10d ' }); result satisfies m.Autocomplete; result.contentSuggestions satisfies m.ContentSuggestion[]; result.phraseSuggestions satisfies m.PhraseSuggestion[]; result.productSuggestions satisfies m.ProductGroup[]; result.contentLists satisfies m.ContentList[]; result.totalHits satisfies number; result.redirectLink satisfies string | undefined; f.assertSnapshot(1); }); }); describe('navigationTree()', () => { it('should send navigation tree request without parameters', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:29', market: 'en9', locale: 'en-US', touchpoint: 'desktop', session: () => ({ customerKey: 'c29', sessionKey: 's29' }) }); await query.navigationTree(); f.assertSnapshot(1); }); it('should pass on correct endpoint, and params, parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:20', market: 'en', locale: 'en-US', touchpoint: 'desktop', session: () => ({ customerKey: 'c20', sessionKey: 's20' }) }); const params = Object.freeze({ priceId: 'SEK', notify: true }); await query.navigationTree(params); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:21', market: 'en2', locale: 'en-US2', touchpoint: 'desktop', session: () => ({ customerKey: 'c21', sessionKey: 's21' }) }); const result = await query.navigationTree({ notify: false, viewId: 'production', priceId: 'default', channels: 'STORE', stores: 'storeZ|storeD' }); result satisfies m.NavigationTree; result.tree satisfies m.NavigationNode; f.assertSnapshot(1); }); }); describe('searchPage()', () => { it('should use GET for requests without body', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:39', market: 'dk9', locale: 'da-DK', touchpoint: 'mobile', session: () => ({ customerKey: 'c39', sessionKey: 's39' }) }); const params = Object.freeze({ q: 'size guide 👘' }); await query.searchPage(params); f.assertSnapshot(1); }); it('should pass on correct endpoint, facets, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:30', market: 'dk', locale: 'da-DK', touchpoint: 'mobile', session: () => ({ customerKey: 'c30', sessionKey: 's30' }) }); const params = Object.freeze({ q: 'shirt', limit: 11, facets: { color: ['steelblue'] } }); const body = Object.freeze({ primaryList: { include: true } }); await query.searchPage(params, body); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const sort: 'RELEVANCE' | 'NEWEST_FIRST' | 'PRICE_ASCENDING' | 'PRICE_DESCENDING' | 'DISCOUNT' | 'RATING' = 'RELEVANCE'; const origin: 'ORGANIC' | 'PHRASE_SUGGEST' | 'DID_YOU_MEAN' | 'UNDO_AUTO_CORRECT' | 'RELATED_SEARCHES' = 'ORGANIC'; const query = new Query({ clusterUrl: 'http://localhost:31', market: 'dk2', locale: 'da-DK2', touchpoint: 'mobile', session: () => ({ customerKey: 'c31', sessionKey: 's31' }) }); const result = await query.searchPage({ q: 'robe', sort, origin, limit: 20, skip: 10, notify: false, viewId: 'production', priceId: 'default', channels: 'ONLINE|STORE', stores: 'storeF', selectedCategory: 'women', templateId: 'list-template', viewAllSecondary: true, presentCustom: ['lorem', 'ipsum'], presentPrices: ['VIP'], facets: { color: ['firebrick'], brand: ['nike', 'adidas'], price: { min: 10, max: 500 }, coolFactor: { min: 9001 }, length: { max: 180 } } }, { navigation: { include: true }, primaryList: { include: true, productFilter: { brand: 'Nike', color: ['RED', 'GREEN'], price: { min: 10, max: 100 } }, productRules: 'rule incl newness 10d ' }, contentLists: [ { id: 'jgf', limit: 11, algorithm: 'TOP_CONTENT', contentFilter: { content_key: 'ck12345' } } ] }); result satisfies m.SearchPage; result.q satisfies string; result.primaryList satisfies m.PrimaryList; result.didYouMean satisfies m.DidYouMean[]; result.autoCorrect satisfies m.AutoCorrect | undefined; result.navigation satisfies m.Navigation | undefined; result.contentLists satisfies m.ContentList[]; result.secondaryList satisfies m.SecondaryList | undefined; result.relatedSearches satisfies m.RelatedSearch[] | undefined; f.assertSnapshot(1); }); }); describe('productPage()', () => { it('should use GET for requests without body', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:49', market: 'no9', locale: 'no-NO', touchpoint: 'desktop', session: () => ({ customerKey: 'c49', sessionKey: 's49' }) }); const params = Object.freeze({ productKey: 'p6574839' }); await query.productPage(params); f.assertSnapshot(1); }); it('should pass on correct endpoint, facets, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:40', market: 'no', locale: 'no-NO', touchpoint: 'desktop', session: () => ({ customerKey: 'c40', sessionKey: 's40' }) }); const params = Object.freeze({ productKey: 'p123456', viewId: 'production' as const }); const body = Object.freeze({ productGroup: { include: true } }); await query.productPage(params, body); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:41', market: 'no2', locale: 'no-NO2', touchpoint: 'desktop', session: () => ({ customerKey: 'c41', sessionKey: 's41' }) }); const result = await query.productPage({ productKey: 'pk1234567', notify: false, viewId: 'production', priceId: 'default', channels: 'STORE|ONLINE', stores: '123|abc|xyz', templateId: 'rec-list-template', presentCustom: ['season', 'age'], presentPrices: ['member'] }, { productGroup: { include: true }, recommendationLists: [ { id: 'top', label: 'Top sellers', limit: 8, algorithm: 'TOP_PRODUCTS', visualization: 'GRID', showMoreLink: '/top', params: { productKey: 'pk1234' } } ] }); result satisfies m.ProductPage; result.productGroup satisfies m.ProductGroup | undefined; result.recommendationLists satisfies m.RecommendationList[]; f.assertSnapshot(1); }); }); describe('cartPage()', () => { it('should use GET for requests without body', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:59', market: 'fi9', locale: 'fi-FI', touchpoint: 'desktop', session: () => ({ customerKey: 'c59', sessionKey: 's59' }) }); const params = Object.freeze({ cart: ['p987123'] }); await query.cartPage(params); f.assertSnapshot(1); }); it('should pass on correct endpoint, facets, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:50', market: 'fi', locale: 'fi-FI', touchpoint: 'desktop', session: () => ({ customerKey: 'c50', sessionKey: 's50' }) }); const params = Object.freeze({ cart: ['p123456', 'p234567'], viewId: 'production' as const }); const body = Object.freeze({ cart: { include: true } }); await query.cartPage(params, body); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:51', market: 'fi2', locale: 'fi-FI2', touchpoint: 'desktop', session: () => ({ customerKey: 'c51', sessionKey: 's51' }) }); const result = await query.cartPage({ cart: ['pk1234567', 'pk2345678'], notify: true, viewId: 'preview', priceId: 'default', channels: 'ONLINE', stores: 'storeA|storeB', templateId: 'rec-list-template', presentCustom: ['red', 'blue'], presentPrices: ['SEK', 'EURO'] }, { cart: { include: true }, recommendationLists: [ { id: 'cart', label: 'Cart recs', limit: 8, algorithm: 'CART', visualization: 'GRID', showMoreLink: '/top', params: { cart: ['pk1234'] } } ] }); result satisfies m.CartPage; result.cart satisfies m.ProductGroup[]; result.recommendationLists satisfies m.RecommendationList[]; f.assertSnapshot(1); }); }); describe('landingPage()', () => { it('should allow being called without parameters', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:60', market: 'de', locale: 'de-DE', touchpoint: 'desktop', session: () => ({ customerKey: 'c60', sessionKey: 's60' }) }); await query.landingPage(); f.assertSnapshot(1); }); it('should pass on correct endpoint, facets, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:61', market: 'de2', locale: 'de-DE2', touchpoint: 'desktop', session: () => ({ customerKey: 'c61', sessionKey: 's61' }) }); const params = Object.freeze({ sort: 'RELEVANCE' as const, limit: 11, facets: { color: ['steelblue'], size: ['XL'] } }); const body = Object.freeze({ primaryList: { include: true } }); await query.landingPage(params, body); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const sort: 'RELEVANCE' | 'NEWEST_FIRST' | 'PRICE_ASCENDING' | 'PRICE_DESCENDING' | 'DISCOUNT' | 'RATING' = 'RELEVANCE'; const query = new Query({ clusterUrl: 'http://localhost:62', market: 'de3', locale: 'de-DE3', touchpoint: 'desktop', session: () => ({ customerKey: 'c62', sessionKey: 's62' }) }); const result = await query.landingPage({ sort, limit: 20, skip: 10, q: 'red dress', notify: false, pageReference: '/women/jackets', includeNavigation: true, viewId: 'production', priceId: 'default', channels: 'ONLINE', stores: 'se|de|no', templateId: 'list-template', presentCustom: ['cut'], presentPrices: ['foo', 'bar'], facets: { color: ['firebrick'], brand: ['nike', 'adidas'], price: { min: 10, max: 500 }, coolFactor: { min: 9001 }, length: { max: 180 } } }, { navigation: { include: true }, primaryList: { include: true, productFilter: { brand: 'Nike', color: ['RED', 'GREEN'], price: { min: 10, max: 100 } }, productRules: 'rule incl newness 10d ' }, recommendationLists: [ { id: 'peronsal', label: 'For you', algorithm: 'PERSONAL', limit: 8, visualization: 'CAROUSEL' } ], contentLists: [ { id: 'blogs', limit: 3, algorithm: 'NEWEST_CONTENT', contentFilter: { type: 'blog' } } ] }); result satisfies m.LandingPage; result.primaryList satisfies m.PrimaryList | undefined; result.recommendationLists satisfies m.RecommendationList[]; result.contentLists satisfies m.ContentList[]; result.navigation satisfies m.Navigation | undefined; result.seo satisfies m.SearchEngineOptimization; f.assertSnapshot(1); }); }); describe('sponsoredPage()', () => { it('should allow being called without parameters', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:82', market: 'fr', locale: 'fr-EN', touchpoint: 'desktop', session: () => ({ customerKey: 'c82', sessionKey: 's82' }) }); await query.sponsoredPage({ pageReference: '/' }); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:84', market: 'fr3', locale: 'fr-EN3', touchpoint: 'desktop', session: () => ({ customerKey: 'c84', sessionKey: 's84' }) }); const result = await query.sponsoredPage({ notify: false, pageReference: '/women/jackets/sponsored', viewId: 'production', templateId: 'sponsored-template' }); result satisfies m.SponsoredPage; result.sponsoredLists satisfies m.SponsoredList[]; f.assertSnapshot(1); }); }); describe('contentInformation()', () => { it('should pass on correct endpoint, and params, parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:70', market: 'fr', locale: 'fr-FR', touchpoint: 'mobile', session: () => ({ customerKey: 'c70', sessionKey: 's70' }) }); const params = Object.freeze({ contentKeys: ['ck1234', 'ck9876'] }); await query.contentInformation(params); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:71', market: 'fr2', locale: 'fr-FR2', touchpoint: 'mobile', session: () => ({ customerKey: 'c71', sessionKey: 's71' }) }); const result = await query.contentInformation({ contentKeys: ['ck123456', 'ck234567'], notify: false, viewId: 'production' }); result satisfies m.ContentInformation; result.items satisfies m.ContentItem[]; f.assertSnapshot(1); }); }); describe('contentSearchPage()', () => { it('should use GET for requests without body', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:80', market: 'es', locale: 'es-ES', touchpoint: 'desktop', session: () => ({ customerKey: 'c80', sessionKey: 's80' }) }); const params = Object.freeze({ q: 'return policy 😬' }); await query.contentSearchPage(params); f.assertSnapshot(1); }); it('should pass on correct endpoint, facets, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:80', market: 'es', locale: 'es-ES', touchpoint: 'desktop', session: () => ({ customerKey: 'c80', sessionKey: 's80' }) }); const params = Object.freeze({ q: 'sizeguide', limit: 11 }); const body = Object.freeze({ primaryList: { contentFilter: { type: 'article' } } }); await query.contentSearchPage(params, body); f.assertSnapshot(1); }); it('should allow all API parameters and body options (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:81', market: 'es2', locale: 'es-ES2', touchpoint: 'mobile', session: () => ({ customerKey: 'c81', sessionKey: 's81' }) }); const result = await query.contentSearchPage({ q: 'faq', sort: 'RELEVANCE', origin: 'UNDO_AUTO_CORRECT', skip: 10, limit: 30, notify: false, viewId: 'preview' }, { primaryList: { contentFilter: { type: 'article', content_key: ['ck123', 'ck234', 'ck345'], 'custom.foo': 'bar' } } }); result satisfies m.ContentSearchPage; result.q satisfies string; result.primaryList satisfies m.PrimaryContentList; result.didYouMean satisfies m.DidYouMean[]; result.autoCorrect satisfies m.AutoCorrect | undefined; f.assertSnapshot(1); }); }); describe('discoveryAgentTopic()', () => { it('should pass on correct endpoint, params, and body parameters to Request', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:91', market: 'se', locale: 'sv-SE', touchpoint: 'mobile', session: () => ({ customerKey: 'c91', sessionKey: 's91' }) }); const params = Object.freeze({ limit: 20 }); await query.discoveryAgentTopic('some_result_ticket', params); f.assertSnapshot(1); }); it('should allow all API parameters (kitchen sink)', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:92', market: 'se2', locale: 'sv-SE2', touchpoint: 'desktop', session: () => ({ customerKey: 'c92', sessionKey: 's92' }) }); const result = await query.discoveryAgentTopic('ticket_value', { limit: 20, skip: 10, notify: false, presentCustom: ['lorem', 'ipsum'], presentPrices: ['VIP'], templateId: 'list-template' }); result satisfies m.DiscoveryAgentTopic; result.primaryList satisfies m.ProductListSummary; f.assertSnapshot(1); }); }); describe('banners()', () => { it('should create GET request with correct query parameters and URL', async () => { using f = withFetchMock(); const query = new Query({ clusterUrl: 'http://localhost:90', market: 'mx', locale: 'es-MX', touchpoint: 'desktop', session: () => ({ customerKey: 'c90', sessionKey: 's90' }) }); const result = await query.banners(Object.freeze({ pageReference: '/women/dresses', viewId: 'preview', templateId: 'default' })); result satisfies m.Banners; result.banners satisfies m.Banner[]; f.assertSnapshot(1); }); }); });