refactor(api): allow router handler injection in tests

This commit is contained in:
2026-04-21 13:10:00 +00:00
parent 0921dfbe5e
commit c95961d596
2 changed files with 48 additions and 5 deletions
+33
View File
@@ -1,5 +1,6 @@
import { assertEquals } from 'jsr:@std/assert@^1.0.0'; import { assertEquals } from 'jsr:@std/assert@^1.0.0';
import { EventEmitter } from 'node:events'; import { EventEmitter } from 'node:events';
import { AuthMiddleware } from '../ts/api/middleware/auth.ts';
import { ApiRouter } from '../ts/api/router.ts'; import { ApiRouter } from '../ts/api/router.ts';
class TestResponse { class TestResponse {
@@ -49,6 +50,38 @@ function createRouter(): ApiRouter {
{} as never, {} as never,
{} as never, {} as never,
['valid-key'], ['valid-key'],
{
authMiddleware: new AuthMiddleware(['valid-key']),
sanityMiddleware: {
validateChatRequest() {
return { valid: true };
},
sanitizeChatRequest(body: Record<string, unknown>) {
return body;
},
validateEmbeddingsRequest() {
return { valid: true };
},
sanitizeEmbeddingsRequest(body: Record<string, unknown>) {
return body;
},
} as never,
chatHandler: {
async handleChatCompletion() {
throw new Error('chat handler should not run in this test');
},
} as never,
modelsHandler: {
async handleListModels() {
throw new Error('models handler should not run in this test');
},
} as never,
embeddingsHandler: {
async handleEmbeddings() {
throw new Error('embeddings handler should not run in this test');
},
} as never,
},
); );
} }
+15 -5
View File
@@ -22,6 +22,14 @@ interface IParsedRequestBody {
body?: unknown; body?: unknown;
} }
interface IApiRouterOptions {
chatHandler?: ChatHandler;
modelsHandler?: ModelsHandler;
embeddingsHandler?: EmbeddingsHandler;
authMiddleware?: AuthMiddleware;
sanityMiddleware?: SanityMiddleware;
}
/** /**
* API Router - routes requests to handlers * API Router - routes requests to handlers
*/ */
@@ -42,6 +50,7 @@ export class ApiRouter {
modelLoader: ModelLoader, modelLoader: ModelLoader,
clusterCoordinator: ClusterCoordinator, clusterCoordinator: ClusterCoordinator,
apiKeys: string[], apiKeys: string[],
options: IApiRouterOptions = {},
) { ) {
this.containerManager = containerManager; this.containerManager = containerManager;
this.modelRegistry = modelRegistry; this.modelRegistry = modelRegistry;
@@ -49,22 +58,23 @@ export class ApiRouter {
this.clusterCoordinator = clusterCoordinator; this.clusterCoordinator = clusterCoordinator;
// Initialize handlers // Initialize handlers
this.chatHandler = new ChatHandler( this.chatHandler = options.chatHandler || new ChatHandler(
containerManager, containerManager,
modelRegistry, modelRegistry,
modelLoader, modelLoader,
clusterCoordinator, clusterCoordinator,
); );
this.modelsHandler = new ModelsHandler(containerManager, modelRegistry, clusterCoordinator); this.modelsHandler =
this.embeddingsHandler = new EmbeddingsHandler( options.modelsHandler || new ModelsHandler(containerManager, modelRegistry, clusterCoordinator);
this.embeddingsHandler = options.embeddingsHandler || new EmbeddingsHandler(
containerManager, containerManager,
modelRegistry, modelRegistry,
clusterCoordinator, clusterCoordinator,
); );
// Initialize middleware // Initialize middleware
this.authMiddleware = new AuthMiddleware(apiKeys); this.authMiddleware = options.authMiddleware || new AuthMiddleware(apiKeys);
this.sanityMiddleware = new SanityMiddleware(modelRegistry); this.sanityMiddleware = options.sanityMiddleware || new SanityMiddleware(modelRegistry);
} }
/** /**