workspace-chat.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 === '产品经理' ? '#e78531' : '#0ea5c6'}`, 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('created roles and editable teams share a durable chat list without plugin calls', async ({
  26. page,
  27. context,
  28. }, testInfo) => {
  29. const pluginCalls: string[] = []
  30. page.on('request', (r) => {
  31. if (r.url().includes('/plugins/yaoyao')) pluginCalls.push(r.url())
  32. })
  33. await page.setViewportSize({ width: 1638, height: 874 })
  34. await login(page)
  35. const rail = page.locator('.desktop-sidebar')
  36. await expect(rail.locator('.sidebar-feature-nav')).toHaveCount(0)
  37. await expect(rail.locator('.sidebar-primary-action')).toHaveCount(0)
  38. await expect(rail).toHaveCSS('width', '360px')
  39. await expect(rail.locator('.sidebar-context__heading')).toHaveCount(0)
  40. await expect(rail.locator('.bot-logo-trigger .brand-mark')).toBeVisible()
  41. await expect(rail.locator('.sidebar-footer')).toBeVisible()
  42. await expect(rail.locator('.sidebar-account-switcher__main')).toContainText('fixture')
  43. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  44. await expect(page.getByRole('menu', { name: '设置与模式' }).getByRole('menuitem')).toHaveText(['进入设置', '进入聊天模式'])
  45. await page.keyboard.press('Escape')
  46. await expect(rail.getByRole('checkbox', { name: '显示已归档' })).toHaveCount(0)
  47. const plus = rail.getByRole('button', { name: '新建', exact: true })
  48. await expect(rail.locator('.sidebar-collapse')).toHaveCount(0)
  49. await plus.click()
  50. await expect(page.getByRole('menu', { name: '新建聊天' }).getByRole('menuitem')).toHaveCount(2)
  51. await page.keyboard.press('Escape')
  52. await rail.getByRole('button', { name: '搜索', exact: true }).click()
  53. await expect(page.getByRole('dialog', { name: '搜索聊天' })).toBeVisible()
  54. await page.keyboard.press('Escape')
  55. await page.goto('/chat')
  56. await expect(rail).toHaveCSS('width', '264px')
  57. await expect(rail.getByRole('button', { name: '聊天', exact: true })).toHaveCount(0)
  58. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  59. const settingsMenu = page.getByRole('menu', { name: '设置与模式', exact: true })
  60. await expect(settingsMenu.getByRole('menuitem')).toHaveText(['进入设置', '进入 Bot 模式'])
  61. await page.screenshot({ path: testInfo.outputPath('chat-settings-mode-menu.png') })
  62. await settingsMenu.getByRole('menuitem', { name: '进入设置', exact: true }).click()
  63. await expect(page.getByRole('dialog', { name: '设置中心', exact: true })).toBeVisible()
  64. await page.getByRole('button', { name: '关闭设置中心', exact: true }).click()
  65. await expect(rail.getByRole('button', { name: '设置与模式', exact: true })).toBeFocused()
  66. await rail.getByRole('button', { name: '设置与模式', exact: true }).click()
  67. await settingsMenu.getByRole('menuitem', { name: '进入 Bot 模式', exact: true }).click()
  68. await expect(page).toHaveURL(/\/conversations$/)
  69. await expect(rail).toHaveCSS('width', '360px')
  70. await page.addInitScript(() => { Object.defineProperty(crypto, 'randomUUID', { value: undefined, configurable: true }) })
  71. await page.reload()
  72. await createAgent(page, '产品经理')
  73. await expect(rail.getByText('聊天', { exact: true })).toHaveCount(0)
  74. await expect(rail.getByText('聊天列表', { exact: true })).toHaveCount(0)
  75. await expect(rail.locator('.sidebar-item__row strong').first()).toHaveCSS('font-size', '15px')
  76. await expect(rail.locator('.sidebar-item__row--secondary').first()).toHaveCSS('font-size', '13px')
  77. await expect(rail.locator('.sidebar-item__row small').first()).toHaveCSS('font-size', '10px')
  78. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请说明角色职责')
  79. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  80. await expect(page.locator('.message--assistant')).toContainText('我是产品经理')
  81. for (const colorScheme of ['light', 'dark'] as const) {
  82. await page.emulateMedia({ colorScheme })
  83. for (const selector of ['.message__avatar.agent-avatar', '.desktop-sidebar .sidebar-item__icon--avatar', '.desktop-sidebar .agent-avatar']) {
  84. await expect(page.locator(selector).first()).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)')
  85. await expect(page.locator(selector).first()).toHaveCSS('background-image', 'none')
  86. }
  87. }
  88. await page.emulateMedia({ colorScheme: 'light' })
  89. await expect(page.locator('.message--assistant a').first()).toHaveAttribute(
  90. 'href',
  91. /\/api\/app\/files\/.+\/download/,
  92. )
  93. await page.locator('.message--assistant a').first().click()
  94. const textPreview = page.getByRole('dialog', { name: '预览 workspace-report.txt', exact: true })
  95. await expect(textPreview).toBeVisible()
  96. await expect(textPreview).toContainText('Fixture report')
  97. await textPreview.getByRole('button', { name: '关闭预览' }).click()
  98. await page.getByRole('button', {name:'置顶聊天',exact:true}).click()
  99. await expect(page.getByRole('button', {name:'取消置顶',exact:true})).toBeVisible()
  100. const directURL = page.url()
  101. await createAgent(page, '开发工程师')
  102. await expect(page.locator('.message')).toHaveCount(0)
  103. await openCreate(page, '新建群聊')
  104. const dialog = page.getByRole('dialog')
  105. await dialog.getByRole('textbox', { name: '名称', exact: true }).fill('产品开发团队')
  106. await dialog.getByRole('checkbox', { name: '产品经理', exact: true }).check()
  107. await dialog.getByRole('checkbox', { name: '开发工程师', exact: true }).check()
  108. await dialog
  109. .getByRole('combobox', { name: '管理员', exact: true })
  110. .selectOption({ label: '产品经理' })
  111. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  112. await expect(page.getByRole('heading', { name: '产品开发团队', exact: true })).toBeVisible()
  113. await expect(page.locator('.desktop-sidebar .sidebar-item')).toHaveCount(3)
  114. // Every cluster portrait must be the same portrait shown in that member's direct chat.
  115. const groupRow = page.locator('.desktop-sidebar .sidebar-item').filter({ hasText: '产品开发团队' })
  116. await expect(groupRow.locator('.team-avatar__member')).toHaveCount(2)
  117. for (const name of ['产品经理', '开发工程师']) {
  118. const portrait = `.agent-avatar[aria-label="${name} 的头像"]`
  119. const direct = page.locator('.desktop-sidebar .sidebar-item').filter({ hasText: name }).filter({ hasNot: page.locator('.team-avatar') }).locator(portrait)
  120. const member = groupRow.locator(portrait)
  121. await expect(member.locator('stop[offset="0.5"]')).toHaveAttribute('stop-color', await direct.locator('stop[offset="0.5"]').getAttribute('stop-color') || '')
  122. expect(await member.locator('.agent-avatar__body > :first-child').evaluate(el => el.tagName))
  123. .toBe(await direct.locator('.agent-avatar__body > :first-child').evaluate(el => el.tagName))
  124. }
  125. await expect(page.locator('.desktop-sidebar')).not.toContainText('话题')
  126. await expect(page.locator('.desktop-sidebar .sidebar-account-switcher__main')).not.toContainText(
  127. '通用助手',
  128. )
  129. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  130. await expect(dialog.getByRole('checkbox', { name: '产品经理', exact: true })).toBeDisabled()
  131. await expect(dialog.getByRole('checkbox', { name: '开发工程师', exact: true })).toBeEnabled()
  132. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  133. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请@开发工程师给出实现结果')
  134. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  135. await expect(page.locator('.message--assistant')).toHaveCount(3)
  136. await expect(page.locator('.message--assistant').nth(1)).toContainText('我是开发工程师')
  137. await expect(page.locator('.run-status')).toHaveCount(0)
  138. const second = await context.newPage()
  139. await second.goto(page.url())
  140. await expect(second.locator('.message--assistant')).toHaveCount(3)
  141. await page.screenshot({ path: testInfo.outputPath('mixed-chats.png'), fullPage: true })
  142. const groupURL=page.url()
  143. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  144. await dialog.getByRole('checkbox', { name: '开发工程师', exact: true }).uncheck()
  145. await expect(dialog.getByRole('checkbox', { name: '产品经理', exact: true })).toBeDisabled()
  146. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  147. await expect(groupRow.locator('.team-avatar__member')).toHaveCount(1)
  148. await expect(page.locator('.message--assistant')).toHaveCount(3)
  149. await expect(second.locator('.desktop-sidebar .sidebar-item').filter({ hasText: '产品开发团队' }).locator('.team-avatar__member')).toHaveCount(1)
  150. await page.reload()
  151. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  152. await expect(dialog.getByRole('checkbox', { name: '开发工程师', exact: true })).not.toBeChecked()
  153. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  154. await createAgent(page, '新成员')
  155. await page.goto(groupURL)
  156. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  157. await dialog.getByRole('checkbox', { name: '新成员', exact: true }).check()
  158. await dialog.getByRole('checkbox', { name: '产品经理', exact: true }).scrollIntoViewIfNeeded()
  159. await page.screenshot({ path: testInfo.outputPath('member-settings.png'), fullPage: true })
  160. await dialog.getByRole('button', { name: '保存', exact: true }).click()
  161. await expect(groupRow.locator('.team-avatar__member')).toHaveCount(2)
  162. await expect(groupRow.locator('[aria-label="开发工程师 的头像"]')).toHaveCount(0)
  163. await expect(groupRow.locator('[aria-label="新成员 的头像"]')).toHaveCount(1)
  164. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请@新成员继续')
  165. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  166. await expect(page.locator('.message--assistant')).toHaveCount(6)
  167. await expect(page.locator('.message--assistant').nth(4)).toContainText('我是新成员')
  168. await page.goto('/files')
  169. await page.getByRole('button',{name:'查看来源',exact:true}).first().click()
  170. await expect(page).toHaveURL(groupURL)
  171. await page.getByRole('button', { name: '归档聊天', exact: true }).click()
  172. await expect(page.locator('.desktop-sidebar .sidebar-item')).toHaveCount(3)
  173. await page.locator('.desktop-sidebar .sidebar-search-trigger').click()
  174. const search = page.getByRole('dialog', { name: '搜索聊天', exact: true })
  175. await expect(search).toBeVisible()
  176. await search.getByRole('searchbox', { name: '搜索聊天', exact: true }).fill('产品开发')
  177. await expect(search.getByRole('option')).toHaveCount(0)
  178. await search.getByRole('tab', { name: '已归档', exact: true }).click()
  179. await expect(search.getByRole('searchbox')).toHaveValue('产品开发')
  180. await expect(search.getByRole('option')).toHaveCount(1)
  181. await page.screenshot({ path: testInfo.outputPath('archived-chat-search.png'), fullPage: true })
  182. await search.getByRole('option').click()
  183. await expect(search).not.toBeVisible()
  184. await page.getByRole('button', { name: '恢复聊天', exact: true }).click()
  185. await page.goto(directURL)
  186. await expect(page.locator('.message--assistant')).toHaveCount(1)
  187. await expect(page.locator('.message--assistant')).toContainText('我是产品经理')
  188. await page.goto('/chat')
  189. await expect(page).toHaveURL(/\/chat/)
  190. expect(pluginCalls).toEqual([])
  191. await second.close()
  192. await page.goto('/conversations')
  193. await openCreate(page, '新建群聊')
  194. await expect(dialog.getByRole('button', { name: '信息收集团队', exact: true })).toBeDisabled()
  195. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  196. await createAgent(page, '模板成员四')
  197. await createAgent(page, '模板成员五')
  198. await openCreate(page, '新建群聊')
  199. for (const name of ['信息收集团队', '产品设计团队', '软件开发团队', '文案书写团队', '项目管理团队', '数据分析团队', '会议协作团队', '运维保障团队']) {
  200. await expect(dialog.getByRole('button', { name, exact: true })).toBeEnabled()
  201. }
  202. await page.screenshot({ path: testInfo.outputPath('restored-team-presets.png'), fullPage: true })
  203. await dialog.getByRole('button', { name: '产品设计团队', exact: true }).click()
  204. await expect(dialog.getByRole('textbox', { name: '名称', exact: true })).toHaveValue('产品设计团队')
  205. await dialog.getByRole('combobox', { name: '产品负责人对应的 Agent', exact: true }).selectOption({ label: '产品经理' })
  206. await dialog.getByRole('combobox', { name: '交互设计对应的 Agent', exact: true }).selectOption({ label: '开发工程师' })
  207. const [submitted] = await Promise.all([
  208. page.waitForRequest(r => r.method() === 'POST' && r.url().endsWith('/api/app/conversations')),
  209. dialog.getByRole('button', { name: '保存', exact: true }).click(),
  210. ])
  211. const payload = submitted.postDataJSON()
  212. expect(new Set(payload.memberIds).size).toBe(5)
  213. expect(Object.keys(payload.memberRoles)).toHaveLength(5)
  214. expect(payload.memberRoles[payload.administratorId].name).toBe('产品负责人')
  215. await expect(page.getByRole('heading', { name: '产品设计团队', exact: true })).toBeVisible()
  216. await page.getByRole('button', { name: '聊天设置', exact: true }).click()
  217. await expect(dialog.getByText('产品经理 · 产品负责人', { exact: false })).toBeVisible()
  218. await expect(dialog.getByText('开发工程师 · 交互设计', { exact: false })).toBeVisible()
  219. await dialog.getByRole('button', { name: '取消', exact: true }).click()
  220. await page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行').fill('请汇总设计方案')
  221. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  222. await expect(page.locator('.message--assistant')).toContainText('我是产品经理')
  223. const composer = page.getByPlaceholder('输入消息,Enter 发送,Shift + Enter 换行')
  224. await composer.fill('[hold-workspace] 正在执行的任务')
  225. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  226. await expect(page.getByRole('button', { name: '停止生成', exact: true })).toBeVisible()
  227. await composer.fill('后续排队任务')
  228. await expect(page.getByRole('button', { name: '发送消息', exact: true })).toBeEnabled()
  229. await page.getByRole('button', { name: '发送消息', exact: true }).click()
  230. await expect(page.locator('.message--user').last()).toContainText('后续排队任务')
  231. const detail = await (await page.request.get(new URL(page.url()).pathname.replace('/conversations/', '/api/app/conversations/'))).json()
  232. expect(detail.conversation.queuedMessageCount).toBe(1)
  233. await page.request.post('http://127.0.0.1:19120/__release')
  234. await expect.poll(async () => (await (await page.request.get(`/api/app/conversations/${detail.conversation.id}`)).json()).conversation.activeRunId).toBeUndefined()
  235. await page.setViewportSize({ width: 390, height: 844 })
  236. await page.getByRole('button', { name: '返回 Bot 列表', exact: true }).click()
  237. await expect(rail).toBeVisible()
  238. await expect(rail).toHaveCSS('width', '390px')
  239. await expect(page.locator('.mobile-header')).toBeHidden()
  240. await expect(rail.locator('.sidebar-feature-nav')).toHaveCount(0)
  241. await page.screenshot({ path: testInfo.outputPath('mobile-bot-list.png') })
  242. await rail.getByRole('button', { name: '新建', exact: true }).click()
  243. await expect(page.getByRole('menuitem', { name: '新建 Bot', exact: true })).toBeVisible()
  244. await page.keyboard.press('Escape')
  245. await rail.getByRole('button', { name: '搜索', exact: true }).click()
  246. await expect(page.getByRole('dialog', { name: '搜索聊天' })).toBeVisible()
  247. await expect(page.getByRole('tab', { name: '未归档', exact: true })).toBeVisible()
  248. await page.getByRole('button', { name: '关闭搜索', exact: true }).click()
  249. await rail.locator('.sidebar-account-switcher__main').click()
  250. await page.getByRole('button', { name: '进入聊天模式', exact: true }).click()
  251. await expect(page).toHaveURL(/\/chat$/)
  252. await page.goto('/')
  253. await expect(page).toHaveURL(/\/chat$/)
  254. await page.setViewportSize({ width: 1280, height: 800 })
  255. await page.getByRole('button', { name: '设置与模式', exact: true }).click()
  256. await page.getByRole('menuitem', { name: '进入 Bot 模式', exact: true }).click()
  257. await page.goto('/')
  258. await expect(page).toHaveURL(/\/conversations$/)
  259. await page.reload()
  260. await expect(rail.locator('.bot-list-header')).toBeVisible()
  261. expect(pluginCalls).toEqual([])
  262. })