mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 04:52:43 +00:00
refactor(core): dedupe gateway runtime and config tests
This commit is contained in:
@@ -58,6 +58,22 @@ async function postChatCompletions(port: number, body: unknown, headers?: Record
|
||||
return res;
|
||||
}
|
||||
|
||||
async function expectChatCompletionsDisabled(
|
||||
start: (port: number) => Promise<{ close: (opts?: { reason?: string }) => Promise<void> }>,
|
||||
) {
|
||||
const port = await getFreePort();
|
||||
const server = await start(port);
|
||||
try {
|
||||
const res = await postChatCompletions(port, {
|
||||
model: "openclaw",
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
});
|
||||
expect(res.status).toBe(404);
|
||||
} finally {
|
||||
await server.close({ reason: "test done" });
|
||||
}
|
||||
}
|
||||
|
||||
function parseSseDataLines(text: string): string[] {
|
||||
return text
|
||||
.split("\n")
|
||||
@@ -68,35 +84,12 @@ function parseSseDataLines(text: string): string[] {
|
||||
|
||||
describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||
it("rejects when disabled (default + config)", { timeout: 120_000 }, async () => {
|
||||
{
|
||||
const port = await getFreePort();
|
||||
const server = await startServerWithDefaultConfig(port);
|
||||
try {
|
||||
const res = await postChatCompletions(port, {
|
||||
model: "openclaw",
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
});
|
||||
expect(res.status).toBe(404);
|
||||
} finally {
|
||||
await server.close({ reason: "test done" });
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const port = await getFreePort();
|
||||
const server = await startServer(port, {
|
||||
await expectChatCompletionsDisabled(startServerWithDefaultConfig);
|
||||
await expectChatCompletionsDisabled((port) =>
|
||||
startServer(port, {
|
||||
openAiChatCompletionsEnabled: false,
|
||||
});
|
||||
try {
|
||||
const res = await postChatCompletions(port, {
|
||||
model: "openclaw",
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
});
|
||||
expect(res.status).toBe(404);
|
||||
} finally {
|
||||
await server.close({ reason: "test done" });
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("handles request validation and routing", async () => {
|
||||
@@ -133,6 +126,15 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||
expect(message).toContain(line);
|
||||
}
|
||||
};
|
||||
const getFirstAgentCall = () =>
|
||||
(agentCommand.mock.calls[0] as unknown[] | undefined)?.[0] as
|
||||
| {
|
||||
sessionKey?: string;
|
||||
message?: string;
|
||||
extraSystemPrompt?: string;
|
||||
}
|
||||
| undefined;
|
||||
const getFirstAgentMessage = () => getFirstAgentCall()?.message ?? "";
|
||||
|
||||
try {
|
||||
{
|
||||
@@ -252,8 +254,7 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const opts = (agentCommand.mock.calls[0] as unknown[] | undefined)?.[0];
|
||||
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
||||
const message = getFirstAgentMessage();
|
||||
expectMessageContext(message, {
|
||||
history: ["User: Hello, who are you?", "Assistant: I am Claude."],
|
||||
current: ["User: What did I just ask you?"],
|
||||
@@ -272,8 +273,7 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const opts = (agentCommand.mock.calls[0] as unknown[] | undefined)?.[0];
|
||||
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
||||
const message = getFirstAgentMessage();
|
||||
expect(message).not.toContain(HISTORY_CONTEXT_MARKER);
|
||||
expect(message).not.toContain(CURRENT_MESSAGE_MARKER);
|
||||
expect(message).toBe("Hello");
|
||||
@@ -291,9 +291,7 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const opts = (agentCommand.mock.calls[0] as unknown[] | undefined)?.[0];
|
||||
const extraSystemPrompt =
|
||||
(opts as { extraSystemPrompt?: string } | undefined)?.extraSystemPrompt ?? "";
|
||||
const extraSystemPrompt = getFirstAgentCall()?.extraSystemPrompt ?? "";
|
||||
expect(extraSystemPrompt).toBe("You are a helpful assistant.");
|
||||
await res.text();
|
||||
}
|
||||
@@ -311,8 +309,7 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const opts = (agentCommand.mock.calls[0] as unknown[] | undefined)?.[0];
|
||||
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
||||
const message = getFirstAgentMessage();
|
||||
expectMessageContext(message, {
|
||||
history: ["User: What's the weather?", "Assistant: Checking the weather."],
|
||||
current: ["Tool: Sunny, 70F."],
|
||||
|
||||
Reference in New Issue
Block a user