|
1 | 1 | import { beforeEach, describe, expect, it } from "vitest"; |
2 | | -import { CelProgram } from "../src/index.js"; |
| 2 | +import { CelProgram, evaluate } from "../src/index.js"; |
| 3 | + |
| 4 | +describe("evaluate", () => { |
| 5 | + it("should evaluate a simple expression", async () => { |
| 6 | + const result = await evaluate("size(message) > 5", { |
| 7 | + message: "Hello World", |
| 8 | + }); |
| 9 | + expect(result).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should handle errors gracefully", async () => { |
| 13 | + await expect(evaluate("invalid expression", {})).rejects.toThrow(); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should handle CEL map return values", async () => { |
| 17 | + const result = await evaluate( |
| 18 | + '{"name": "test", "items": [1, 2, 3].map(i, {"id": i})}', |
| 19 | + {}, |
| 20 | + ); |
| 21 | + expect(result).toEqual({ |
| 22 | + name: "test", |
| 23 | + items: [{ id: 1 }, { id: 2 }, { id: 3 }], |
| 24 | + }); |
| 25 | + }); |
| 26 | +}); |
3 | 27 |
|
4 | 28 | describe("CelProgram", () => { |
5 | 29 | it("should evaluate a simple expression", async () => { |
@@ -94,39 +118,44 @@ describe("Performance measurements", () => { |
94 | 118 |
|
95 | 119 | it("should measure compile vs execute time", async () => { |
96 | 120 | // Complex expression that requires significant parsing |
97 | | - const expr = 'has(items) && items.map(i, i.price).filter(p, p < max_price).size() > 0'; |
| 121 | + const expr = |
| 122 | + "has(items) && items.map(i, i.price).filter(p, p < max_price).size() > 0"; |
98 | 123 | const context = { |
99 | 124 | items: Array.from({ length: 100 }, (_, i) => ({ id: i, price: i * 10 })), |
100 | | - max_price: 500 |
| 125 | + max_price: 500, |
101 | 126 | }; |
102 | 127 |
|
103 | 128 | // Measure compilation time |
104 | | - const [program, compileTime] = await measureTime(() => |
105 | | - CelProgram.compile(expr) |
| 129 | + const [program, compileTime] = await measureTime(() => |
| 130 | + CelProgram.compile(expr), |
106 | 131 | ); |
107 | 132 | console.log(`Compilation took ${compileTime.toFixed(0)} nanoseconds`); |
108 | 133 |
|
109 | 134 | // Measure execution time |
110 | 135 | const [result, executeTime] = await measureTime(() => |
111 | | - program.execute(context) |
| 136 | + program.execute(context), |
112 | 137 | ); |
113 | 138 | console.log(`Execution took ${executeTime.toFixed(0)} nanoseconds`); |
114 | 139 |
|
115 | 140 | // Measure one-step evaluation time |
116 | 141 | const [, evaluateTime] = await measureTime(() => |
117 | | - CelProgram.evaluate(expr, context) |
| 142 | + CelProgram.evaluate(expr, context), |
| 143 | + ); |
| 144 | + console.log( |
| 145 | + `One-step evaluation took ${evaluateTime.toFixed(0)} nanoseconds`, |
118 | 146 | ); |
119 | | - console.log(`One-step evaluation took ${evaluateTime.toFixed(0)} nanoseconds`); |
120 | 147 |
|
121 | 148 | // Basic sanity check that the timing data is reasonable |
122 | 149 | expect(compileTime).toBeGreaterThan(0); |
123 | 150 | expect(executeTime).toBeGreaterThan(0); |
124 | 151 | expect(evaluateTime).toBeGreaterThan(0); |
125 | | - |
| 152 | + |
126 | 153 | // The one-step evaluation should be approximately the sum of compile and execute |
127 | 154 | const tolerance = 0.5; // Allow 50% variation due to system noise |
128 | 155 | const expectedEvaluateTime = compileTime + executeTime; |
129 | | - expect(evaluateTime).toBeGreaterThan(expectedEvaluateTime * (1 - tolerance)); |
| 156 | + expect(evaluateTime).toBeGreaterThan( |
| 157 | + expectedEvaluateTime * (1 - tolerance), |
| 158 | + ); |
130 | 159 | expect(evaluateTime).toBeLessThan(expectedEvaluateTime * (1 + tolerance)); |
131 | 160 | }); |
132 | 161 | }); |
|
0 commit comments