121 lines
4.6 KiB
TypeScript
121 lines
4.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import type { RouteDefinition, RouteModule } from '../../source/routes/types.ts';
|
|
import { ApplicationRouter } from '../../source/services/router.ts';
|
|
import { TestConnection } from '../helpers/test-connection.ts';
|
|
|
|
const moduleWith = (routes: RouteDefinition[]): RouteModule => {
|
|
return {
|
|
async getRoutes(): Promise<RouteDefinition[]> {
|
|
return routes;
|
|
},
|
|
};
|
|
};
|
|
|
|
describe('ApplicationRouter initialization', (): void => {
|
|
it('rejects duplicate exact paths during startup', async (): Promise<void> => {
|
|
const route = { url: '/echo', handler: (): void => undefined };
|
|
|
|
await expect(ApplicationRouter.create([ moduleWith([ route ]), moduleWith([ route ]) ])).rejects.toThrow('Duplicate application route: /echo');
|
|
});
|
|
|
|
it.each([ 'echo', '/', '/items/:id', '/items?active=true', '/items#active' ])('rejects the invalid route path %s', async (url): Promise<void> => {
|
|
await expect(ApplicationRouter.create([ moduleWith([{ url, handler: (): void => undefined }]) ])).rejects.toThrow('Invalid application route');
|
|
});
|
|
});
|
|
|
|
describe('ApplicationRouter dispatch', (): void => {
|
|
it('binds the connection, body, and request ID to one route stream', async (): Promise<void> => {
|
|
const connection = new TestConnection(false, false);
|
|
const router = await ApplicationRouter.create([
|
|
moduleWith([
|
|
{
|
|
url: '/echo',
|
|
handler: async (stream): Promise<void> => {
|
|
expect(stream.connection).toBe(connection);
|
|
expect(stream.path).toBe('/echo');
|
|
expect(stream.headers).toEqual({ 'x-request-token': 'route-1' });
|
|
await stream.send(stream.body);
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
await router.dispatch({ path: '/echo', body: { value: 1 }, requestId: 'request-1', headers: { 'x-request-token': 'route-1' } }, connection);
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: 'request-1',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { value: 1 },
|
|
},
|
|
]);
|
|
|
|
await expect(router.dispatch({ path: '/echo/other', body: {} }, connection)).rejects.toMatchObject({ statusCode: 404 });
|
|
});
|
|
|
|
it('preserves correlation when concurrent requests finish out of order', async (): Promise<void> => {
|
|
const completions = new Map<string, () => void>();
|
|
const router = await ApplicationRouter.create([
|
|
moduleWith([
|
|
{
|
|
url: '/delayed',
|
|
handler: async (stream): Promise<void> => {
|
|
const key = (stream.body as { key: string }).key;
|
|
const token = stream.headers['x-request-token'];
|
|
await new Promise<void>((resolve) => completions.set(key, resolve));
|
|
await stream.send({ key, token });
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
const connection = new TestConnection(true, true);
|
|
|
|
const first = router.dispatch(
|
|
{ path: '/delayed', body: { key: 'A' }, requestId: 'A', headers: { 'x-request-token': 'token-a' } },
|
|
connection,
|
|
);
|
|
const second = router.dispatch(
|
|
{ path: '/delayed', body: { key: 'B' }, requestId: 'B', headers: { 'x-request-token': 'token-b' } },
|
|
connection,
|
|
);
|
|
|
|
completions.get('B')?.();
|
|
await second;
|
|
completions.get('A')?.();
|
|
await first;
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: 'B',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'B', token: 'token-b' },
|
|
},
|
|
{
|
|
id: 'A',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'A', token: 'token-a' },
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('propagates route failures without infrastructure-specific cleanup', async (): Promise<void> => {
|
|
const error = new Error('route failed');
|
|
const router = await ApplicationRouter.create([
|
|
moduleWith([
|
|
{
|
|
url: '/failure',
|
|
handler: (): void => {
|
|
throw error;
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
await expect(router.dispatch({ path: '/failure' }, new TestConnection(false, false))).rejects.toBe(error);
|
|
});
|
|
});
|