desktop-hosts.spec.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { test, expect } from '@playwright/test'
  2. test('registers a desktop host, downloads its private config and disables it', async ({ page }, testInfo) => {
  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 page.locator('.desktop-sidebar .sidebar-account-switcher__main').click()
  8. await page.getByRole('menuitem', { name: '我的设置', exact: true }).click()
  9. const dialog = page.getByRole('dialog', { name: '我的设置' })
  10. await dialog.getByRole('button', { name: 'Hermes 连接', exact: true }).click()
  11. await dialog.locator('details.desktop-host-settings summary').click()
  12. const panel = dialog.getByRole('region', { name: '桌面主机设置' })
  13. await expect(panel).toContainText('尚未注册桌面主机')
  14. await panel.getByRole('textbox', { name: '主机名称', exact: true }).fill('书房 iMac')
  15. await page.screenshot({ path: testInfo.outputPath('desktop-host-registration.png') })
  16. await panel.getByRole('button', { name: '注册主机', exact: true }).click()
  17. await expect(panel).toContainText('待连接')
  18. const downloaded = page.waitForEvent('download')
  19. await panel.getByRole('button', { name: '下载主机配置', exact: true }).click()
  20. const download = await downloaded
  21. expect(download.suggestedFilename()).toBe('desktop-host.json')
  22. const stream = await download.createReadStream(); const chunks: Buffer[] = []
  23. for await (const chunk of stream!) chunks.push(Buffer.from(chunk))
  24. const configuration = JSON.parse(Buffer.concat(chunks).toString())
  25. expect(configuration).toMatchObject({ protocol: 1, serverURL: expect.stringMatching(/^https?:\/\//) })
  26. expect(configuration.hostId).toMatch(/^[0-9a-f-]{36}$/)
  27. expect(configuration.token.length).toBeGreaterThan(32)
  28. const result = await (await page.request.get('/api/app/admin/desktop-hosts')).json()
  29. expect(JSON.stringify(result)).not.toContain(configuration.token)
  30. expect(JSON.stringify(result)).not.toContain('tokenHash')
  31. await page.setViewportSize({ width: 390, height: 844 })
  32. await panel.getByRole('button', { name: '停用 书房 iMac' }).scrollIntoViewIfNeeded()
  33. await page.screenshot({ path: testInfo.outputPath('desktop-host-registered-mobile.png') })
  34. await panel.getByRole('button', { name: '停用 书房 iMac' }).click()
  35. await expect(panel).toContainText('已停用')
  36. expect((await (await page.request.get('/api/app/admin/desktop-hosts')).json()).hosts.find((h: any) => h.id === configuration.hostId).enabled).toBe(false)
  37. })