Skip to content
This repository was archived by the owner on Dec 30, 2025. It is now read-only.

Commit a058e26

Browse files
committed
chore: add payload tests
1 parent 37ed59e commit a058e26

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

test/payload.test.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import test from "node:test";
2+
import assert from "node:assert";
3+
import { ACT, stringify, parse } from "../src/payload.js";
4+
5+
test("ACT enum has correct values", () => {
6+
assert.strictEqual(ACT.GET, 1);
7+
assert.strictEqual(ACT.SET, 2);
8+
assert.strictEqual(ACT.ADD, 3);
9+
assert.strictEqual(ACT.DEL, 4);
10+
assert.strictEqual(ACT.GL, 5);
11+
assert.strictEqual(ACT.GS, 6);
12+
assert.strictEqual(ACT.OP, 7);
13+
assert.strictEqual(ACT.CGI, 8);
14+
});
15+
16+
test("stringify single action with array attributes", () => {
17+
const result = stringify([[ACT.GET, "some_oid", ["attr1=value1", "attr2=value2"]]]);
18+
19+
assert.strictEqual(
20+
result,
21+
"1\r\n[some_oid#0,0,0,0,0,0#0,0,0,0,0,0]0,2\r\nattr1=value1\r\nattr2=value2\r\n"
22+
);
23+
});
24+
25+
test("stringify single action with object attributes", () => {
26+
const result = stringify([[ACT.SET, "some_oid", { key1: "val1", key2: "val2" }]]);
27+
28+
assert.strictEqual(
29+
result,
30+
"2\r\n[some_oid#0,0,0,0,0,0#0,0,0,0,0,0]0,2\r\nkey1=val1\r\nkey2=val2\r\n"
31+
);
32+
});
33+
34+
test("stringify action with custom stack values", () => {
35+
const result = stringify([[ACT.GET, "oid", [], "1,2,3,4,5,6", "7,8,9,10,11,12"]]);
36+
37+
assert.strictEqual(result, "1\r\n[oid#1,2,3,4,5,6#7,8,9,10,11,12]0,0\r\n");
38+
});
39+
40+
test("stringify multiple actions", () => {
41+
const result = stringify([
42+
[ACT.GET, "oid1", ["a=1"]],
43+
[ACT.SET, "oid2", ["b=2"]],
44+
]);
45+
46+
assert.strictEqual(
47+
result,
48+
"1&2\r\n[oid1#0,0,0,0,0,0#0,0,0,0,0,0]0,1\r\na=1\r\n[oid2#0,0,0,0,0,0#0,0,0,0,0,0]1,1\r\nb=2\r\n"
49+
);
50+
});
51+
52+
test("stringify action with no attributes", () => {
53+
const result = stringify([[ACT.GET, "simple_oid"]]);
54+
55+
assert.strictEqual(result, "1\r\n[simple_oid#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n");
56+
});
57+
58+
test("parse simple response with attributes", () => {
59+
const data = "[some_stack]0\nattr1=value1\nattr2=value2";
60+
const result = parse(data);
61+
62+
assert.strictEqual(result.error, null);
63+
assert.strictEqual(result.actions.length, 1);
64+
assert.strictEqual(result.actions[0].stack, "some_stack");
65+
assert.strictEqual(result.actions[0].actionIndex, 0);
66+
assert.deepStrictEqual(result.actions[0].attributes, {
67+
attr1: "value1",
68+
attr2: "value2",
69+
});
70+
});
71+
72+
test("parse response with error section", () => {
73+
const data = "[error]5";
74+
const result = parse(data);
75+
76+
assert.strictEqual(result.error, 5);
77+
assert.strictEqual(result.actions.length, 0);
78+
});
79+
80+
test("parse response with cgi script", () => {
81+
const data = "[cgi]0\nconsole.log('hello');\nvar x = 1;";
82+
const result = parse(data);
83+
84+
assert.strictEqual(result.error, null);
85+
assert.strictEqual(result.actions[0].stack, "cgi");
86+
assert.strictEqual(result.actions[0].script, "console.log('hello');\nvar x = 1;\n");
87+
});
88+
89+
test("parse response with multiple sections for same action", () => {
90+
const data = "[stack1]0\na=1\n[stack2]0\nb=2";
91+
const result = parse(data);
92+
93+
assert.strictEqual(result.error, null);
94+
assert.ok(Array.isArray(result.actions[0]));
95+
assert.strictEqual(result.actions[0].length, 2);
96+
assert.deepStrictEqual(result.actions[0][0].attributes, { a: "1" });
97+
assert.deepStrictEqual(result.actions[0][1].attributes, { b: "2" });
98+
});
99+
100+
test("parse response with gaps in action indices", () => {
101+
const data = "[stack]2\na=1";
102+
const result = parse(data);
103+
104+
assert.strictEqual(result.actions.length, 3);
105+
assert.strictEqual(result.actions[0].actionIndex, 0);
106+
assert.strictEqual(result.actions[1].actionIndex, 1);
107+
assert.strictEqual(result.actions[2].actionIndex, 2);
108+
assert.deepStrictEqual(result.actions[2].attributes, { a: "1" });
109+
});
110+
111+
test("parse attribute with equals sign in value", () => {
112+
const data = "[stack]0\nkey=value=with=equals";
113+
const result = parse(data);
114+
115+
assert.strictEqual(result.actions[0].attributes.key, "value=with=equals");
116+
});
117+
118+
test("stringify and parse roundtrip preserves structure", () => {
119+
const original = [[ACT.GET, "test_oid", { foo: "bar", num: "123" }]];
120+
const stringified = stringify(original);
121+
122+
// The response format is different from request format,
123+
// but we can verify the attributes are preserved in a response-like format
124+
const responseFormat = "[test_oid#0,0,0,0,0,0#0,0,0,0,0,0]0\nfoo=bar\nnum=123";
125+
const parsed = parse(responseFormat);
126+
127+
assert.deepStrictEqual(parsed.actions[0].attributes, { foo: "bar", num: "123" });
128+
});

0 commit comments

Comments
 (0)