205 lines
6.9 KiB
TypeScript
205 lines
6.9 KiB
TypeScript
import { describe, expect, it, vi } 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';
|
|
|
|
import type { AuthSecp256k1 } from '../../source/auth/auth.ts';
|
|
|
|
/**
|
|
* A controlled request is a request that is controlled by the test.
|
|
* It is used to control the request flow and ensure that the request is completed in the correct order.
|
|
*/
|
|
type ControlledRequest = {
|
|
request: Promise<void>;
|
|
started: Promise<void>;
|
|
release: () => void;
|
|
};
|
|
|
|
/**
|
|
* A mock of the AuthSecp256k1 service
|
|
*/
|
|
const auth = {
|
|
verifySignature: vi.fn().mockResolvedValue(true),
|
|
verifyUniqueRequest: vi.fn().mockResolvedValue(true),
|
|
assertTimestampFreshness: vi.fn().mockResolvedValue(true),
|
|
} as unknown as AuthSecp256k1;
|
|
|
|
/**
|
|
* A helper function to create a route module with the given routes
|
|
* @param routes - The routes to create the module with
|
|
* @returns The created route module
|
|
*/
|
|
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({ auth }, [ 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({ auth }, [ 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({ auth }, [
|
|
moduleWith([
|
|
{
|
|
url: '/echo',
|
|
handler: async (stream): Promise<void> => {
|
|
expect(stream.connection).toBe(connection);
|
|
expect(stream.path).toBe('/echo');
|
|
expect(stream.headers).toEqual({ 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' });
|
|
await stream.send(stream.body);
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
await router.dispatch(
|
|
{
|
|
path: '/echo',
|
|
body: { value: 1 },
|
|
requestId: '1',
|
|
headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' },
|
|
},
|
|
connection,
|
|
);
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: '1',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { value: 1 },
|
|
},
|
|
]);
|
|
|
|
await expect(router.dispatch(
|
|
{ path: '/echo/other', body: {}, headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' } },
|
|
connection,
|
|
)).rejects.toMatchObject({ statusCode: 404 });
|
|
});
|
|
|
|
it('preserves correlation when concurrent requests finish out of order', async (): Promise<void> => {
|
|
const router = await ApplicationRouter.create({ auth }, [
|
|
moduleWith([
|
|
{
|
|
url: '/delayed',
|
|
handler: async (stream): Promise<void> => {
|
|
const { key, signalStarted, released } = stream.body as {
|
|
key: string;
|
|
signalStarted: () => void;
|
|
released: Promise<void>;
|
|
};
|
|
|
|
signalStarted();
|
|
|
|
await released;
|
|
await stream.send({ key });
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
const connection = new TestConnection(false, false);
|
|
|
|
const createControlledRequest = (key: string): ControlledRequest => {
|
|
const { promise: started, resolve: signalStarted } = Promise.withResolvers<void>();
|
|
|
|
const { promise: released, resolve: release } = Promise.withResolvers<void>();
|
|
|
|
const request = router.dispatch(
|
|
{
|
|
path: '/delayed',
|
|
body: {
|
|
key,
|
|
signalStarted,
|
|
released,
|
|
},
|
|
requestId: key,
|
|
headers: {
|
|
'x-public-key': 'public-key',
|
|
'x-signature': 'signature',
|
|
'x-timestamp': '1000',
|
|
},
|
|
},
|
|
connection,
|
|
);
|
|
|
|
return {
|
|
request,
|
|
started,
|
|
release,
|
|
};
|
|
};
|
|
|
|
const first = createControlledRequest('A');
|
|
const second = createControlledRequest('B');
|
|
|
|
expect(connection.messages).toEqual([]);
|
|
|
|
await Promise.all([ first.started, second.started ]);
|
|
|
|
second.release();
|
|
await second.request;
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: 'B',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'B' },
|
|
},
|
|
]);
|
|
|
|
first.release();
|
|
await first.request;
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: 'B',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'B' },
|
|
},
|
|
{
|
|
id: 'A',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'A' },
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('propagates route failures without infrastructure-specific cleanup', async (): Promise<void> => {
|
|
const error = new Error('route failed');
|
|
const router = await ApplicationRouter.create({ auth }, [
|
|
moduleWith([
|
|
{
|
|
url: '/failure',
|
|
handler: (): void => {
|
|
throw error;
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
await expect(router.dispatch(
|
|
{ path: '/failure', headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' } },
|
|
new TestConnection(false, false),
|
|
)).rejects.toBe(error);
|
|
});
|
|
});
|