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
68 lines (59 loc) · 2.12 KB
/
Copy pathruntime-guards.test.ts
File metadata and controls
68 lines (59 loc) · 2.12 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { describe, expect, it } from "vitest";
import {
assertMaxToolCalls,
assertNonEmptyValue,
assertRuntimeStarted,
RuntimeError
} from "../src/index.js";
describe("runtime guards", () => {
describe("assertMaxToolCalls", () => {
it("allows tool call when count is strictly below the limit", () => {
expect(() => assertMaxToolCalls(0, 3)).not.toThrow();
expect(() => assertMaxToolCalls(2, 3)).not.toThrow();
});
it("throws MAX_TOOL_CALLS_EXCEEDED when count reaches or exceeds the limit", () => {
expect(() => assertMaxToolCalls(3, 3)).toThrowError(RuntimeError);
try {
assertMaxToolCalls(3, 3);
} catch (error) {
expect(error).toBeInstanceOf(RuntimeError);
const runtimeErr = error as RuntimeError;
expect(runtimeErr.code).toBe("MAX_TOOL_CALLS_EXCEEDED");
expect(runtimeErr.message).toContain(
"maximum allowed tool calls limit of 3"
);
expect(runtimeErr.details).toEqual({
currentToolCalls: 3,
maxToolCalls: 3
});
}
});
it("throws when maxToolCalls is 0 on any invocation attempt", () => {
expect(() => assertMaxToolCalls(0, 0)).toThrowError(RuntimeError);
});
it("does not throw when maxToolCalls is negative (unrestricted / disabled)", () => {
expect(() => assertMaxToolCalls(10, -1)).not.toThrow();
});
});
describe("assertRuntimeStarted", () => {
it("passes when runtime is started", () => {
expect(() => assertRuntimeStarted(true)).not.toThrow();
});
it("throws RUNTIME_NOT_STARTED when false", () => {
expect(() => assertRuntimeStarted(false)).toThrowError(RuntimeError);
});
});
describe("assertNonEmptyValue", () => {
it("passes on non-empty string", () => {
expect(() => assertNonEmptyValue("valid", "testField")).not.toThrow();
});
it("throws on empty or whitespace string", () => {
expect(() => assertNonEmptyValue("", "testField")).toThrowError(
RuntimeError
);
expect(() => assertNonEmptyValue(" ", "testField")).toThrowError(
RuntimeError
);
});
});
});