loader-composition.spec.ts 2.9 KB

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