conversation-session-file-tools-atomic-write.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { chmod, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData, DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  4. import {
  5. getV4ConversationState,
  6. prepareV4ConversationE2E,
  7. sendV4Prompt,
  8. waitForV4AssistantMessageContaining,
  9. waitForV4ComposerText,
  10. waitForV4UserMessageContaining,
  11. } from "#e2e/helpers/v4-conversation";
  12. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  13. import {
  14. ensureToolCrossProductFullAccessMode,
  15. respondToToolCrossProductBlockers,
  16. } from "#e2e/helpers/conversation-session-tool-cross-product";
  17. const CASE_MARKER = "E2E_FILE_TOOLS_ATOMIC_WRITE";
  18. const FINAL_MARKER = `${CASE_MARKER}_OK`;
  19. const CASE_RELATIVE_ROOT = "file-tools-atomic-write-e2e";
  20. const WRITE_RELATIVE_PATH = `${CASE_RELATIVE_ROOT}/write.sh`;
  21. const EDIT_RELATIVE_PATH = `${CASE_RELATIVE_ROOT}/edit.sh`;
  22. const CASE_ROOT = join(DEFAULT_WORKSPACE, CASE_RELATIVE_ROOT);
  23. const WRITE_PATH = join(DEFAULT_WORKSPACE, WRITE_RELATIVE_PATH);
  24. const EDIT_PATH = join(DEFAULT_WORKSPACE, EDIT_RELATIVE_PATH);
  25. const EXECUTABLE_MODE = 0o755;
  26. describe("会话区 file tools 原子写权限 E2E", () => {
  27. before(async function () {
  28. this.timeout(120000);
  29. await prepareV4ConversationE2E();
  30. await ensureToolCrossProductFullAccessMode();
  31. });
  32. afterEach(async () => {
  33. await rm(CASE_ROOT, { recursive: true, force: true });
  34. });
  35. after(async () => {
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. });
  39. it("Write 和 Edit 修改可执行脚本后应保留 POSIX 执行位", async function () {
  40. if (process.platform === "win32") {
  41. // Windows 不具备 POSIX 执行位语义,明确记为跳过。
  42. this.skip();
  43. }
  44. this.timeout(180000);
  45. await writeExecutableFixtures();
  46. const runId = Date.now();
  47. const sourceMarker = `${CASE_MARKER}_${runId}`;
  48. const prompt = [
  49. `${sourceMarker}: Follow these file tool steps exactly.`,
  50. `1. Use Read on ${WRITE_RELATIVE_PATH}.`,
  51. `2. Use Write on ${WRITE_RELATIVE_PATH} to replace the full content with a bash script that echoes new-write.`,
  52. `3. Use Read on ${EDIT_RELATIVE_PATH}.`,
  53. `4. Use Edit on ${EDIT_RELATIVE_PATH}; replace exactly "old-edit" with "new-edit".`,
  54. `5. Reply with exactly "${FINAL_MARKER}" and no other text.`,
  55. ].join(" ");
  56. await sendV4Prompt(prompt);
  57. await waitForV4ComposerText("", "file tools atomic write: 首发后输入框没有清空");
  58. await waitForV4UserMessageContaining(sourceMarker);
  59. await waitForToolCallBlockByToolName("Write", 90000);
  60. await waitForToolCallBlockByToolName("Edit", 90000);
  61. await waitForV4AssistantMessageContaining(FINAL_MARKER);
  62. await waitForStableCompleted("file tools atomic write: 工具链没有回到 idle");
  63. await expectExecutableFile(WRITE_PATH, "#!/usr/bin/env bash\necho new-write\n");
  64. await expectExecutableFile(EDIT_PATH, "#!/usr/bin/env bash\necho new-edit\n");
  65. });
  66. });
  67. async function writeExecutableFixtures() {
  68. await rm(CASE_ROOT, { recursive: true, force: true });
  69. await mkdir(CASE_ROOT, { recursive: true });
  70. await writeFile(WRITE_PATH, "#!/usr/bin/env bash\necho old-write\n", "utf-8");
  71. await chmod(WRITE_PATH, EXECUTABLE_MODE);
  72. await writeFile(EDIT_PATH, "#!/usr/bin/env bash\necho old-edit\n", "utf-8");
  73. await chmod(EDIT_PATH, EXECUTABLE_MODE);
  74. }
  75. async function expectExecutableFile(filePath: string, expectedContent: string) {
  76. expect(await readFile(filePath, "utf-8")).toBe(expectedContent);
  77. expect((await stat(filePath)).mode & 0o777).toBe(EXECUTABLE_MODE);
  78. }
  79. async function waitForStableCompleted(timeoutMsg: string) {
  80. let latest = "not-started";
  81. await browser.waitUntil(
  82. async () => {
  83. await respondToToolCrossProductBlockers();
  84. const snapshot = await getV4ConversationState();
  85. latest = JSON.stringify(snapshot);
  86. return snapshot.state === "idle" && snapshot.queueCount === 0;
  87. },
  88. {
  89. timeout: 120000,
  90. timeoutMsg: `${timeoutMsg}; latest=${latest}`,
  91. },
  92. );
  93. }