load-path.spec.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Real-load-path guard for @deepseek-ai/dsh-tool-fs-search. `tool-fs-search` is
  3. * a NAMESPACE plugin with `inject` — so a stray `export default apply` would
  4. * make the cordis Loader's `unwrapExports` (`exports.default ?? exports`)
  5. * collapse the module to the bare `apply` function, DROPPING `inject`. The
  6. * plugin would then read `ctx.subprocess` without having injected it and throw
  7. * `cannot get property … without inject` the moment it loads (postmortem 0001).
  8. *
  9. * A hand-built `ctx.plugin({ apply, inject })` mount CANNOT catch that — it
  10. * bypasses `unwrapExports`. So this test unwraps the module through the REAL
  11. * `Loader.prototype.unwrapExports` and mounts the result over the real local
  12. * subprocess service, exercising the exact path the Loader uses. Prove the
  13. * guard bites: add `export default apply` to `src/index.ts`, watch this go
  14. * red, revert.
  15. */
  16. import { describe, expect, it } from 'vitest'
  17. import { Context } from 'cordis'
  18. import Loader from '@cordisjs/plugin-loader'
  19. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  20. import ToolRegistry from '@deepseek-ai/dsh-tools'
  21. import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
  22. import * as toolFsSearch from '@deepseek-ai/dsh-tool-fs-search'
  23. describe('dsh-tool-fs-search real-load-path guard', () => {
  24. it('has no default export and keeps name/inject/Config through unwrapExports', () => {
  25. expect('default' in toolFsSearch).toBe(false)
  26. const loader = Object.create(Loader.prototype) as Loader
  27. const unwrapped = loader.unwrapExports(toolFsSearch) as Record<string, unknown>
  28. expect(unwrapped).toBe(toolFsSearch)
  29. expect(unwrapped.name).toBe('tool-fs-search')
  30. expect(unwrapped.inject).toEqual(['tools', 'systemPrompt', 'subprocess'])
  31. expect(typeof unwrapped.Config).toBe('function')
  32. expect(typeof unwrapped.apply).toBe('function')
  33. })
  34. it('boots over ctx.subprocess through the unwrapped module without an inject error', async () => {
  35. const ctx = new Context()
  36. await ctx.plugin(SystemPrompt)
  37. await ctx.plugin(ToolRegistry)
  38. await ctx.plugin(LocalSubprocessService)
  39. const loader = Object.create(Loader.prototype) as Loader
  40. const unwrapped = loader.unwrapExports(toolFsSearch) as Parameters<Context['plugin']>[0]
  41. // A collapsed export shape (dropped inject) would throw "without inject" here.
  42. const fiber = await ctx.plugin(unwrapped, { sampleOverCapGlobResults: true })
  43. expect(ctx.tools.schemas().map(s => s.name)).toEqual(expect.arrayContaining(['glob', 'grep']))
  44. await fiber.dispose()
  45. })
  46. })