path-diff.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Differential check of this package's POSIX path shim against Node's
  3. * `path.posix`.
  4. *
  5. * Differential rather than example-based: the shim's contract is "behaves like
  6. * `node:path/posix`", so Node itself is the oracle and every case is compared
  7. * rather than asserted against a hand-written expectation. The corpus is the
  8. * shapes a VFS path actually takes (absolute image paths, `node_modules`
  9. * specifiers, `.bin` entries) plus edge forms that stress lexical handling
  10. * (repeated slashes, trailing dots, `..` past the root).
  11. *
  12. * Imports go through the package name so the harness and the shim resolve to one
  13. * module instance (see `../polyfill/als-shim.spec.ts` for why that matters).
  14. */
  15. import { expect, test } from 'vitest'
  16. import { posix as nodePosix } from 'node:path'
  17. import * as shim from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/path.ts'
  18. const CASES = [
  19. '', '.', '..', '/', '//', '///', 'a', '/a', 'a/', '/a/', 'a/b', '/a/b/c', 'a//b', '/a//b/',
  20. './a', '../a', 'a/./b', 'a/../b', '/a/../..', '/../a', 'a/b/../../c', '.hidden', 'a.b.c',
  21. '/a/b/c.txt', 'c.txt', '.txt', 'a/.txt', 'a/b.', '/a/b/.', '/a/b/..', 'foo/bar/../baz/./qux',
  22. '/dsh/node_modules/@deepseek-ai/dsh-session/lib/index.js', 'node_modules/.bin/x',
  23. ]
  24. const JOINS: string[][] = [
  25. ['a', 'b'], ['/a', 'b'], ['a', '/b'], ['a', '..'], ['a', '../..'], ['', 'b'], ['a', ''],
  26. ['/dsh', 'config', 'cordis.yml'], ['/dsh/node_modules', '@scope/pkg', 'lib/index.js'],
  27. ['a/', '/b'], ['.', 'a'], ['..', 'a'], ['/', 'a'], [],
  28. ]
  29. const RESOLVES: string[][] = [
  30. ['a'], ['/a', 'b'], ['/a', '/b'], ['a', '..'], ['/dsh', './config/../config/cordis.yml'],
  31. ['/a/b', '../c'], ['/'], ['', 'a'], ['/dsh/node_modules/pkg', './lib/../lib/index.js'],
  32. ]
  33. const RELATIVES: [string, string][] = [
  34. ['/a/b', '/a/b/c'], ['/a/b/c', '/a/b'], ['/a', '/b'], ['/a/b', '/a/b'], ['/', '/a'],
  35. ['/dsh/node_modules/a', '/dsh/node_modules/b/lib/x.js'],
  36. ]
  37. const compare = (label: string, actual: unknown, expected: unknown): void => {
  38. const [shimmed, node] = [JSON.stringify(actual), JSON.stringify(expected)]
  39. test(label, () => { expect(shimmed).toBe(node) })
  40. }
  41. // resolve() consults process.cwd() on both sides; pin it so they agree, then put
  42. // it back before any case runs so the rest of the run keeps the repository root.
  43. const originalCwd = process.cwd()
  44. process.chdir('/')
  45. for (const value of CASES) {
  46. for (const fn of ['normalize', 'dirname', 'basename', 'extname', 'isAbsolute', 'parse'] as const) {
  47. try {
  48. compare(`${fn}(${JSON.stringify(value)})`, (shim[fn] as (v: string) => unknown)(value), (nodePosix[fn] as (v: string) => unknown)(value))
  49. } catch (error) {
  50. const thrown = String(error)
  51. test(`${fn}(${JSON.stringify(value)}) does not throw`, () => { expect.unreachable(thrown) })
  52. }
  53. }
  54. compare(`basename(${JSON.stringify(value)}, '.txt')`, shim.basename(value, '.txt'), nodePosix.basename(value, '.txt'))
  55. }
  56. for (const parts of JOINS) compare(`join(${JSON.stringify(parts)})`, shim.join(...parts), nodePosix.join(...parts))
  57. for (const parts of RESOLVES) compare(`resolve(${JSON.stringify(parts)})`, shim.resolve(...parts), nodePosix.resolve(...parts))
  58. for (const [from, to] of RELATIVES) compare(`relative(${from}, ${to})`, shim.relative(from, to), nodePosix.relative(from, to))
  59. for (const value of CASES) {
  60. const parsed = nodePosix.parse(value)
  61. compare(`format(parse(${JSON.stringify(value)}))`, shim.format(parsed), nodePosix.format(parsed))
  62. }
  63. process.chdir(originalCwd)