sshLifecycleProjectDraft.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  2. import {
  3. connectSSHWorkspaceAndSendFirstTurn,
  4. listRemoteAgentProcesses,
  5. } from "#e2e/helpers/ssh-remote-lifecycle";
  6. const state = vi.hoisted(() => {
  7. const sent: Array<{ identity: string; model: string }> = [];
  8. return {
  9. identity: "",
  10. model: "local-default",
  11. localDraftCreations: 0,
  12. remoteIdentity: "remote:server:fixture:/work",
  13. sent,
  14. runSSHCommand: vi.fn(),
  15. };
  16. });
  17. vi.mock("#e2e/helpers/project-sidebar", () => ({}));
  18. vi.mock("#e2e/helpers/desktop-app", () => ({ waitForWorkspaceApp: vi.fn() }));
  19. vi.mock("#e2e/helpers/custom-openai-provider", () => ({}));
  20. vi.mock("#e2e/helpers/upstream-provider", () => ({
  21. selectUpstreamProviderModelById: async (model: string) => {
  22. state.model = model;
  23. },
  24. selectUpstreamThoughtLevelValue: vi.fn(),
  25. }));
  26. vi.mock("#e2e/helpers/conversation-session-network", () => ({
  27. getUpstreamRequestRecordCount: async () => state.sent.length,
  28. waitForUpstreamRequest: vi.fn(),
  29. }));
  30. vi.mock("#e2e/helpers/v4-conversation", () => ({
  31. // 全局新建按产品合同切换到本地未分类任务,不能冒充远程项目内新建。
  32. startNewV4Draft: async () => {
  33. state.localDraftCreations += 1;
  34. state.identity = "";
  35. state.model = "local-default";
  36. },
  37. sendV4PromptAndWaitAccepted: async () => {
  38. state.sent.push({ identity: state.identity, model: state.model });
  39. },
  40. waitForV4ConversationState: async (predicate: (value: unknown) => boolean) => {
  41. const snapshot = {
  42. taskId: state.identity ? "remote-task" : "local-task",
  43. state: "idle",
  44. queueCount: 0,
  45. };
  46. expect(predicate(snapshot)).toBe(true);
  47. return snapshot;
  48. },
  49. waitForV4AssistantMessageContaining: vi.fn(),
  50. }));
  51. vi.mock("#e2e/helpers/ssh-remote-p0", () => ({
  52. SSH_WORKSPACE_READY_TIMEOUT_MS: 1_000,
  53. runSSHCommand: (...args: unknown[]) => state.runSSHCommand(...args),
  54. clickLastEnabledSSHDialogButton: vi.fn(),
  55. fillSSHPasswordSettings: vi.fn(),
  56. openSSHRemoteDialog: vi.fn(),
  57. ensureSSHProjectDirectoryStep: vi.fn(),
  58. selectSSHRemoteDirectory: async () => {
  59. state.identity = state.remoteIdentity;
  60. },
  61. waitForRemoteTab: async () => ({
  62. remoteKind: "ssh",
  63. remoteHost: "127.0.0.1",
  64. remoteSessionId: "attachment",
  65. secretPersistedInTab: false,
  66. workspaceIdentity: state.remoteIdentity,
  67. }),
  68. readRemoteTabSnapshot: async () => ({
  69. activeWorkspaceIdentity: state.identity || null,
  70. activeWorkspacePath: state.identity ? "/work" : "/local",
  71. }),
  72. }));
  73. beforeEach(() => {
  74. vi.stubGlobal("expect", expect);
  75. state.identity = "";
  76. state.model = "local-default";
  77. state.localDraftCreations = 0;
  78. state.sent.length = 0;
  79. });
  80. afterEach(() => vi.unstubAllGlobals());
  81. it("远程项目首发保留新建项目草稿和已选模型,不跳到全局本地任务", async () => {
  82. const task = await connectSSHWorkspaceAndSendFirstTurn({
  83. caseMarker: "E2E_REMOTE_DRAFT",
  84. config: { host: "127.0.0.1", port: 43210, username: "dev", password: "fixture" },
  85. label: "A",
  86. workspacePath: "/work",
  87. provider: {
  88. id: "fixture-provider",
  89. name: "Fixture provider",
  90. tunnel: { baseURL: "http://127.0.0.1:43211", close: async () => {} },
  91. },
  92. });
  93. expect(state.sent).toEqual([
  94. { identity: state.remoteIdentity, model: "e2e-ssh-remote-lifecycle-model" },
  95. ]);
  96. expect(task.taskId).toBe("remote-task");
  97. expect(task.identity).toBe(state.remoteIdentity);
  98. expect(state.localDraftCreations).toBe(0);
  99. });
  100. it("冷恢复只终止 Core 所属 Agent,不把查询 shell 或插件进程当成 Agent", async () => {
  101. const ps = [
  102. "146 135 /runtime/node /runtime/server-core.js 1",
  103. "174 146 zcode-cli",
  104. "180 146 /runtime/node /runtime/zcode.cjs app-server --stdio",
  105. "236 174 zcode-cli",
  106. "316 314 sh -c ps -eo pid,args | awk '/app-server --stdio/'",
  107. ].join("\n");
  108. const detailCommands: string[] = [];
  109. state.runSSHCommand.mockImplementation(async (_config, command: string) => {
  110. if (command === "ps -eo pid=,ppid=,args=") return { code: 0, stdout: ps, stderr: "" };
  111. detailCommands.push(command);
  112. // 真实进程快照:旧正则仅匹配到查询 shell;新路径须按已验证的 PID 读取 /proc。
  113. const pids = command
  114. .match(/^for pid in ([\d ]+);/)?.[1]
  115. ?.trim()
  116. .split(" ") ?? ["316"];
  117. return {
  118. code: 0,
  119. stdout: pids.map((pid) => `${pid}\t146\t1000\t/home/dev`).join("\n"),
  120. stderr: "",
  121. };
  122. });
  123. expect(
  124. await listRemoteAgentProcesses({
  125. host: "127.0.0.1",
  126. port: 22,
  127. username: "dev",
  128. password: "fixture",
  129. }),
  130. ).toEqual([
  131. { pid: 174, ppid: 146, startTicks: 1000, cwd: "/home/dev" },
  132. { pid: 180, ppid: 146, startTicks: 1000, cwd: "/home/dev" },
  133. ]);
  134. expect(detailCommands).toHaveLength(1);
  135. });