local-ttft-process.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { spawn } from "node:child_process";
  2. import { once } from "node:events";
  3. import { setTimeout as delay } from "node:timers/promises";
  4. import { fileURLToPath } from "node:url";
  5. import { build } from "esbuild";
  6. import { expect, it } from "vitest";
  7. it.skipIf(process.platform === "win32")(
  8. "受控 CLI 进程暂停标记质量,强杀不补造 terminal",
  9. async () => {
  10. const bundle = await build({
  11. stdin: {
  12. resolveDir: fileURLToPath(new URL(".", import.meta.url)),
  13. contents: `
  14. import { LocalTtftRecorder } from "../src/zcode-protocol-v4/local-ttft.ts";
  15. const recorder = new LocalTtftRecorder(undefined, undefined, (fact) => process.send({ fact }));
  16. recorder.receive({ commandId: "process-input", clientId: "client", sessionId: "session", type: "sendText", payload: {}, issuedAt: Date.now(), ttft: { version: 1, observationId: "e0b15fc2-3a50-4d73-854d-61fa148cbfd0" } }, false);
  17. recorder.admitted("process-input");
  18. process.send({ ready: true });
  19. setInterval(() => {}, 1000);
  20. `,
  21. },
  22. bundle: true,
  23. platform: "node",
  24. format: "cjs",
  25. write: false,
  26. logLevel: "silent",
  27. });
  28. const child = spawn(process.execPath, [], { stdio: ["pipe", "ignore", "pipe", "ipc"] });
  29. child.stdin!.end(bundle.outputFiles[0]!.text);
  30. const facts: Array<{ clockInvalid?: boolean; terminal?: string }> = [];
  31. try {
  32. await new Promise<void>((resolve, reject) => {
  33. child.on("error", reject);
  34. child.on("message", (message: { ready?: boolean; fact?: (typeof facts)[number] }) => {
  35. if (message.fact) facts.push(message.fact);
  36. if (message.ready) resolve();
  37. });
  38. child.once("exit", () => reject(new Error("worker exited before ready")));
  39. });
  40. child.kill("SIGSTOP");
  41. await delay(5200);
  42. child.kill("SIGCONT");
  43. await expect
  44. .poll(() => facts.some((fact) => fact.clockInvalid), { timeout: 3000 })
  45. .toBe(true);
  46. child.kill("SIGKILL");
  47. await once(child, "exit");
  48. expect(facts.some((fact) => fact.terminal !== undefined)).toBe(false);
  49. } finally {
  50. if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
  51. }
  52. },
  53. 15000,
  54. );