workspace-chat.spec.ts 38 KB

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