Browse Source

test: add E2E tests for session-level system prompt override

Co-authored-by: CodeFree <codefree@chinatelcom.cn>
feat/agent-independence
npmrun 2 months ago
parent
commit
d0d097c3af
  1. 4
      e2e/global-setup.ts
  2. 81
      e2e/specs/agent-chat.spec.ts

4
e2e/global-setup.ts

@ -57,7 +57,9 @@ function startMockLlm(): Promise<void> {
? parsed.messages.find((m: any) => m.role === "system")
: undefined;
if (systemMsg?.content) {
(globalThis as any).__lastMockSystemPrompt = systemMsg.content;
try {
writeFileSync("/tmp/e2e-last-system-prompt.txt", String(systemMsg.content));
} catch {}
}
const chars = Array.from(MOCK_REPLY);
res.writeHead(200, {

81
e2e/specs/agent-chat.spec.ts

@ -1,8 +1,17 @@
import { test, expect } from "@playwright/test";
import { readFileSync } from "node:fs";
import { E2E_ADMIN, FIXED_CAPTCHA } from "../fixtures/test-users";
const MOCK_REPLY = "你好!这是一个测试回复。";
function getLastSystemPrompt(): string {
try {
return readFileSync("/tmp/e2e-last-system-prompt.txt", "utf-8");
} catch {
return "";
}
}
async function loginViaUI(page: import("@playwright/test").Page) {
await page.goto("/auth/login");
await page.fill("#login-username", E2E_ADMIN.username);
@ -127,4 +136,76 @@ test.describe("Agent 对话", () => {
await expect(page.locator(".agent-message.is-user")).toHaveCount(2, { timeout: 10000 });
await expect(page.locator(".agent-message.is-assistant")).toHaveCount(2, { timeout: 30000 });
});
test("修改系统提示词后发送消息使用自定义提示词", async ({ page }) => {
await loginViaUI(page);
const createBody = await page.evaluate(async () => {
const res = await fetch("/api/agents/default/sessions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ systemPrompt: "You are a pirate. Always respond in pirate speak." }),
});
return res.json();
});
const sessionId = createBody.data.id;
await page.goto(`/chat/default?session=${sessionId}`);
await page.waitForTimeout(1000);
const newChatBtn = page.locator(".new-chat-btn").first();
if (await newChatBtn.isVisible()) {
await newChatBtn.click();
await page.waitForTimeout(500);
}
await page.fill(".agent-textarea", "hello");
await page.click(".btn-send");
await expect(
page.locator(".agent-message.is-assistant .agent-markdown").first(),
).toContainText(MOCK_REPLY, { timeout: 30000 });
const lastPrompt = getLastSystemPrompt();
expect(lastPrompt).toBe("You are a pirate. Always respond in pirate speak.");
});
test("重置系统提示词后回退到 Agent 默认", async ({ page }) => {
await loginViaUI(page);
const createBody = await page.evaluate(async () => {
const res = await fetch("/api/agents/default/sessions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ systemPrompt: "temporary custom prompt" }),
});
return res.json();
});
const sessionId = createBody.data.id;
await page.evaluate(async (id) => {
await fetch(`/api/agents/default/sessions/${id}/config`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ systemPrompt: null }),
});
}, sessionId);
await page.goto(`/chat/default?session=${sessionId}`);
await page.waitForTimeout(1000);
const newChatBtn = page.locator(".new-chat-btn").first();
if (await newChatBtn.isVisible()) {
await newChatBtn.click();
await page.waitForTimeout(500);
}
await page.fill(".agent-textarea", "hello again");
await page.click(".btn-send");
await expect(
page.locator(".agent-message.is-assistant .agent-markdown").first(),
).toContainText(MOCK_REPLY, { timeout: 30000 });
const lastPrompt = getLastSystemPrompt();
expect(lastPrompt).toBe("You are a helpful AI assistant.");
});
});

Loading…
Cancel
Save