55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import { assertRouteIdMatchesBody } from '../src/helpers.ts';
|
|
import { normalizeLoggedError } from '../src/utils/logger.ts';
|
|
|
|
void test('assertRouteIdMatchesBody allows matching top-level body id', () => {
|
|
assert.doesNotThrow(() =>
|
|
assertRouteIdMatchesBody({
|
|
params: { id: 'route-id' },
|
|
body: { id: 'route-id' },
|
|
}),
|
|
);
|
|
});
|
|
|
|
void test('assertRouteIdMatchesBody allows matching data body id', () => {
|
|
assert.doesNotThrow(() =>
|
|
assertRouteIdMatchesBody({
|
|
params: { id: 'route-id' },
|
|
body: { data: { id: 'route-id' } },
|
|
}),
|
|
);
|
|
});
|
|
|
|
void test('assertRouteIdMatchesBody rejects mismatched body id', () => {
|
|
assert.throws(
|
|
() =>
|
|
assertRouteIdMatchesBody({
|
|
params: { id: 'route-id' },
|
|
body: { data: { id: 'body-id' } },
|
|
}),
|
|
/Request body id does not match route id/,
|
|
);
|
|
});
|
|
|
|
void test('normalizeLoggedError preserves Error instances', () => {
|
|
const error = new Error('Original failure');
|
|
|
|
assert.equal(normalizeLoggedError(error), error);
|
|
});
|
|
|
|
void test('normalizeLoggedError wraps non-Error values', () => {
|
|
const reason = { code: 'E_UNKNOWN' };
|
|
const error = normalizeLoggedError(reason);
|
|
|
|
assert.equal(error.message, 'Non-Error value thrown or rejected');
|
|
assert.equal(error.cause, reason);
|
|
});
|
|
|
|
void test('normalizeLoggedError uses string reasons as messages', () => {
|
|
const error = normalizeLoggedError('plain rejection');
|
|
|
|
assert.equal(error.message, 'plain rejection');
|
|
});
|