This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createPricingProvider, createPricingProviderRegistry } from '../../src/modules/pricing/providers/pricing-provider.js';
|
||||
|
||||
test('registers pricing providers behind a validated adapter contract', async () => {
|
||||
const provider = createPricingProvider({
|
||||
id: 'example',
|
||||
async observe() { return [{ priceMinor: 2500, sourceIdentifier: 'example-1' }]; },
|
||||
});
|
||||
const registry = createPricingProviderRegistry([provider]);
|
||||
|
||||
assert.equal(registry.get('example'), provider);
|
||||
assert.deepEqual(await registry.get('example').observe({}), [{ priceMinor: 2500, sourceIdentifier: 'example-1' }]);
|
||||
assert.deepEqual(registry.list(), [provider]);
|
||||
});
|
||||
|
||||
test('rejects malformed provider adapters and responses', async () => {
|
||||
assert.throws(() => createPricingProvider({ id: 'broken' }), /observe function is required/);
|
||||
const provider = createPricingProvider({ id: 'broken', async observe() { return null; } });
|
||||
await assert.rejects(() => provider.observe({}), /must return an observation array/);
|
||||
});
|
||||
@@ -34,4 +34,16 @@ test('requires a reason for an audited market-value override', async () => {
|
||||
() => service.override({ sellerId: 'seller-1', actorUserId: 'user-1', inventoryItemId: 'inventory-1', valueMinor: 2000, reason: 'x' }),
|
||||
(error) => error.code === 'OVERRIDE_REASON_REQUIRED' && error.statusCode === 400,
|
||||
);
|
||||
});
|
||||
|
||||
test('fails clearly when a requested pricing provider is unavailable', async () => {
|
||||
const service = createPricingService({
|
||||
pricingRepository: {},
|
||||
pricingProviderRegistry: { get() { return null; } },
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => service.refresh({ sellerId: 'seller-1', inventoryItemId: 'inventory-1', providerId: 'missing' }),
|
||||
(error) => error.code === 'PRICING_PROVIDER_UNAVAILABLE' && error.statusCode === 503,
|
||||
);
|
||||
});
|
||||
@@ -44,4 +44,30 @@ test('accepts a CSRF-protected market-value override', async (testContext) => {
|
||||
assert.equal(response.statusCode, 201);
|
||||
assert.equal(received.actorUserId, 'user-1');
|
||||
assert.equal(response.json().data.valueMinor, 2600);
|
||||
});
|
||||
|
||||
test('returns a clear unavailable response when no provider is configured', async (testContext) => {
|
||||
const app = buildApp({
|
||||
authenticateAdmin: async () => ({ sellerId: 'seller-1' }),
|
||||
authorizeUnsafeRequest: async () => {},
|
||||
pricingService: {
|
||||
async refresh() {
|
||||
const error = new Error('Pricing provider is not configured.');
|
||||
error.code = 'PRICING_PROVIDER_UNAVAILABLE';
|
||||
error.statusCode = 503;
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
});
|
||||
testContext.after(() => app.close());
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/admin/inventory/inventory-1/pricing/refresh',
|
||||
headers: { 'x-csrf-token': 'csrf' },
|
||||
payload: { providerId: 'missing' },
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 503);
|
||||
assert.equal(response.json().error.code, 'PRICING_PROVIDER_UNAVAILABLE');
|
||||
});
|
||||
Reference in New Issue
Block a user