File size: 1,955 Bytes
fcd8223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { describe, expect, it } from "vitest";
import { coerceNodeInvokeInputPayload, coerceNodeInvokePayload } from "./invoke-payload.js";

describe("coerceNodeInvokePayload", () => {
  it.each([
    ["preserves the exact owning session", "agent:main:managed", "agent:main:managed"],
    ["normalizes the owning session", "  agent:main:managed  ", "agent:main:managed"],
    ["omits an absent owning session", undefined, undefined],
    ["omits a blank owning session", "  ", undefined],
    ["omits a non-string owning session", 42, undefined],
  ])("%s", (_name, sessionKey, expectedSessionKey) => {
    expect(
      coerceNodeInvokePayload({
        id: "invoke-1",
        nodeId: "node-1",
        command: "plugin.workspace",
        sessionKey,
      }),
    ).toEqual({
      id: "invoke-1",
      nodeId: "node-1",
      command: "plugin.workspace",
      paramsJSON: null,
      timeoutMs: null,
      idempotencyKey: null,
      ...(expectedSessionKey ? { sessionKey: expectedSessionKey } : {}),
    });
  });
});

describe("coerceNodeInvokeInputPayload", () => {
  it("accepts a bounded well-formed input payload", () => {
    expect(
      coerceNodeInvokeInputPayload({
        id: "invoke-1",
        nodeId: "node-1",
        seq: 0,
        payloadJSON: JSON.stringify({ kind: "data", data: "keys" }),
      }),
    ).toEqual({
      invokeId: "invoke-1",
      nodeId: "node-1",
      seq: 0,
      payloadJSON: JSON.stringify({ kind: "data", data: "keys" }),
    });
  });

  it.each([
    [
      "oversized payloadJSON",
      { id: "i", nodeId: "n", seq: 0, payloadJSON: "x".repeat(16 * 1024 + 1) },
    ],
    ["negative seq", { id: "i", nodeId: "n", seq: -1, payloadJSON: "{}" }],
    ["fractional seq", { id: "i", nodeId: "n", seq: 0.5, payloadJSON: "{}" }],
    ["array frame", []],
    ["string frame", "input"],
  ])("rejects %s", (_name, payload) => {
    expect(coerceNodeInvokeInputPayload(payload)).toBeNull();
  });
});