world-read-input.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // world-read / world-run 节点的有界输入(docs/dynamic-workflow/transcript-and-notifications.md):
  2. // 小输入原样、原类型;超限逐项截成预览并置 truncated;不可序列化的实参走同一条预览路。
  3. import { describe, expect, it } from "vitest";
  4. import { WORLD_READ_INPUT_MAX_BYTES } from "../../src/engine/types.js";
  5. import { boundWorldReadInput } from "../../src/engine/world-read-input.js";
  6. describe("boundWorldReadInput", () => {
  7. it("keeps a small input verbatim, argument types included", () => {
  8. const input = boundWorldReadInput("run", ["pnpm", ["vitest", "run"], { timeoutMs: 60_000 }]);
  9. expect(input).toEqual({ op: "run", args: ["pnpm", ["vitest", "run"], { timeoutMs: 60_000 }] });
  10. expect("truncated" in input).toBe(false);
  11. });
  12. it("copies the args array so later mutation by the caller cannot leak into the journal", () => {
  13. const args: unknown[] = ["src/a.ts"];
  14. const input = boundWorldReadInput("read", args);
  15. args.push("mutated");
  16. expect(input.args).toEqual(["src/a.ts"]);
  17. });
  18. it("truncates an oversized input into per-argument previews and says so", () => {
  19. const code = "x".repeat(WORLD_READ_INPUT_MAX_BYTES * 4);
  20. const input = boundWorldReadInput("run", ["node", ["-e", code], { timeoutMs: 1 }]);
  21. expect(input.op).toBe("run");
  22. expect(input.truncated).toBe(true);
  23. expect(input.args).toHaveLength(3);
  24. expect(input.args[0]).toBe("node");
  25. // 数组实参变成 JSON 预览、切尾加省略号;序列化后整体落在上限之内。
  26. expect(typeof input.args[1]).toBe("string");
  27. expect((input.args[1] as string).endsWith("…")).toBe(true);
  28. expect(new TextEncoder().encode(JSON.stringify(input)).length).toBeLessThanOrEqual(
  29. WORLD_READ_INPUT_MAX_BYTES,
  30. );
  31. });
  32. it("caps the number of previewed arguments", () => {
  33. const many = Array.from({ length: 40 }, (_, i) => "a".repeat(300) + i);
  34. const input = boundWorldReadInput("run", many);
  35. expect(input.truncated).toBe(true);
  36. expect(input.args.length).toBeLessThanOrEqual(8);
  37. });
  38. it("survives an unserialisable argument by falling back to previews", () => {
  39. const cyclic: Record<string, unknown> = {};
  40. cyclic.self = cyclic;
  41. const input = boundWorldReadInput("run", ["cmd", cyclic]);
  42. expect(input.truncated).toBe(true);
  43. expect(input.args[0]).toBe("cmd");
  44. expect(typeof input.args[1]).toBe("string");
  45. });
  46. });