| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
- import { join } from "node:path";
- import { clearAppData, DEFAULT_WORKSPACE } from "../../../helpers/desktop-app.js";
- import {
- E2E_REPLY_TOKEN,
- countUpstreamRequests,
- listToolCallBlocks,
- prepareConversationE2E,
- sendPrompt,
- waitForAssistantMessageContaining,
- waitForChatState,
- waitForComposerText,
- waitForUpstreamRequestContaining,
- waitForToolCallBlockByToolName,
- waitForUserMessageContaining,
- } from "../../../helpers/conversation-session.js";
- import { ensureToolCrossProductFullAccessMode } from "../../../helpers/conversation-session-tool-cross-product.js";
- const CASE_ROOT = join(DEFAULT_WORKSPACE, "bash-read-state-e2e");
- const TARGET_RELATIVE_PATH = "bash-read-state-e2e/target.txt";
- const FORMATTER_RELATIVE_PATH = "bash-read-state-e2e/overwrite-target.mjs";
- const TARGET_PATH = join(DEFAULT_WORKSPACE, TARGET_RELATIVE_PATH);
- const FORMATTER_PATH = join(DEFAULT_WORKSPACE, FORMATTER_RELATIVE_PATH);
- const INITIAL_CONTENT = "alpha\nbeta\n";
- const FORMATTED_CONTENT = "delta\nbeta\n";
- const STALE_HINT = "Call Read before editing";
- const NOT_READ_ERROR = "File has not been read yet. Read it first before writing to it.";
- describe("会话区 Bash read-state E2E", () => {
- afterEach(async () => {
- await rm(CASE_ROOT, { recursive: true, force: true });
- });
- after(async () => {
- await browser.electron.restoreAllMocks();
- await rm(CASE_ROOT, { recursive: true, force: true });
- await clearAppData();
- });
- it("Bash cat 应允许后续 Edit,并在 formatter Bash 后向模型暴露 stale hint", async function () {
- this.timeout(180000);
- const runId = Date.now();
- const marker = `E2E_BASH_READ_STATE_${runId}`;
- await writeFixtures();
- await prepareConversationE2E();
- await ensureToolCrossProductFullAccessMode();
- const prompt = buildPrompt(marker);
- await sendPrompt(prompt);
- await waitForComposerText("", "Bash read-state 首发后输入框没有清空");
- await waitForUserMessageContaining(marker);
- await waitForUpstreamRequestContaining(marker, 60000);
- await waitForToolCallBlockByToolName("Bash", 90000);
- await waitForToolCallBlockByToolName("Edit", 90000);
- await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
- await waitForChatState(
- (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
- "Bash read-state 工具链没有回到 idle",
- 120000,
- );
- const toolBlocks = await listToolCallBlocks();
- expect(toolBlocks.filter((block) => block.toolName === "Bash").length).toBeGreaterThanOrEqual(2);
- expect(toolBlocks.some((block) => block.toolName === "Edit")).toBe(true);
- expect(await readFile(TARGET_PATH, "utf-8")).toBe(FORMATTED_CONTENT);
- expect(await countUpstreamRequests({ includes: [STALE_HINT] })).toBeGreaterThan(0);
- expect(await countUpstreamRequests({ includes: [NOT_READ_ERROR] })).toBe(0);
- });
- });
- function buildPrompt(marker: string) {
- return [
- `${marker}: Follow these tool steps exactly.`,
- `1. Use Bash with exactly this command: cat ${TARGET_RELATIVE_PATH}`,
- `2. Use Edit on ${TARGET_PATH}; replace exactly "alpha" with "gamma".`,
- `3. Use Bash with exactly this command: node ${FORMATTER_RELATIVE_PATH} --fix`,
- `4. Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
- "Do not use the Read tool in this case.",
- ].join(" ");
- }
- async function writeFixtures() {
- await mkdir(CASE_ROOT, { recursive: true });
- await writeFile(TARGET_PATH, INITIAL_CONTENT, "utf-8");
- await writeFile(
- FORMATTER_PATH,
- [
- "import { writeFile } from 'node:fs/promises';",
- "await new Promise((resolve) => setTimeout(resolve, 30));",
- `await writeFile(${JSON.stringify(TARGET_PATH)}, ${JSON.stringify(FORMATTED_CONTENT)});`,
- "console.log('formatted target');",
- "",
- ].join("\n"),
- "utf-8",
- );
- }
|