client-handler.spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * Wire-protocol coverage over the isomorphic point: InProcessApiClient →
  3. * toFetchHandler(scripted impl) runs the real envelope wrap/unwrap, zod
  4. * two-level parse, and rpcId discipline with no network or browser. Each case
  5. * scripts its own minimal ApiProxy.
  6. */
  7. import { describe, expect, it, vi } from 'vitest'
  8. import type { SessionId } from '@deepseek-ai/dsh-session'
  9. import type { ApiProxy, GoalRef, RpcMessage, RpcRequest, RpcResponse } from '@deepseek-ai/dsh-host-apiproxy'
  10. import { InProcessApiClient, RpcId, toFetchHandler } from '@deepseek-ai/dsh-host-apiproxy'
  11. const sid = (id: string): SessionId => id as SessionId
  12. function ok<T>(request: RpcRequest<unknown>, value: T): Promise<RpcResponse<T>> {
  13. return Promise.resolve({ rpcId: request.rpcId, result: { ok: true, value } })
  14. }
  15. /** Scripted impl: every method resolves an empty-ish OK unless a case overrides it. */
  16. function scriptedApi(overrides: {
  17. subagents?: Partial<ApiProxy['subagents']>
  18. host?: Partial<ApiProxy['host']>
  19. skills?: Partial<ApiProxy['skills']>
  20. agentPresets?: Partial<ApiProxy['agentPresets']>
  21. goals?: Partial<ApiProxy['goals']>
  22. settings?: Partial<ApiProxy['settings']>
  23. credentials?: Partial<ApiProxy['credentials']>
  24. llm?: Partial<ApiProxy['llm']>
  25. } = {}): ApiProxy {
  26. const err = <T>(r: RpcRequest<unknown>): Promise<RpcResponse<T>> =>
  27. Promise.resolve({ rpcId: r.rpcId, result: { ok: false, error: { code: 'internal' as const, message: 'stub', details: {} } } })
  28. return {
  29. subagents: {
  30. list: r => ok(r, { entries: [], parentAvailable: false }),
  31. prompt: r => ok(r, { messageId: 'message-1' as never }),
  32. interrupt: r => ok(r, { accepted: true as const }),
  33. ...overrides.subagents,
  34. },
  35. host: {
  36. describe: r => ok(r, {
  37. version: '0-test', cwd: '/t', attachedSessions: 0, home: '/h', canOpenPath: true,
  38. }),
  39. pickDirectory: r => ok(r, { path: null }),
  40. listDirectory: r => ok(r, { path: '/t', home: '/t', crumbs: [], entries: [], truncated: false }),
  41. createDirectory: r => ok(r, { path: '/t/new' }),
  42. openPath: r => ok(r, { opened: true as const }),
  43. ...overrides.host,
  44. },
  45. skills: { list: r => ok(r, { skills: [] }), ...overrides.skills },
  46. agentPresets: {
  47. list: r => ok(r, { presets: [], authorable: false, hasDocument: false }),
  48. select: r => ok(r, { agentPreset: r.payload.agentPreset }),
  49. read: r => ok(r, { agentPreset: r.payload.agentPreset, trust: 'user' as const, content: '' }),
  50. copy: r => ok(r, { agentPreset: r.payload.agentPreset }),
  51. openDocument: r => ok(r, { opened: true as const }),
  52. remove: r => ok(r, {}),
  53. ...overrides.agentPresets,
  54. },
  55. goals: {
  56. create: err,
  57. edit: err,
  58. pause: err,
  59. resume: err,
  60. complete: err,
  61. clear: err,
  62. ...overrides.goals,
  63. },
  64. settings: {
  65. describe: r => ok(r, { writable: true, hasDocument: false, namespaces: [] }),
  66. openDocument: r => ok(r, { opened: true as const }),
  67. update: err,
  68. replace: err,
  69. mutate: err,
  70. ...overrides.settings,
  71. },
  72. credentials: {
  73. describe: r => ok(r, { credentials: {} }),
  74. set: err,
  75. unset: err,
  76. ...overrides.credentials,
  77. },
  78. llm: {
  79. providers: r => ok(r, { providers: [] }),
  80. models: r => ok(r, { groups: [], failures: [] }),
  81. discoverModels: err,
  82. ...overrides.llm,
  83. },
  84. downloads: { sessionLog: async () => new Response('stub', { status: 404 }) },
  85. }
  86. }
  87. function client(api: ApiProxy, timeoutMs?: number): InProcessApiClient {
  88. return new InProcessApiClient(toFetchHandler(api), timeoutMs)
  89. }
  90. /** Wrap one scripted method to record its invocation into `seen` before responding. */
  91. function recorderInto(seen: { method: string; payload: unknown }[]) {
  92. return <P, V>(method: string, respond: (r: RpcRequest<P>) => Promise<RpcResponse<V>>) =>
  93. (r: RpcRequest<P>): Promise<RpcResponse<V>> => {
  94. seen.push({ method, payload: r.payload })
  95. return respond(r)
  96. }
  97. }
  98. describe('unary round trip', () => {
  99. it('carries payload out and value back through the full wire form', async () => {
  100. let seen: RpcRequest<{}> | undefined
  101. const api = scriptedApi({
  102. host: {
  103. describe: (request) => {
  104. seen = request
  105. return ok(request, { version: '0-test', cwd: '/t', attachedSessions: 0, home: '/h', canOpenPath: true })
  106. },
  107. },
  108. })
  109. const response = await client(api).host.describe({})
  110. expect(seen?.payload).toEqual({})
  111. expect(seen?.rpcId).toBeTruthy()
  112. expect(response.rpcId).toBe(seen?.rpcId)
  113. expect(response.result).toMatchObject({ ok: true, value: { version: '0-test' } })
  114. })
  115. it('routes the agent-preset roster and switch through the wire', async () => {
  116. const c = client(scriptedApi())
  117. const listed = await c.agentPresets.list({})
  118. expect(listed.result).toEqual({ ok: true, value: { presets: [], authorable: false, hasDocument: false } })
  119. // The switch carries the session it is about: the host refuses one whose
  120. // conversation has started, and it can only know which by id.
  121. const selected = await c.agentPresets.select({ sessionId: sid('s1'), agentPreset: 'standard' })
  122. expect(selected.result).toEqual({ ok: true, value: { agentPreset: 'standard' } })
  123. })
  124. it('passes business errors through as 200 + err result, not a throw', async () => {
  125. const api = scriptedApi({
  126. host: {
  127. describe: request => Promise.resolve({
  128. rpcId: request.rpcId,
  129. result: { ok: false, error: { code: 'internal', message: 'nope', details: {} } },
  130. }),
  131. },
  132. })
  133. const response = await client(api).host.describe({})
  134. expect(response.result).toEqual({ ok: false, error: { code: 'internal', message: 'nope', details: {} } })
  135. })
  136. it('throws on rpcId echo mismatch', async () => {
  137. const api = scriptedApi({
  138. host: {
  139. describe: () => Promise.resolve({
  140. rpcId: RpcId('forged'),
  141. result: { ok: true, value: { version: '0-test', cwd: '/t', attachedSessions: 0, home: '/h', canOpenPath: true } },
  142. }),
  143. },
  144. })
  145. await expect(client(api).host.describe({})).rejects.toThrow(/rpcId mismatch/)
  146. })
  147. it('round-trips subagent.interrupt and rejects a one-shot or incomplete address', async () => {
  148. const interrupt = vi.fn((r: RpcRequest<unknown>) => ok(r, { accepted: true as const }))
  149. const api = scriptedApi({ subagents: { interrupt } })
  150. const c = client(api)
  151. const accepted = await c.subagents.interrupt({
  152. parentSessionId: sid('parent'), childSessionId: sid('child'), mode: 'continuable',
  153. })
  154. expect(accepted.result).toEqual({ ok: true, value: { accepted: true } })
  155. expect(interrupt).toHaveBeenCalledTimes(1)
  156. // The wire schema owns the mode fence: a one-shot address never reaches the impl.
  157. const oneShot = await c.subagents.interrupt({
  158. parentSessionId: sid('parent'), childSessionId: sid('child'), mode: 'one-shot',
  159. } as never)
  160. expect(oneShot.result.ok).toBe(false)
  161. if (!oneShot.result.ok) expect(oneShot.result.error.code).toBe('bad-request')
  162. const incomplete = await c.subagents.interrupt({
  163. parentSessionId: sid('parent'), mode: 'continuable',
  164. } as never)
  165. expect(incomplete.result.ok).toBe(false)
  166. if (!incomplete.result.ok) expect(incomplete.result.error.code).toBe('bad-request')
  167. expect(interrupt).toHaveBeenCalledTimes(1)
  168. })
  169. it('rejects a method/path mismatch as bad-request', async () => {
  170. const handler = toFetchHandler(scriptedApi())
  171. const body = { type: 'client-request', rpcId: 'r1', method: 'host.describe', payload: {} }
  172. const response = await handler.fetch('http://dsh.internal/api/skill.list', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
  173. expect(response.status).toBe(200)
  174. const parsed = await response.json() as { result: { ok: boolean; error?: { code: string; message: string } } }
  175. expect(parsed.result.ok).toBe(false)
  176. expect(parsed.result.error?.code).toBe('bad-request')
  177. expect(parsed.result.error?.message).toMatch(/does not match path/)
  178. })
  179. it('rejects a malformed envelope as bad-request, salvaging the rpcId or falling back to the sentinel', async () => {
  180. const handler = toFetchHandler(scriptedApi())
  181. // No salvageable rpcId → the fixed invalid-request sentinel keeps the response a valid ServerResponse.
  182. const noId = await handler.fetch('http://dsh.internal/api/host.describe', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ nonsense: true }) })
  183. expect(noId.status).toBe(200)
  184. const noIdParsed = await noId.json() as { rpcId: string; result: { ok: boolean } }
  185. expect(noIdParsed.result.ok).toBe(false)
  186. expect(noIdParsed.rpcId).toBe('invalid-request')
  187. // A string rpcId in the otherwise-bad body is salvaged for correlation.
  188. const withId = await handler.fetch('http://dsh.internal/api/host.describe', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ rpcId: 'salvage-me', nonsense: true }) })
  189. const withIdParsed = await withId.json() as { rpcId: string; result: { ok: boolean } }
  190. expect(withIdParsed.result.ok).toBe(false)
  191. expect(withIdParsed.rpcId).toBe('salvage-me')
  192. })
  193. it('maps carrier failures to HTTP statuses and the client throws transport failure', async () => {
  194. const handler = toFetchHandler(scriptedApi())
  195. // Unknown method → 404.
  196. const notFound = await handler.fetch('http://dsh.internal/api/no.such', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' })
  197. expect(notFound.status).toBe(404)
  198. // Non-JSON body → 400.
  199. const badBody = await handler.fetch('http://dsh.internal/api/host.describe', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{oops' })
  200. expect(badBody.status).toBe(400)
  201. // Impl crash → 500, and through the client that is a throw, not an err result.
  202. const crashing = scriptedApi({ host: { describe: () => { throw new Error('impl exploded') } } })
  203. await expect(client(crashing).host.describe({})).rejects.toThrow(/transport failure .*500/)
  204. })
  205. it('rejects non-JSON media types before executing anything (cross-site simple-request fence)', async () => {
  206. const describe = vi.fn((request: RpcRequest<{}>) => ok(request, {
  207. version: '0-test', cwd: '/t', attachedSessions: 0, home: '/h', canOpenPath: true,
  208. }))
  209. const handler = toFetchHandler(scriptedApi({ host: { describe } }))
  210. const body = JSON.stringify({ type: 'client-request', rpcId: 'r1', method: 'host.describe', payload: {} })
  211. // A "simple" browser POST (text/plain — sent with no CORS preflight) is
  212. // refused at the carrier before the impl runs.
  213. const plain = await handler.fetch('http://dsh.internal/api/host.describe', { method: 'POST', headers: { 'content-type': 'text/plain' }, body })
  214. expect(plain.status).toBe(415)
  215. // A string body with no explicit header defaults to text/plain — same fence.
  216. const unlabelled = await handler.fetch('http://dsh.internal/api/host.describe', { method: 'POST', body })
  217. expect(unlabelled.status).toBe(415)
  218. expect(describe).not.toHaveBeenCalled()
  219. // Media-type parameters pass: the fence checks the type, not the exact string.
  220. const charset = await handler.fetch('http://dsh.internal/api/host.describe', { method: 'POST', headers: { 'content-type': 'application/json; charset=utf-8' }, body })
  221. expect(charset.status).toBe(200)
  222. expect(describe).toHaveBeenCalledTimes(1)
  223. })
  224. it('rejects when the transport never resolves within timeoutMs', async () => {
  225. // AbortSignal.timeout is immune to fake timers; a short real timeout keeps this fast.
  226. const never = new InProcessApiClient({
  227. fetch: (_i: RequestInfo | URL, init?: RequestInit) => new Promise<Response>((_resolve, reject) => {
  228. init?.signal?.addEventListener('abort', () => { reject(new Error('aborted by timeout')) })
  229. }),
  230. }, 25)
  231. await expect(never.host.describe({})).rejects.toThrow()
  232. })
  233. it('aborts a unary call through the caller-supplied external signal', async () => {
  234. // Real-fetch semantics: on abort the rejection is the signal's reason, and the abort
  235. // works even when the transport ignores the signal entirely (hung impl).
  236. const gate = new AbortController()
  237. const hung = new InProcessApiClient({ fetch: () => new Promise<Response>(() => {}) }, 60_000)
  238. const call = hung.host.describe({}, gate.signal)
  239. gate.abort(new Error('externally aborted'))
  240. await expect(call).rejects.toThrow(/externally aborted/)
  241. })
  242. it('rejects an already-aborted signal before touching the transport, mapping a string reason to an Error', async () => {
  243. let touched = false
  244. const c = new InProcessApiClient({
  245. fetch: () => {
  246. touched = true
  247. return Promise.resolve(new Response('{}'))
  248. },
  249. }, 60_000)
  250. const gate = new AbortController()
  251. gate.abort('gone before start')
  252. await expect(c.host.describe({}, gate.signal)).rejects.toThrow('gone before start')
  253. expect(touched).toBe(false)
  254. })
  255. it('maps a non-Error, non-string abort reason to the default AbortError message', async () => {
  256. const gate = new AbortController()
  257. const hung = new InProcessApiClient({ fetch: () => new Promise<Response>(() => {}) }, 60_000)
  258. const call = hung.host.describe({}, gate.signal)
  259. gate.abort(42)
  260. await expect(call).rejects.toThrow('This operation was aborted')
  261. })
  262. it('passes a signal-less doFetch straight through to the handler', async () => {
  263. class Probe extends InProcessApiClient {
  264. direct(url: URL): Promise<Response> {
  265. return this.doFetch(url)
  266. }
  267. }
  268. const probe = new Probe({ fetch: () => Promise.resolve(new Response('raw')) })
  269. const response = await probe.direct(new URL('http://dsh.internal/probe'))
  270. expect(await response.text()).toBe('raw')
  271. })
  272. it('throws on an S→C ok value that fails the method value schema (second-level parse)', async () => {
  273. // Impl echoes rpcId but returns a wrong-shaped value: envelope parse passes, value parse must reject.
  274. const api = scriptedApi({
  275. host: { describe: request => Promise.resolve({ rpcId: request.rpcId, result: { ok: true, value: { version: 1 } } }) as never },
  276. })
  277. await expect(client(api).host.describe({})).rejects.toThrow()
  278. })
  279. })
  280. describe('goals unary surface', () => {
  281. const ref: GoalRef = { id: 'goal-1' as GoalRef['id'], revision: 1 }
  282. /** The `{ ref }` acknowledgement every non-clear mutation answers (state travels on the projection). */
  283. const ack = { ref: { id: 'goal-1' as GoalRef['id'], revision: 2 } }
  284. it('round-trips every goal method with its own payload and value shape', async () => {
  285. const seen: { method: string; payload: unknown }[] = []
  286. const record = recorderInto(seen)
  287. const api = scriptedApi({
  288. goals: {
  289. create: record('goal.create', r => ok(r, ack)),
  290. edit: record('goal.edit', r => ok(r, { ref: { ...ack.ref, revision: 3 } })),
  291. pause: record('goal.pause', r => ok(r, ack)),
  292. resume: record('goal.resume', r => ok(r, ack)),
  293. complete: record('goal.complete', r => ok(r, ack)),
  294. clear: record('goal.clear', r => ok(r, { cleared: true as const })),
  295. },
  296. })
  297. const c = client(api)
  298. const created = await c.goals.create({ sessionId: sid('s1'), objective: 'ship it', maxGoalRounds: 4 })
  299. expect(created.result).toEqual({ ok: true, value: ack })
  300. const edited = await c.goals.edit({ sessionId: sid('s1'), ref, objective: 'ship v2' })
  301. expect(edited.result).toEqual({ ok: true, value: { ref: { ...ack.ref, revision: 3 } } })
  302. expect((await c.goals.pause({ sessionId: sid('s1'), ref })).result).toEqual({ ok: true, value: ack })
  303. expect((await c.goals.resume({ sessionId: sid('s1'), ref })).result).toEqual({ ok: true, value: ack })
  304. expect((await c.goals.complete({ sessionId: sid('s1'), ref })).result).toEqual({ ok: true, value: ack })
  305. const cleared = await c.goals.clear({ sessionId: sid('s1'), ref })
  306. expect(cleared.result).toEqual({ ok: true, value: { cleared: true } })
  307. // The handler dispatched each call through its own route row: payload parsed per method.
  308. expect(seen.map(s => s.method)).toEqual(['goal.create', 'goal.edit', 'goal.pause', 'goal.resume', 'goal.complete', 'goal.clear'])
  309. expect(seen[0]?.payload).toEqual({ sessionId: 's1', objective: 'ship it', maxGoalRounds: 4 })
  310. expect(seen[1]?.payload).toEqual({ sessionId: 's1', ref, objective: 'ship v2' })
  311. })
  312. it('passes business errors through as results, not throws', async () => {
  313. // Default scripted goals impl answers an err result: it must arrive as a result, not a throw.
  314. const failed = await client(scriptedApi()).goals.pause({ sessionId: sid('s1'), ref })
  315. expect(failed.result.ok).toBe(false)
  316. if (!failed.result.ok) expect(failed.result.error.code).toBe('internal')
  317. })
  318. it('rejects an invalid goal payload at the handler as bad-request', async () => {
  319. const response = await client(scriptedApi()).goals.create({ sessionId: sid('s1'), objective: '' })
  320. expect(response.result.ok).toBe(false)
  321. if (!response.result.ok) expect(response.result.error.code).toBe('bad-request')
  322. let editCalls = 0
  323. const api = scriptedApi({ goals: { edit: (r) => { editCalls++; return ok(r, ack) } } })
  324. const emptyEdit = await client(api).goals.edit({ sessionId: sid('s1'), ref })
  325. expect(emptyEdit.result.ok).toBe(false)
  326. if (!emptyEdit.result.ok) expect(emptyEdit.result.error.code).toBe('bad-request')
  327. expect(editCalls).toBe(0)
  328. })
  329. })
  330. describe('envelope tap', () => {
  331. it('delivers one microtask batch of full forms per unary call', async () => {
  332. const api = scriptedApi()
  333. const tapped = client(api)
  334. const batches: (readonly RpcMessage[])[] = []
  335. tapped.subscribeEnvelopes(batch => batches.push(batch))
  336. await tapped.host.describe({})
  337. await vi.waitFor(() => { expect(batches.length).toBeGreaterThan(0) })
  338. const all = batches.flat()
  339. expect(all.map(m => m.type)).toEqual(['client-request', 'server-response'])
  340. expect(all[0]?.rpcId).toBe(all[1]?.rpcId)
  341. })
  342. it('isolates a throwing listener and keeps serving the call', async () => {
  343. const api = scriptedApi()
  344. const tapped = client(api)
  345. const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
  346. try {
  347. const good: string[] = []
  348. tapped.subscribeEnvelopes(() => { throw new Error('listener bug') })
  349. tapped.subscribeEnvelopes(batch => good.push(...batch.map(m => m.type)))
  350. const response = await tapped.host.describe({})
  351. expect(response.result.ok).toBe(true)
  352. await vi.waitFor(() => { expect(good).toContain('server-response') })
  353. } finally {
  354. errorSpy.mockRestore()
  355. }
  356. })
  357. it('buffers nothing with zero subscribers and unsubscribes cleanly', async () => {
  358. const api = scriptedApi()
  359. const tapped = client(api)
  360. await tapped.host.describe({}) // no subscribers: must not accumulate
  361. const batches: (readonly RpcMessage[])[] = []
  362. const unsubscribe = tapped.subscribeEnvelopes(batch => batches.push(batch))
  363. unsubscribe()
  364. await tapped.host.describe({})
  365. await new Promise(resolve => setTimeout(resolve, 0))
  366. expect(batches).toEqual([])
  367. })
  368. })
  369. describe('config unary surface', () => {
  370. it('round-trips every settings/credentials/llm method with its own payload and value shape', async () => {
  371. const seen: { method: string; payload: unknown }[] = []
  372. const record = recorderInto(seen)
  373. const view = {
  374. ns: 'llm-deepseek',
  375. schema: { uid: 1, refs: { 1: { type: 'object' } } },
  376. value: { baseURL: 'https://next' },
  377. user: { baseURL: 'https://next' },
  378. applies: 'live' as const,
  379. secrets: [{ path: ['apiKey'], set: true }],
  380. revision: 0,
  381. }
  382. const providerRow = {
  383. provider: 'openai',
  384. displayName: 'openai',
  385. settingsNs: 'llm-pi-ai',
  386. settingsPath: ['providers', 'openai'],
  387. active: false,
  388. }
  389. const group = { id: 'deepseek-official', name: 'DeepSeek', models: [{ id: 'deepseek-v4-flash', name: 'Flash' }] }
  390. const api = scriptedApi({
  391. settings: {
  392. describe: record('settings.describe', r => ok(r, { writable: true, hasDocument: false, namespaces: [view] })),
  393. openDocument: record('settings.openDocument', r => ok(r, { opened: true as const })),
  394. update: record('settings.update', r => ok(r, view)),
  395. replace: record('settings.replace', r => ok(r, view)),
  396. mutate: record('settings.mutate', r => ok(r, view)),
  397. },
  398. credentials: {
  399. describe: record('credentials.describe', r => ok(r, { credentials: { OPENAI_API_KEY: { configured: true, source: 'file', writable: true } } })),
  400. set: record('credentials.set', r => ok(r, {})),
  401. unset: record('credentials.unset', r => ok(r, {})),
  402. },
  403. llm: {
  404. providers: record('llm.providers', r => ok(r, { providers: [providerRow] })),
  405. models: record('llm.models', r => ok(r, { groups: [group], failures: [] })),
  406. discoverModels: record('llm.discoverModels', r => ok(r, { models: [{ id: 'acme-large', contextWindow: 65536 }] })),
  407. },
  408. })
  409. const c = client(api)
  410. const described = await c.settings.describe({})
  411. expect(described.result).toEqual({ ok: true, value: { writable: true, hasDocument: false, namespaces: [view] } })
  412. expect((await c.settings.openDocument({})).result).toEqual({ ok: true, value: { opened: true } })
  413. const updated = await c.settings.update({ ns: 'llm-deepseek', patch: { baseURL: 'https://next' } })
  414. expect(updated.result).toEqual({ ok: true, value: view })
  415. const replaced = await c.settings.replace({ ns: 'llm-deepseek', section: {} })
  416. expect(replaced.result).toEqual({ ok: true, value: view })
  417. const mutated = await c.settings.mutate({
  418. ns: 'llm-deepseek',
  419. ops: [{ op: 'unset', path: ['baseURL'] }],
  420. expectedRevision: 0,
  421. })
  422. expect(mutated.result).toEqual({ ok: true, value: view })
  423. const creds = await c.credentials.describe({ refs: ['OPENAI_API_KEY'] })
  424. expect(creds.result).toEqual({ ok: true, value: { credentials: { OPENAI_API_KEY: { configured: true, source: 'file', writable: true } } } })
  425. expect((await c.credentials.set({ ref: 'OPENAI_API_KEY', value: 'sk-x' })).result).toEqual({ ok: true, value: {} })
  426. expect((await c.credentials.unset({ ref: 'OPENAI_API_KEY' })).result).toEqual({ ok: true, value: {} })
  427. const providers = await c.llm.providers({})
  428. expect(providers.result).toEqual({ ok: true, value: { providers: [providerRow] } })
  429. const models = await c.llm.models({})
  430. expect(models.result).toEqual({ ok: true, value: { groups: [group], failures: [] } })
  431. const discovered = await c.llm.discoverModels({
  432. settingsNs: 'llm-pi-ai',
  433. baseURL: 'https://gateway.acme.example/v1',
  434. api: 'openai-completions',
  435. apiKey: 'probe-key',
  436. })
  437. expect(discovered.result).toEqual({ ok: true, value: { models: [{ id: 'acme-large', contextWindow: 65536 }] } })
  438. expect(seen.map(call => call.method)).toEqual([
  439. 'settings.describe', 'settings.openDocument', 'settings.update', 'settings.replace', 'settings.mutate',
  440. 'credentials.describe', 'credentials.set', 'credentials.unset',
  441. 'llm.providers', 'llm.models', 'llm.discoverModels',
  442. ])
  443. expect(seen[2]?.payload).toEqual({ ns: 'llm-deepseek', patch: { baseURL: 'https://next' } })
  444. expect(seen[4]?.payload)
  445. .toEqual({ ns: 'llm-deepseek', ops: [{ op: 'unset', path: ['baseURL'] }], expectedRevision: 0 })
  446. expect(seen[6]?.payload).toEqual({ ref: 'OPENAI_API_KEY', value: 'sk-x' })
  447. // The draft crosses whole, credential included: the host needs it for this
  448. // one interrogation and stores none of it.
  449. expect(seen[10]?.payload).toEqual({
  450. settingsNs: 'llm-pi-ai',
  451. baseURL: 'https://gateway.acme.example/v1',
  452. api: 'openai-completions',
  453. apiKey: 'probe-key',
  454. })
  455. })
  456. it('rejects an invalid credential reference name at the carrier boundary', async () => {
  457. const api = scriptedApi()
  458. const response = await client(api).credentials.set({ ref: 'not a var', value: 'x' })
  459. expect(response.result.ok).toBe(false)
  460. if (response.result.ok) throw new Error('unreachable')
  461. expect(response.result.error.code).toBe('bad-request')
  462. })
  463. })