output-collector-tail.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * 请求级尾部(`ExecutionOutputLimit.tailBytes` → `ExecutionStreamResult.tail`)。
  3. *
  4. * 消费方是 dwf 的 `world.run(..., { output: { tail: n } })`:它要的是「不限总输出、只留最后 n
  5. * 字节」。所以这里钉住三件事:尾部与 inline 截断互不影响;进程照常读到退出、不因 inline 截断
  6. * 失败;没请求就不出现 `tail` 字段(老调用方的结果形状不变)。
  7. */
  8. import { mkdtemp, rm } from "node:fs/promises";
  9. import { tmpdir } from "node:os";
  10. import { join } from "node:path";
  11. import { describe, expect, it } from "vitest";
  12. import { NodeExecutionAdapter } from "../src/exec/index.js";
  13. import { OutputCollector } from "../src/exec/output-collector.js";
  14. function collector(options: { maxInlineBytes: number; requestedTailBytes?: number }) {
  15. return new OutputCollector({
  16. maxInlineBytes: options.maxInlineBytes,
  17. maxPersistedBytes: 0,
  18. legacyOutputEncoding: null,
  19. maxTailBytes: 0,
  20. persistOutput: "none",
  21. requestedTailBytes: options.requestedTailBytes,
  22. });
  23. }
  24. describe("OutputCollector — requested tail", () => {
  25. it("keeps the last n bytes across many chunks, independent of the inline cap", () => {
  26. const out = collector({ maxInlineBytes: 0, requestedTailBytes: 5 });
  27. for (const piece of ["abc", "defg", "", "hijklmnop", "q"]) out.append(Buffer.from(piece));
  28. const result = out.result();
  29. expect(result).toMatchObject({ text: "", bytes: 17, truncated: true, tail: "mnopq" });
  30. });
  31. it("returns the whole stream when it is shorter than n", () => {
  32. const out = collector({ maxInlineBytes: 0, requestedTailBytes: 64 });
  33. out.append(Buffer.from("short"));
  34. expect(out.result().tail).toBe("short");
  35. });
  36. it("cuts a single chunk larger than n", () => {
  37. const out = collector({ maxInlineBytes: 0, requestedTailBytes: 3 });
  38. out.append(Buffer.from("0123456789"));
  39. expect(out.result().tail).toBe("789");
  40. });
  41. it("is an empty string for an empty stream when requested", () => {
  42. expect(collector({ maxInlineBytes: 0, requestedTailBytes: 8 }).result().tail).toBe("");
  43. });
  44. it("leaves the result shape unchanged when no tail was requested", () => {
  45. const out = collector({ maxInlineBytes: 16 });
  46. out.append(Buffer.from("hello"));
  47. expect(out.result()).not.toHaveProperty("tail");
  48. });
  49. });
  50. describe("NodeExecutionAdapter — requested tail", () => {
  51. it("runs a large-output process to exit with no inline text and the tail of each stream", async () => {
  52. const root = await mkdtemp(join(tmpdir(), "zcode-exec-tail-"));
  53. const adapter = new NodeExecutionAdapter({ outputRootDir: root });
  54. try {
  55. // 1 MiB 的 stdout 加尾标记、stderr 同理,然后非零退出:inline 为 0 时只能从 tail 拿到尾巴。
  56. const script = [
  57. "process.stdout.write('x'.repeat(1024 * 1024) + 'OUT-END');",
  58. "process.stderr.write('y'.repeat(1024 * 1024) + 'ERR-END');",
  59. "process.exitCode = 3;",
  60. ].join("");
  61. const result = await adapter.run({
  62. command: { mode: "argv", file: process.execPath, args: ["-e", script] },
  63. outputLimit: { maxInlineBytes: 0, tailBytes: 7 },
  64. });
  65. expect(result.status).toBe("failed");
  66. expect(result.error).toBeUndefined();
  67. expect(result.exitCode).toBe(3);
  68. expect(result.stdout).toMatchObject({ text: "", truncated: true, tail: "OUT-END" });
  69. expect(result.stdout.bytes).toBe(1024 * 1024 + 7);
  70. expect(result.stderr).toMatchObject({ text: "", truncated: true, tail: "ERR-END" });
  71. } finally {
  72. await adapter.close();
  73. await rm(root, { recursive: true, force: true });
  74. }
  75. });
  76. });