bridge.spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { mkdtemp, rm } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { PROTOCOL_VERSION } from '@agentclientprotocol/sdk'
  6. import { makeBridgeHarness, textResponse, toolCallResponse, type BridgeHarness } from './harness.ts'
  7. import { SessionId } from '@deepseek-ai/dsh-session'
  8. import { encodeSessionReferenceUri, formatSessionReferenceMention } from '@deepseek-ai/dsh-session-reference'
  9. /**
  10. * End-to-end bridge specs over an in-memory transport: a real
  11. * ClientSideConnection drives the bridge's AgentSideConnection, so every
  12. * assertion exercises actual JSON-RPC framing and the harness event taxonomy.
  13. */
  14. describe('acp bridge', () => {
  15. let storageDir: string
  16. let harness: BridgeHarness | undefined
  17. beforeEach(async () => {
  18. storageDir = await mkdtemp(join(tmpdir(), 'acp-test-'))
  19. })
  20. afterEach(async () => {
  21. // e2e/integration tests own their resources (docs/testing.md): dispose even on
  22. // failure so a flaky run never leaks a context or persistence dir.
  23. if (harness) await harness.dispose()
  24. harness = undefined
  25. await rm(storageDir, { recursive: true, force: true })
  26. })
  27. it('initialize negotiates the protocol version and advertises capabilities', async () => {
  28. harness = await makeBridgeHarness({ storageDir })
  29. const res = await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  30. expect(res.protocolVersion).toBe(PROTOCOL_VERSION)
  31. expect(res.agentCapabilities?.loadSession).toBe(true)
  32. expect(res.agentCapabilities?.promptCapabilities).toMatchObject({ image: false, audio: false })
  33. expect(res.agentInfo).toEqual({ name: 'deepseek-harness-acp', version: '0.0.1' })
  34. })
  35. it('session/new creates a session and a full prompt turn streams text then settles end_turn', async () => {
  36. harness = await makeBridgeHarness({ storageDir, script: [textResponse('hello there')] })
  37. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  38. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  39. expect(sessionId).toBeTruthy()
  40. const res = await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'hi' }] })
  41. expect(res.stopReason).toBe('end_turn')
  42. // The streamed text arrived as agent_message_chunk updates.
  43. const text = harness.updates
  44. .filter(u => u.sessionUpdate === 'agent_message_chunk')
  45. .map(u => (u.content.type === 'text' ? u.content.text : ''))
  46. .join('')
  47. expect(text).toBe('hello there')
  48. })
  49. it('routes ask_user_question through ACP form elicitation and continues with the selected option', async () => {
  50. harness = await makeBridgeHarness({
  51. storageDir,
  52. withAskUser: true,
  53. script: [
  54. toolCallResponse('ask-1', 'ask_user_question', {
  55. questions: [{
  56. id: 'language',
  57. header: 'Project config',
  58. question: 'Which language should I use?',
  59. options: [
  60. { label: 'TypeScript', description: 'Good for UI apps' },
  61. { label: 'Python', description: 'Good for scripts' },
  62. ],
  63. }],
  64. }),
  65. textResponse('Python it is.'),
  66. ],
  67. })
  68. harness.onElicitation = () => ({ action: 'accept', content: { choice: 'Python' } })
  69. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  70. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  71. const result = await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'ask me' }] })
  72. expect(result.stopReason).toBe('end_turn')
  73. expect(harness.elicitationRequests).toHaveLength(1)
  74. expect(harness.elicitationRequests[0]).toMatchObject({
  75. sessionId,
  76. mode: 'form',
  77. message: 'Which language should I use?',
  78. requestedSchema: {
  79. title: 'Project config',
  80. properties: {
  81. choice: {
  82. oneOf: [
  83. { const: 'TypeScript', title: 'TypeScript: Good for UI apps' },
  84. { const: 'Python', title: 'Python: Good for scripts' },
  85. ],
  86. },
  87. custom: { type: 'string' },
  88. },
  89. required: [],
  90. },
  91. })
  92. const toolResult = harness.ctx.agents.get(SessionId(sessionId))!.session.events.find(event => event.type === 'tool/result')
  93. const toolResultBlock = toolResult?.type === 'tool/result' ? toolResult.data.content[0] : undefined
  94. const toolResultText = toolResultBlock?.type === 'text' ? toolResultBlock.text : undefined
  95. expect(toolResultText).toBe('{"answers":[{"id":"language","selected":["Python"]}]}')
  96. })
  97. it('routes optionless ask_user_question through an ACP free-form answer field', async () => {
  98. harness = await makeBridgeHarness({
  99. storageDir,
  100. withAskUser: true,
  101. script: [
  102. toolCallResponse('ask-1', 'ask_user_question', {
  103. questions: [{ id: 'name', question: 'What should I name it?' }],
  104. }),
  105. textResponse('Name recorded.'),
  106. ],
  107. })
  108. harness.onElicitation = () => ({ action: 'accept', content: { custom: 'apollo' } })
  109. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  110. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  111. await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'ask me' }] })
  112. expect(harness.elicitationRequests[0]).toMatchObject({
  113. requestedSchema: {
  114. properties: { custom: { type: 'string', title: 'What should I name it?' } },
  115. required: ['custom'],
  116. },
  117. })
  118. const toolResult = harness.ctx.agents.get(SessionId(sessionId))!.session.events.find(event => event.type === 'tool/result')
  119. expect(JSON.stringify(toolResult)).toContain('apollo')
  120. })
  121. it('supports ACP custom answers alongside choices', async () => {
  122. harness = await makeBridgeHarness({ storageDir, withAskUser: true })
  123. harness.onElicitation = () => ({ action: 'accept', content: { custom: 'Use Zig' } })
  124. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  125. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  126. const agent = harness.ctx.agents.get(SessionId(sessionId))!
  127. const result = await harness.ctx.userInteraction.ask({
  128. agent,
  129. questions: [{
  130. id: 'language',
  131. question: 'Which language?',
  132. detail: 'Choose the implementation language for this project.',
  133. options: [{ label: 'TypeScript' }],
  134. }],
  135. })
  136. expect(result).toEqual({ answers: [{ id: 'language', selected: [], custom: 'Use Zig' }] })
  137. expect(harness.elicitationRequests[0]).toMatchObject({
  138. message: 'Which language?\n\nChoose the implementation language for this project.',
  139. requestedSchema: {
  140. properties: {
  141. choice: {
  142. title: 'Which language?',
  143. description: 'Choose one option, or fill a custom answer below.',
  144. oneOf: [{ const: 'TypeScript', title: 'TypeScript' }],
  145. },
  146. custom: { type: 'string' },
  147. },
  148. required: [],
  149. },
  150. })
  151. })
  152. it('treats ACP custom answers as overriding selected choices', async () => {
  153. harness = await makeBridgeHarness({ storageDir, withAskUser: true })
  154. harness.onElicitation = () => ({ action: 'accept', content: { choice: 'TypeScript', custom: 'Use Zig' } })
  155. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  156. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  157. const agent = harness.ctx.agents.get(SessionId(sessionId))!
  158. await expect(harness.ctx.userInteraction.ask({
  159. agent,
  160. questions: [{
  161. id: 'language',
  162. question: 'Which language?',
  163. options: [{ label: 'TypeScript' }],
  164. }],
  165. })).resolves.toEqual({ answers: [{ id: 'language', selected: [], custom: 'Use Zig' }] })
  166. })
  167. it('supports ACP multi-select answers', async () => {
  168. harness = await makeBridgeHarness({ storageDir, withAskUser: true })
  169. harness.onElicitation = () => ({ action: 'accept', content: { choice: ['Tests', 'Docs'] } })
  170. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  171. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  172. const agent = harness.ctx.agents.get(SessionId(sessionId))!
  173. await expect(harness.ctx.userInteraction.ask({
  174. agent,
  175. questions: [{
  176. id: 'targets',
  177. question: 'Pick',
  178. options: [{ label: 'Tests' }, { label: 'Docs' }],
  179. multiSelect: true,
  180. }],
  181. })).resolves.toEqual({ answers: [{ id: 'targets', selected: ['Tests', 'Docs'] }] })
  182. })
  183. it('reports ACP ask-user routing and answer failures as structured errors', async () => {
  184. harness = await makeBridgeHarness({ storageDir, withAskUser: true })
  185. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  186. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  187. const agent = harness.ctx.agents.get(SessionId(sessionId))!
  188. await expect(harness.ctx.userInteraction.ask({ questions: [{ id: 'x', question: 'No agent?' }] }))
  189. .rejects.toMatchObject({ name: 'UserInteractionError', code: 'NO_AGENT' })
  190. const impostor = { session: { id: agent.session.id } } as typeof agent
  191. await expect(harness.ctx.userInteraction.ask({ agent: impostor, questions: [{ id: 'x', question: 'No session?' }] }))
  192. .rejects.toMatchObject({ code: 'NO_SESSION' })
  193. harness.onElicitation = () => ({ action: 'cancel' })
  194. await expect(harness.ctx.userInteraction.ask({ agent, questions: [{ id: 'x', question: 'Cancel?' }] }))
  195. .rejects.toMatchObject({ code: 'ASK_CANCELLED' })
  196. harness.onElicitation = () => ({ action: 'accept', content: {} })
  197. await expect(harness.ctx.userInteraction.ask({ agent, questions: [{ id: 'x', question: 'Empty?' }] }))
  198. .rejects.toMatchObject({ code: 'NO_ANSWER' })
  199. harness.onElicitation = () => { throw new Error('client boom') }
  200. await expect(harness.ctx.userInteraction.ask({ agent, questions: [{ id: 'x', question: 'Client fails?' }], signal: new AbortController().signal }))
  201. .rejects.toMatchObject({ code: 'ASK_FAILED' })
  202. })
  203. it('aborts ACP ask-user requests before and while waiting for elicitation', async () => {
  204. harness = await makeBridgeHarness({ storageDir, withAskUser: true })
  205. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  206. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  207. const agent = harness.ctx.agents.get(SessionId(sessionId))!
  208. const alreadyAborted = new AbortController()
  209. alreadyAborted.abort()
  210. await expect(harness.ctx.userInteraction.ask({ agent, questions: [{ id: 'x', question: 'Already?' }], signal: alreadyAborted.signal }))
  211. .rejects.toMatchObject({ code: 'ASK_ABORTED' })
  212. let abortedReads = 0
  213. const racingAbort = {
  214. get aborted() { return abortedReads++ > 0 },
  215. addEventListener() {},
  216. removeEventListener() {},
  217. dispatchEvent() { return false },
  218. onabort: null,
  219. reason: undefined,
  220. throwIfAborted() {},
  221. } as AbortSignal
  222. await expect(harness.ctx.userInteraction.ask({ agent, questions: [{ id: 'x', question: 'Raced?' }], signal: racingAbort }))
  223. .rejects.toMatchObject({ code: 'ASK_ABORTED' })
  224. let release: ((value: { action: 'accept'; content: { custom: string } }) => void) | undefined
  225. harness.onElicitation = () => new Promise((resolve) => { release = resolve })
  226. const pendingAbort = new AbortController()
  227. const ask = harness.ctx.userInteraction.ask({ agent, questions: [{ id: 'x', question: 'Pending?' }], signal: pendingAbort.signal })
  228. await new Promise(resolve => setImmediate(resolve))
  229. pendingAbort.abort()
  230. await expect(ask).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  231. release?.({ action: 'accept', content: { custom: 'too late' } })
  232. })
  233. it('allows multiple concurrent sessions, each with a distinct id', async () => {
  234. harness = await makeBridgeHarness({ storageDir, script: [] })
  235. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  236. const a = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  237. const b = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  238. expect(a.sessionId).toBeTruthy()
  239. expect(b.sessionId).toBeTruthy()
  240. expect(a.sessionId).not.toBe(b.sessionId)
  241. // Both agents are live and independently registered.
  242. expect(harness.ctx.agents.get(SessionId(a.sessionId))).toBeDefined()
  243. expect(harness.ctx.agents.get(SessionId(b.sessionId))).toBeDefined()
  244. })
  245. it('rejects a non-absolute cwd but accepts any absolute cwd (per-session workspace)', async () => {
  246. harness = await makeBridgeHarness({ storageDir })
  247. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  248. // Relative cwd is still rejected (it becomes the session header / bash workdir).
  249. await expect(harness.client.newSession({ cwd: 'relative/path', mcpServers: [] }))
  250. .rejects.toThrow(/absolute/)
  251. // An absolute cwd that differs from the server launch dir is now ACCEPTED —
  252. // the per-session cwd is honored (routed to the bash workdir), so the server
  253. // no longer has to launch in the workspace.
  254. const res = await harness.client.newSession({ cwd: '/tmp', mcpServers: [] })
  255. expect(res.sessionId).toBeTruthy()
  256. // The session header records that cwd, so its bash tools run there.
  257. expect(harness.ctx.agents.get(SessionId(res.sessionId))!.session.header.cwd).toBe('/tmp')
  258. })
  259. it('rejects non-empty additionalDirectories', async () => {
  260. harness = await makeBridgeHarness({ storageDir })
  261. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  262. await expect(harness.client.newSession({ cwd: process.cwd(), mcpServers: [], additionalDirectories: ['/x'] }))
  263. .rejects.toThrow(/additionalDirectories/)
  264. })
  265. it('rejects an empty prompt without hanging', async () => {
  266. harness = await makeBridgeHarness({ storageDir, script: [] })
  267. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  268. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  269. await expect(harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: ' ' }] }))
  270. .rejects.toThrow(/empty prompt/)
  271. })
  272. it('rejects image content in a prompt (text-only capabilities)', async () => {
  273. harness = await makeBridgeHarness({ storageDir, script: [] })
  274. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  275. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  276. await expect(harness.client.prompt({
  277. sessionId,
  278. prompt: [{ type: 'image', mimeType: 'image/png', data: 'AA==' }],
  279. })).rejects.toThrow(/text/)
  280. })
  281. it('accepts a resource_link prompt by rendering the link into the text sent to the agent', async () => {
  282. harness = await makeBridgeHarness({ storageDir, script: [textResponse('ok')] })
  283. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  284. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  285. const result = await harness.client.prompt({
  286. sessionId,
  287. prompt: [
  288. { type: 'text', text: 'fix the bug in' },
  289. { type: 'resource_link', uri: 'file:///x.ts', name: 'x.ts' },
  290. ],
  291. })
  292. expect(result.stopReason).toBe('end_turn')
  293. const user = harness.ctx.agents.get(SessionId(sessionId))!.session.events.find(event => event.type === 'user/message')
  294. expect(JSON.stringify(user)).toContain('resource_link')
  295. })
  296. it('rejects canonical session references when the optional capability is not mounted', async () => {
  297. harness = await makeBridgeHarness({ storageDir, script: [] })
  298. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  299. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  300. await expect(harness.client.prompt({
  301. sessionId,
  302. prompt: [{ type: 'resource_link', uri: encodeSessionReferenceUri(SessionId('source')), name: 'source' }],
  303. })).rejects.toThrow(/session reference capability unavailable/)
  304. expect(harness.ctx.agents.get(SessionId(sessionId))?.session.events).toHaveLength(0)
  305. })
  306. it('reports malformed inline session references at the ACP request boundary', async () => {
  307. harness = await makeBridgeHarness({ storageDir, script: [] })
  308. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  309. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  310. await expect(harness.client.prompt({
  311. sessionId,
  312. prompt: [{ type: 'text', text: 'use dsh-session:IiJ' }],
  313. })).rejects.toThrow(/invalid session reference/)
  314. expect(harness.ctx.agents.get(SessionId(sessionId))?.session.events).toHaveLength(0)
  315. })
  316. it('injects ACP session references before sending the direct prompt', async () => {
  317. harness = await makeBridgeHarness({ storageDir, withSessionReferences: true, script: [textResponse('ok')] })
  318. const source = harness.ctx.sessions.create(SessionId('source'), { meta: { cwd: '/source' } })
  319. source.append('user/message', {
  320. content: [{ type: 'text', text: 'source background' }],
  321. source: { kind: 'user' },
  322. }, { surfaceOp: 'append' })
  323. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  324. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  325. const mention = formatSessionReferenceMention({ sessionId: source.id, label: 'source-inline' })
  326. const result = await harness.client.prompt({
  327. sessionId,
  328. prompt: [
  329. { type: 'text', text: `use ${mention} and ` },
  330. { type: 'resource_link', uri: encodeSessionReferenceUri(source.id), name: 'source-link' },
  331. ],
  332. })
  333. expect(result.stopReason).toBe('end_turn')
  334. const target = harness.ctx.agents.get(SessionId(sessionId))!.session
  335. const context = target.events.find(event =>
  336. event.type === 'user/message' && event.data.source.kind === 'session-reference')
  337. expect(context?.type === 'user/message' && context.data.source).toMatchObject({
  338. kind: 'session-reference',
  339. references: [{ sessionId: 'source', label: 'source-inline' }],
  340. })
  341. const user = target.events.find(event =>
  342. event.type === 'user/message' && event.data.source.kind === 'user')
  343. expect(user?.type === 'user/message' && user.data.content).toEqual([
  344. { type: 'text', text: 'use @source-inline and @source-link' },
  345. ])
  346. const request = JSON.stringify(harness.adapter.requests[0]?.messages)
  347. expect(request).toContain('untrusted, read-only snapshot')
  348. expect(request).toContain('source background')
  349. expect(request.indexOf('source background')).toBeLessThan(request.indexOf('use @source-inline and @source-link'))
  350. })
  351. it('rejects a failed referenced-session read before starting a turn', async () => {
  352. harness = await makeBridgeHarness({ storageDir, withSessionReferences: true, script: [] })
  353. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  354. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  355. await expect(harness.client.prompt({
  356. sessionId,
  357. prompt: [{ type: 'resource_link', uri: encodeSessionReferenceUri(SessionId('missing')), name: 'missing' }],
  358. })).rejects.toThrow(/preparation failed/)
  359. expect(harness.ctx.agents.get(SessionId(sessionId))?.session.events).toHaveLength(0)
  360. })
  361. it('cancels reference preparation before a turn is created', async () => {
  362. harness = await makeBridgeHarness({ storageDir, withSessionReferences: true, script: [] })
  363. const source = harness.ctx.sessions.create(SessionId('source'))
  364. const snapshot = await harness.ctx.sessionQuery.readSurface(source.id)
  365. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  366. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  367. let releaseRead: (() => void) | undefined
  368. const readSurface = vi.spyOn(harness.ctx.sessionQuery, 'readSurface').mockImplementationOnce(async () => {
  369. await new Promise<void>((resolve) => { releaseRead = resolve })
  370. return snapshot
  371. })
  372. const pending = harness.client.prompt({
  373. sessionId,
  374. prompt: [{ type: 'resource_link', uri: encodeSessionReferenceUri(source.id), name: 'source' }],
  375. })
  376. await vi.waitFor(() => { expect(releaseRead).toBeTypeOf('function') })
  377. await harness.client.cancel({ sessionId })
  378. await expect(pending).resolves.toEqual({ stopReason: 'cancelled' })
  379. expect(harness.ctx.agents.get(SessionId(sessionId))?.session.events).toHaveLength(0)
  380. releaseRead?.()
  381. await Promise.resolve()
  382. readSurface.mockRestore()
  383. })
  384. it('rejects a prompt for an unknown session', async () => {
  385. harness = await makeBridgeHarness({ storageDir })
  386. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  387. await expect(harness.client.prompt({ sessionId: 'nope', prompt: [{ type: 'text', text: 'hi' }] }))
  388. .rejects.toThrow(/unknown session/)
  389. })
  390. it('negotiates an unsupported protocol version down to the supported one', async () => {
  391. harness = await makeBridgeHarness({ storageDir })
  392. const res = await harness.client.initialize({ protocolVersion: 999, clientCapabilities: {} })
  393. expect(res.protocolVersion).toBe(PROTOCOL_VERSION)
  394. })
  395. it('a cancel for an unknown/absent session is a silent no-op', async () => {
  396. harness = await makeBridgeHarness({ storageDir })
  397. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  398. // No session created yet — cancel must not throw.
  399. await expect(harness.client.cancel({ sessionId: 'nope' })).resolves.toBeUndefined()
  400. })
  401. it('authenticate is a no-op (no auth methods advertised)', async () => {
  402. harness = await makeBridgeHarness({ storageDir })
  403. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  404. await expect(harness.client.authenticate({ methodId: 'whatever' })).resolves.toBeDefined()
  405. })
  406. it('renders the deployment persona into ACP-created agents\' requests', async () => {
  407. harness = await makeBridgeHarness({
  408. storageDir,
  409. script: [textResponse('ok')],
  410. persona: 'be terse',
  411. })
  412. await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
  413. // Create + prompt so the system-prompt plugin's persona section reaches
  414. // the model request of an agent the BRIDGE created (session/new).
  415. const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
  416. await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'hi' }] })
  417. expect(harness.adapter.requests[0]?.system).toContain('be terse')
  418. })
  419. })