129 lines
4.5 KiB
JavaScript
129 lines
4.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import { buildApp } from '../../src/app/build-app.js';
|
|
import { createAdminExportService } from '../../src/modules/exports/services/admin-export-service.js';
|
|
|
|
test('downloads seller-scoped inventory JSON for an authenticated admin', async (testContext) => {
|
|
const app = buildApp({
|
|
authenticateAdmin: async () => ({ sellerId: 'seller-1' }),
|
|
adminExportService: createAdminExportService({
|
|
inventoryItemService: {
|
|
async listBySellerId(sellerId) {
|
|
assert.equal(sellerId, 'seller-1');
|
|
return [{
|
|
id: 'item-1',
|
|
productId: 'product-1',
|
|
conditionId: 'condition-1',
|
|
status: 'ACTIVE',
|
|
notes: 'Private note',
|
|
}];
|
|
},
|
|
},
|
|
}),
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/admin/exports/inventory.json',
|
|
});
|
|
|
|
assert.equal(response.statusCode, 200);
|
|
assert.equal(response.headers['content-type'], 'application/json; charset=utf-8');
|
|
assert.equal(response.headers['content-disposition'], 'attachment; filename="inventory.json"');
|
|
assert.deepEqual(JSON.parse(response.body), [{
|
|
id: 'item-1',
|
|
productId: 'product-1',
|
|
conditionId: 'condition-1',
|
|
status: 'ACTIVE',
|
|
notes: 'Private note',
|
|
}]);
|
|
});
|
|
|
|
test('requires authentication for inventory JSON exports', async (testContext) => {
|
|
const app = buildApp({
|
|
adminExportService: createAdminExportService({
|
|
inventoryItemService: { async listBySellerId() { return []; } },
|
|
}),
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/admin/exports/inventory.json',
|
|
});
|
|
|
|
assert.equal(response.statusCode, 401);
|
|
assert.equal(response.json().error.code, 'UNAUTHORIZED');
|
|
});
|
|
|
|
test('downloads seller-scoped inventory CSV for an authenticated admin', async (testContext) => {
|
|
const app = buildApp({
|
|
authenticateAdmin: async () => ({ sellerId: 'seller-1' }),
|
|
adminExportService: {
|
|
async inventoryCsv({ sellerId }) {
|
|
assert.equal(sellerId, 'seller-1');
|
|
return 'id,status\r\n"item-1","ACTIVE"';
|
|
},
|
|
},
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/admin/exports/inventory.csv',
|
|
});
|
|
|
|
assert.equal(response.statusCode, 200);
|
|
assert.equal(response.headers['content-type'], 'text/csv; charset=utf-8');
|
|
assert.equal(response.headers['content-disposition'], 'attachment; filename="inventory.csv"');
|
|
assert.equal(response.body, 'id,status\r\n"item-1","ACTIVE"');
|
|
});
|
|
|
|
test('downloads seller-scoped inventory Excel XML for an authenticated admin', async (testContext) => {
|
|
const app = buildApp({
|
|
authenticateAdmin: async () => ({ sellerId: 'seller-1' }),
|
|
adminExportService: {
|
|
async inventoryExcel({ sellerId }) {
|
|
assert.equal(sellerId, 'seller-1');
|
|
return '<?xml version="1.0"?><Workbook />';
|
|
},
|
|
},
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const response = await app.inject({ method: 'GET', url: '/api/v1/admin/exports/inventory.xls' });
|
|
|
|
assert.equal(response.statusCode, 200);
|
|
assert.equal(response.headers['content-type'], 'application/vnd.ms-excel; charset=utf-8');
|
|
assert.equal(response.headers['content-disposition'], 'attachment; filename="inventory.xls"');
|
|
assert.equal(response.body, '<?xml version="1.0"?><Workbook />');
|
|
});
|
|
|
|
test('enqueues and polls an asynchronous inventory export', async (testContext) => {
|
|
const app = buildApp({
|
|
authenticateAdmin: async () => ({ sellerId: 'seller-1' }),
|
|
adminExportService: {
|
|
async enqueueInventoryJson({ sellerId }) {
|
|
assert.equal(sellerId, 'seller-1');
|
|
return { id: 'job-1', status: 'PENDING', createdAt: '2026-09-10' };
|
|
},
|
|
async getExportJob({ sellerId, jobId }) {
|
|
assert.equal(sellerId, 'seller-1');
|
|
assert.equal(jobId, 'job-1');
|
|
return { id: jobId, format: 'JSON', status: 'COMPLETED', output: [{ id: 'item-1' }] };
|
|
},
|
|
},
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const enqueue = await app.inject({ method: 'POST', url: '/api/v1/admin/exports/inventory.json/jobs' });
|
|
const poll = await app.inject({ method: 'GET', url: '/api/v1/admin/exports/jobs/job-1' });
|
|
|
|
assert.equal(enqueue.statusCode, 202);
|
|
assert.equal(enqueue.json().data.id, 'job-1');
|
|
assert.equal(poll.statusCode, 200);
|
|
assert.deepEqual(poll.json().data.output, [{ id: 'item-1' }]);
|
|
});
|