loader-composition.client.spec.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 type { Agent } from '@deepseek-ai/dsh-agent'
  10. import CommandRuntime from '@deepseek-ai/dsh-commands'
  11. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  12. import * as SessionLogDownload from '@deepseek-ai/dsh-session-log-export'
  13. let root: string | undefined
  14. let context: Context | undefined
  15. afterEach(async () => {
  16. await context?.fiber.dispose()
  17. context = undefined
  18. if (root !== undefined) await rm(root, { recursive: true, force: true })
  19. root = undefined
  20. })
  21. describe('session-log-download real Loader composition', () => {
  22. it('discovers and executes /export through the assembled command plane', async () => {
  23. root = await mkdtemp(join(tmpdir(), 'dsh-session-export-loader-'))
  24. const configPath = join(root, 'cordis.yml')
  25. await writeFile(configPath, [
  26. "- name: '@deepseek-ai/dsh-session'",
  27. "- name: '@deepseek-ai/dsh-commands'",
  28. "- name: '@deepseek-ai/dsh-session-log-export'",
  29. '',
  30. ].join('\n'))
  31. context = new Context()
  32. context.baseUrl = pathToFileURL(root).href + '/'
  33. await context.plugin(Loader)
  34. context.loader.builtins.include = Include
  35. const modules = new Map<string, unknown>([
  36. ['@deepseek-ai/dsh-session', SessionStore],
  37. ['@deepseek-ai/dsh-commands', CommandRuntime],
  38. ['@deepseek-ai/dsh-session-log-export', SessionLogDownload],
  39. ])
  40. context.loader.internal = {
  41. version: 'v2',
  42. async import(specifier: string) {
  43. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  44. return modules.get(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. const session = (context.get('sessions') as unknown as SessionStore)
  53. .create(SessionId('loader-session-export'), { meta: { createdAt: 1 } })
  54. const agent = { session, status: 'idle', options: {} } as unknown as Agent
  55. expect(context.commands.list(agent)).toContainEqual({
  56. name: 'export', description: 'Download this Session log as a ZIP archive',
  57. })
  58. const execution = await context.commands.execute(agent, '/export', [], new AbortController().signal)
  59. expect(execution?.result).toEqual({ kind: 'success', text: 'Session log download requested.' })
  60. expect(session.events.map(event => event.type)).toEqual(['command/run', 'command/done'])
  61. expect(session.deriveMessages()).toEqual([])
  62. })
  63. })