|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { ScriptCreator } from "../src/ScriptCreator"; |
| 3 | +import type { NodeWorkerFnType } from "../src/Interfaces"; |
| 4 | + |
| 5 | +const createVmFunction = <T>(content: string) => new Function("vm", content) as unknown as (() => T); |
| 6 | + |
| 7 | +describe("ScriptCreator", () => { |
| 8 | + it("createStandaloneScript includes transitive deps and required hooks", () => { |
| 9 | + const scriptCreator = new ScriptCreator(0); |
| 10 | + const workerFn: NodeWorkerFnType = { |
| 11 | + workerFn: () => ({ |
| 12 | + helper: createVmFunction<unknown>("return vm.flush();"), |
| 13 | + used: createVmFunction<unknown>("return vm.helper();"), |
| 14 | + unused: () => "drop me", |
| 15 | + flush: () => undefined, |
| 16 | + onMessageHandler: () => undefined |
| 17 | + }) |
| 18 | + }; |
| 19 | + |
| 20 | + const script = scriptCreator.createStandaloneScript(workerFn, "return 42;", ["used"]); |
| 21 | + |
| 22 | + expect(script).toContain("return 42;"); |
| 23 | + expect(script).toContain("used:"); |
| 24 | + expect(script).toContain("helper:"); |
| 25 | + expect(script).toContain("flush:"); |
| 26 | + expect(script).toContain("onMessageHandler:"); |
| 27 | + expect(script).not.toContain("unused:"); |
| 28 | + expect(script).toContain("globalThis.LocoBasicVm = vm;"); |
| 29 | + expect(script).toContain("parentPort.on('message', (data) => vm.onMessageHandler(data));"); |
| 30 | + expect(script).toMatch(/\bon\s*\(_event,/); |
| 31 | + }); |
| 32 | + |
| 33 | + it("createStandaloneScript warns for missing used instructions in debug mode", () => { |
| 34 | + const scriptCreator = new ScriptCreator(1); |
| 35 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => undefined); |
| 36 | + const workerFn: NodeWorkerFnType = { |
| 37 | + workerFn: () => ({ |
| 38 | + existing: () => undefined, |
| 39 | + flush: () => undefined, |
| 40 | + onMessageHandler: () => undefined |
| 41 | + }) |
| 42 | + }; |
| 43 | + |
| 44 | + try { |
| 45 | + scriptCreator.createStandaloneScript(workerFn, "return 0;", ["missing"]); |
| 46 | + |
| 47 | + expect(warnSpy).toHaveBeenCalledWith("Warning: Function 'missing' does not exist in vm object"); |
| 48 | + } finally { |
| 49 | + warnSpy.mockRestore(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("createStandaloneScript serializes required values/functions and prunes unused ones", () => { |
| 54 | + const scriptCreator = new ScriptCreator(0); |
| 55 | + const workerFn: NodeWorkerFnType = { |
| 56 | + workerFn: () => ({ |
| 57 | + count: 3, |
| 58 | + text: "abc", |
| 59 | + run: createVmFunction<number>("return vm.count;"), |
| 60 | + flush: () => undefined, |
| 61 | + onMessageHandler: () => undefined |
| 62 | + }) |
| 63 | + }; |
| 64 | + |
| 65 | + /* |
| 66 | + const test1 = workerFn.workerFn({ |
| 67 | + on: () => undefined, |
| 68 | + postMessage: () => undefined, |
| 69 | + }); |
| 70 | + expect(test1.count).toBe(3); |
| 71 | + */ |
| 72 | + |
| 73 | + const script = scriptCreator.createStandaloneScript(workerFn, "return 1;", ["run"]); |
| 74 | + |
| 75 | + expect(script).toContain("count: 3"); |
| 76 | + expect(script).toContain("run: function anonymous"); |
| 77 | + expect(script).not.toContain('text: "abc"'); |
| 78 | + }); |
| 79 | + |
| 80 | + it("createStandaloneScript excludes cls private underscore dependencies (special)", () => { |
| 81 | + const scriptCreator = new ScriptCreator(0); |
| 82 | + const workerFn: NodeWorkerFnType = { |
| 83 | + workerFn: () => ({ |
| 84 | + _hidden: 1, |
| 85 | + helper: () => "ok", |
| 86 | + cls: createVmFunction<string>("vm._hidden; return vm.helper();"), |
| 87 | + flush: () => undefined, |
| 88 | + onMessageHandler: () => undefined |
| 89 | + }) |
| 90 | + }; |
| 91 | + |
| 92 | + const script = scriptCreator.createStandaloneScript(workerFn, "return 0;", ["cls"]); |
| 93 | + |
| 94 | + expect(script).toContain("helper:"); |
| 95 | + expect(script).toContain("cls:"); |
| 96 | + expect(script).toContain("flush:"); |
| 97 | + expect(script).toContain("onMessageHandler:"); |
| 98 | + expect(script).not.toContain("_hidden:"); |
| 99 | + }); |
| 100 | +}); |
0 commit comments