diff --git a/src/gateway/hooks-test-helpers.ts b/src/gateway/hooks-test-helpers.ts new file mode 100644 index 00000000000..ca0988edbfe --- /dev/null +++ b/src/gateway/hooks-test-helpers.ts @@ -0,0 +1,42 @@ +import type { IncomingMessage } from "node:http"; +import type { HooksConfigResolved } from "./hooks.js"; + +export function createHooksConfig(): HooksConfigResolved { + return { + basePath: "/hooks", + token: "hook-secret", + maxBodyBytes: 1024, + mappings: [], + agentPolicy: { + defaultAgentId: "main", + knownAgentIds: new Set(["main"]), + allowedAgentIds: undefined, + }, + sessionPolicy: { + allowRequestSessionKey: false, + defaultSessionKey: undefined, + allowedSessionKeyPrefixes: undefined, + }, + }; +} + +export function createGatewayRequest(params: { + path: string; + authorization?: string; + method?: string; + remoteAddress?: string; + host?: string; +}): IncomingMessage { + const headers: Record = { + host: params.host ?? "localhost:18789", + }; + if (params.authorization) { + headers.authorization = params.authorization; + } + return { + method: params.method ?? "GET", + url: params.path, + headers, + socket: { remoteAddress: params.remoteAddress ?? "127.0.0.1" }, + } as IncomingMessage; +} diff --git a/src/gateway/server-http.hooks-request-timeout.test.ts b/src/gateway/server-http.hooks-request-timeout.test.ts index 577ffe1ab43..0452cab7b9a 100644 --- a/src/gateway/server-http.hooks-request-timeout.test.ts +++ b/src/gateway/server-http.hooks-request-timeout.test.ts @@ -1,7 +1,7 @@ import type { IncomingMessage, ServerResponse } from "node:http"; import { beforeEach, describe, expect, test, vi } from "vitest"; import type { createSubsystemLogger } from "../logging/subsystem.js"; -import type { HooksConfigResolved } from "./hooks.js"; +import { createGatewayRequest, createHooksConfig } from "./hooks-test-helpers.js"; const { readJsonBodyMock } = vi.hoisted(() => ({ readJsonBodyMock: vi.fn(), @@ -19,39 +19,18 @@ import { createHooksRequestHandler } from "./server-http.js"; type HooksHandlerDeps = Parameters[0]; -function createHooksConfig(): HooksConfigResolved { - return { - basePath: "/hooks", - token: "hook-secret", - maxBodyBytes: 1024, - mappings: [], - agentPolicy: { - defaultAgentId: "main", - knownAgentIds: new Set(["main"]), - allowedAgentIds: undefined, - }, - sessionPolicy: { - allowRequestSessionKey: false, - defaultSessionKey: undefined, - allowedSessionKeyPrefixes: undefined, - }, - }; -} - function createRequest(params?: { authorization?: string; remoteAddress?: string; url?: string; }): IncomingMessage { - return { + return createGatewayRequest({ method: "POST", - url: params?.url ?? "/hooks/wake", - headers: { - host: "127.0.0.1:18789", - authorization: params?.authorization ?? "Bearer hook-secret", - }, - socket: { remoteAddress: params?.remoteAddress ?? "127.0.0.1" }, - } as IncomingMessage; + path: params?.url ?? "/hooks/wake", + host: "127.0.0.1:18789", + authorization: params?.authorization ?? "Bearer hook-secret", + remoteAddress: params?.remoteAddress, + }); } function createResponse(): { diff --git a/src/gateway/server.plugin-http-auth.test.ts b/src/gateway/server.plugin-http-auth.test.ts index cfeefe33eec..4db9e04329f 100644 --- a/src/gateway/server.plugin-http-auth.test.ts +++ b/src/gateway/server.plugin-http-auth.test.ts @@ -2,7 +2,7 @@ import type { IncomingMessage, ServerResponse } from "node:http"; import { describe, expect, test, vi } from "vitest"; import type { createSubsystemLogger } from "../logging/subsystem.js"; import type { ResolvedGatewayAuth } from "./auth.js"; -import type { HooksConfigResolved } from "./hooks.js"; +import { createGatewayRequest, createHooksConfig } from "./hooks-test-helpers.js"; import { canonicalizePathVariant, isProtectedPluginRoutePath } from "./security-path.js"; import { createGatewayHttpServer, createHooksRequestHandler } from "./server-http.js"; import { withTempConfig } from "./test-temp-config.js"; @@ -29,18 +29,11 @@ function createRequest(params: { authorization?: string; method?: string; }): IncomingMessage { - const headers: Record = { - host: "localhost:18789", - }; - if (params.authorization) { - headers.authorization = params.authorization; - } - return { - method: params.method ?? "GET", - url: params.path, - headers, - socket: { remoteAddress: "127.0.0.1" }, - } as IncomingMessage; + return createGatewayRequest({ + path: params.path, + authorization: params.authorization, + method: params.method, + }); } function createResponse(): { @@ -146,25 +139,6 @@ function expectUnauthorizedResponse( expect(response.getBody(), label).toContain("Unauthorized"); } -function createHooksConfig(): HooksConfigResolved { - return { - basePath: "/hooks", - token: "hook-secret", - maxBodyBytes: 1024, - mappings: [], - agentPolicy: { - defaultAgentId: "main", - knownAgentIds: new Set(["main"]), - allowedAgentIds: undefined, - }, - sessionPolicy: { - allowRequestSessionKey: false, - defaultSessionKey: undefined, - allowedSessionKeyPrefixes: undefined, - }, - }; -} - function canonicalizePluginPath(pathname: string): string { return canonicalizePathVariant(pathname); } diff --git a/vitest.extensions.config.ts b/vitest.extensions.config.ts index 900c71ca778..9a2df2faa2c 100644 --- a/vitest.extensions.config.ts +++ b/vitest.extensions.config.ts @@ -1,15 +1,3 @@ -import { defineConfig } from "vitest/config"; -import baseConfig from "./vitest.config.ts"; +import { createScopedVitestConfig } from "./vitest.scoped-config.ts"; -const base = baseConfig as unknown as Record; -const baseTest = (baseConfig as { test?: { exclude?: string[] } }).test ?? {}; -const exclude = baseTest.exclude ?? []; - -export default defineConfig({ - ...base, - test: { - ...baseTest, - include: ["extensions/**/*.test.ts"], - exclude, - }, -}); +export default createScopedVitestConfig(["extensions/**/*.test.ts"]); diff --git a/vitest.gateway.config.ts b/vitest.gateway.config.ts index 0f7e835d1d1..b8f85a89bca 100644 --- a/vitest.gateway.config.ts +++ b/vitest.gateway.config.ts @@ -1,15 +1,3 @@ -import { defineConfig } from "vitest/config"; -import baseConfig from "./vitest.config.ts"; +import { createScopedVitestConfig } from "./vitest.scoped-config.ts"; -const base = baseConfig as unknown as Record; -const baseTest = (baseConfig as { test?: { exclude?: string[] } }).test ?? {}; -const exclude = baseTest.exclude ?? []; - -export default defineConfig({ - ...base, - test: { - ...baseTest, - include: ["src/gateway/**/*.test.ts"], - exclude, - }, -}); +export default createScopedVitestConfig(["src/gateway/**/*.test.ts"]); diff --git a/vitest.scoped-config.ts b/vitest.scoped-config.ts new file mode 100644 index 00000000000..d3fe9f7c50d --- /dev/null +++ b/vitest.scoped-config.ts @@ -0,0 +1,17 @@ +import { defineConfig } from "vitest/config"; +import baseConfig from "./vitest.config.ts"; + +export function createScopedVitestConfig(include: string[]) { + const base = baseConfig as unknown as Record; + const baseTest = (baseConfig as { test?: { exclude?: string[] } }).test ?? {}; + const exclude = baseTest.exclude ?? []; + + return defineConfig({ + ...base, + test: { + ...baseTest, + include, + exclude, + }, + }); +}