process-shim.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * The worker's process shim: the layout-derived environment and the Node 22
  3. * `getBuiltinModule` face, which must answer the loader's module proxies for
  4. * builtin ids and undefined for everything else — never an image resolution.
  5. */
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. import { installProcessGlobal } from '../../src/node/globals/process.ts'
  8. import { setActiveModuleLoader, WorkerModuleLoader } from '../../src/module-system/module-loader.ts'
  9. import { MemoryVfs } from '../../src/storage/memory.ts'
  10. const realProcess = globalThis.process
  11. afterEach(() => {
  12. ;(globalThis as { process: unknown }).process = realProcess
  13. })
  14. describe('process shim', () => {
  15. it('publishes cwd, env, and version zero for the loader probe', () => {
  16. const shim = installProcessGlobal({ cwd: '/dsh', env: { DSH_HOME: '/dsh/home' } })
  17. expect(shim.cwd()).toBe('/dsh')
  18. expect(shim.env.DSH_HOME).toBe('/dsh/home')
  19. expect(shim.title).toBe('dsh-webworker')
  20. // "0.0.0" keeps the vendored Loader off Node internals so the worker owns
  21. // the module seam.
  22. expect(shim.versions.node).toBe('0.0.0')
  23. })
  24. it('answers getBuiltinModule from the module proxies and undefined otherwise', () => {
  25. const fs = { marker: 'fs-proxy' }
  26. // The table holds factories, and a builtin must keep one identity across
  27. // requires (`instanceof`, `Buffer.isBuffer`), so this one answers with the
  28. // same object every time.
  29. const factory = (): unknown => fs
  30. const vfs = new MemoryVfs()
  31. vfs.seedDirectory('/dsh')
  32. const loader = new WorkerModuleLoader({
  33. vfs,
  34. root: '/dsh',
  35. staticModules: { 'node:fs': factory, 'fs': factory },
  36. })
  37. setActiveModuleLoader(loader)
  38. const shim = installProcessGlobal({ cwd: '/dsh', env: {} })
  39. // The shim calls the factory: a caller receives the module, never the thunk.
  40. expect(shim.getBuiltinModule('fs')).toBe(fs)
  41. expect(shim.getBuiltinModule('node:fs')).toBe(fs)
  42. expect(shim.getBuiltinModule('no-such-builtin')).toBeUndefined()
  43. })
  44. })