'use strict'; const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'); const {Store}=require('../../src/modules/dashboard/store.cjs'),{createDashboard}=require('../../src/app/dashboard.cjs'),{loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs'); const OLD='Test-password-123',NEW='Changed-password-456'; test('account UI lets every role change passwords, validates confirmation, and gives admins a member reset form',{timeout:90000},async t=>{ const dir=fs.mkdtempSync(path.join(os.tmpdir(),'account-ui-')),store=new Store(dir);const admin=store.bootstrap('admin',OLD),viewer=store.addUser({username:'viewer',password:OLD,role:'viewer'},'admin');store.addUser({username:'operator',password:OLD,role:'operator'},'admin'); const server=createDashboard({store,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));const base='http://127.0.0.1:'+server.address().port;let browser; t.after(async()=>{await browser?.close();server.closeAllConnections();await new Promise(r=>server.close(r));fs.rmSync(dir,{recursive:true,force:true});});browser=await loadPlaywright().chromium.launch(browserOptions());const errors=[]; const login=async(page,username,password=OLD)=>{await page.getByLabel('账号',{exact:true}).fill(username);await page.getByLabel('密码',{exact:true}).fill(password);await page.getByRole('button',{name:'登录',exact:true}).click();await page.getByRole('link',{name:'修改密码',exact:true}).waitFor({timeout:5000});}; const page=await browser.newPage({viewport:{width:1440,height:1000}});page.on('pageerror',e=>errors.push(e.message));await page.goto(base+'/#users');await login(page,'admin'); assert.equal(await page.locator('[data-reset-user="'+admin.id+'"]').count(),0);await page.locator('[data-reset-user="'+viewer.id+'"]').click(); await page.getByLabel('管理员当前密码',{exact:true}).fill(OLD);await page.getByLabel('成员新密码',{exact:true}).fill(NEW);await page.getByLabel('确认成员新密码',{exact:true}).fill(NEW);await page.getByRole('button',{name:'确认重置密码',exact:true}).click();await page.getByText('成员密码已重置,该账号需要重新登录。',{exact:true}).waitFor();assert.ok(store.authenticate('viewer',NEW));assert.equal(await page.locator('#password-reset-form').count(),0); // The separate create-user flow remains usable after introducing reset controls. await page.getByLabel('登录账号',{exact:true}).fill('new-member');await page.getByLabel('初始密码',{exact:true}).fill(OLD);await page.getByRole('button',{name:'创建账号',exact:true}).click();await page.getByText('new-member',{exact:true}).waitFor(); for(const [username,password]of [['admin',OLD],['operator',OLD],['viewer',NEW]]){ const account=username==='admin'?page:await browser.newPage();if(account!==page){account.on('pageerror',e=>errors.push(e.message));await account.goto(base+'/#account');await login(account,username,password);} await account.getByRole('link',{name:'修改密码',exact:true}).click();await account.getByLabel('当前密码',{exact:true}).fill('incorrect');await account.getByLabel('新密码',{exact:true}).fill('Final-password-789');await account.getByLabel('确认新密码',{exact:true}).fill('different-password-789');await account.getByRole('button',{name:'保存新密码',exact:true}).click();await account.getByText('两次输入的新密码不一致。',{exact:true}).waitFor(); await account.getByLabel('确认新密码',{exact:true}).fill('Final-password-789');await account.getByRole('button',{name:'保存新密码',exact:true}).click();await account.getByText('当前密码不正确。',{exact:true}).waitFor();assert.equal(await account.getByLabel('新密码',{exact:true}).inputValue(),'Final-password-789'); await account.getByLabel('当前密码',{exact:true}).fill(password); if(username==='admin'){await account.setViewportSize({width:390,height:844});assert.equal(await account.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);await fs.promises.mkdir(path.resolve('output/verification'),{recursive:true});await account.screenshot({path:path.resolve('output/verification/account-password-mobile.png'),fullPage:true});await account.setViewportSize({width:1440,height:1000});} let release,arrived;const pending=new Promise(r=>arrived=r); if(username==='operator')await account.route('**/api/account/password',async route=>{arrived();await new Promise(r=>release=r);await route.continue();},{times:1}); await account.getByRole('button',{name:'保存新密码',exact:true}).click(); if(username==='operator'){await pending;await account.getByRole('link',{name:'系统设置',exact:true}).click();release();} await account.getByRole('heading',{name:'登录工作台',exact:true}).waitFor({timeout:5000});await account.getByText('密码已修改,请使用新密码重新登录。',{exact:true}).waitFor();await login(account,username,'Final-password-789');assert.ok(store.authenticate(username,'Final-password-789')); } assert.deepEqual(errors,[]); });