Files
deepseek-harness/packages/experimental/webworker-runtime/tests/node/process-shim.spec.ts
T
imccyu 93d2ad2d25 feat(webworker): boot the preview from the shipped profile and fetch lazy chunks on demand
The packer images the web template bundles, every optional bundle, and an installation manifest instead of a dumped composition, and the Worker boots that profile through app-boot like dsh web. Package-local lazy client chunks move into content-addressed archives that the VFS fetches on first asynchronous read; client-modules reads chunks asynchronously.

The Worker replaces node-addon-require-builtin with Node v24.18.0 internal-module interfaces and reports Node 24, so plugin metadata, the Cordis Loader, and HMR reach one cascaded loader backed by the Worker loader. Resolution failures carry Node error codes, the empty load cache lets HMR apply profile changes, and every optional bundle switches on and off live. Platform stubs throw ERR_FEATURE_UNAVAILABLE_ON_PLATFORM.
2026-10-15 11:30:45 +08:00

58 lines
2.6 KiB
TypeScript

/**
* The worker's process shim: the layout-derived environment and the Node 22
* `getBuiltinModule` face, which must answer the loader's module proxies for
* builtin ids and undefined for everything else — never an image resolution.
*/
import { afterEach, describe, expect, it } from 'vitest'
import { installProcessGlobal } from '../../src/node/globals/process.ts'
import { setActiveModuleLoader, WorkerModuleLoader } from '../../src/module-system/module-loader.ts'
import { MemoryVfs } from '../../src/storage/memory.ts'
import { spawnSync } from '../../src/node/builtin_modules/implemented/child_process.ts'
const realProcess = globalThis.process
afterEach(() => {
;(globalThis as { process: unknown }).process = realProcess
})
describe('process shim', () => {
it('publishes cwd, env, and the Node release its internal modules follow', () => {
const shim = installProcessGlobal({ cwd: '/dsh', env: { DSH_HOME: '/dsh/home' } })
expect(shim.cwd()).toBe('/dsh')
expect(shim.env.DSH_HOME).toBe('/dsh/home')
expect(shim.title).toBe('dsh-webworker')
// The vendored Loader reaches Node internals from Node 22 on; execArgv carries no --expose-internals.
expect(shim.versions.node).toBe('24.18.0')
expect(shim.execArgv).toEqual([])
})
it('exposes an executable identity without enabling Node programs', () => {
const shim = installProcessGlobal({ cwd: '/dsh', env: {} })
expect(shim.execPath).toBe('/dsh/bin/node')
// No entry script: consumers that read argv[1] as the main module find none.
expect(shim.argv).toEqual([shim.execPath])
expect(spawnSync(shim.execPath, ['--eval', 'throw new Error("must not execute")']).error?.code).toBe('ENOENT')
})
it('answers getBuiltinModule from the module proxies and undefined otherwise', () => {
const fs = { marker: 'fs-proxy' }
// The table holds factories, and a builtin must keep one identity across
// requires (`instanceof`, `Buffer.isBuffer`), so this one answers with the
// same object every time.
const factory = (): unknown => fs
const vfs = new MemoryVfs()
vfs.seedDirectory('/dsh')
const loader = new WorkerModuleLoader({
vfs,
root: '/dsh',
staticModules: { 'node:fs': factory, 'fs': factory },
})
setActiveModuleLoader(loader)
const shim = installProcessGlobal({ cwd: '/dsh', env: {} })
// The shim calls the factory: a caller receives the module, never the thunk.
expect(shim.getBuiltinModule('fs')).toBe(fs)
expect(shim.getBuiltinModule('node:fs')).toBe(fs)
expect(shim.getBuiltinModule('no-such-builtin')).toBeUndefined()
})
})