process-shim.spec.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // "0.0.0" keeps the vendored Loader off Node internals so the worker owns
  20. // the module seam.
  21. expect(shim.versions.node).toBe('0.0.0')
  22. })
  23. it('answers getBuiltinModule from the module proxies and undefined otherwise', () => {
  24. const fs = { marker: 'fs-proxy' }
  25. // The table holds factories, and a builtin must keep one identity across
  26. // requires (`instanceof`, `Buffer.isBuffer`), so this one answers with the
  27. // same object every time.
  28. const factory = (): unknown => fs
  29. const vfs = new MemoryVfs()
  30. vfs.seedDirectory('/dsh')
  31. const loader = new WorkerModuleLoader({
  32. vfs,
  33. root: '/dsh',
  34. staticModules: { 'node:fs': factory, 'fs': factory },
  35. })
  36. setActiveModuleLoader(loader)
  37. const shim = installProcessGlobal({ cwd: '/dsh', env: {} })
  38. // The shim calls the factory: a caller receives the module, never the thunk.
  39. expect(shim.getBuiltinModule('fs')).toBe(fs)
  40. expect(shim.getBuiltinModule('node:fs')).toBe(fs)
  41. expect(shim.getBuiltinModule('no-such-builtin')).toBeUndefined()
  42. })
  43. })