|
| 1 | +// Description: Offline tests for the Game Pass property formatting helpers (output shaping, extractors, store-page slug) |
| 2 | + |
| 3 | +import { describe, it, after } from 'node:test'; |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | +import fs from 'node:fs'; |
| 6 | +import os from 'node:os'; |
| 7 | +import path from 'node:path'; |
| 8 | + |
| 9 | +import { formatData, getStorePageUrl, getCategories } from '../js/gamePass.js'; |
| 10 | +import { initConfig } from '../js/utils.js'; |
| 11 | + |
| 12 | +// A shared temp output directory so initConfig's setupOutput never writes into the repo |
| 13 | +const outputDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'gpa-format-')); |
| 14 | +after(() => fs.rmSync(outputDirectory, { recursive: true, force: true })); |
| 15 | + |
| 16 | +// Point CONFIG at the temp directory while setting the fields a given test needs |
| 17 | +function useConfig(overrides = {}) { |
| 18 | + initConfig({ language: 'en-us', treatEmptyStringsAsNull: true, outputDirectory, ...overrides }); |
| 19 | +} |
| 20 | + |
| 21 | +// A minimal display-catalog product with the fields the extractors read |
| 22 | +function product(overrides = {}) { |
| 23 | + return { |
| 24 | + ProductId: 'ABC123', |
| 25 | + LocalizedProperties: [{ ProductTitle: 'Halo Infinite', DeveloperName: '343 Industries', PublisherName: 'Xbox Game Studios' }], |
| 26 | + Properties: { Category: 'Shooter', Categories: ['Action', 'Shooter'] }, |
| 27 | + ...overrides |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +describe('formatData output shaping', () => { |
| 32 | + it('produces an array keyed by insertion order', () => { |
| 33 | + useConfig({ outputFormat: 'array', includedProperties: { productTitle: true } }); |
| 34 | + const out = formatData({ Products: [product(), product({ ProductId: 'D2', LocalizedProperties: [{ ProductTitle: 'Forza' }] })] }, 'console'); |
| 35 | + assert.ok(Array.isArray(out)); |
| 36 | + assert.deepEqual(out, [{ productTitle: 'Halo Infinite' }, { productTitle: 'Forza' }]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('keys a dictionary by productId', () => { |
| 40 | + useConfig({ outputFormat: 'productId', includedProperties: { productTitle: true } }); |
| 41 | + const out = formatData({ Products: [product()] }, 'console'); |
| 42 | + assert.deepEqual(out, { ABC123: { productTitle: 'Halo Infinite' } }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('keys a dictionary by productTitle and disambiguates duplicate titles', () => { |
| 46 | + useConfig({ outputFormat: 'productTitle', includedProperties: { productId: true } }); |
| 47 | + const out = formatData({ Products: [product(), product({ ProductId: 'DEF456' })] }, 'console'); |
| 48 | + assert.deepEqual(Object.keys(out), ['Halo Infinite', 'Halo Infinite (DEF456)']); |
| 49 | + assert.equal(out['Halo Infinite'].productId, 'ABC123'); |
| 50 | + assert.equal(out['Halo Infinite (DEF456)'].productId, 'DEF456'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('keys a dictionary by rolling integer for 0-indexed', () => { |
| 54 | + useConfig({ outputFormat: '0-indexed', includedProperties: { productTitle: true } }); |
| 55 | + const out = formatData({ Products: [product(), product({ ProductId: 'D2' })] }, 'console'); |
| 56 | + assert.deepEqual(Object.keys(out), ['0', '1']); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('formatData property extraction', () => { |
| 61 | + it('includes only the requested properties', () => { |
| 62 | + useConfig({ outputFormat: 'array', includedProperties: { productTitle: true, productId: true, developerName: true, publisherName: true } }); |
| 63 | + const [entry] = formatData({ Products: [product()] }, 'console'); |
| 64 | + assert.deepEqual(entry, { |
| 65 | + productTitle: 'Halo Infinite', |
| 66 | + productId: 'ABC123', |
| 67 | + developerName: '343 Industries', |
| 68 | + publisherName: 'Xbox Game Studios' |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('uses null for an empty value when treatEmptyStringsAsNull is true', () => { |
| 73 | + useConfig({ outputFormat: 'array', treatEmptyStringsAsNull: true, includedProperties: { developerName: true } }); |
| 74 | + const [entry] = formatData({ Products: [product({ LocalizedProperties: [{ ProductTitle: 'X', DeveloperName: '' }] })] }, 'console'); |
| 75 | + assert.equal(entry.developerName, null); |
| 76 | + }); |
| 77 | + |
| 78 | + it('uses an empty string for an empty value when treatEmptyStringsAsNull is false', () => { |
| 79 | + useConfig({ outputFormat: 'array', treatEmptyStringsAsNull: false, includedProperties: { developerName: true } }); |
| 80 | + const [entry] = formatData({ Products: [product({ LocalizedProperties: [{ ProductTitle: 'X', DeveloperName: '' }] })] }, 'console'); |
| 81 | + assert.equal(entry.developerName, ''); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('getStorePageUrl', () => { |
| 86 | + it('builds a slugged Xbox store URL from the title and product ID', () => { |
| 87 | + useConfig({ language: 'en-us' }); |
| 88 | + assert.equal(getStorePageUrl(product(), true), 'https://www.xbox.com/en-us/games/store/halo-infinite/ABC123'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('collapses punctuation and repeated separators into single dashes', () => { |
| 92 | + useConfig({ language: 'de-de' }); |
| 93 | + const url = getStorePageUrl(product({ LocalizedProperties: [{ ProductTitle: "Marvel's Guardians: The Game!!" }] }), true); |
| 94 | + assert.equal(url, 'https://www.xbox.com/de-de/games/store/marvel-s-guardians-the-game/ABC123'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns the empty-value placeholder when the title or ID is missing', () => { |
| 98 | + useConfig({ treatEmptyStringsAsNull: true }); |
| 99 | + assert.equal(getStorePageUrl(product({ ProductId: '' }), true), null); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('getCategories', () => { |
| 104 | + it('merges the main Category into the Categories list without duplicating it', () => { |
| 105 | + useConfig(); |
| 106 | + assert.deepEqual(getCategories(product(), true), ['Action', 'Shooter']); |
| 107 | + }); |
| 108 | + |
| 109 | + it('appends the main Category when it is not already listed', () => { |
| 110 | + useConfig(); |
| 111 | + assert.deepEqual(getCategories(product({ Properties: { Category: 'RPG', Categories: ['Action'] } }), true), ['Action', 'RPG']); |
| 112 | + }); |
| 113 | + |
| 114 | + it('returns undefined when the property is disabled', () => { |
| 115 | + useConfig(); |
| 116 | + assert.equal(getCategories(product(), false), undefined); |
| 117 | + }); |
| 118 | +}); |
0 commit comments