forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-guards.test.ts
More file actions
54 lines (49 loc) · 1.8 KB
/
Copy pathruntime-guards.test.ts
File metadata and controls
54 lines (49 loc) · 1.8 KB
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
import { describe, it, expect } from "vitest";
import { assertNonEmptyValue } from "../../src/guards/runtime-guards.js";
import { RuntimeError } from "../../src/errors/runtime-errors.js";
describe("assertNonEmptyValue", () => {
it("throws with default INVALID_TASK code for empty string", () => {
expect(() => assertNonEmptyValue("", "taskId")).toThrow(RuntimeError);
try {
assertNonEmptyValue("", "taskId");
} catch (e) {
const err = e as RuntimeError;
expect(err.code).toBe("INVALID_TASK");
expect(err.message).toContain("taskId must be a non-empty string.");
expect(err.details?.fieldName).toBe("taskId");
}
});
it("throws for whitespace-only string", () => {
expect(() => assertNonEmptyValue(" ", "name")).toThrow(RuntimeError);
try {
assertNonEmptyValue(" ", "name");
} catch (e) {
const err = e as RuntimeError;
expect(err.code).toBe("INVALID_TASK");
expect(err.details?.fieldName).toBe("name");
}
});
it("does not throw for valid padded string", () => {
expect(() => assertNonEmptyValue(" valid ", "field")).not.toThrow();
});
it("respects overridden EXECUTION_FAILED error code", () => {
try {
assertNonEmptyValue("", "taskRef", "EXECUTION_FAILED");
expect.fail("should have thrown");
} catch (e) {
const err = e as RuntimeError;
expect(err.code).toBe("EXECUTION_FAILED");
expect(err.details?.fieldName).toBe("taskRef");
}
});
it("includes fieldName in details matching input parameter", () => {
try {
assertNonEmptyValue("\t\n", "customField");
expect.fail("should have thrown");
} catch (e) {
const err = e as RuntimeError;
expect(err.details?.fieldName).toBe("customField");
expect(err.message).toContain("customField");
}
});
});