workspace-chat.spec.ts 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. import { test, expect, type Page } from '@playwright/test'
  2. async function login(page: Page) {
  3. await page.goto('/conversations')
  4. await page.getByRole('textbox', { name: '账号', exact: true }).fill('fixture')
  5. await page.getByRole('textbox', { name: '密码', exact: true }).fill('fixture-pass')
  6. await page.getByRole('button', { name: '登录', exact: true }).click()
  7. await expect(page.getByRole('heading', { name: '还没有聊天' })).toBeVisible()
  8. }
  9. async function openCreate(page: Page, name: '新建 Bot' | '新建群聊') {
  10. await page.locator('.desktop-sidebar .sidebar-create-trigger').click()
  11. await page.getByRole('menuitem', { name, exact: true }).click()
  12. }
  13. async function createAgent(page: Page, name: string) {
  14. await openCreate(page, '新建 Bot')
  15. const dialog = page.getByRole('dialog')
  16. await dialog.getByRole('textbox', { name: '名称', exact: true }).fill(name)
  17. await dialog
  18. .getByRole('textbox', { name: '角色提示词与规则', exact: true })
  19. .fill(`你是${name},每次回复给出验收证据。`)
  20. await dialog.getByRole('button', { name: `使用颜色 ${name === '产品经理' ? '#ff6b00' : '#00b9ac'}`, exact: true }).click()
  21. await dialog.locator('.mascot-grid--shape button').filter({ hasText: name === '产品经理' ? '圆形' : '小三角' }).first().click()
  22. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  23. await expect(page.getByRole('heading', { name, exact: true })).toBeVisible()
  24. }
  25. test('Bot unread dots sync across viewers and neither detail nor read requests delay navigation', async ({ page, context }, testInfo) => {
  26. await login(page)
  27. const capabilities = await (await page.request.get('/api/realtime/capabilities')).json()
  28. const headers = { 'X-CSRF-Token': capabilities.csrfToken, Origin: new URL(page.url()).origin }
  29. const create = async (name: string) => {
  30. const response = await page.request.post('/api/app/agents', { headers, data: { name, profile: 'default' } })
  31. expect(response.ok(), await response.text()).toBe(true)
  32. return (await response.json()).agent
  33. }
  34. const first = await create('红点测试甲'), second = await create('红点测试乙')
  35. const conversations = (await (await page.request.get('/api/app/conversations')).json()).conversations
  36. const direct = conversations.find((c: any) => c.kind === 'direct' && c.memberIds[0] === first.id)
  37. const other = conversations.find((c: any) => c.kind === 'direct' && c.memberIds[0] === second.id)
  38. const groupResponse = await page.request.post('/api/app/conversations', { headers, data: { name: '红点测试群', memberIds: [first.id, second.id], administratorId: first.id } })
  39. expect(groupResponse.ok(), await groupResponse.text()).toBe(true)
  40. const group = (await groupResponse.json()).conversation
  41. await page.goto(`/conversations/${other.id}`)
  42. const viewer = await context.newPage()
  43. await viewer.goto(`/conversations/${other.id}`)
  44. try {
  45. for (const target of [direct, group]) {
  46. const sent = await page.request.post(`/api/app/conversations/${target.id}/messages`, { headers, data: { requestId: crypto.randomUUID(), content: '给出测试结果' } })
  47. expect(sent.ok(), await sent.text()).toBe(true)
  48. const row = page.locator(`.desktop-sidebar [data-sidebar-id="${target.id}"]`)
  49. const viewerRow = viewer.locator(`.desktop-sidebar [data-sidebar-id="${target.id}"]`)
  50. await expect(row.getByRole('img', { name: '未读', exact: true })).toBeVisible()
  51. await expect(viewerRow.getByRole('img', { name: '未读', exact: true })).toBeVisible()
  52. await expect(row.locator('b')).toHaveCount(0)
  53. await page.screenshot({ path: testInfo.outputPath(`unread-${target.kind}-dot.png`) })
  54. let releaseDetail!: () => void, releaseRead!: () => void
  55. const detailGate = new Promise<void>(resolve => { releaseDetail = resolve })
  56. const readGate = new Promise<void>(resolve => { releaseRead = resolve })
  57. const detailPattern = new RegExp(`/api/app/conversations/${target.id}(?:\\?.*)?$`)
  58. const readPattern = new RegExp(`/api/app/conversations/${target.id}/read(?:\\?.*)?$`)
  59. await page.route(detailPattern, async route => { await detailGate; await route.continue() })
  60. await page.route(readPattern, async route => { await readGate; await route.continue() })
  61. try {
  62. // Warm transcripts are already in the snapshot; a detail request is optional.
  63. await row.click()
  64. await expect(page.getByRole('heading', { name: target.name, exact: true })).toBeVisible()
  65. releaseDetail()
  66. await expect(page.getByRole('textbox', { name: '输入消息,Enter 发送,Shift + Enter 换行' })).toBeEnabled()
  67. await expect(row.getByRole('img', { name: '未读', exact: true })).toHaveCount(0)
  68. releaseRead()
  69. await expect(viewerRow.getByRole('img', { name: '未读', exact: true })).toHaveCount(0)
  70. } finally {
  71. releaseDetail(); releaseRead()
  72. await page.unroute(detailPattern); await page.unroute(readPattern)
  73. }
  74. await page.locator(`.desktop-sidebar [data-sidebar-id="${other.id}"]`).click()
  75. }
  76. await expect(page.getByRole('textbox', { name: '输入消息,Enter 发送,Shift + Enter 换行' })).toBeEnabled()
  77. await page.screenshot({ path: testInfo.outputPath('unread-desktop.png') })
  78. const sent = await page.request.post(`/api/app/conversations/${group.id}/messages`, { headers, data: { requestId: crypto.randomUUID(), content: '再次确认结果' } })
  79. expect(sent.ok(), await sent.text()).toBe(true)
  80. await page.setViewportSize({ width: 375, height: 812 })
  81. await page.emulateMedia({ colorScheme: 'dark', reducedMotion: 'reduce' })
  82. await page.goto('/conversations')
  83. await expect(page.getByRole('option').filter({ hasText: group.name }).getByRole('img', { name: '未读', exact: true })).toBeVisible()
  84. await page.screenshot({ path: testInfo.outputPath('unread-mobile.png') })
  85. } finally {
  86. await viewer.close()
  87. // This suite shares its fixture account; remove only the records created above.
  88. const archived = await page.request.patch(`/api/app/conversations/${group.id}`, { headers, data: { archived: true } })
  89. expect(archived.ok(), await archived.text()).toBe(true)
  90. const removed = await page.request.delete(`/api/app/conversations/${group.id}`, { headers })
  91. expect(removed.ok(), await removed.text()).toBe(true)
  92. for (const agent of [first, second]) {
  93. const archived = await page.request.patch(`/api/app/agents/${agent.id}`, { headers, data: { archived: true } })
  94. expect(archived.ok(), await archived.text()).toBe(true)
  95. const removed = await page.request.delete(`/api/app/agents/${agent.id}`, { headers })
  96. expect(removed.ok(), await removed.text()).toBe(true)
  97. }
  98. }
  99. })
  100. test('an open Web chat list receives cross-client sessions and real title events without reload', async ({
  101. page,
  102. context,
  103. }) => {
  104. await login(page)
  105. const pageAChannelOpened = page.waitForResponse(response =>
  106. response.request().method() === 'POST'
  107. && response.url().endsWith('/api/realtime/channels'))
  108. await page.goto('/chat')
  109. await pageAChannelOpened
  110. await expect(page.getByRole('button', { name: '新建聊天', exact: true })).toBeVisible()
  111. const marker = `${Date.now()}-${Math.random().toString(16).slice(2)}`
  112. const initialTitle = `跨端新会话 ${marker}`
  113. const generatedTitle = `自动标题 ${marker}`
  114. const sentinel = `page-a-${marker}`
  115. await page.evaluate(value => {
  116. ;(window as Window & { __crossClientProjectionSentinel?: string })
  117. .__crossClientProjectionSentinel = value
  118. }, sentinel)
  119. const pageB = await context.newPage()
  120. const pageBChannelOpened = pageB.waitForResponse(response =>
  121. response.request().method() === 'POST'
  122. && response.url().endsWith('/api/realtime/channels'))
  123. await pageB.goto('/chat')
  124. await pageBChannelOpened
  125. await pageB.getByRole('button', { name: '新建聊天', exact: true }).click()
  126. const pageBComposer = pageB.getByRole('textbox', {
  127. name: '输入消息,Enter 发送,Shift + Enter 换行',
  128. })
  129. await pageBComposer.fill(`[cross-client-title:${initialTitle}] 创建跨端会话`)
  130. const pageBSend = pageB.getByRole('button', { name: '发送消息', exact: true })
  131. await expect(pageBSend).toBeEnabled()
  132. await pageBSend.click()
  133. await expect(pageB).toHaveURL(/\/chat\/(?!draft-)[^/?]+(?:\?.*)?$/)
  134. const sessionId = decodeURIComponent(new URL(pageB.url()).pathname.split('/').at(-1) || '')
  135. expect(sessionId).not.toMatch(/^draft-/)
  136. await expect(pageB.locator('.message--assistant')).toContainText('会话独立保存')
  137. const cwdCalls = await (await page.request.get('http://127.0.0.1:19120/__calls')).json()
  138. const created = cwdCalls.calls.filter((call: { method: string }) => call.method === 'session.create').at(-1)
  139. expect(created.params.cwd).toBe('/tmp/hermes-fixture/default')
  140. const aligned = cwdCalls.calls.filter((call: { method: string }) => call.method === 'session.cwd.set').at(-1)
  141. expect(aligned.params.cwd).toBe('/tmp/hermes-fixture/default')
  142. const sessionRow = page.locator(`.desktop-sidebar .sidebar-item[data-sidebar-id="${sessionId}"]`)
  143. await expect(sessionRow).toContainText(initialTitle)
  144. expect(await page.evaluate(() =>
  145. (window as Window & { __crossClientProjectionSentinel?: string })
  146. .__crossClientProjectionSentinel)).toBe(sentinel)
  147. await pageB.close()
  148. const titled = await page.request.post(
  149. `http://127.0.0.1:19120/__test/sessions/title?id=${encodeURIComponent(sessionId)}`
  150. + `&title=${encodeURIComponent(generatedTitle)}`,
  151. )
  152. expect(titled.ok(), await titled.text()).toBe(true)
  153. await expect(sessionRow).toContainText(generatedTitle)
  154. await expect(sessionRow).not.toContainText(initialTitle)
  155. expect(await page.evaluate(() =>
  156. (window as Window & { __crossClientProjectionSentinel?: string })
  157. .__crossClientProjectionSentinel)).toBe(sentinel)
  158. })
  159. test('an idle Web viewer follows an iOS-origin run and restores thinking after reload', async ({ page }, testInfo) => {
  160. await login(page)
  161. const capabilities = await (await page.request.get('/api/realtime/capabilities')).json()
  162. const headers = { 'X-CSRF-Token': capabilities.csrfToken, Origin: new URL(page.url()).origin }
  163. const opened = await page.request.post('/api/realtime/channels', { headers, data: { channel: 'chat' } })
  164. expect(opened.ok(), await opened.text()).toBe(true)
  165. const channel = (await opened.json()).id
  166. let sequence = 0
  167. async function command(method: string, params: Record<string, unknown>) {
  168. const response = await page.request.post(`/api/realtime/channels/${channel}/commands`, {
  169. headers: { ...headers, 'Idempotency-Key': `ios-live-${Date.now()}-${++sequence}` },
  170. data: { jsonrpc: '2.0', method, params },
  171. })
  172. expect(response.ok(), await response.text()).toBe(true)
  173. const receipt = await response.json()
  174. expect(receipt.state).toBe('confirmed')
  175. return receipt.response.result
  176. }
  177. const session = await command('session.create', { profile: 'default', source: 'ios' })
  178. const subscribed = page.waitForResponse(response => response.url().endsWith('/commands')
  179. && response.request().postDataJSON()?.method === 'session.resume')
  180. await page.goto(`/chat/${session.stored_session_id}?profile=default`)
  181. await subscribed
  182. await command('prompt.submit', { session_id: session.session_id, text: '[cross-client-live] 检查文件' })
  183. await expect(page.getByRole('status', { name: '正在思考', exact: true })).toBeVisible()
  184. await page.getByText('思考与工具', { exact: true }).click()
  185. await expect(page.getByText('read_file', { exact: true })).toBeVisible()
  186. await page.screenshot({ path: testInfo.outputPath('ios-run-web-thinking-tools.png') })
  187. // This Web page has never submitted a prompt or written an in-flight marker.
  188. await page.evaluate(() => sessionStorage.clear())
  189. await page.reload()
  190. await expect(page.getByRole('status', { name: '正在思考', exact: true })).toBeVisible()
  191. await page.request.post('http://127.0.0.1:19120/__release')
  192. await expect(page.locator('.message--assistant')).toContainText('会话独立保存')
  193. await expect(page.getByRole('status', { name: '正在思考', exact: true })).toHaveCount(0)
  194. await page.screenshot({ path: testInfo.outputPath('ios-run-web-completed.png') })
  195. })
  196. test('created roles and editable teams share a durable chat list without plugin calls', async ({
  197. page,
  198. context,
  199. }, testInfo) => {
  200. const pluginCalls: string[] = []
  201. page.on('request', (r) => {
  202. if (r.url().includes('/plugins/yaoyao')) pluginCalls.push(r.url())
  203. })
  204. await page.setViewportSize({ width: 1638, height: 874 })
  205. await login(page)
  206. const rail = page.locator('.desktop-sidebar')
  207. await expect(rail.locator('.sidebar-feature-nav')).toHaveCount(0)
  208. await expect(rail.locator('.sidebar-primary-action')).toHaveCount(0)
  209. await expect(rail).toHaveCSS('width', '360px')
  210. await expect(rail.locator('.sidebar-context__heading')).toHaveCount(0)
  211. await expect(rail.locator('.bot-logo-trigger .brand-mark')).toBeVisible()
  212. await expect(rail.locator('.sidebar-footer')).toBeVisible()
  213. await expect(rail.locator('.sidebar-account-switcher__main')).toContainText('fixture')
  214. await expect(rail.locator('.sidebar-account-switcher__main .account-initial-avatar')).toHaveText('F')
  215. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  216. await expect(page.getByRole('menu', { name: '账号菜单' }).getByRole('menuitem')).toHaveText(['设置', '关于', '帮助', '进入聊天模式'])
  217. await page.getByRole('menuitem', { name: '关于', exact: true }).click()
  218. const about = page.getByRole('dialog', { name: '关于夭夭 AI', exact: true })
  219. await expect(about).toBeVisible()
  220. await expect(about.locator('.about-version')).toHaveText(/^版本 \d+\.\d+\.\d+$/)
  221. await expect(page.getByRole('dialog', { name: '设置中心', exact: true })).toHaveCount(0)
  222. await page.screenshot({ path: testInfo.outputPath('independent-about.png') })
  223. await page.keyboard.press('Escape')
  224. await expect(about).toHaveCount(0)
  225. await rail.getByRole('button', { name: '工具', exact: true }).click()
  226. await page.getByRole('menuitem', { name: '已连接应用', exact: true }).click()
  227. const apps = page.getByRole('dialog', { name: '已连接应用', exact: true })
  228. await expect(apps).toBeVisible()
  229. await expect(apps.getByRole('tab', { name: 'MCP 服务', exact: true })).toBeVisible()
  230. await expect(page.locator('.settings-sidebar')).toHaveCount(0)
  231. await page.screenshot({ path: testInfo.outputPath('independent-apps.png') })
  232. await apps.getByRole('button', { name: '关闭已连接应用', exact: true }).click()
  233. await rail.getByRole('button', { name: '工具', exact: true }).click()
  234. await page.getByRole('menuitem', { name: '自动化', exact: true }).click()
  235. await expect(page).toHaveURL(/\/conversations\/automations(?:\?.*)?$/)
  236. await expect(page.getByRole('main', { name: '自动化工作区' })).toBeVisible()
  237. await expect(page.locator('.schedule-day')).toHaveCount(7)
  238. await page.getByRole('tab', { name: '运行日志', exact: true }).click()
  239. await page.screenshot({ path: testInfo.outputPath('independent-automations.png') })
  240. await page.getByRole('button', { name: '返回聊天', exact: true }).click()
  241. await expect(page).toHaveURL(/\/conversations$/)
  242. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  243. await page.getByRole('menuitem', { name: '设置', exact: true }).click()
  244. const settings = page.getByRole('dialog', { name: '设置中心', exact: true })
  245. await expect(settings.locator('.settings-account-summary')).toContainText('fixture')
  246. await expect(settings.locator('.settings-account-summary .account-initial-avatar')).toHaveText('F')
  247. await settings.locator('.account-avatar-card input[type=file]').setInputFiles({name:'account.png',mimeType:'image/png',buffer:Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=','base64')})
  248. await expect(settings.locator('.settings-account-summary .account-initial-avatar img')).toBeVisible()
  249. expect((await (await page.request.get('/api/app/bootstrap')).json()).user.avatar).toMatch(/^data:image\/png;base64,/)
  250. await settings.getByRole('button', {name:'使用首字母',exact:true}).click()
  251. await expect(settings.locator('.settings-account-summary .account-initial-avatar')).toHaveText('F')
  252. await settings.getByRole('button', { name: '身份与头像', exact: true }).click()
  253. await settings.getByRole('button', { name: '造型:星星', exact: true }).click()
  254. await settings.getByRole('button', { name: '使用颜色 #1488ff', exact: true }).click()
  255. await settings.locator('.mascot-grid--expression button').filter({ hasText: '开心' }).click()
  256. await settings.getByRole('button', { name: '保存更改', exact: true }).click()
  257. await expect(settings.getByRole('button', { name: '保存更改', exact: true })).toBeDisabled()
  258. await settings.getByRole('button', { name: '关闭设置中心', exact: true }).click()
  259. await rail.locator('.sidebar-account-switcher__main').click()
  260. await page.getByRole('menuitem', {name:'设置',exact:true}).click()
  261. await settings.getByRole('button', { name: '身份与头像', exact: true }).click()
  262. await expect(settings.getByRole('button', { name: '造型:星星', exact: true })).toHaveAttribute('aria-pressed', 'true')
  263. await expect(settings.getByRole('button', { name: '使用颜色 #1488ff', exact: true })).toHaveAttribute('aria-pressed', 'true')
  264. await expect(settings.locator('.mascot-grid--expression button').filter({ hasText: '开心' })).toHaveAttribute('aria-pressed', 'true')
  265. await settings.getByRole('button', { name: '关闭设置中心', exact: true }).click()
  266. await expect(rail.getByRole('checkbox', { name: '显示已归档' })).toHaveCount(0)
  267. const plus = rail.getByRole('button', { name: '新建', exact: true })
  268. await expect(rail.locator('.sidebar-collapse')).toHaveCount(0)
  269. await plus.click()
  270. await expect(page.getByRole('menu', { name: '新建聊天' }).getByRole('menuitem')).toHaveText(['新建 Bot', '添加远程机器人', '新建群聊'])
  271. await page.keyboard.press('Escape')
  272. await rail.getByRole('button', { name: '搜索', exact: true }).click()
  273. await expect(page.getByRole('dialog', { name: '搜索聊天' })).toBeVisible()
  274. await page.keyboard.press('Escape')
  275. await page.goto('/chat')
  276. await expect(rail).toHaveCSS('width', '264px')
  277. await expect(rail.getByRole('button', { name: '聊天', exact: true })).toHaveCount(0)
  278. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  279. const settingsMenu = page.getByRole('menu', { name: '账号菜单', exact: true })
  280. await expect(settingsMenu.getByRole('menuitem')).toHaveText(['设置', '关于', '帮助', '进入 Bot 模式'])
  281. await page.screenshot({ path: testInfo.outputPath('chat-settings-mode-menu.png') })
  282. await settingsMenu.getByRole('menuitem', { name: '设置', exact: true }).click()
  283. await expect(page.getByRole('dialog', { name: '设置中心', exact: true })).toBeVisible()
  284. await page.getByRole('button', { name: '关闭设置中心', exact: true }).click()
  285. await expect(rail.getByRole('button', { name: '设置与模式', exact: true })).toBeFocused()
  286. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  287. await settingsMenu.getByRole('menuitem', { name: '进入 Bot 模式', exact: true }).click()
  288. await expect(page).toHaveURL(/\/conversations$/)
  289. await expect(rail).toHaveCSS('width', '360px')
  290. await page.addInitScript(() => { Object.defineProperty(crypto, 'randomUUID', { value: undefined, configurable: true }) })
  291. await page.reload()
  292. await createAgent(page, '产品经理')
  293. await page.getByRole('button', {name:'聊天设置',exact:true}).click()
  294. const avatarEditor = page.getByRole('dialog')
  295. await expect(avatarEditor.locator('.color-grid button')).toHaveCount(11)
  296. await expect(avatarEditor.locator('.mascot-grid--expression button')).toHaveCount(10)
  297. await avatarEditor.getByRole('button', {name:'造型:星星',exact:true}).click()
  298. await avatarEditor.locator('.mascot-grid--expression button').filter({hasText:'自豪'}).click()
  299. await avatarEditor.getByRole('button', {name:'使用颜色 #000000',exact:true}).click()
  300. await avatarEditor.getByRole('button', {name:'保存',exact:true}).click()
  301. const identities = await (await page.request.get('/api/app/agents')).json()
  302. const edited = identities.agents.find((a: {name:string}) => a.name === '产品经理')
  303. expect(JSON.parse(edited.avatar.slice('yaoyao-avatar:v2:'.length))).toMatchObject({version:2,bodyId:'star',expression:'proud',color:'#000000'})
  304. await page.reload()
  305. await page.getByRole('button', {name:'聊天设置',exact:true}).click()
  306. await expect(avatarEditor.getByRole('button', {name:'造型:星星',exact:true})).toHaveAttribute('aria-pressed','true')
  307. await avatarEditor.locator('input[type=file]').setInputFiles({name:'avatar.png',mimeType:'image/png',buffer:Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=','base64')})
  308. await expect(avatarEditor.getByText('图片裁剪',{exact:true})).toBeVisible()
  309. let photoBytes=''
  310. for(const [label,crop] of [['圆形','circle'],['方形','square'],['圆角方形','rounded']]){
  311. await avatarEditor.getByRole('button',{name:label,exact:true}).click()
  312. await avatarEditor.getByRole('button',{name:'保存',exact:true}).click()
  313. const payload=await (await page.request.get('/api/app/agents')).json()
  314. const avatar=JSON.parse(payload.agents.find((a:{name:string})=>a.name==='产品经理').avatar.slice('yaoyao-avatar:v2:'.length))
  315. expect(avatar).toMatchObject({avatarMode:'image',imageCrop:crop})
  316. if(photoBytes)expect(avatar.imageDataURL).toBe(photoBytes)
  317. else photoBytes=avatar.imageDataURL
  318. await page.getByRole('button',{name:'聊天设置',exact:true}).click()
  319. }
  320. await avatarEditor.getByRole('button',{name:'动态吉祥物',exact:true}).click()
  321. await avatarEditor.locator('.mascot-grid--shape button').filter({hasText:'圆形'}).first().click()
  322. await avatarEditor.getByRole('button', {name:'使用颜色 #ff6b00',exact:true}).click()
  323. await avatarEditor.locator('.mascot-grid--expression button').filter({hasText:'默认'}).click()
  324. await avatarEditor.getByRole('button', {name:'保存',exact:true}).click()
  325. await expect(avatarEditor).toBeHidden()
  326. const composerInput = page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行')
  327. const sendButton = page.getByRole('button', { name: '发送消息', exact: true })
  328. await expect(composerInput).toBeEnabled()
  329. await expect(rail.getByText('聊天', { exact: true })).toHaveCount(0)
  330. await expect(rail.getByText('聊天列表', { exact: true })).toHaveCount(0)
  331. await expect(rail.locator('.sidebar-item__row strong').first()).toHaveCSS('font-size', '15px')
  332. await expect(rail.locator('.sidebar-item__row--secondary').first()).toHaveCSS('font-size', '13px')
  333. await expect(rail.locator('.sidebar-item__row small').first()).toHaveCSS('font-size', '10px')
  334. await composerInput.fill('请说明角色职责')
  335. await expect(sendButton).toBeEnabled()
  336. await sendButton.click()
  337. await expect(page.locator('.message--assistant')).toContainText('我是产品经理')
  338. for (const colorScheme of ['light', 'dark'] as const) {
  339. await page.emulateMedia({ colorScheme })
  340. for (const selector of ['.message__avatar.agent-avatar', '.desktop-sidebar .sidebar-item__icon--avatar', '.desktop-sidebar .agent-avatar']) {
  341. await expect(page.locator(selector).first()).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)')
  342. await expect(page.locator(selector).first()).toHaveCSS('background-image', 'none')
  343. }
  344. }
  345. await page.emulateMedia({ colorScheme: 'light' })
  346. await expect(page.locator('.message--assistant a').first()).toHaveAttribute(
  347. 'href',
  348. /\/api\/app\/files\/.+\/download/,
  349. )
  350. await page.locator('.message--assistant a').first().click()
  351. const textPreview = page.getByRole('dialog', { name: '预览 workspace-report.txt', exact: true })
  352. await expect(textPreview).toBeVisible()
  353. await expect(textPreview).toContainText('Fixture report')
  354. await textPreview.getByRole('button', { name: '关闭预览' }).click()
  355. const managerRow = rail.getByRole('option').filter({ hasText: '产品经理' })
  356. await managerRow.click({ button: 'right' })
  357. await page.getByRole('menuitem', {name:'置顶聊天',exact:true}).click()
  358. await managerRow.click({ button: 'right' })
  359. await expect(page.getByRole('menuitem', {name:'取消置顶',exact:true})).toBeVisible()
  360. await page.getByRole('menuitem', {name:'取消置顶',exact:true}).press('Escape')
  361. const directURL = page.url()
  362. await createAgent(page, '开发工程师')
  363. await expect(page.locator('.message')).toHaveCount(0)
  364. await openCreate(page, '新建群聊')
  365. const dialog = page.getByRole('dialog')
  366. await dialog.getByRole('textbox', { name: '名称', exact: true }).fill('产品开发团队')
  367. await dialog.getByRole('checkbox', { name: '产品经理', exact: true }).check()
  368. await dialog.getByRole('checkbox', { name: '开发工程师', exact: true }).check()
  369. await dialog
  370. .getByRole('combobox', { name: '负责人', exact: true })
  371. .selectOption({ label: '产品经理' })
  372. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  373. await expect(page.getByRole('heading', { name: '产品开发团队', exact: true })).toBeVisible()
  374. await expect(page.locator('.desktop-sidebar .sidebar-item')).toHaveCount(3)
  375. // Every cluster portrait must be the same portrait shown in that member's direct chat.
  376. const groupRow = page.locator('.desktop-sidebar .sidebar-item').filter({ hasText: '产品开发团队' })
  377. await expect(groupRow.locator('.team-avatar__member')).toHaveCount(2)
  378. for (const name of ['产品经理', '开发工程师']) {
  379. const portrait = `.agent-avatar[aria-label="${name} 的头像"]`
  380. const direct = page.locator('.desktop-sidebar .sidebar-item').filter({ hasText: name }).filter({ hasNot: page.locator('.team-avatar') }).locator(portrait)
  381. const member = groupRow.locator(portrait)
  382. await expect(member.locator('[data-part=outline] > :first-child')).toHaveAttribute('fill', await direct.locator('[data-part=outline] > :first-child').getAttribute('fill') || '')
  383. expect(await member.locator('[data-part=outline] > :first-child').evaluate(el => el.tagName))
  384. .toBe(await direct.locator('[data-part=outline] > :first-child').evaluate(el => el.tagName))
  385. }
  386. await expect(page.locator('.desktop-sidebar')).not.toContainText('话题')
  387. await expect(page.locator('.desktop-sidebar .sidebar-account-switcher__main')).not.toContainText(
  388. '通用助手',
  389. )
  390. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  391. await expect(dialog.getByRole('checkbox', { name: '产品经理', exact: true })).toBeDisabled()
  392. await expect(dialog.getByRole('checkbox', { name: '开发工程师', exact: true })).toBeEnabled()
  393. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  394. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请@开发工程师给出实现结果')
  395. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  396. await expect(page.locator('.message--assistant')).toHaveCount(3)
  397. await expect(page.locator('.message--assistant').nth(1)).toContainText('我是开发工程师')
  398. await expect(page.locator('.run-status')).toHaveCount(0)
  399. const second = await context.newPage()
  400. await second.goto(page.url())
  401. await expect(second.locator('.message--assistant')).toHaveCount(3)
  402. await page.screenshot({ path: testInfo.outputPath('mixed-chats.png'), fullPage: true })
  403. const groupURL=page.url()
  404. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  405. await dialog.getByRole('checkbox', { name: '开发工程师', exact: true }).uncheck()
  406. await expect(dialog.getByRole('checkbox', { name: '产品经理', exact: true })).toBeDisabled()
  407. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  408. await expect(groupRow.locator('.team-avatar__member')).toHaveCount(1)
  409. await expect(page.locator('.message--assistant')).toHaveCount(3)
  410. await expect(second.locator('.desktop-sidebar .sidebar-item').filter({ hasText: '产品开发团队' }).locator('.team-avatar__member')).toHaveCount(1)
  411. await page.reload()
  412. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  413. await expect(dialog.getByRole('checkbox', { name: '开发工程师', exact: true })).not.toBeChecked()
  414. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  415. await createAgent(page, '新成员')
  416. await page.goto(groupURL)
  417. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  418. await dialog.getByRole('checkbox', { name: '新成员', exact: true }).check()
  419. await dialog.getByRole('checkbox', { name: '产品经理', exact: true }).scrollIntoViewIfNeeded()
  420. await page.screenshot({ path: testInfo.outputPath('member-settings.png'), fullPage: true })
  421. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  422. await expect(groupRow.locator('.team-avatar__member')).toHaveCount(2)
  423. await expect(groupRow.locator('[aria-label="开发工程师 的头像"]')).toHaveCount(0)
  424. await expect(groupRow.locator('[aria-label="新成员 的头像"]')).toHaveCount(1)
  425. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请@新成员继续')
  426. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  427. await expect(page.locator('.message--assistant')).toHaveCount(6)
  428. await expect(page.locator('.message--assistant').nth(4)).toContainText('我是新成员')
  429. await page.goto('/files')
  430. await page.getByRole('button',{name:'查看来源',exact:true}).first().click()
  431. await expect(page).toHaveURL(groupURL)
  432. await groupRow.click({ button: 'right' })
  433. page.once('dialog', dialog => dialog.accept())
  434. await page.getByRole('menuitem', { name: '归档聊天', exact: true }).click()
  435. await expect(page.locator('.desktop-sidebar .sidebar-item')).toHaveCount(3)
  436. await page.locator('.desktop-sidebar .sidebar-search-trigger').click()
  437. const search = page.getByRole('dialog', { name: '搜索聊天', exact: true })
  438. await expect(search).toBeVisible()
  439. await search.getByRole('searchbox', { name: '搜索聊天', exact: true }).fill('产品开发')
  440. await expect(search.getByRole('listitem')).toHaveCount(0)
  441. await search.getByRole('tab', { name: '已归档', exact: true }).click()
  442. await expect(search.getByRole('searchbox')).toHaveValue('产品开发')
  443. await expect(search.getByRole('listitem')).toHaveCount(1)
  444. await page.screenshot({ path: testInfo.outputPath('archived-chat-search.png'), fullPage: true })
  445. await search.getByRole('listitem').getByRole('button').filter({ hasText: '产品开发团队' }).click()
  446. await expect(search).not.toBeVisible()
  447. await expect(page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行')).toBeDisabled()
  448. await page.goto(directURL)
  449. await expect(page.locator('.message--assistant')).toHaveCount(1)
  450. await expect(page.locator('.message--assistant')).toContainText('我是产品经理')
  451. await page.goto('/chat')
  452. await expect(page).toHaveURL(/\/chat/)
  453. expect(pluginCalls).toEqual([])
  454. await second.close()
  455. await page.goto('/conversations')
  456. await openCreate(page, '新建群聊')
  457. await dialog.locator('summary').filter({hasText:'从团队模板选择'}).click()
  458. await expect(dialog.getByRole('button', { name: '信息收集团队', exact: true })).toBeDisabled()
  459. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  460. await createAgent(page, '模板成员四')
  461. await createAgent(page, '模板成员五')
  462. await openCreate(page, '新建群聊')
  463. await dialog.locator('summary').filter({hasText:'从团队模板选择'}).click()
  464. for (const name of ['信息收集团队', '产品设计团队', '软件开发团队', '文案书写团队', '项目管理团队', '数据分析团队', '会议协作团队', '运维保障团队']) {
  465. await expect(dialog.getByRole('button', { name, exact: true })).toBeEnabled()
  466. }
  467. await page.screenshot({ path: testInfo.outputPath('restored-team-presets.png'), fullPage: true })
  468. await dialog.getByRole('button', { name: '产品设计团队', exact: true }).click()
  469. await expect(dialog.getByRole('textbox', { name: '名称', exact: true })).toHaveValue('产品设计团队')
  470. await dialog.getByRole('combobox', { name: '产品负责人对应的机器人', exact: true }).selectOption({ label: '产品经理' })
  471. await dialog.getByRole('combobox', { name: '交互设计对应的机器人', exact: true }).selectOption({ label: '开发工程师' })
  472. const [submitted] = await Promise.all([
  473. page.waitForRequest(r => r.method() === 'POST' && r.url().endsWith('/api/app/conversations')),
  474. dialog.getByRole('button', { name: '保存', exact: true }).click(),
  475. ])
  476. const payload = submitted.postDataJSON()
  477. expect(new Set(payload.memberIds).size).toBe(5)
  478. expect(Object.keys(payload.memberRoles)).toHaveLength(5)
  479. expect(payload.memberRoles[payload.administratorId].name).toBe('产品负责人')
  480. await expect(page.getByRole('heading', { name: '产品设计团队', exact: true })).toBeVisible()
  481. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  482. await expect(dialog.getByText('产品经理 · 产品负责人', { exact: false })).toBeVisible()
  483. await expect(dialog.getByText('开发工程师 · 交互设计', { exact: false })).toBeVisible()
  484. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  485. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请汇总设计方案')
  486. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  487. await expect(page.locator('.message--assistant')).toContainText('我是产品经理')
  488. const composer = page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行')
  489. await composer.fill('[hold-workspace] 正在执行的任务')
  490. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  491. await expect(page.getByRole('button', { name: '停止生成', exact: true })).toBeVisible()
  492. await composer.fill('后续排队任务')
  493. await expect(page.getByRole('button', { name: '发送消息', exact: true })).toBeEnabled()
  494. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  495. await expect(page.locator('.message--user').last()).toContainText('后续排队任务')
  496. const detail = await (await page.request.get(new URL(page.url()).pathname.replace('/conversations/', '/api/app/conversations/'))).json()
  497. expect(detail.conversation.queuedMessageCount).toBe(1)
  498. await page.request.post('http://127.0.0.1:19120/__release')
  499. await expect.poll(async () => (await (await page.request.get(`/api/app/conversations/${detail.conversation.id}`)).json()).conversation.activeRunId).toBeUndefined()
  500. await page.setViewportSize({ width: 390, height: 844 })
  501. await page.getByRole('button', { name: '返回 Bot 列表', exact: true }).click()
  502. await expect(rail).toBeVisible()
  503. await expect(rail).toHaveCSS('width', '390px')
  504. await expect(page.locator('.mobile-header')).toBeHidden()
  505. await expect(rail.locator('.sidebar-feature-nav')).toHaveCount(0)
  506. await page.screenshot({ path: testInfo.outputPath('mobile-bot-list.png') })
  507. await rail.getByRole('button', { name: '新建', exact: true }).click()
  508. await expect(page.getByRole('menuitem', { name: '新建 Bot', exact: true })).toBeVisible()
  509. await page.keyboard.press('Escape')
  510. await rail.getByRole('button', { name: '搜索', exact: true }).click()
  511. await expect(page.getByRole('dialog', { name: '搜索聊天' })).toBeVisible()
  512. await expect(page.getByRole('tab', { name: '未归档', exact: true })).toBeVisible()
  513. await page.getByRole('button', { name: '关闭搜索', exact: true }).click()
  514. await rail.locator('.sidebar-account-switcher__main').click()
  515. await page.getByRole('menuitem', { name: '进入聊天模式', exact: true }).click()
  516. await expect(page).toHaveURL(/\/chat$/)
  517. await page.goto('/')
  518. await expect(page).toHaveURL(/\/chat$/)
  519. await page.setViewportSize({ width: 1280, height: 800 })
  520. await page.getByRole('button', { name: '设置与模式', exact: true }).click()
  521. await page.getByRole('menuitem', { name: '进入 Bot 模式', exact: true }).click()
  522. await page.goto('/')
  523. await expect(page).toHaveURL(/\/conversations$/)
  524. await page.reload()
  525. await expect(rail.locator('.bot-list-header')).toBeVisible()
  526. expect(pluginCalls).toEqual([])
  527. })
  528. test('subaccount allocation and Bot-only navigation work through the browser', async ({ page, browser }, testInfo) => {
  529. await login(page)
  530. await page.locator('.desktop-sidebar').getByRole('button', { name: '设置与模式', exact: true }).click()
  531. await page.getByRole('menuitem', { name: '设置', exact: true }).click()
  532. const settings = page.getByRole('dialog', { name: '设置中心', exact: true })
  533. await settings.getByRole('button', { name: '用户与权限', exact: true }).click()
  534. const username = `child-${Date.now()}`
  535. await settings.locator('input[name=managed-username]').fill(username)
  536. await settings.locator('input[name=managed-temporary-password]').fill('temporary-pass')
  537. await settings.getByRole('listbox', { name: '新子账号的基础机器人' }).selectOption('default')
  538. await settings.getByRole('button', { name: '创建用户', exact: true }).click()
  539. await expect(settings.getByRole('listbox', { name: `分配给 ${username} 的基础机器人`, exact: true })).toHaveValues(['default'])
  540. await page.screenshot({ path: testInfo.outputPath('admin-agent-allocation.png'), fullPage: true })
  541. const childContext = await browser.newContext({ viewport: { width: 1280, height: 800 } })
  542. const child = await childContext.newPage()
  543. await child.goto(new URL('/chat', page.url()).href)
  544. await child.getByRole('textbox', { name: '账号', exact: true }).fill(username)
  545. await child.getByRole('textbox', { name: '密码', exact: true }).fill('temporary-pass')
  546. await child.getByRole('button', { name: '登录', exact: true }).click()
  547. await child.getByLabel('当前密码', { exact: true }).fill('temporary-pass')
  548. await child.getByLabel('新密码', { exact: true }).fill('permanent-pass')
  549. await child.getByLabel('确认新密码', { exact: true }).fill('permanent-pass')
  550. await child.getByRole('button', { name: '保存并进入夭夭', exact: true }).click()
  551. await expect(child).toHaveURL(/\/conversations$/)
  552. await expect(child.getByRole('heading', { name: '还没有聊天' })).toBeVisible()
  553. await child.locator('.desktop-sidebar').getByRole('button', { name: '设置与模式', exact: true }).click()
  554. await expect(child.getByRole('menu', { name: '账号菜单' }).getByRole('menuitem')).toHaveText(['设置', '关于', '帮助'])
  555. await child.keyboard.press('Escape')
  556. await openCreate(child, '新建 Bot')
  557. const dialog = child.getByRole('dialog')
  558. await expect(dialog.getByLabel('基础机器人').locator('option')).toHaveCount(1)
  559. await expect(dialog.getByLabel('基础机器人').locator('option')).toContainText('通用助手')
  560. await dialog.getByLabel('基础机器人').scrollIntoViewIfNeeded()
  561. await child.screenshot({ path: testInfo.outputPath('subaccount-assigned-source.png'), fullPage: true })
  562. await dialog.getByRole('textbox', { name: '名称', exact: true }).fill('子账号助手')
  563. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  564. await expect(child.getByRole('heading', { name: '子账号助手', exact: true })).toBeVisible()
  565. for (const path of ['/history', '/kanban', '/files', '/chat']) {
  566. await child.goto(new URL(path, page.url()).href)
  567. await expect(child).toHaveURL(/\/conversations$/)
  568. }
  569. await childContext.close()
  570. })
  571. for (const mode of ['普通聊天', 'Bot 单聊', '群聊', '普通长回复'] as const) {
  572. test(`${mode} 在完成前格式化流式 Markdown 并保留最终内容`, async ({ page }, testInfo) => {
  573. await login(page)
  574. if (mode.startsWith('普通')) {
  575. await page.goto('/chat')
  576. await page.getByRole('button', { name: '新建聊天', exact: true }).click()
  577. } else {
  578. await createAgent(page, `${mode}流式验证助手`)
  579. if (mode === '群聊') {
  580. await createAgent(page, '群聊流式协作成员')
  581. await openCreate(page, '新建群聊')
  582. const dialog = page.getByRole('dialog')
  583. await dialog.getByRole('textbox', { name: '名称', exact: true }).fill('流式验证群')
  584. await dialog.getByRole('checkbox', { name: `${mode}流式验证助手`, exact: true }).check()
  585. await dialog.getByRole('checkbox', { name: '群聊流式协作成员', exact: true }).check()
  586. await dialog.getByRole('combobox', { name: '负责人', exact: true }).selectOption({ label: `${mode}流式验证助手` })
  587. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  588. await expect(page.getByRole('heading', { name: '流式验证群', exact: true })).toBeVisible()
  589. }
  590. }
  591. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill(`[streaming-markdown] ${mode === '普通长回复' ? '[long]' : ''} 演示排版`)
  592. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  593. const message = page.locator('.message--assistant').last()
  594. await expect(message.locator('.markdown--streaming h1')).toHaveText('流式标题')
  595. await expect(message.locator('strong').filter({ hasText: '即时格式化' })).toBeVisible()
  596. await expect(message.locator('li')).toHaveText(['第一项', '第二项'])
  597. await expect(message.locator('pre code').first()).toContainText('const answer = 42')
  598. await expect(message).not.toContainText('最终完整标记')
  599. await page.screenshot({ path: testInfo.outputPath('streaming-markdown.png'), fullPage: true })
  600. if (mode === '普通长回复') {
  601. await expect(message.getByText('流式末尾标记', { exact: true })).toBeInViewport()
  602. await page.locator('.timeline').evaluate(el => { el.scrollTop = 0; el.dispatchEvent(new Event('scroll')) })
  603. await expect(message.getByRole('heading', { name: '流式标题', exact: true })).toBeInViewport()
  604. }
  605. const released = await page.request.post('http://127.0.0.1:19120/__release')
  606. expect(released.ok()).toBe(true)
  607. await expect(message).toContainText('最终完整标记')
  608. await expect(message.locator('table')).toContainText('项目')
  609. await expect(message.locator('.markdown--streaming')).toHaveCount(0)
  610. if (mode === '普通长回复') {
  611. await expect(message.getByRole('heading', { name: '流式标题', exact: true })).toBeInViewport()
  612. await expect(message.getByText('最终完整标记', { exact: true })).not.toBeInViewport()
  613. }
  614. })
  615. }
  616. test('only chat messages introduce files across transcript and library', async ({ page }, testInfo) => {
  617. await login(page)
  618. await createAgent(page, '文件展示验收')
  619. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('[message-files] 请发送报告')
  620. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  621. const report = page.locator('.message--assistant .file-link-card').first()
  622. await expect(report).toBeVisible()
  623. const process = page.locator('.turn-trace')
  624. await expect(process).toHaveCount(1)
  625. await process.locator('summary').first().click()
  626. await expect(process).toContainText('/tmp/process.pdf')
  627. await expect(process.locator('img, .file-link-card')).toHaveCount(0)
  628. const conversationId = new URL(page.url()).pathname.split('/').at(-1)!
  629. const files = await (await page.request.get(`/api/app/files?session_id=${encodeURIComponent(conversationId)}`)).json()
  630. expect(files.items.map((file: { name: string }) => file.name)).toEqual(['workspace-report.txt'])
  631. await report.click()
  632. const preview = page.getByRole('dialog', { name: '预览 workspace-report.txt', exact: true })
  633. await expect(preview).toContainText('Fixture report')
  634. await preview.getByRole('button', { name: '关闭预览' }).click()
  635. await page.reload()
  636. await expect(page.locator('.message--assistant .file-link-card')).toHaveCount(1)
  637. await page.locator('.turn-trace summary').first().click()
  638. await page.setViewportSize({ width: 1280, height: 1100 })
  639. await page.screenshot({ path: testInfo.outputPath('message-files-web.png') })
  640. })
  641. test('Bot transcript switches from cache and preserves the viewport during history and live updates', async ({ page }, testInfo) => {
  642. await login(page)
  643. const bootstrap = await (await page.request.get('/api/app/bootstrap')).json()
  644. const headers = { 'X-CSRF-Token': bootstrap.csrfToken }
  645. const seed = await (await page.request.post('/__test/workspace-transcript', { headers })).json()
  646. try {
  647. await page.goto(`/conversations/${seed.conversationId}`)
  648. await expect(page.locator('.message')).toHaveCount(50)
  649. const scroller = page.locator('.timeline')
  650. for (let index = 0; index < 4; index++) {
  651. await scroller.evaluate(el => { el.scrollTop = 0 })
  652. const before = await page.locator('.message').first().getAttribute('data-message-id')
  653. await page.getByRole('button', { name: '加载更早消息', exact: true }).click()
  654. await expect(page.locator('.message')).toHaveCount(100 + index * 50)
  655. expect(await page.locator(`[data-message-id="${before}"]`).evaluate(el => {
  656. const scroller = el.closest('.timeline')!; return el.getBoundingClientRect().top - scroller.getBoundingClientRect().top
  657. })).toBeGreaterThanOrEqual(-10)
  658. }
  659. // Returning keeps loaded history in memory but mounts only the 120-message tail.
  660. await page.locator('.desktop-sidebar .bot-logo-trigger').click()
  661. await expect(page.locator('.desktop-sidebar .sidebar-item').filter({ hasText: '长会话性能验收' })).toBeVisible()
  662. const requests: string[] = []
  663. page.on('request', request => { if (request.method() === 'GET') requests.push(new URL(request.url()).pathname) })
  664. await page.locator(`.desktop-sidebar [data-sidebar-id="${seed.conversationId}"]`).click()
  665. await expect(page.locator('.message')).toHaveCount(120)
  666. expect(requests).not.toContain(`/api/app/conversations/${seed.conversationId}`)
  667. await scroller.hover(); await page.mouse.wheel(0, -600)
  668. await expect(page.getByRole('button', { name: '回到底部', exact: true })).toBeVisible()
  669. const top = await scroller.evaluate(el => el.scrollTop)
  670. await page.request.post(`/__test/workspace-transcript?id=${seed.conversationId}`, { headers })
  671. await expect(page.locator('.message').last()).toContainText('实时追加的内容')
  672. expect(Math.abs(await scroller.evaluate(el => el.scrollTop) - top)).toBeLessThan(4)
  673. expect(requests).not.toContain('/api/app/agents')
  674. expect(requests).not.toContain('/api/app/events')
  675. await page.screenshot({ path: testInfo.outputPath('bot-cached-transcript.png') })
  676. await page.setViewportSize({ width: 375, height: 812 })
  677. await page.emulateMedia({ colorScheme: 'dark', reducedMotion: 'reduce' })
  678. await expect(page.getByRole('textbox', { name: '输入消息,Enter 发送,Shift + Enter 换行' })).toBeVisible()
  679. await page.screenshot({ path: testInfo.outputPath('bot-cached-transcript-mobile-dark.png') })
  680. } finally {
  681. await page.request.patch(`/api/app/agents/${seed.agentId}`, { headers, data: { archived: true } })
  682. await page.request.delete(`/api/app/agents/${seed.agentId}`, { headers })
  683. }
  684. })