queue-dock.client.spec.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // @vitest-environment jsdom
  2. /**
  3. * QueueDock rendering and operations: authoritative rows, inline editing,
  4. * collapse state, removal, QueueDock Steer, failure notices, and live retirement.
  5. */
  6. import { afterEach, describe, expect, it, vi } from 'vitest'
  7. import { act, cleanup, fireEvent, render, waitFor } from '@testing-library/react'
  8. import { useSyncExternalStore } from 'react'
  9. import type {
  10. QueuedMessage, SessionListState, SessionSnapshot,
  11. } from '@deepseek-ai/dsh-api-session-controller/client'
  12. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  13. import type { SnapshotSelectorHook } from '@deepseek-ai/dsh-client-ui-slots'
  14. import { createSnapshotStore } from '@deepseek-ai/dsh-client-store'
  15. import {
  16. bindSnapshotSelector, conversationSnapshot, makeTranslate,
  17. } from '@deepseek-ai/dsh-client-test-runtime'
  18. import type { SessionPendingInteractionSnapshot } from '@deepseek-ai/dsh-client-ui-session/client'
  19. import { zh as commonZh } from '@deepseek-ai/dsh-client-locale/src/locales/zh.ts'
  20. import type { QueueItemId } from '../src/client/contract/queue.ts'
  21. import type { InputState } from '../src/client/contract/input.ts'
  22. import { zh } from '../src/client/locales.ts'
  23. import { QueueDock, queueDockEntry, type QueueDockInjected, type QueueDockProps } from '../src/client/queue/QueueDock.tsx'
  24. afterEach(cleanup)
  25. const SID = 's1' as SessionId
  26. const iid = (id: string): QueueItemId => id as QueueItemId
  27. function row(id: string, text: string | null, preview = text ?? '[image]'): QueuedMessage {
  28. return {
  29. id: iid(id), messageId: `message-${id}` as never, placement: 'queued',
  30. content: text === null ? [{ type: 'image', data: 'x' } as never] : [{ type: 'text', text }],
  31. preview, text,
  32. }
  33. }
  34. function snapshotWith(queue: QueuedMessage[]): SessionSnapshot {
  35. return {
  36. sessionId: SID, queue, running: true, removed: false, openState: 'open', openError: null,
  37. hasMore: false, loadingOlder: false, promptError: null, blank: false, subagent: null,
  38. pendingSubmissions: [],
  39. lastAgentError: null, promptAttempted: true, awaitingFirstTurn: false,
  40. }
  41. }
  42. /** Minimal live source backing the useSession stub. */
  43. function liveSession(initial: SessionSnapshot) {
  44. let snapshot = initial
  45. const listeners = new Set<() => void>()
  46. const useSession: SnapshotSelectorHook<SessionSnapshot> = selector =>
  47. useSyncExternalStore(
  48. (listener) => {
  49. listeners.add(listener)
  50. return () => listeners.delete(listener)
  51. },
  52. () => selector(snapshot),
  53. )
  54. return {
  55. useSession,
  56. push(next: SessionSnapshot): void {
  57. snapshot = next
  58. for (const listener of [...listeners]) listener()
  59. },
  60. }
  61. }
  62. const INPUT_STATE: InputState = { draft: '', attachmentIds: [], draftRev: 0, phase: 'plain', occurrences: [], queue: [] }
  63. const t: QueueDockProps['t'] = makeTranslate(zh, commonZh)
  64. function kitFor(snapshot: SessionSnapshot, injected: Partial<QueueDockInjected> = {}) {
  65. return {
  66. sessionId: SID,
  67. t,
  68. useSessions: (() => { throw new Error('unused') }) as unknown as SnapshotSelectorHook<SessionListState>,
  69. useSessionPendingInteraction: bindSnapshotSelector(
  70. createSnapshotStore<SessionPendingInteractionSnapshot>(new Map()),
  71. ),
  72. useWorkspaces: (() => { throw new Error('unused') }) as never,
  73. useProjection: (() => undefined) as never,
  74. useConversation: bindSnapshotSelector(createSnapshotStore(conversationSnapshot())),
  75. useChat: (() => { throw new Error('unused') }) as QueueDockProps['useChat'],
  76. useTrajectory: (() => { throw new Error('unused') }) as QueueDockProps['useTrajectory'],
  77. useInput: (() => { throw new Error('unused') }) as never,
  78. inputActions: { setDraft: () => {}, submit: () => {} } as never,
  79. session: snapshot,
  80. input: INPUT_STATE,
  81. updateQueue: vi.fn(() => Promise.resolve()),
  82. notify: vi.fn(),
  83. loadImage: vi.fn(() => Promise.resolve('blob:unused')),
  84. ...injected,
  85. }
  86. }
  87. /** One queued row carrying a durable image reference (plus optional leading text). */
  88. function imageRow(id: string, refId: string, text = ''): QueuedMessage {
  89. return {
  90. id: iid(id), messageId: `message-${id}` as never, placement: 'queued',
  91. content: [
  92. ...text === '' ? [] : [{ type: 'text' as const, text }],
  93. {
  94. type: 'image',
  95. attachment: { attachmentId: refId, mediaType: 'image/png', bytes: 1, width: 1, height: 1 },
  96. } as never,
  97. ],
  98. preview: text, text: null,
  99. }
  100. }
  101. describe('QueueDock', () => {
  102. it('renders null while the queue is empty', () => {
  103. const snap = snapshotWith([])
  104. const source = liveSession(snap)
  105. const { container } = render(<QueueDock {...kitFor(snap)} useSession={source.useSession} />)
  106. expect(container.innerHTML).toBe('')
  107. })
  108. it('renders a queued local echo in the dock and hands off by rpcId', () => {
  109. const pending = {
  110. ...snapshotWith([]),
  111. pendingSubmissions: [{
  112. requestId: 'req-local-queue' as never,
  113. placement: 'queued' as const,
  114. time: 1,
  115. text: '等待上传',
  116. attachments: [
  117. {
  118. type: 'image' as const,
  119. value: { previewUrl: 'blob:queue-preview', name: 'queue.png' },
  120. },
  121. {
  122. type: 'file' as const,
  123. value: {
  124. attachmentId: 'file-local' as never,
  125. name: 'notes.txt',
  126. bytes: 2447 * 1024 * 1024,
  127. },
  128. },
  129. ],
  130. }],
  131. }
  132. const source = liveSession(pending)
  133. const props = kitFor(pending)
  134. const view = render(<QueueDock {...props} useSession={source.useSession} />)
  135. expect(view.getByText('等待上传').closest('[data-submission-echo]')).not.toBeNull()
  136. expect(view.getByRole('img', { name: '排队消息图片' }).getAttribute('src')).toBe('blob:queue-preview')
  137. expect(view.getByLabelText('排队文件 notes.txt').textContent).toContain('2.4GB')
  138. expect(view.getByRole('status').textContent).toBe('发送中…')
  139. for (const name of ['编辑排队消息', '删除排队消息', '插话发送']) {
  140. const button = view.getByRole('button', { name }) as HTMLButtonElement
  141. expect(button.disabled).toBe(true)
  142. fireEvent.click(button)
  143. }
  144. expect(props.updateQueue).not.toHaveBeenCalled()
  145. expect(view.queryByRole('textbox')).toBeNull()
  146. act(() => {
  147. source.push({
  148. ...pending,
  149. queue: [{ ...row('accepted', '等待上传'), rpcId: 'req-local-queue' as never }],
  150. })
  151. })
  152. expect(view.getAllByText('等待上传')).toHaveLength(1)
  153. expect(view.container.querySelector('[data-submission-echo]')).toBeNull()
  154. expect(view.queryByRole('status')).toBeNull()
  155. for (const name of ['编辑排队消息', '删除排队消息', '插话发送']) {
  156. expect((view.getByRole('button', { name }) as HTMLButtonElement).disabled).toBe(false)
  157. }
  158. fireEvent.click(view.getByRole('button', { name: '编辑排队消息' }))
  159. expect((view.getByRole('textbox') as HTMLInputElement).value).toBe('等待上传')
  160. })
  161. it('keeps sending status visible while a queue containing local submissions is collapsed', () => {
  162. const pending: SessionSnapshot = {
  163. ...snapshotWith([row('accepted', '已排队')]),
  164. pendingSubmissions: [{
  165. requestId: 'req-waiting' as never, placement: 'queued', time: 1,
  166. text: '等待发送', attachments: [],
  167. }],
  168. }
  169. const source = liveSession(pending)
  170. const view = render(<QueueDock {...kitFor(pending)} useSession={source.useSession} />)
  171. expect(view.getByRole('status').textContent).toBe('发送中…')
  172. const header = view.getByRole('button', { name: /2 条排队消息\s*发送中…/ })
  173. expect(header.getAttribute('aria-expanded')).toBe('false')
  174. fireEvent.click(header)
  175. expect(view.getAllByRole('status')).toHaveLength(1)
  176. expect(view.getByRole('status').closest('[data-submission-echo]')).not.toBeNull()
  177. act(() => { source.push(snapshotWith([row('accepted', '已排队')])) })
  178. expect(view.queryByRole('status')).toBeNull()
  179. })
  180. it('leaves pending steering to the conversation flow', () => {
  181. const steering = { ...row('s-1', 'interrupt'), placement: 'steering' as const }
  182. const snap = snapshotWith([steering])
  183. const source = liveSession(snap)
  184. const { container } = render(<QueueDock {...kitFor(snap)} useSession={source.useSession} />)
  185. expect(container.innerHTML).toBe('')
  186. })
  187. it('renders one row directly and defaults multiple rows to a collapsible count header', () => {
  188. const single = snapshotWith([row('i-1', 'one')])
  189. const source = liveSession(single)
  190. const view = render(<QueueDock {...kitFor(single)} useSession={source.useSession} />)
  191. expect(view.queryByRole('button', { name: '1 条排队消息' })).toBeNull()
  192. expect(view.getByText('one')).toBeTruthy()
  193. act(() => { source.push(snapshotWith([row('i-1', 'one'), row('i-2', 'two')])) })
  194. const header = view.getByRole('button', { name: '2 条排队消息' })
  195. expect(header.getAttribute('aria-expanded')).toBe('false')
  196. expect(document.getElementById(header.getAttribute('aria-controls')!)).toBeTruthy()
  197. expect(view.queryByText('one')).toBeNull()
  198. expect(view.queryByText('two')).toBeNull()
  199. fireEvent.click(header)
  200. expect(header.getAttribute('aria-expanded')).toBe('true')
  201. expect(view.getByText('one')).toBeTruthy()
  202. expect(view.getByText('two')).toBeTruthy()
  203. fireEvent.click(header)
  204. expect(header.getAttribute('aria-expanded')).toBe('false')
  205. expect(view.queryByText('one')).toBeNull()
  206. })
  207. it('keeps an active single-row editor visible when another item arrives', () => {
  208. const single = snapshotWith([row('i-edit', 'before')])
  209. const source = liveSession(single)
  210. const view = render(<QueueDock {...kitFor(single)} useSession={source.useSession} />)
  211. fireEvent.click(view.getByLabelText('编辑排队消息'))
  212. fireEvent.change(view.getByLabelText('编辑排队消息'), { target: { value: 'draft' } })
  213. act(() => {
  214. source.push(snapshotWith([row('i-edit', 'before'), row('i-2', 'second')]))
  215. })
  216. const header = view.getByRole('button', { name: '2 条排队消息' })
  217. expect(header).toHaveProperty('disabled', true)
  218. expect(header.getAttribute('aria-expanded')).toBe('true')
  219. expect(view.getByRole('textbox', { name: '编辑排队消息' })).toHaveProperty('value', 'draft')
  220. expect(view.getByText('second')).toBeTruthy()
  221. fireEvent.click(view.getByLabelText('取消编辑'))
  222. expect(header).toHaveProperty('disabled', false)
  223. expect(header.getAttribute('aria-expanded')).toBe('false')
  224. expect(view.queryByText('second')).toBeNull()
  225. })
  226. it('keeps an in-flight row action visible when another item arrives', async () => {
  227. const single = snapshotWith([row('i-remove', 'remove me')])
  228. const source = liveSession(single)
  229. let finishUpdate: (() => void) | undefined
  230. const updateQueue = vi.fn(() => new Promise<void>((resolve) => { finishUpdate = resolve }))
  231. const view = render(
  232. <QueueDock {...kitFor(single, { updateQueue })} useSession={source.useSession} />,
  233. )
  234. fireEvent.click(view.getByLabelText('删除排队消息'))
  235. act(() => {
  236. source.push(snapshotWith([row('i-remove', 'remove me'), row('i-2', 'second')]))
  237. })
  238. const header = view.getByRole('button', { name: '2 条排队消息' })
  239. expect(header).toHaveProperty('disabled', true)
  240. expect(header.getAttribute('aria-expanded')).toBe('true')
  241. expect(view.getByText('remove me')).toBeTruthy()
  242. expect(view.getByText('second')).toBeTruthy()
  243. expect(updateQueue).toHaveBeenCalledOnce()
  244. await act(async () => {
  245. finishUpdate?.()
  246. await Promise.resolve()
  247. })
  248. await waitFor(() => {
  249. expect(header).toHaveProperty('disabled', false)
  250. expect(header.getAttribute('aria-expanded')).toBe('false')
  251. })
  252. })
  253. it('defaults a new multi-row queue to collapsed after the prior queue empties', () => {
  254. const first = snapshotWith([row('i-1', 'one'), row('i-2', 'two')])
  255. const source = liveSession(first)
  256. const view = render(<QueueDock {...kitFor(first)} useSession={source.useSession} />)
  257. fireEvent.click(view.getByRole('button', { name: '2 条排队消息' }))
  258. expect(view.getByText('one')).toBeTruthy()
  259. act(() => { source.push(snapshotWith([])) })
  260. expect(view.container.innerHTML).toBe('')
  261. act(() => {
  262. source.push(snapshotWith([row('i-3', 'three'), row('i-4', 'four')]))
  263. })
  264. const header = view.getByRole('button', { name: '2 条排队消息' })
  265. expect(header.getAttribute('aria-expanded')).toBe('false')
  266. expect(view.queryByText('three')).toBeNull()
  267. })
  268. it('renders active actions and disables editing for mixed-content rows', () => {
  269. const snap = snapshotWith([
  270. row('i-1', '第一条排队消息'),
  271. row('i-2', null, 'image [image]'),
  272. ])
  273. const source = liveSession(snap)
  274. const { container, getByRole } = render(<QueueDock {...kitFor(snap)} useSession={source.useSession} />)
  275. fireEvent.click(getByRole('button', { name: '2 条排队消息' }))
  276. expect([...container.querySelectorAll('li')].map(item => item.textContent))
  277. .toEqual(['第一条排队消息', 'image [image]'])
  278. expect(container.querySelectorAll('button')).toHaveLength(7)
  279. expect(container.querySelectorAll('[aria-label="编辑排队消息"]')).toHaveLength(2)
  280. expect(container.querySelectorAll('[aria-label="删除排队消息"]')).toHaveLength(2)
  281. expect(container.querySelectorAll('[aria-label="插话发送"]')).toHaveLength(2)
  282. expect((container.querySelectorAll('[aria-label="编辑排队消息"]')[0] as HTMLButtonElement).disabled).toBe(false)
  283. expect((container.querySelectorAll('[aria-label="编辑排队消息"]')[1] as HTMLButtonElement).disabled).toBe(true)
  284. expect(container.querySelectorAll('[aria-label="编辑排队消息"]')[1]?.getAttribute('title'))
  285. .toBe('包含非文本内容,暂不支持编辑')
  286. })
  287. it('renders queued image thumbnails from durable references beside the text preview', async () => {
  288. const loadImage = vi.fn(() => Promise.resolve('blob:thumb-1'))
  289. const snap = snapshotWith([imageRow('i-img', 'att-9', '带图消息')])
  290. const source = liveSession(snap)
  291. const { container } = render(
  292. <QueueDock {...kitFor(snap, { loadImage })} useSession={source.useSession} />,
  293. )
  294. await waitFor(() => {
  295. expect(container.querySelector('img')?.getAttribute('src')).toBe('blob:thumb-1')
  296. })
  297. expect(loadImage).toHaveBeenCalledWith(expect.objectContaining({ attachmentId: 'att-9' }))
  298. expect(container.querySelector('img')?.getAttribute('alt')).toBe('排队消息图片')
  299. expect(container.querySelector('li')?.textContent).toBe('带图消息')
  300. })
  301. it('renders durable files and images in their original queue order', async () => {
  302. const loadImage = vi.fn(() => Promise.resolve('blob:mixed'))
  303. const mixed: QueuedMessage = {
  304. id: iid('i-mixed'), messageId: 'message-i-mixed' as never, placement: 'queued',
  305. content: [
  306. {
  307. type: 'file',
  308. attachment: { attachmentId: 'file-durable' as never, name: 'report.csv', bytes: 427 },
  309. },
  310. {
  311. type: 'image',
  312. attachment: {
  313. attachmentId: 'image-durable' as never,
  314. mediaType: 'image/png', bytes: 1, width: 1, height: 1,
  315. },
  316. },
  317. ],
  318. preview: '', text: null,
  319. }
  320. const snap = snapshotWith([mixed])
  321. const source = liveSession(snap)
  322. const view = render(<QueueDock {...kitFor(snap, { loadImage })} useSession={source.useSession} />)
  323. await waitFor(() => { expect(view.container.querySelector('img')).not.toBeNull() })
  324. const group = view.getByLabelText('排队文件 report.csv').parentElement
  325. expect(group?.children).toHaveLength(2)
  326. expect(group?.children[0]?.getAttribute('aria-label')).toBe('排队文件 report.csv')
  327. expect(group?.children[1]?.tagName).toBe('IMG')
  328. })
  329. it('keeps the empty thumbnail placeholder when the image read fails', async () => {
  330. const loadImage = vi.fn(() => Promise.reject(new Error('read denied')))
  331. const snap = snapshotWith([imageRow('i-broken', 'att-x')])
  332. const source = liveSession(snap)
  333. const { container } = render(
  334. <QueueDock {...kitFor(snap, { loadImage })} useSession={source.useSession} />,
  335. )
  336. await act(async () => { await Promise.resolve() })
  337. expect(loadImage).toHaveBeenCalled()
  338. expect(container.querySelector('img')).toBeNull()
  339. })
  340. it('ignores a thumbnail resolution landing after unmount', async () => {
  341. let resolveUrl: ((url: string) => void) | undefined
  342. const loadImage = vi.fn(() => new Promise<string>((resolve) => { resolveUrl = resolve }))
  343. const snap = snapshotWith([imageRow('i-late', 'att-late')])
  344. const source = liveSession(snap)
  345. const { unmount } = render(
  346. <QueueDock {...kitFor(snap, { loadImage })} useSession={source.useSession} />,
  347. )
  348. unmount()
  349. await act(async () => {
  350. resolveUrl?.('blob:late')
  351. await Promise.resolve()
  352. })
  353. expect(loadImage).toHaveBeenCalledTimes(1)
  354. })
  355. it('edits text inline with save and cancel controls, then saves with the same item identity', async () => {
  356. const snap = snapshotWith([row('i-edit', 'before')])
  357. const source = liveSession(snap)
  358. const updateQueue = vi.fn(() => Promise.resolve())
  359. const { getByLabelText, queryByLabelText } = render(
  360. <QueueDock {...kitFor(snap, { updateQueue })} useSession={source.useSession} />,
  361. )
  362. fireEvent.click(getByLabelText('编辑排队消息'))
  363. const editor = getByLabelText('编辑排队消息') as HTMLInputElement
  364. expect(getByLabelText('保存排队消息')).toBeTruthy()
  365. expect(getByLabelText('取消编辑')).toBeTruthy()
  366. expect(queryByLabelText('删除排队消息')).toBeNull()
  367. fireEvent.change(editor, { target: { value: 'after' } })
  368. fireEvent.keyDown(editor, { key: 'Enter' })
  369. await waitFor(() => {
  370. expect(updateQueue).toHaveBeenCalledWith(iid('i-edit'), {
  371. kind: 'edit',
  372. content: [{ type: 'text', text: 'after' }],
  373. })
  374. })
  375. })
  376. it('cancels an edit by button or Escape without mutating the queue', () => {
  377. const snap = snapshotWith([row('i-edit', 'before')])
  378. const source = liveSession(snap)
  379. const updateQueue = vi.fn(() => Promise.resolve())
  380. const { getByLabelText, getByText } = render(
  381. <QueueDock {...kitFor(snap, { updateQueue })} useSession={source.useSession} />,
  382. )
  383. fireEvent.click(getByLabelText('编辑排队消息'))
  384. fireEvent.change(getByLabelText('编辑排队消息'), { target: { value: 'abandoned' } })
  385. fireEvent.click(getByLabelText('取消编辑'))
  386. expect(getByText('before')).toBeTruthy()
  387. fireEvent.click(getByLabelText('编辑排队消息'))
  388. fireEvent.keyDown(getByLabelText('编辑排队消息'), { key: 'Escape' })
  389. expect(getByText('before')).toBeTruthy()
  390. expect(updateQueue).not.toHaveBeenCalled()
  391. })
  392. it('keeps editing during IME composition and disables a blank save', () => {
  393. const snap = snapshotWith([row('i-edit', 'before')])
  394. const source = liveSession(snap)
  395. const updateQueue = vi.fn(() => Promise.resolve())
  396. const { getByLabelText } = render(
  397. <QueueDock {...kitFor(snap, { updateQueue })} useSession={source.useSession} />,
  398. )
  399. fireEvent.click(getByLabelText('编辑排队消息'))
  400. const editor = getByLabelText('编辑排队消息')
  401. fireEvent.change(editor, { target: { value: ' ' } })
  402. expect(getByLabelText('保存排队消息')).toHaveProperty('disabled', true)
  403. fireEvent.change(editor, { target: { value: '输入中' } })
  404. fireEvent.keyDown(editor, { key: 'Enter', isComposing: true })
  405. expect(updateQueue).not.toHaveBeenCalled()
  406. expect(getByLabelText('编辑排队消息')).toBeTruthy()
  407. })
  408. it('removes the addressed row', async () => {
  409. const snap = snapshotWith([row('i-1', 'one'), row('i-2', 'two')])
  410. const source = liveSession(snap)
  411. const updateQueue = vi.fn(() => Promise.resolve())
  412. const { getAllByLabelText, getByRole } = render(
  413. <QueueDock {...kitFor(snap, { updateQueue })} useSession={source.useSession} />,
  414. )
  415. fireEvent.click(getByRole('button', { name: '2 条排队消息' }))
  416. fireEvent.click(getAllByLabelText('删除排队消息')[0]!)
  417. await waitFor(() => {
  418. expect(updateQueue).toHaveBeenCalledWith(iid('i-1'), { kind: 'remove' })
  419. })
  420. })
  421. it('strictly steers complete row content only while the agent is running', async () => {
  422. const running = snapshotWith([row('i-steer', null, 'image [image]')])
  423. const source = liveSession(running)
  424. const updateQueue = vi.fn(() => Promise.resolve())
  425. const rendered = render(
  426. <QueueDock {...kitFor(running, { updateQueue })} useSession={source.useSession} />,
  427. )
  428. const button = rendered.getByLabelText('插话发送')
  429. expect(button).toHaveProperty('disabled', false)
  430. fireEvent.click(button)
  431. await waitFor(() => {
  432. expect(updateQueue).toHaveBeenCalledWith(iid('i-steer'), { kind: 'steer' })
  433. })
  434. act(() => { source.push({ ...running, running: false }) })
  435. expect(rendered.getByLabelText('插话发送')).toHaveProperty('disabled', true)
  436. expect(rendered.getByLabelText('插话发送').getAttribute('title')).toBe('仅运行中可插话发送')
  437. })
  438. it('renders ordinary queue actions for a continuable child', () => {
  439. const snap = {
  440. ...snapshotWith([row('i-subagent', 'pending child follow-up')]),
  441. subagent: {
  442. address: {
  443. parentSessionId: 'parent' as SessionId,
  444. childSessionId: SID,
  445. mode: 'continuable' as const,
  446. },
  447. parentAvailable: false,
  448. },
  449. }
  450. const source = liveSession(snap)
  451. const view = render(
  452. <QueueDock {...kitFor(snap)} useSession={source.useSession} />,
  453. )
  454. expect(view.getByText('pending child follow-up')).toBeTruthy()
  455. expect(view.getByLabelText('编辑排队消息')).toBeTruthy()
  456. expect(view.getByLabelText('删除排队消息')).toBeTruthy()
  457. expect(view.getByLabelText('插话发送')).toBeTruthy()
  458. })
  459. it('keeps a one-shot child Queue read-only', () => {
  460. const snap = {
  461. ...snapshotWith([row('i-subagent', 'pending child follow-up')]),
  462. subagent: {
  463. address: {
  464. parentSessionId: 'parent' as SessionId,
  465. childSessionId: SID,
  466. mode: 'one-shot' as const,
  467. },
  468. parentAvailable: true,
  469. },
  470. }
  471. const source = liveSession(snap)
  472. const view = render(
  473. <QueueDock {...kitFor(snap)} useSession={source.useSession} />,
  474. )
  475. expect(view.getByText('pending child follow-up')).toBeTruthy()
  476. expect(view.queryByLabelText('编辑排队消息')).toBeNull()
  477. expect(view.queryByLabelText('删除排队消息')).toBeNull()
  478. expect(view.queryByLabelText('插话发送')).toBeNull()
  479. })
  480. it('keeps the row and reports a genuine steer failure', async () => {
  481. const snap = snapshotWith([row('i-steer-race', 'pending steer')])
  482. const source = liveSession(snap)
  483. const notify = vi.fn()
  484. const updateQueue = vi.fn(() => Promise.reject(new Error('transport failed')))
  485. const { getByLabelText, getByText } = render(
  486. <QueueDock {...kitFor(snap, { updateQueue, notify })} useSession={source.useSession} />,
  487. )
  488. fireEvent.click(getByLabelText('插话发送'))
  489. await waitFor(() => {
  490. expect(notify).toHaveBeenCalledWith(
  491. 'error',
  492. '插话发送失败,请重试。',
  493. )
  494. })
  495. expect(getByText('pending steer')).toBeTruthy()
  496. })
  497. it('keeps the row and surfaces a notice when an operation loses the claim race', async () => {
  498. const snap = snapshotWith([row('i-race', 'pending')])
  499. const source = liveSession(snap)
  500. const notify = vi.fn()
  501. const updateQueue = vi.fn(() => Promise.reject(new Error('not found')))
  502. const { getByLabelText, getByText } = render(
  503. <QueueDock {...kitFor(snap, { updateQueue, notify })} useSession={source.useSession} />,
  504. )
  505. fireEvent.click(getByLabelText('删除排队消息'))
  506. await waitFor(() => {
  507. expect(notify).toHaveBeenCalledWith('error', '删除失败:这条消息可能已经开始发送。')
  508. })
  509. expect(getByText('pending')).toBeTruthy()
  510. })
  511. it('follows authoritative retirement back to null', () => {
  512. const snap = snapshotWith([row('i-1', '在场')])
  513. const source = liveSession(snap)
  514. const { container } = render(<QueueDock {...kitFor(snap)} useSession={source.useSession} />)
  515. expect(container.textContent).toContain('在场')
  516. act(() => { source.push(snapshotWith([])) })
  517. expect(container.innerHTML).toBe('')
  518. })
  519. it('registers as the terminal composer-context entry', () => {
  520. expect(queueDockEntry.name).toBe('conversation-queue-dock')
  521. expect(queueDockEntry.inject).toEqual(['slots', 'conversation', 'sessions', 'uiConversation'])
  522. const register = vi.fn(() => () => undefined)
  523. const inject = vi.fn((_name: string, callback: () => () => void) => callback())
  524. queueDockEntry.apply({ slots: { inject, register } } as never)
  525. expect(inject).toHaveBeenCalledWith('conversation.input.dock', expect.any(Function))
  526. expect(register).toHaveBeenCalledWith(
  527. expect.objectContaining({ name: 'conversation.input.dock', id: 'queue', order: 20 }),
  528. QueueDock,
  529. )
  530. })
  531. })