mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
refactor(test): extract shared gateway hook and vitest scoped config helpers
This commit is contained in:
42
src/gateway/hooks-test-helpers.ts
Normal file
42
src/gateway/hooks-test-helpers.ts
Normal 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;
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||||
import type { createSubsystemLogger } from "../logging/subsystem.js";
|
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(() => ({
|
const { readJsonBodyMock } = vi.hoisted(() => ({
|
||||||
readJsonBodyMock: vi.fn(),
|
readJsonBodyMock: vi.fn(),
|
||||||
@@ -19,39 +19,18 @@ import { createHooksRequestHandler } from "./server-http.js";
|
|||||||
|
|
||||||
type HooksHandlerDeps = Parameters<typeof createHooksRequestHandler>[0];
|
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?: {
|
function createRequest(params?: {
|
||||||
authorization?: string;
|
authorization?: string;
|
||||||
remoteAddress?: string;
|
remoteAddress?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
}): IncomingMessage {
|
}): IncomingMessage {
|
||||||
return {
|
return createGatewayRequest({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: params?.url ?? "/hooks/wake",
|
path: params?.url ?? "/hooks/wake",
|
||||||
headers: {
|
|
||||||
host: "127.0.0.1:18789",
|
host: "127.0.0.1:18789",
|
||||||
authorization: params?.authorization ?? "Bearer hook-secret",
|
authorization: params?.authorization ?? "Bearer hook-secret",
|
||||||
},
|
remoteAddress: params?.remoteAddress,
|
||||||
socket: { remoteAddress: params?.remoteAddress ?? "127.0.0.1" },
|
});
|
||||||
} as IncomingMessage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createResponse(): {
|
function createResponse(): {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
|
|||||||
import { describe, expect, test, vi } from "vitest";
|
import { describe, expect, test, vi } from "vitest";
|
||||||
import type { createSubsystemLogger } from "../logging/subsystem.js";
|
import type { createSubsystemLogger } from "../logging/subsystem.js";
|
||||||
import type { ResolvedGatewayAuth } from "./auth.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 { canonicalizePathVariant, isProtectedPluginRoutePath } from "./security-path.js";
|
||||||
import { createGatewayHttpServer, createHooksRequestHandler } from "./server-http.js";
|
import { createGatewayHttpServer, createHooksRequestHandler } from "./server-http.js";
|
||||||
import { withTempConfig } from "./test-temp-config.js";
|
import { withTempConfig } from "./test-temp-config.js";
|
||||||
@@ -29,18 +29,11 @@ function createRequest(params: {
|
|||||||
authorization?: string;
|
authorization?: string;
|
||||||
method?: string;
|
method?: string;
|
||||||
}): IncomingMessage {
|
}): IncomingMessage {
|
||||||
const headers: Record<string, string> = {
|
return createGatewayRequest({
|
||||||
host: "localhost:18789",
|
path: params.path,
|
||||||
};
|
authorization: params.authorization,
|
||||||
if (params.authorization) {
|
method: params.method,
|
||||||
headers.authorization = params.authorization;
|
});
|
||||||
}
|
|
||||||
return {
|
|
||||||
method: params.method ?? "GET",
|
|
||||||
url: params.path,
|
|
||||||
headers,
|
|
||||||
socket: { remoteAddress: "127.0.0.1" },
|
|
||||||
} as IncomingMessage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createResponse(): {
|
function createResponse(): {
|
||||||
@@ -146,25 +139,6 @@ function expectUnauthorizedResponse(
|
|||||||
expect(response.getBody(), label).toContain("Unauthorized");
|
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 {
|
function canonicalizePluginPath(pathname: string): string {
|
||||||
return canonicalizePathVariant(pathname);
|
return canonicalizePathVariant(pathname);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
import { defineConfig } from "vitest/config";
|
import { createScopedVitestConfig } from "./vitest.scoped-config.ts";
|
||||||
import baseConfig from "./vitest.config.ts";
|
|
||||||
|
|
||||||
const base = baseConfig as unknown as Record<string, unknown>;
|
export default createScopedVitestConfig(["extensions/**/*.test.ts"]);
|
||||||
const baseTest = (baseConfig as { test?: { exclude?: string[] } }).test ?? {};
|
|
||||||
const exclude = baseTest.exclude ?? [];
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
...base,
|
|
||||||
test: {
|
|
||||||
...baseTest,
|
|
||||||
include: ["extensions/**/*.test.ts"],
|
|
||||||
exclude,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
import { defineConfig } from "vitest/config";
|
import { createScopedVitestConfig } from "./vitest.scoped-config.ts";
|
||||||
import baseConfig from "./vitest.config.ts";
|
|
||||||
|
|
||||||
const base = baseConfig as unknown as Record<string, unknown>;
|
export default createScopedVitestConfig(["src/gateway/**/*.test.ts"]);
|
||||||
const baseTest = (baseConfig as { test?: { exclude?: string[] } }).test ?? {};
|
|
||||||
const exclude = baseTest.exclude ?? [];
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
...base,
|
|
||||||
test: {
|
|
||||||
...baseTest,
|
|
||||||
include: ["src/gateway/**/*.test.ts"],
|
|
||||||
exclude,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|||||||
17
vitest.scoped-config.ts
Normal file
17
vitest.scoped-config.ts
Normal 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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user