| 1 | import assert from 'node:assert/strict'; |
| 2 | import {createWorkbench} from './workbench-fixture.mjs'; |
| 3 | let fixture; |
| 4 | const calls=[]; |
| 5 | let cancelBiometric=false; |
| 6 | let unavailable=false, supported=true, legacyLauncher=false, failEnrollment=false, sequence=0; |
| 7 | const keychain=new Map(); |
| 8 | fixture=await createWorkbench({nativeRequest:async(body)=>{ |
| 9 | if(body.action!=='accountDevice')return null; |
| 10 | const {operation,kind}=body; |
| 11 | const a=fixture?.state.account || {};a.devices ||= {}; |
| 12 | const binding=a.device_binding || 'original-directory'; |
| 13 | calls.push({operation,kind,binding}); |
| 14 | if(operation==='status'&&legacyLauncher)return {supported:true,biometric_available:true}; |
| 15 | if(operation==='status')return {supported,biometric_available:true,device_status:Object.fromEntries(['automatic','biometric'].map(k=>[ |
| 16 | k,!a.devices[k]?'disabled':unavailable?'unavailable':keychain.get(binding+'.'+k)===a.devices[k]?(k==='biometric'?'authentication_required':'available'):'missing', |
| 17 | ]))}; |
| 18 | if(operation==='enable'){ |
| 19 | a.devices[kind]='fixture-'+kind+'-'+(++sequence); |
| 20 | if(failEnrollment){delete a.devices[kind];throw Error('钥匙串保存失败');} |
| 21 | keychain.set(binding+'.'+kind,a.devices[kind]); |
| 22 | } |
| 23 | if(operation==='disable'){delete a.devices[kind];keychain.delete(binding+'.'+kind);} |
| 24 | if(operation==='unlock'){ |
| 25 | assert.equal(keychain.get(binding+'.'+kind),a.devices[kind],'attempted unlock with missing credentials'); |
| 26 | if(kind==='biometric'&&cancelBiometric)throw Error('已取消指纹验证,可继续使用密码'); |
| 27 | if(kind==='automatic'&&a.require_authentication)throw Error('手动锁定'); |
| 28 | a.unlocked=true;a.require_authentication=false; |
| 29 | } |
| 30 | return {ok:true}; |
| 31 | }}); |
| 32 | const {page,state,browser,errors}=fixture; |
| 33 | const menu=()=>page.getByRole('button',{name:/^账户菜单,/}); |
| 34 | try { |
| 35 | Object.assign(state.account,{exists:false,unlocked:false,username:'',devices:{}}); |
| 36 | await page.reload(); |
| 37 | const defaultRemember=page.getByRole('checkbox',{name:/记住登录/}); |
| 38 | await defaultRemember.waitFor();assert.equal(await defaultRemember.isChecked(),true); |
| 39 | await page.getByLabel('用户名',{exact:true}).fill('林间工作室'); |
| 40 | await page.getByLabel('设置密码',{exact:true}).fill('fixture-password'); |
| 41 | await page.getByLabel('确认密码',{exact:true}).fill('fixture-password'); |
| 42 | await page.getByRole('button',{name:'创建本地账户',exact:true}).click();await menu().waitFor(); |
| 43 | await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 44 | const automatic=page.getByRole('checkbox',{name:/记住登录/}); |
| 45 | const biometric=page.getByRole('checkbox',{name:/使用 Touch ID 解锁/}); |
| 46 | await page.waitForFunction(()=>document.querySelector('.account-device-settings input')?.checked); |
| 47 | assert.equal(await automatic.isChecked(),true); |
| 48 | await biometric.click();await page.getByRole('status').filter({hasText:'已开启'}).waitFor(); |
| 49 | assert.ok(state.account.devices.automatic&&state.account.devices.biometric); |
| 50 | await page.screenshot({path:'.oil-ui/local-account/shots/device-settings.png'}); |
| 51 | |
| 52 | state.account.unlocked=false;await page.reload();await menu().waitFor(); |
| 53 | assert.equal(calls.filter(x=>x.operation==='unlock'&&x.kind==='automatic').length,1); |
| 54 | await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 55 | await automatic.click();await page.getByRole('status').filter({hasText:'已关闭'}).waitFor(); |
| 56 | await page.reload();await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 57 | assert.equal(await automatic.isChecked(),false,'explicit opt-out was overridden'); |
| 58 | await automatic.click();await page.getByRole('status').filter({hasText:'已开启'}).waitFor(); |
| 59 | await page.getByRole('button',{name:'返回工作台',exact:true}).click(); |
| 60 | await menu().click();await page.getByRole('menuitem',{name:/锁定 Cage/}).click(); |
| 61 | await page.getByRole('region',{name:'解锁 Cage',exact:true}).waitFor(); |
| 62 | await page.reload();await page.getByRole('button',{name:'使用 Touch ID 解锁',exact:true}).waitFor(); |
| 63 | assert.equal(calls.filter(x=>x.operation==='unlock'&&x.kind==='automatic').length,1); |
| 64 | cancelBiometric=true;await page.getByRole('button',{name:'使用 Touch ID 解锁',exact:true}).click(); |
| 65 | await page.getByRole('alert').filter({hasText:'已取消指纹验证'}).waitFor(); |
| 66 | assert.equal(state.account.unlocked,false); |
| 67 | await page.screenshot({path:'.oil-ui/local-account/shots/touch-id-fallback.png'}); |
| 68 | cancelBiometric=false;await page.getByRole('button',{name:'使用 Touch ID 解锁',exact:true}).click();await menu().waitFor(); |
| 69 | |
| 70 | |
| 71 | |
| 72 | const workbenchURL=page.url(); |
| 73 | await page.goto('about:blank'); |
| 74 | const oldDevices=structuredClone(state.account.devices); |
| 75 | const unlocksBeforeMigration=calls.filter(x=>x.operation==='unlock').length; |
| 76 | Object.assign(state.account,{device_binding:'migrated-directory',unlocked:false,require_authentication:false}); |
| 77 | await page.goto(workbenchURL); |
| 78 | await page.getByRole('status').filter({hasText:'当前目录的快捷登录凭据缺失'}).waitFor(); |
| 79 | assert.equal(calls.filter(x=>x.operation==='unlock').length,unlocksBeforeMigration,JSON.stringify(calls)); |
| 80 | assert.equal(await page.getByRole('button',{name:'使用 Touch ID 解锁',exact:true}).count(),0); |
| 81 | await page.reload();await page.getByRole('status').filter({hasText:'当前目录的快捷登录凭据缺失'}).waitFor(); |
| 82 | assert.equal(calls.filter(x=>x.operation==='unlock').length,unlocksBeforeMigration,'reopen retried invalid enrollment'); |
| 83 | await page.getByLabel('账户密码',{exact:true}).fill('fixture-password'); |
| 84 | await page.getByRole('button',{name:'解锁 Cage',exact:true}).click();await menu().waitFor(); |
| 85 | await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 86 | await page.getByText('当前目录的凭据缺失或已失效,请重新开启。',{exact:true}).first().waitFor(); |
| 87 | assert.equal(await automatic.isChecked(),false);assert.equal(await biometric.isChecked(),false); |
| 88 | assert.deepEqual(state.account.devices,oldDevices,'checking status mutated authorization'); |
| 89 | await page.locator('.account-device-settings').screenshot({path:'.oil-ui/local-account/shots/device-migrated.png'}); |
| 90 | failEnrollment=true;await automatic.click();await page.getByRole('alert').filter({hasText:'钥匙串保存失败'}).waitFor(); |
| 91 | await automatic.waitFor();assert.equal(await automatic.isChecked(),false);assert.equal(state.account.devices.automatic,undefined); |
| 92 | failEnrollment=false;await automatic.click();await page.getByRole('status').filter({hasText:'已开启'}).waitFor(); |
| 93 | await page.waitForFunction(()=>document.querySelector('.account-device-settings input')?.checked); |
| 94 | await biometric.click();await page.getByRole('status').filter({hasText:'已开启'}).waitFor(); |
| 95 | await page.waitForFunction(()=>[...document.querySelectorAll('.account-device-settings input')].every(e=>e.checked)); |
| 96 | assert.equal(keychain.get('original-directory.automatic'),oldDevices.automatic,'repair touched original binding'); |
| 97 | state.account.unlocked=false;await page.reload();await menu().waitFor(); |
| 98 | assert.equal(calls.filter(x=>x.operation==='unlock').length,unlocksBeforeMigration+1,'repaired enrollment did not unlock'); |
| 99 | |
| 100 | |
| 101 | unavailable=true;await page.reload();await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 102 | await page.getByRole('button',{name:'重新检查快捷登录'}).waitFor(); |
| 103 | assert.equal(await automatic.isChecked(),false);assert.equal(await automatic.isDisabled(),true); |
| 104 | const savedDevices=structuredClone(state.account.devices); |
| 105 | unavailable=false;await page.getByRole('button',{name:'重新检查快捷登录'}).click(); |
| 106 | await page.waitForFunction(()=>document.querySelector('.account-device-settings input')?.checked); |
| 107 | assert.deepEqual(state.account.devices,savedDevices); |
| 108 | |
| 109 | |
| 110 | legacyLauncher=true;await page.reload();await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 111 | await page.getByRole('status').filter({hasText:'当前应用启动器需要更新'}).waitFor(); |
| 112 | assert.equal(await automatic.count(),0);assert.equal(await biometric.count(),0); |
| 113 | assert.deepEqual(state.account.devices,savedDevices); |
| 114 | legacyLauncher=false; |
| 115 | |
| 116 | supported=false;state.account.unlocked=false;await page.reload(); |
| 117 | await page.getByLabel('账户密码',{exact:true}).waitFor(); |
| 118 | assert.equal(await page.getByRole('button',{name:'使用 Touch ID 解锁',exact:true}).count(),0); |
| 119 | assert.equal(calls.filter(x=>x.operation==='unlock').length,unlocksBeforeMigration+1); |
| 120 | await page.getByLabel('账户密码',{exact:true}).fill('fixture-password'); |
| 121 | await page.getByRole('button',{name:'解锁 Cage',exact:true}).click();await menu().waitFor(); |
| 122 | await menu().click();await page.getByRole('menuitem',{name:'设置',exact:true}).click(); |
| 123 | assert.equal(await automatic.count(),0); |
| 124 | supported=true;await page.reload();await menu().waitFor(); |
| 125 | |
| 126 | |
| 127 | const beforeRestart=calls.filter(x=>x.operation==='unlock').length; |
| 128 | Object.assign(state.account,{device_binding:'restarted-directory',unlocked:false,require_authentication:false}); |
| 129 | await page.evaluate(()=>window.dispatchEvent(new Event('cage-account-locked'))); |
| 130 | await page.getByRole('status').filter({hasText:'当前目录的快捷登录凭据缺失'}).waitFor(); |
| 131 | assert.equal(calls.filter(x=>x.operation==='unlock').length,beforeRestart,'locked response reused old availability'); |
| 132 | await page.getByLabel('账户密码',{exact:true}).fill('fixture-password'); |
| 133 | await page.getByRole('button',{name:'解锁 Cage',exact:true}).click();await menu().waitFor(); |
| 134 | await menu().click();await page.getByRole('menuitem',{name:'退出账户',exact:true}).click(); |
| 135 | await page.getByRole('dialog').getByRole('button',{name:'停止环境并退出账户',exact:true}).click(); |
| 136 | await page.getByRole('region',{name:'解锁 Cage',exact:true}).waitFor();await page.reload(); |
| 137 | await page.getByLabel('账户密码',{exact:true}).waitFor();assert.deepEqual(state.account.devices,{}); |
| 138 | assert.equal(await page.getByRole('button',{name:'使用 Touch ID 解锁',exact:true}).count(),0); |
| 139 | assert.deepEqual(errors,[]); |
| 140 | console.log('PASS: enrollment, reopen, manual lock, cancellation, migration, repair, failed save, temporary Keychain failure, unsupported platforms and logout'); |
| 141 | }finally{await browser.close();} |