sshControlCommandTimeout.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. const control = vi.hoisted(() => ({ exec: vi.fn(), end: vi.fn(), destroy: vi.fn() }));
  3. vi.mock("ssh2", () => ({
  4. Client: class {
  5. once(event: string, handler: () => void) {
  6. if (event === "ready") queueMicrotask(handler);
  7. return this;
  8. }
  9. connect() {}
  10. exec = control.exec;
  11. end = control.end;
  12. destroy = control.destroy;
  13. },
  14. }));
  15. vi.mock("./e2e/helpers/desktop-app.js", () => ({}));
  16. vi.mock("./e2e/helpers/custom-openai-provider-store.js", () => ({}));
  17. vi.mock("./e2e/helpers/v4-conversation.js", () => ({}));
  18. import { runSSHCommand } from "./e2e/helpers/ssh-remote-p0.js";
  19. afterEach(() => {
  20. vi.useRealTimers();
  21. vi.clearAllMocks();
  22. });
  23. describe("SSH 控制命令截止", () => {
  24. it("握手成功但 exec ACK 永不返回时仍然超时,迟到 channel 被销毁", async () => {
  25. vi.useFakeTimers();
  26. let failure = "";
  27. void runSSHCommand(
  28. { host: "fixture", port: 22, username: "dev", password: "fixture" },
  29. "probe",
  30. { timeoutMs: 1000 },
  31. ).catch((error: Error) => {
  32. failure = error.message;
  33. });
  34. await vi.advanceTimersByTimeAsync(1100);
  35. expect(failure).toContain("SSH 控制命令超过");
  36. expect(control.destroy).toHaveBeenCalledOnce();
  37. expect(control.end).toHaveBeenCalledOnce();
  38. const channel = { destroy: vi.fn() };
  39. control.exec.mock.calls[0]![1](null, channel);
  40. expect(channel.destroy).toHaveBeenCalledOnce();
  41. });
  42. });