Files
pokemon-inventory/apps/web/tests/catalog-service.test.js
2026-09-10 06:46:11 -04:00

189 lines
7.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { getPublicListing } from '../src/services/catalog-service.js';
test('fetches a public listing detail through the API service', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
assert.equal(url, '/api/v1/public/listings/listing-1');
return { ok: true, async json() { return { data: { id: 'listing-1' } }; } };
};
try {
assert.deepEqual(await getPublicListing('listing-1'), { id: 'listing-1' });
} finally {
globalThis.fetch = originalFetch;
}
});
test('encodes catalog search pagination in the API request', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
assert.equal(url, '/api/v1/public/search?query=card+set&page=3&limit=20');
return { ok: true, async json() { return { data: { items: [], pagination: {} } }; } };
};
try {
const { searchCatalog } = await import('../src/services/catalog-service.js');
await searchCatalog({ query: 'card set', page: 3 });
} finally {
globalThis.fetch = originalFetch;
}
});
test('fetches a public product using its stable slug URL', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
assert.equal(url, '/api/v1/public/products/pikachu-plush');
return { ok: true, async json() { return { data: { slug: 'pikachu-plush' } }; } };
};
try {
const { getPublicProduct } = await import('../src/services/catalog-service.js');
assert.deepEqual(await getPublicProduct('pikachu-plush'), { slug: 'pikachu-plush' });
} finally {
globalThis.fetch = originalFetch;
}
});
test('archives selected admin inventory items through the bulk API', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(url, '/api/v1/admin/bulk');
assert.equal(options.method, 'POST');
assert.equal(options.headers['x-csrf-token'], 'csrf-token');
assert.deepEqual(JSON.parse(options.body), {
action: 'ARCHIVE',
inventoryItemIds: ['item-1'],
});
return { ok: true, async json() { return { data: [{ id: 'item-1', status: 'ARCHIVED' }] }; } };
};
try {
const { archiveAdminInventory } = await import('../src/services/admin-catalog-service.js');
assert.deepEqual(await archiveAdminInventory(['item-1'], 'csrf-token'), [
{ id: 'item-1', status: 'ARCHIVED' },
]);
} finally {
globalThis.fetch = originalFetch;
}
});
test('fetches an admin inventory export job through the bulk job API', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(url, '/api/v1/admin/exports/jobs/job-1');
return { ok: true, async json() { return { data: { id: 'job-1', status: 'COMPLETED' } }; } };
};
try {
const { getAdminInventoryExportJob } = await import('../src/services/admin-catalog-service.js');
assert.deepEqual(await getAdminInventoryExportJob('job-1'), { id: 'job-1', status: 'COMPLETED' });
} finally {
globalThis.fetch = originalFetch;
}
});
test('enqueues selected admin inventory pricing refreshes through the bulk API', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(url, '/api/v1/admin/bulk');
assert.equal(options.method, 'POST');
assert.equal(options.headers['x-csrf-token'], 'csrf-token');
assert.deepEqual(JSON.parse(options.body), {
action: 'PRICING_REFRESH',
inventoryItemIds: ['item-1'],
providerId: 'fixture-provider',
});
return { ok: true, async json() { return { data: [{ id: 'job-1', status: 'PENDING' }] }; } };
};
try {
const { refreshAdminInventoryPricing } = await import('../src/services/admin-catalog-service.js');
assert.deepEqual(await refreshAdminInventoryPricing(['item-1'], 'fixture-provider', 'csrf-token'), [
{ id: 'job-1', status: 'PENDING' },
]);
} finally {
globalThis.fetch = originalFetch;
}
});
test('updates selected admin inventory tags through the bulk API', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(url, '/api/v1/admin/bulk');
assert.equal(options.method, 'POST');
assert.deepEqual(JSON.parse(options.body), {
action: 'TAGS',
inventoryItemIds: ['item-1'],
add: ['binder'],
remove: [],
});
return { ok: true, async json() { return { data: [{ id: 'product-1', tags: ['binder'] }] }; } };
};
try {
const { updateAdminInventoryTags } = await import('../src/services/admin-catalog-service.js');
assert.deepEqual(await updateAdminInventoryTags(['item-1'], ['binder'], [], 'csrf-token'), [
{ id: 'product-1', tags: ['binder'] },
]);
} finally {
globalThis.fetch = originalFetch;
}
});
test('updates admin settings through the CSRF-protected API', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(url, '/api/v1/admin/settings');
assert.equal(options.method, 'PATCH');
assert.equal(options.headers['x-csrf-token'], 'csrf-token');
assert.deepEqual(JSON.parse(options.body), { pricing: { defaultStrategy: 'median' } });
return { ok: true, async json() { return { data: { pricing: { defaultStrategy: 'median' } } }; } };
};
try {
const { updateAdminSettings } = await import('../src/services/admin-settings-service.js');
assert.deepEqual(await updateAdminSettings({ pricing: { defaultStrategy: 'median' } }, 'csrf-token'), {
pricing: { defaultStrategy: 'median' },
});
} finally {
globalThis.fetch = originalFetch;
}
});
test('creates an admin saved view through the CSRF-protected API', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(url, '/api/v1/admin/saved-views');
assert.equal(options.method, 'POST');
assert.equal(options.headers['x-csrf-token'], 'csrf-token');
assert.deepEqual(JSON.parse(options.body), { name: 'Recent', filters: { sort: 'recent' } });
return { ok: true, async json() { return { data: { id: 'view-1', name: 'Recent', filters: { sort: 'recent' } } }; } };
};
try {
const { createAdminSavedView } = await import('../src/services/admin-saved-view-service.js');
assert.deepEqual(await createAdminSavedView({ name: 'Recent', filters: { sort: 'recent' } }, 'csrf-token'), {
id: 'view-1', name: 'Recent', filters: { sort: 'recent' },
});
} finally {
globalThis.fetch = originalFetch;
}
});
test('lists seller-scoped latest market valuations', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
assert.equal(url, '/api/v1/admin/valuations');
return { ok: true, async json() { return { data: [{ id: 'valuation-1', inventoryItemId: 'item-1', valueMinor: 2500 }] }; } };
};
try {
const { listAdminValuations } = await import('../src/services/admin-pricing-service.js');
assert.deepEqual(await listAdminValuations(), [{ id: 'valuation-1', inventoryItemId: 'item-1', valueMinor: 2500 }]);
} finally {
globalThis.fetch = originalFetch;
}
});