loader-composition.host.spec.ts 3.0 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 '@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. context.provide('connection', {
  34. fetch: { register: () => () => Promise.resolve() },
  35. } as never)
  36. await context.plugin(Loader)
  37. context.loader.builtins.include = Include
  38. const modules = new Map<string, unknown>([
  39. ['@deepseek-ai/dsh-session', SessionStore],
  40. ['@deepseek-ai/dsh-commands', CommandRuntime],
  41. ['@deepseek-ai/dsh-session-log-export', SessionLogDownload],
  42. ])
  43. context.loader.internal = {
  44. version: 'v2',
  45. async import(specifier: string) {
  46. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  47. return modules.get(specifier)
  48. },
  49. } as unknown as NonNullable<typeof context.loader.internal>
  50. await context.loader.create({
  51. name: 'cordis:include',
  52. config: { path: pathToFileURL(configPath).href },
  53. })
  54. await context.loader.await()
  55. const session = (context.get('sessions') as unknown as SessionStore)
  56. .create(SessionId('loader-session-export'), { meta: { createdAt: 1 } })
  57. const agent = { session, status: 'idle', options: {} } as unknown as Agent
  58. expect(context.commands.list(agent)).toContainEqual({
  59. name: 'export', description: 'Download this Session log as a ZIP archive',
  60. })
  61. const execution = await context.commands.execute(agent, '/export', [], new AbortController().signal)
  62. expect(execution?.result).toEqual({ kind: 'success', text: 'Session log download requested.' })
  63. expect(session.snapshotEvents().map(event => event.type)).toEqual(['command/run', 'command/done'])
  64. expect(session.deriveMessages()).toEqual([])
  65. })
  66. })