工程化:增加package.json、Prettier与ESLint配置,修复测试连接未关闭导致挂起

- 新账系统/package.json:仅开发依赖(prettier、eslint、@eslint/js、globals),运行时仍零依赖
- Prettier 只处理 .js/.mjs/.cjs/.css;ESLint 扁平配置区分后端、浏览器脚本、测试与 e2e
- scripts/gen-browser-globals.js 生成浏览器脚本间共享的全局变量清单
- 测试 after 钩子关闭服务前先 closeAllConnections(),避免未读完的响应让 server.close() 永久等待
- 4 处读取源码文本的测试断言改为不依赖空白写法(格式化前后均通过),断言含义不变

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
黄健铭andClaude Opus 5.5 committed 2026-12-04 08:55:46 -08:00
1 parent 91479fd13f
commit 3743d687e7
59 files changed
+2625 -57

No files matched your search

+2 -2
View File
@@ -3,7 +3,7 @@ const test=require('node:test'),assert=require('node:assert/strict'),crypto=requ
const {createApp}=require('../server');
test('HTTP权限、登录、CSRF、并发重试、客户页面和备份访问',async t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'maimai-http-test-'));const app=createApp({mode:'demo',port:0,dataDir:dir});await new Promise(r=>app.server.listen(0,'127.0.0.1',r));
t.after(async()=>{await new Promise(r=>app.server.close(r));app.ledger.close();fs.rmSync(dir,{recursive:true,force:true});});
t.after(async()=>{await new Promise(r=>{app.server.closeAllConnections();app.server.close(r);});app.ledger.close();fs.rmSync(dir,{recursive:true,force:true});});
const base='http://127.0.0.1:'+app.server.address().port;
let cookie='';const call=async(route,data,key=crypto.randomUUID(),extra={})=>{const r=await fetch(base+route,{method:data?'POST':'GET',headers:{...(cookie?{Cookie:cookie}:{}),...(data?{'Content-Type':'application/json',Origin:base,'X-Requested-With':'maimai-ledger','Idempotency-Key':key}:{}),...extra},...(data?{body:JSON.stringify(data)}:{})});return r;};
assert.equal((await call('/api/state')).status,401);assert.equal((await call('/api/demo-login',{role:'owner'},undefined,{Origin:'https://evil.example'})).status,403);
@@ -23,7 +23,7 @@ test('HTTP权限、登录、CSRF、并发重试、客户页面和备份访问',a
test('初始化及密码均哈希保存,非本机演示启动被阻止',async t=>{
assert.throws(()=>createApp({mode:'demo',host:'0.0.0.0'}),/只能绑定本机/);
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'maimai-auth-test-'));const app=createApp({mode:'trial',port:0,dataDir:dir});await new Promise(r=>app.server.listen(0,'127.0.0.1',r));
t.after(async()=>{await new Promise(r=>app.server.close(r));app.ledger.close();fs.rmSync(dir,{recursive:true,force:true});});const base='http://127.0.0.1:'+app.server.address().port;
t.after(async()=>{await new Promise(r=>{app.server.closeAllConnections();app.server.close(r);});app.ledger.close();fs.rmSync(dir,{recursive:true,force:true});});const base='http://127.0.0.1:'+app.server.address().port;
const call=(p,b)=>fetch(base+p,{method:'POST',headers:{'Content-Type':'application/json','X-Requested-With':'maimai-ledger',Origin:base},body:JSON.stringify(b)});
const passwords={ownerPassword:'synthetic-owner-123',assistantPassword:'synthetic-helper-123'};assert.equal((await call('/api/setup',passwords)).status,200);assert.equal((await call('/api/setup',passwords)).status,403);assert.equal((await call('/api/login',{name:'麦麦',password:passwords.ownerPassword})).status,200);assert.equal((await call('/api/login',{name:'麦麦',password:'bad'})).status,401);assert.ok(!JSON.stringify(app.ledger.rows('SELECT * FROM users')).includes(passwords.ownerPassword));
});