onboarding-renderer.test.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { readFile } from 'node:fs/promises'
  4. import { JSDOM } from 'jsdom'
  5. test('the first paint and every restoring snapshot hide the entire login guide', async () => {
  6. const html = await readFile(new URL('./boot.html', import.meta.url), 'utf8')
  7. const script = await readFile(new URL('./boot.js', import.meta.url), 'utf8')
  8. const dom = new JSDOM(html, { runScripts: 'outside-only' })
  9. const { window } = dom
  10. const guide = window.document.getElementById('onboarding')
  11. const loading = window.document.getElementById('session-loading')
  12. let respond, poll
  13. window.yaoyaoDesktop = { status: () => new Promise(resolve => { respond = resolve }) }
  14. window.setInterval = callback => { poll = callback; return 1 }
  15. try {
  16. assert.equal(guide.hidden, true, 'HTML hides the guide before JavaScript or IPC runs')
  17. assert.equal(loading.hidden, false)
  18. window.eval(script)
  19. for (const phase of ['idle', 'preparing', 'ready', 'entering', 'complete']) {
  20. respond({ phase, restoring: true })
  21. await new Promise(resolve => setImmediate(resolve))
  22. assert.equal(guide.hidden, true, phase)
  23. assert.equal(loading.hidden, false, phase)
  24. poll()
  25. }
  26. respond({ mode: 'remote', phase: 'ready', restoring: false, history: [], serverURL: 'http://remote.test' })
  27. await new Promise(resolve => setImmediate(resolve))
  28. assert.equal(guide.hidden, false, 'an expired session shows the account form')
  29. assert.equal(loading.hidden, true)
  30. assert.equal(window.document.getElementById('login-form').hidden, false)
  31. } finally { window.close() }
  32. })
  33. test('only the active page is shown; back retains server and remembered choice; expired sessions open login directly', async () => {
  34. const { DesktopOnboarding } = await import('./onboarding.mjs')
  35. const html = await readFile(new URL('./boot.html', import.meta.url), 'utf8')
  36. const script = await readFile(new URL('./boot.js', import.meta.url), 'utf8')
  37. const dom = new JSDOM(html, {runScripts:'outside-only',url:'https://preview.test'})
  38. const w=dom.window, $=id=>w.document.getElementById(id)
  39. const flow = new DesktopOnboarding({ remoteServer:()=>'',inspect:async()=>({authenticated:false}) })
  40. w.yaoyaoDesktop={status:async()=>flow.snapshot(),selectServer:async mode=>flow.select(mode),prepareServer:input=>flow.prepare(input)}
  41. w.setInterval=()=>1
  42. const tick=()=>new Promise(resolve=>setImmediate(resolve))
  43. const visible=()=>['choice-section','prepare-section','account-section'].filter(id=>!$(id).hidden)
  44. try {
  45. w.eval(script);await tick()
  46. assert.deepEqual(visible(),['choice-section'])
  47. w.document.querySelector('[value=remote]').click();$('remember').checked=true;$('continue').click();await tick()
  48. assert.deepEqual(visible(),['prepare-section'])
  49. $('server').value='https://server.test';$('server').dispatchEvent(new w.Event('input'))
  50. $('connection-form').dispatchEvent(new w.Event('submit',{cancelable:true}));await tick()
  51. assert.deepEqual(visible(),['account-section'])
  52. $('change-server').click();await tick()
  53. assert.deepEqual(visible(),['prepare-section'])
  54. assert.equal($('server').value,'https://server.test');assert.equal($('remember').checked,true)
  55. $('back').click();await tick()
  56. assert.deepEqual(visible(),['choice-section'])
  57. } finally {w.close()}
  58. })