loader-composition.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Real-composition guard for the dormant pi-ai posture: LlmService,
  3. * settings-local, credentials-local, and a bare `llm-pi-ai` row boot from a
  4. * test-only cordis.yml through the actual Loader + Include path, an external
  5. * edit of settings.yaml registers the route live, and the next request
  6. * carries the credential the .env supplies. A hand-mounted `ctx.plugin` cannot
  7. * catch Loader export-shape failures, which is why the twin adapter has the
  8. * same guard.
  9. */
  10. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  11. import { tmpdir } from 'node:os'
  12. import { join } from 'node:path'
  13. import { pathToFileURL } from 'node:url'
  14. import { afterEach, describe, expect, it, vi } from 'vitest'
  15. import { Context } from 'cordis'
  16. import Loader from '@cordisjs/plugin-loader'
  17. import Include from '@cordisjs/plugin-include'
  18. import LlmService from '@deepseek-ai/dsh-llm'
  19. import CredentialsLocal from '@deepseek-ai/dsh-credentials-local'
  20. import SettingsLocal from '@deepseek-ai/dsh-settings-local'
  21. import * as LlmPiAi from '@deepseek-ai/dsh-llm-pi-ai'
  22. import { assemble } from './assemble.ts'
  23. import { closeMockServers, mockServer, textEvents } from './mock-server.ts'
  24. let root: string | undefined
  25. let context: Context | undefined
  26. afterEach(async () => {
  27. await context?.fiber.dispose()
  28. context = undefined
  29. if (root !== undefined) await rm(root, { recursive: true, force: true })
  30. root = undefined
  31. await closeMockServers()
  32. vi.unstubAllEnvs()
  33. })
  34. /** Boot the dormant composition: a bare `llm-pi-ai` row with no config at all. */
  35. async function loadComposition(): Promise<{ ctx: Context; settingsPath: string }> {
  36. root = await mkdtemp(join(tmpdir(), 'dsh-pi-composition-'))
  37. const settingsPath = join(root, 'settings.yaml')
  38. await writeFile(settingsPath, '# personal settings\n')
  39. await writeFile(join(root, '.env'), 'PI_COMPOSITION_KEY=key-from-store\n')
  40. const configPath = join(root, 'cordis.yml')
  41. await writeFile(configPath, [
  42. '- id: llm',
  43. " name: 'test-llm-service'",
  44. '- id: settings',
  45. " name: '@deepseek-ai/dsh-settings-local'",
  46. ' config:',
  47. ` path: ${JSON.stringify(settingsPath)}`,
  48. ' debounceMs: 10',
  49. '- id: credentials',
  50. " name: '@deepseek-ai/dsh-credentials-local'",
  51. ' config:',
  52. ` path: ${JSON.stringify(join(root, '.env'))}`,
  53. ' debounceMs: 10',
  54. '- id: llm-pi-ai',
  55. " name: '@deepseek-ai/dsh-llm-pi-ai'",
  56. '',
  57. ].join('\n'))
  58. const ctx = new Context()
  59. context = ctx
  60. ctx.baseUrl = pathToFileURL(root).href + '/'
  61. await ctx.plugin(Loader)
  62. ctx.loader.builtins.include = Include
  63. const modules = new Map<string, unknown>([
  64. ['test-llm-service', LlmService],
  65. ['@deepseek-ai/dsh-settings-local', SettingsLocal],
  66. ['@deepseek-ai/dsh-credentials-local', CredentialsLocal],
  67. ['@deepseek-ai/dsh-llm-pi-ai', LlmPiAi],
  68. ])
  69. ctx.loader.internal = {
  70. version: 'v2',
  71. async import(specifier: string) {
  72. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  73. return modules.get(specifier)
  74. },
  75. } as unknown as NonNullable<typeof ctx.loader.internal>
  76. await ctx.loader.create({
  77. name: 'cordis:include',
  78. config: { path: pathToFileURL(configPath).href },
  79. })
  80. await ctx.loader.await()
  81. return { ctx, settingsPath }
  82. }
  83. describe('llm-pi-ai real dormant composition', () => {
  84. it('boots with zero routes and registers one the moment settings supply a profile', async () => {
  85. vi.stubEnv('PI_COMPOSITION_KEY', '')
  86. const server = await mockServer([{ events: textEvents }])
  87. const { ctx, settingsPath } = await loadComposition()
  88. // The shipped posture: the adapter exists, no route does.
  89. expect(ctx.llm.listProviders()).toEqual([])
  90. // Exactly what the web Models page leaves on disk.
  91. await writeFile(settingsPath, [
  92. 'llm-pi-ai:',
  93. ' providers:',
  94. ' deepseek:',
  95. ' apiKeyEnv: PI_COMPOSITION_KEY',
  96. ` baseURL: ${server.url}`,
  97. '',
  98. ].join('\n'))
  99. await vi.waitFor(() => {
  100. expect(ctx.llm.listProviders().map(provider => provider.id)).toEqual(['deepseek'])
  101. }, { timeout: 5000 })
  102. const result = await assemble(ctx, { provider: 'deepseek', model: 'deepseek-v4-flash', messages: [] })
  103. expect(result.message.content).toEqual([{ type: 'text', text: 'hello' }])
  104. expect(server.headers[0]?.authorization).toBe('Bearer key-from-store')
  105. })
  106. })