From d0d097c3afd42c6bbdc4b12a51e032033fc59af6 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Sun, 9 Aug 2026 11:54:07 +0800 Subject: [PATCH] test: add E2E tests for session-level system prompt override Co-authored-by: CodeFree --- e2e/global-setup.ts | 4 ++- e2e/specs/agent-chat.spec.ts | 81 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/e2e/global-setup.ts b/e2e/global-setup.ts index f4c958e..a248956 100644 --- a/e2e/global-setup.ts +++ b/e2e/global-setup.ts @@ -57,7 +57,9 @@ function startMockLlm(): Promise { ? 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, { diff --git a/e2e/specs/agent-chat.spec.ts b/e2e/specs/agent-chat.spec.ts index 31f31c6..45b4a21 100644 --- a/e2e/specs/agent-chat.spec.ts +++ b/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."); + }); });