settings.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /** The `web-search-deepseek` settings section layered over the composition entry. */
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { Context } from '@deepseek-ai/cordis'
  4. import type { Fiber } from '@deepseek-ai/cordis'
  5. import { SettingsProvider } from '@deepseek-ai/dsh-settings'
  6. import type { SettingsNamespace } from '@deepseek-ai/dsh-settings'
  7. import WebRuntime from '@deepseek-ai/dsh-web'
  8. import * as deepseekPlugin from '@deepseek-ai/dsh-web-search-deepseek'
  9. import { WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-web-search-deepseek'
  10. /** The smallest real provider: one in-memory document, always writable. */
  11. class MemorySettings extends SettingsProvider {
  12. doc: Record<string, unknown> = {}
  13. get writable(): boolean {
  14. return true
  15. }
  16. protected load(): Promise<Record<string, unknown>> {
  17. return Promise.resolve(structuredClone(this.doc))
  18. }
  19. protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
  20. this.doc = { ...this.doc, [ns]: structuredClone(section) }
  21. return Promise.resolve()
  22. }
  23. }
  24. function jsonResponse(body: unknown): Response {
  25. return new Response(JSON.stringify(body), {
  26. status: 200,
  27. headers: { 'content-type': 'application/json' },
  28. })
  29. }
  30. /** The smallest Anthropic-shaped answer the provider accepts — enough to observe the request. */
  31. const ONE_RESULT = {
  32. content: [
  33. { type: 'text', text: 'ok' },
  34. {
  35. type: 'web_search_tool_result',
  36. content: [{ type: 'web_search_result', url: 'https://a.test', title: 'A' }],
  37. },
  38. ],
  39. }
  40. async function boot(): Promise<{ ctx: Context; settingsFiber: Fiber; pluginFiber: Fiber }> {
  41. const ctx = new Context()
  42. await ctx.plugin(WebRuntime, {})
  43. const settingsFiber = ctx.plugin(MemorySettings)
  44. await settingsFiber.await()
  45. const pluginFiber = ctx.plugin(deepseekPlugin, { apiKey: 'ds-key', baseURL: 'https://search.entry.test/v1' })
  46. await pluginFiber.await()
  47. return { ctx, settingsFiber, pluginFiber }
  48. }
  49. afterEach(() => {
  50. vi.restoreAllMocks()
  51. })
  52. /**
  53. * Run one search and answer the endpoint it reached. A fresh `Response` per
  54. * call because a body can only be read once, and the call history is cleared
  55. * because repeated `spyOn` returns the same spy.
  56. * @param ctx - context whose `ctx.web` serves the search.
  57. * @returns the URL the provider fetched.
  58. */
  59. async function searchOnce(ctx: Context): Promise<string> {
  60. const fetchSpy = vi.spyOn(globalThis, 'fetch')
  61. .mockImplementation(() => Promise.resolve(jsonResponse(ONE_RESULT)))
  62. fetchSpy.mockClear()
  63. await ctx.web.search({ query: 'anything' })
  64. return String((fetchSpy.mock.calls.at(-1)?.[0] as URL | string | undefined) ?? '')
  65. }
  66. describe('web-search-deepseek settings section', () => {
  67. it('serves a stored endpoint to the next search without re-registering the provider', async () => {
  68. const bench = await boot()
  69. expect(await searchOnce(bench.ctx)).toContain('https://search.entry.test/v1')
  70. await bench.ctx.settings.update(WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE, {
  71. baseURL: 'https://search.stored.test/v1',
  72. })
  73. expect(await searchOnce(bench.ctx)).toContain('https://search.stored.test/v1')
  74. await bench.ctx.fiber.dispose()
  75. })
  76. it('keeps the literal key out of every described layer', async () => {
  77. const bench = await boot()
  78. await bench.ctx.settings.update(WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE, { apiKey: 'ds-stored-secret' })
  79. const [descriptor] = bench.ctx.settings.describe({ redactSecrets: true })
  80. .filter(row => String(row.ns) === 'web-search-deepseek')
  81. expect(JSON.stringify(descriptor)).not.toContain('ds-stored-secret')
  82. expect(descriptor?.secrets).toEqual([{ path: ['apiKey'], set: true }])
  83. await bench.ctx.fiber.dispose()
  84. })
  85. it('falls back to the composition entry when the settings provider detaches', async () => {
  86. const bench = await boot()
  87. await bench.ctx.settings.update(WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE, {
  88. baseURL: 'https://search.stored.test/v1',
  89. })
  90. expect(await searchOnce(bench.ctx)).toContain('https://search.stored.test/v1')
  91. await bench.settingsFiber.dispose()
  92. expect(await searchOnce(bench.ctx)).toContain('https://search.entry.test/v1')
  93. await bench.ctx.fiber.dispose()
  94. })
  95. it('releases the namespace when the plugin unloads', async () => {
  96. const bench = await boot()
  97. expect(bench.ctx.settings.describe().map(row => String(row.ns))).toContain('web-search-deepseek')
  98. await bench.pluginFiber.dispose()
  99. expect(bench.ctx.settings.describe().map(row => String(row.ns))).not.toContain('web-search-deepseek')
  100. await bench.ctx.fiber.dispose()
  101. })
  102. })