Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

wasm-location.test.ts

70 lines2.1 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2​
3const quickJSMocks = vi.hoisted(() => {
4 const contextError = new Error('context created')
5 const releaseVariant = { type: 'sync' }
6​
7 return {
8 contextError,
9 releaseVariant,
10 getQuickJS: vi.fn(),
11 newVariant: vi.fn(() => releaseVariant),
12 newQuickJSWASMModule: vi.fn(async () => ({
13 newContext: () => {
14 throw contextError
15 },
16 })),
17 }
18})
19​
20vi.mock('quickjs-emscripten', () => ({
21 getQuickJS: quickJSMocks.getQuickJS,
22 newQuickJSWASMModule: quickJSMocks.newQuickJSWASMModule,
23 newVariant: quickJSMocks.newVariant,
24 RELEASE_SYNC: quickJSMocks.releaseVariant,
25}))
26​
27import { createQuickJSIsolateDriver } from '../src/isolate-driver'
28​
29describe('QuickJS WASM loading', () => {
30 beforeEach(() => {
31 vi.clearAllMocks()
32 })
33​
34 it('uses the shared QuickJS module by default', async () => {
35 quickJSMocks.getQuickJS.mockResolvedValue({
36 newContext: () => {
37 throw quickJSMocks.contextError
38 },
39 })
40 const driver = createQuickJSIsolateDriver()
41​
42 await expect(driver.createContext({ bindings: {} })).rejects.toThrow(
43 quickJSMocks.contextError,
44 )
45​
46 expect(quickJSMocks.getQuickJS).toHaveBeenCalledOnce()
47 expect(quickJSMocks.newVariant).not.toHaveBeenCalled()
48 expect(quickJSMocks.newQuickJSWASMModule).not.toHaveBeenCalled()
49 })
50​
51 it('loads a custom WASM location once per driver', async () => {
52 const wasmLocation = 'https://cdn.example.com/quickjs.wasm'
53 const driver = createQuickJSIsolateDriver({ wasmLocation })
54​
55 await expect(driver.createContext({ bindings: {} })).rejects.toThrow(
56 quickJSMocks.contextError,
57 )
58 await expect(driver.createContext({ bindings: {} })).rejects.toThrow(
59 quickJSMocks.contextError,
60 )
61​
62 expect(quickJSMocks.getQuickJS).not.toHaveBeenCalled()
63 expect(quickJSMocks.newVariant).toHaveBeenCalledOnce()
64 expect(quickJSMocks.newVariant).toHaveBeenCalledWith(
65 quickJSMocks.releaseVariant,
66 { wasmLocation },
67 )
68 expect(quickJSMocks.newQuickJSWASMModule).toHaveBeenCalledOnce()
69 })
70})