confirmedProcess.test.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { expect, it } from "vitest";
  2. import { runConfirmedProcess } from "#e2e/helpers/confirmed-process";
  3. it("读取两个真实提示后分别确认;提示可跨 stdout chunk,不使用固定等待", async () => {
  4. const code = `
  5. process.stdout.write("First confirmation: ");
  6. process.stdin.once("data", (first) => {
  7. if (String(first) !== "DELETE\\n") process.exit(11);
  8. process.stdout.write("Second ");
  9. setImmediate(() => process.stdout.write("confirmation: "));
  10. process.stdin.once("data", (second) => {
  11. if (String(second) !== "DELETE\\n") process.exit(12);
  12. process.stdout.write('{"uninstalled":true}');
  13. process.stdin.pause();
  14. });
  15. });
  16. `;
  17. const result = await runConfirmedProcess(process.execPath, ["-e", code], {
  18. prompts: ["First confirmation: ", "Second confirmation: "],
  19. answer: "DELETE",
  20. timeoutMs: 5000,
  21. });
  22. expect(result.stdout).toContain('"uninstalled":true');
  23. });
  24. it("进程未完成所有确认即退出不能报告卸载成功", async () => {
  25. await expect(
  26. runConfirmedProcess(process.execPath, ["-e", 'process.stdout.write("early exit")'], {
  27. prompts: ["Confirm"],
  28. answer: "DELETE",
  29. timeoutMs: 5000,
  30. }),
  31. ).rejects.toThrow("确认 0/1");
  32. });