Many fixes
This commit is contained in:
@@ -1,52 +1,27 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import type { RouteDefinition, RouteModule } from '../../source/routes/types.ts';
|
||||
// Source
|
||||
import { ApplicationRouter } from '../../source/services/router.ts';
|
||||
|
||||
// Helpers
|
||||
import { createMockAuth, toRoutes } from '../helpers/misc.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;
|
||||
};
|
||||
import { createControlledRequest } from '../helpers/controlled-request.ts';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
},
|
||||
};
|
||||
};
|
||||
const auth = createMockAuth();
|
||||
|
||||
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');
|
||||
await expect(ApplicationRouter.create({ auth }, [ toRoutes([ route ]), toRoutes([ 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');
|
||||
await expect(ApplicationRouter.create({ auth }, [ toRoutes([{ url, handler: (): void => undefined }]) ])).rejects.toThrow('Invalid application route');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,7 +29,7 @@ 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([
|
||||
toRoutes([
|
||||
{
|
||||
url: '/echo',
|
||||
handler: async (stream): Promise<void> => {
|
||||
@@ -94,7 +69,7 @@ describe('ApplicationRouter dispatch', (): void => {
|
||||
|
||||
it('preserves correlation when concurrent requests finish out of order', async (): Promise<void> => {
|
||||
const router = await ApplicationRouter.create({ auth }, [
|
||||
moduleWith([
|
||||
toRoutes([
|
||||
{
|
||||
url: '/delayed',
|
||||
handler: async (stream): Promise<void> => {
|
||||
@@ -115,38 +90,8 @@ describe('ApplicationRouter dispatch', (): void => {
|
||||
|
||||
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');
|
||||
const first = createControlledRequest({ router, connection, path: '/delayed', requestId: 'A', body: { key: 'A' } });
|
||||
const second = createControlledRequest({ router, connection, path: '/delayed', requestId: 'B', body: { key: 'B' } });
|
||||
|
||||
expect(connection.messages).toEqual([]);
|
||||
|
||||
@@ -181,12 +126,12 @@ describe('ApplicationRouter dispatch', (): void => {
|
||||
body: { key: 'A' },
|
||||
},
|
||||
]);
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
it('propagates route failures without infrastructure-specific cleanup', async (): Promise<void> => {
|
||||
const error = new Error('route failed');
|
||||
const router = await ApplicationRouter.create({ auth }, [
|
||||
moduleWith([
|
||||
toRoutes([
|
||||
{
|
||||
url: '/failure',
|
||||
handler: (): void => {
|
||||
|
||||
Reference in New Issue
Block a user