1
0

loader-composition.spec.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * REAL-composition proof: the shipped YAML shape (session + projection
  3. * registry + session-stats) boots through the vendored Loader, the function
  4. * plugin's namespace survives (no default export), and a full logged turn
  5. * serves `{turns: 1, steps: 1}` through the composed registry.
  6. */
  7. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  8. import { tmpdir } from 'node:os'
  9. import { join } from 'node:path'
  10. import { pathToFileURL } from 'node:url'
  11. import { afterEach, describe, expect, it } from 'vitest'
  12. import { Context } from '@deepseek-ai/cordis'
  13. import Loader from '@deepseek-ai/cordis-plugin-loader'
  14. import Include from '@deepseek-ai/cordis-plugin-include'
  15. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  16. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  17. import * as SessionStatsPlugin from '@deepseek-ai/dsh-session-stats'
  18. let root: string | undefined
  19. let context: Context | undefined
  20. afterEach(async () => {
  21. await context?.fiber.dispose()
  22. context = undefined
  23. if (root !== undefined) await rm(root, { recursive: true, force: true })
  24. root = undefined
  25. })
  26. async function loadYaml(lines: readonly string[]): Promise<Context> {
  27. root = await mkdtemp(join(tmpdir(), 'dsh-session-stats-loader-'))
  28. const configPath = join(root, 'cordis.yml')
  29. await writeFile(configPath, [...lines, ''].join('\n'))
  30. context = new Context()
  31. context.baseUrl = pathToFileURL(root).href + '/'
  32. await context.plugin(Loader)
  33. context.loader.builtins.include = Include
  34. const modules = new Map<string, unknown>([
  35. ['@deepseek-ai/dsh-session', SessionStore],
  36. ['@deepseek-ai/dsh-session-projection', SessionProjectionRegistry],
  37. ['@deepseek-ai/dsh-session-stats', SessionStatsPlugin],
  38. ])
  39. context.loader.internal = {
  40. version: 'v2',
  41. async import(specifier: string) {
  42. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  43. return modules.get(specifier)
  44. },
  45. } as unknown as NonNullable<typeof context.loader.internal>
  46. await context.loader.create({
  47. name: 'cordis:include',
  48. config: { path: pathToFileURL(configPath).href },
  49. })
  50. await context.loader.await()
  51. return context
  52. }
  53. describe('real Loader composition', () => {
  54. it('loads the shipped session-stats YAML shape and serves whole-log counts', async () => {
  55. const loaded = await loadYaml([
  56. "- name: '@deepseek-ai/dsh-session'",
  57. "- name: '@deepseek-ai/dsh-session-projection'",
  58. "- name: '@deepseek-ai/dsh-session-stats'",
  59. ])
  60. const unloaded = [...loaded.loader.entries()]
  61. .filter(entry => entry.fiber === undefined && !entry.disabled)
  62. .map(entry => entry.options.name)
  63. expect(unloaded).toEqual([])
  64. const session = loaded.sessions.create(SessionId('composed'))
  65. session.append('turn/start', { turn: 1 })
  66. session.append('step/start', { turn: 1, step: 1 })
  67. session.append('step/end', { turn: 1, step: 1 })
  68. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  69. expect(loaded.sessionProjections.snapshot(session).values.sessionStats)
  70. .toMatchObject({ turns: 1, steps: 1 })
  71. })
  72. it('keeps the function-plugin namespace free of a default export', () => {
  73. // A default export beside the named form makes the Loader discard the
  74. // namespace (postmortem 0001) — pin its absence.
  75. expect('default' in SessionStatsPlugin).toBe(false)
  76. })
  77. })