loader-composition.spec.ts 2.6 KB

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