loader-composition.spec.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { pathToFileURL } from 'node:url'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { Context } from 'cordis'
  7. import Loader from '@cordisjs/plugin-loader'
  8. import Include from '@cordisjs/plugin-include'
  9. import ToolResultPruneService from '@deepseek-ai/dsh-compact-tool-result-prune'
  10. let root: string | undefined
  11. let context: Context | undefined
  12. afterEach(async () => {
  13. await context?.fiber.dispose()
  14. context = undefined
  15. if (root !== undefined) await rm(root, { recursive: true, force: true })
  16. root = undefined
  17. })
  18. describe('compact-tool-result-prune real Loader composition', () => {
  19. it('loads and resolves the flat YAML plugin shape', async () => {
  20. root = await mkdtemp(join(tmpdir(), 'dsh-compact-tool-result-prune-loader-'))
  21. const configPath = join(root, 'cordis.yml')
  22. await writeFile(configPath, [
  23. "- name: '@deepseek-ai/dsh-compact-tool-result-prune'",
  24. ' config:',
  25. ' thresholdChars: 100',
  26. ' headChars: 20',
  27. ' tailChars: 10',
  28. '',
  29. ].join('\n'))
  30. context = new Context()
  31. context.baseUrl = pathToFileURL(root).href + '/'
  32. await context.plugin(Loader)
  33. context.loader.builtins.include = Include
  34. context.loader.internal = {
  35. version: 'v2',
  36. async import(specifier: string) {
  37. if (specifier !== '@deepseek-ai/dsh-compact-tool-result-prune') {
  38. throw new Error(`unexpected Loader import: ${specifier}`)
  39. }
  40. return ToolResultPruneService
  41. },
  42. } as unknown as NonNullable<typeof context.loader.internal>
  43. await context.loader.create({
  44. name: 'cordis:include',
  45. config: { path: pathToFileURL(configPath).href },
  46. })
  47. await context.loader.await()
  48. expect(context.get('toolResultPrune')).toBeInstanceOf(ToolResultPruneService)
  49. expect(context.toolResultPrune.config).toEqual({
  50. thresholdChars: 100,
  51. headChars: 20,
  52. tailChars: 10,
  53. })
  54. })
  55. it('rejects stale config after plugin schema normalization', async () => {
  56. context = new Context()
  57. await expect(context.plugin(ToolResultPruneService, {
  58. maxChars: 100,
  59. } as never)).rejects.toThrow(/unknown key "maxChars"/)
  60. })
  61. })