refactor(test): extract shared gateway hook and vitest scoped config helpers

This commit is contained in:
Peter Steinberger
2026-03-02 14:35:49 +00:00
parent 741e74972b
commit e41f9998f7
6 changed files with 76 additions and 88 deletions

View File

@@ -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<string, string> = {
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;
}

View File

@@ -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<typeof createHooksRequestHandler>[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(): {

View File

@@ -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<string, string> = {
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);
}

View File

@@ -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<string, unknown>;
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"]);

View File

@@ -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<string, unknown>;
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"]);

17
vitest.scoped-config.ts Normal file
View File

@@ -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<string, unknown>;
const baseTest = (baseConfig as { test?: { exclude?: string[] } }).test ?? {};
const exclude = baseTest.exclude ?? [];
return defineConfig({
...base,
test: {
...baseTest,
include,
exclude,
},
});
}