feat: add public listing catalog boundary
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createPublicListingService } from '../../src/modules/listings/services/public-listing-service.js';
|
||||
|
||||
test('lists visible public listings through the shared repository search boundary', async () => {
|
||||
const service = createPublicListingService({
|
||||
listingRepository: {
|
||||
async searchPublic(filters) {
|
||||
assert.deepEqual(filters, { query: '', page: 2, limit: 5 });
|
||||
return { items: [{ id: 'listing-1' }], total: 1 };
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(await service.list({ page: 2, limit: 5 }), {
|
||||
items: [{ id: 'listing-1' }],
|
||||
total: 1,
|
||||
});
|
||||
});
|
||||
|
||||
test('returns a not-found application error for a hidden public listing', async () => {
|
||||
const service = createPublicListingService({
|
||||
listingRepository: { async findPublicById() { return null; } },
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => service.getById('listing-1'),
|
||||
(error) => error.code === 'LISTING_NOT_FOUND' && error.statusCode === 404,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { buildApp } from '../../src/app/build-app.js';
|
||||
|
||||
function createTestApp() {
|
||||
return buildApp({
|
||||
publicListingService: {
|
||||
async list() {
|
||||
return {
|
||||
items: [{
|
||||
id: 'listing-1',
|
||||
inventoryItemId: 'private-inventory-id',
|
||||
productName: 'Pikachu Plush',
|
||||
categoryId: 'category-1',
|
||||
conditionId: 'condition-1',
|
||||
askingPriceMinor: 2499,
|
||||
status: 'PENDING',
|
||||
}],
|
||||
pagination: { page: 1, limit: 20, total: 1, totalPages: 1 },
|
||||
};
|
||||
},
|
||||
async getById(id) {
|
||||
if (id !== 'listing-1') {
|
||||
const error = new Error('Listing was not found.');
|
||||
error.code = 'LISTING_NOT_FOUND';
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
inventoryItemId: 'private-inventory-id',
|
||||
productName: 'Pikachu Plush',
|
||||
categoryId: 'category-1',
|
||||
conditionId: 'condition-1',
|
||||
askingPriceMinor: 2499,
|
||||
status: 'PENDING',
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
test('returns public listing collections and omits inventory identity', async (testContext) => {
|
||||
const app = createTestApp();
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/listings?page=1&limit=20',
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.equal(response.json().data.items[0].id, 'listing-1');
|
||||
assert.equal('inventoryItemId' in response.json().data.items[0], false);
|
||||
assert.equal(response.json().data.pagination.total, 1);
|
||||
});
|
||||
|
||||
test('returns a public listing detail or a not-found response', async (testContext) => {
|
||||
const app = createTestApp();
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const detailResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/listings/listing-1',
|
||||
});
|
||||
const missingResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/public/listings/hidden-listing',
|
||||
});
|
||||
|
||||
assert.equal(detailResponse.statusCode, 200);
|
||||
assert.equal('inventoryItemId' in detailResponse.json().data, false);
|
||||
assert.equal(missingResponse.statusCode, 404);
|
||||
assert.equal(missingResponse.json().error.code, 'LISTING_NOT_FOUND');
|
||||
});
|
||||
@@ -141,6 +141,10 @@ test('searches public listings with pagination and hides sold items by default',
|
||||
assert.equal(visible.total, 1);
|
||||
assert.equal(visible.items.length, 1);
|
||||
assert.equal(visible.items[0].status, 'PENDING');
|
||||
assert.equal(
|
||||
(await listingRepository.findPublicById(visible.items[0].id))?.id,
|
||||
visible.items[0].id,
|
||||
);
|
||||
|
||||
await database.db
|
||||
.update(listings)
|
||||
@@ -153,6 +157,7 @@ test('searches public listings with pagination and hides sold items by default',
|
||||
|
||||
const hidden = await listingRepository.searchPublic({ page: 1, limit: 10 });
|
||||
assert.equal(hidden.total, 0);
|
||||
assert.equal(await listingRepository.findPublicById(visible.items[0].id), null);
|
||||
|
||||
const included = await listingRepository.searchPublic({
|
||||
page: 1,
|
||||
|
||||
Reference in New Issue
Block a user