execution-auth.spec.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {test,expect} from '@playwright/test'
  2. import {rmSync,writeFileSync} from 'node:fs'
  3. test('authorizes in a separate window, persists the login and retains file import on mobile',async({page,context},testInfo)=>{
  4. const marker=process.env.YAOYAO_CODEX_FIXTURE_COMPLETE!
  5. rmSync(marker,{force:true})
  6. try{
  7. await context.route('https://auth.openai.com/codex/device',route=>route.fulfill({contentType:'text/html; charset=utf-8',body:'<!doctype html><html lang="zh"><title>模拟账号授权</title><h1>模拟账号授权</h1><p>仅用于测试,不连接真实账号。</p><button>完成授权</button></html>'}))
  8. await page.goto('/conversations')
  9. await page.getByRole('textbox',{name:'账号',exact:true}).fill('fixture');await page.getByRole('textbox',{name:'密码',exact:true}).fill('fixture-pass');await page.getByRole('button',{name:'登录',exact:true}).click()
  10. await page.locator('.desktop-sidebar').getByRole('button',{name:'工具',exact:true}).click();await page.getByRole('menuitem',{name:'Bot 设置',exact:true}).click()
  11. const settings=page.getByRole('dialog',{name:'Bot 设置',exact:true})
  12. await settings.getByRole('button',{name:'环境与授权',exact:true}).click();await settings.getByRole('button',{name:'添加授权或变量',exact:true}).click()
  13. const dialog=page.getByRole('dialog',{name:'添加授权',exact:true})
  14. await expect(dialog.getByRole('button',{name:'浏览器授权(推荐)',exact:true})).toHaveAttribute('aria-pressed','true')
  15. await expect(dialog.getByRole('button',{name:'保存授权',exact:true})).toBeDisabled()
  16. const popupPromise=context.waitForEvent('page');await dialog.getByRole('button',{name:'打开浏览器授权',exact:true}).click();const popup=await popupPromise
  17. await expect(popup.getByRole('heading',{name:'模拟账号授权'})).toBeVisible()
  18. await expect(dialog.getByText('TEST-12345',{exact:true})).toBeVisible()
  19. await page.screenshot({path:testInfo.outputPath('browser-authorization.png')})
  20. await popup.getByRole('button',{name:'完成授权',exact:true}).click();writeFileSync(marker,'complete');await popup.close();await page.bringToFront()
  21. await expect(dialog.getByText('浏览器授权成功。选择允许使用的 Bot,然后保存授权。',{exact:true})).toBeVisible()
  22. await dialog.getByRole('textbox',{name:'名称',exact:true}).fill('浏览器授权验收')
  23. await dialog.getByRole('button',{name:'保存授权',exact:true}).click();await expect(dialog).toBeHidden()
  24. await expect(settings.getByRole('button',{name:'编辑 浏览器授权验收',exact:true})).toBeVisible()
  25. const resources=await(await page.request.get('/api/app/admin/execution')).json()
  26. expect(resources.resources[0]).toMatchObject({name:'浏览器授权验收',kind:'codex',agentIds:[],configured:true})
  27. expect(JSON.stringify(resources)).not.toContain('codex-browser-fixture-token')
  28. await settings.getByRole('button',{name:'编辑 浏览器授权验收',exact:true}).click()
  29. const edit=page.getByRole('dialog',{name:'编辑授权',exact:true});await expect(edit.getByText('已有登录会保留;完成新授权并保存后才会替换。',{exact:true})).toBeVisible()
  30. await page.setViewportSize({width:390,height:844})
  31. await edit.getByRole('button',{name:'导入授权文件',exact:true}).click()
  32. await edit.getByLabel('选择授权文件').setInputFiles({name:'auth.json',mimeType:'application/json',buffer:Buffer.from(JSON.stringify({tokens:{access_token:'file-fixture-token'}}))})
  33. await expect(edit.getByText('已读取 auth.json',{exact:true})).toBeVisible()
  34. await page.screenshot({path:testInfo.outputPath('file-import-mobile.png')})
  35. expect(await edit.evaluate(el=>el.scrollWidth<=el.clientWidth)).toBe(true)
  36. await edit.getByRole('button',{name:'保存授权',exact:true}).click();await expect(edit).toBeHidden()
  37. const updated=await(await page.request.get('/api/app/admin/execution')).json();expect(updated.resources[0].revision).toBe(2)
  38. }finally{rmSync(marker,{force:true})}
  39. })