-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.test.ts
More file actions
36 lines (28 loc) · 1.23 KB
/
cache.test.ts
File metadata and controls
36 lines (28 loc) · 1.23 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
import { buildEvaluateFn, testGetScriptCacheSize } from "../evaluate";
import type { ScreenContextDefinition } from "../../state";
const importScript = `function shared(x){return x+1}; const sharedConst=42;`;
const globalScript1 = `const unique=10; function calc(){return shared(unique)+sharedConst}`;
const globalScript2 = `const unique=20; function calc(){return shared(unique)+sharedConst}`;
// construct a minimal ScreenContextDefinition subset that buildEvaluateFn expects
const makeScreen = (global: string): Partial<ScreenContextDefinition> => ({
model: {
id: "test",
name: "test",
body: { name: "Row", properties: {} },
importedScripts: importScript,
global,
},
});
it("caches import script only once across multiple screens", () => {
const before = testGetScriptCacheSize();
const fn1 = buildEvaluateFn(makeScreen(globalScript1), "calc()");
fn1();
const fn2 = buildEvaluateFn(makeScreen(globalScript2), "calc()");
fn2();
const after = testGetScriptCacheSize();
// cache should have grown by exactly 3 entries: 1 import + 2 globals
expect(after - before).toBe(3);
// validating evaluated results
expect(fn1() as number).toBe(53); // 10 + 1 + 42
expect(fn2() as number).toBe(63); // 20 + 1 + 42
});