loader-shape.compat.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. import { mkdtempSync, rmSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { pathToFileURL } from 'node:url'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import Loader from '@deepseek-ai/cordis-plugin-loader'
  7. import { describe, expect, it } from 'vitest'
  8. describe('Loader internal shape detection', () => {
  9. it('tags the running Node loader with the resolver signature that runtime accepts', async () => {
  10. const dir = mkdtempSync(join(tmpdir(), 'dsh-loader-shape-'))
  11. const baseUrl = pathToFileURL(dir).href + '/'
  12. const ctx = new Context()
  13. ctx.baseUrl = baseUrl
  14. await ctx.plugin(Loader)
  15. try {
  16. const internal = ctx.loader.internal
  17. expect(internal, 'Node module internals are unreachable; HMR reload and client-module resolution both need them').toBeDefined()
  18. // Resolving through the tag is exactly what Hmr._resolve() and the
  19. // client-modules registry do. A tag taken from the Node major instead of
  20. // the loader's own API rejects every call on 24.0-24.11.1, which report
  21. // major 24 while carrying the v1 loader: v2 arrived only in 24.12.0.
  22. const resolved = internal!.version === 'v2'
  23. ? internal!.resolveSync(baseUrl, { specifier: 'node:path', attributes: {} })
  24. : internal!.resolveSync('node:path', baseUrl, {})
  25. expect(resolved.url).toBe('node:path')
  26. } finally {
  27. await ctx.fiber.dispose()
  28. rmSync(dir, { recursive: true, force: true })
  29. }
  30. })
  31. })