onboarding.mjs 1.3 KB

12345678910111213141516171819202122
  1. /** Enter through the native local-admin flow, then set isolated fixture credentials. */
  2. export async function enterLocal(page, { username = 'desktop-fixture', password = 'desktop-fixture-password' } = {}) {
  3. await page.waitForURL(url => url.protocol === 'http:' || url.protocol === 'https:', { timeout: 90000 })
  4. await page.evaluate(async ({username,password}) => {
  5. const bootstrap = await (await fetch('/api/app/bootstrap')).json()
  6. if (!bootstrap.user?.localDesktop) throw new Error('Native administrator session was not established')
  7. const response = await fetch('/api/app/account/credentials', { method: 'PUT',
  8. headers: {'Content-Type':'application/json','X-CSRF-Token':bootstrap.csrfToken},
  9. body: JSON.stringify({username,currentPassword:'',newPassword:password}) })
  10. if (!response.ok) throw new Error('Fixture account configuration failed')
  11. }, {username,password})
  12. // The native service may finish its own navigation after credentials rotate.
  13. // Retry only interrupted navigation; all other browser failures remain errors.
  14. const url = new URL('/conversations', page.url()).href
  15. for (let attempt = 0; ; attempt++) {
  16. try { await page.goto(url); break }
  17. catch (error) {
  18. if (attempt >= 2 || !String(error.message).includes('net::ERR_ABORTED')) throw error
  19. await page.waitForLoadState('load')
  20. }
  21. }
  22. }