perf: optimize full interface loading and refresh paths
This commit is contained in:
1 parent
649a0df832
commit
f317314a3c
57 files changed
+791
-136
No files matched your search
@@ -6,7 +6,7 @@ test('history UI saves IMAP-only credentials locally, explicitly queries sent ma
|
||||
t.after(async()=>{await browser?.close();await runtime.close();fs.rmSync(dir,{recursive:true,force:true});});
|
||||
let connects=0;
|
||||
const raw=Buffer.from('From: sender@example.test\r\nTo: orders@example.test\r\nSubject: Test notice\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n<img src="https://tracker.example.test/pixel"> Huizhou evidence');
|
||||
runtime.mail.imapFactory=()=>({mailbox:{uidValidity:1n},on(){},async connect(){connects++;},async list(){return [{path:'Sent',specialUse:'\\Sent'}];},async getMailboxLock(_path,opts){assert.equal(opts.readOnly,true);return {release(){}};},async search(){return [1];},async fetchOne(uid,q){return q.source?{source:raw}:{uid,size:raw.length,envelope:{subject:'Test notice'}};},async logout(){},close(){}});
|
||||
runtime.mail.imapFactory=()=>({mailbox:{uidValidity:1n},on(){},async connect(){connects++;},async list(){return [{path:'Sent',specialUse:'\\Sent'}];},async getMailboxLock(_path,opts){assert.equal(opts.readOnly,true);return {release(){}};},async search(){return [1];},async *fetch(){yield {uid:1,size:raw.length,envelope:{subject:'Test notice'}};},async fetchOne(uid,q){return q.source?{source:raw}:{uid,size:raw.length,envelope:{subject:'Test notice'}};},async logout(){},close(){}});
|
||||
browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage();const requests=[];page.on('request',r=>requests.push(r.url()));
|
||||
await page.goto('http://127.0.0.1:'+runtime.server.address().port+'/#mail');await login(page);await page.getByRole('button',{name:'只读查阅已发送邮件',exact:true}).click();
|
||||
await page.getByRole('button',{name:'添加只读邮箱',exact:true}).click({timeout:5000});
|
||||
|
||||
@@ -3,13 +3,42 @@ const {test}=require('node:test');const assert=require('node:assert/strict');
|
||||
const {MailService}=require('../../src/modules/mail/service.cjs');
|
||||
const fs=require('node:fs'),os=require('node:os'),path=require('node:path');
|
||||
const {MailStore}=require('../../src/modules/mail/store.cjs');
|
||||
const {readHistory}=require('../../src/modules/mail/history.cjs');
|
||||
const raw=Buffer.from('From: sender@example.test\r\nTo: orders@example.test\r\nCc: factory@example.test\r\nSubject: Order notice\r\nDate: Sun, 18 Oct 2026 10:00:00 +0800\r\nMessage-ID: <notice@example.test>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nOrder body');
|
||||
function fixture(){
|
||||
const calls=[],store={channelSecret:()=>({version:1,imap:{}}),guard(){}};
|
||||
const client={mailbox:{uidValidity:9n},on(){},async connect(){},async logout(){},close(){},async list(){return [{path:'Sent',specialUse:'\\Sent',flags:new Set()},{path:'INBOX',flags:new Set()}];},async getMailboxLock(folder,options){calls.push({folder,...options});return {release(){}};},async search(query,options){calls.push({query,...options});return [1,2,3];},async fetchOne(uid,query,options){calls.push({uid,query,...options});return query.source?{source:raw}:{uid,size:raw.length,envelope:{subject:'Order notice',date:new Date('2026-10-18T02:00:00Z'),to:[{address:'orders@example.test'}]}};}};
|
||||
client.fetch=async function*(range,query,options){for(const uid of range.split(',').map(Number))yield await client.fetchOne(uid,query,options);};
|
||||
const service=new MailService({store,imapFactory:()=>client,smtpFactory:()=>{throw Error('SMTP must not be used');}});
|
||||
return {service,calls,client};
|
||||
}
|
||||
|
||||
test('history batches bounded UID envelopes, keeps descending order and releases its read lock',async()=>{
|
||||
let locked=false,released=0,batches=0;
|
||||
const client={mailbox:{uidValidity:42n},list:async()=>[{path:'Sent',specialUse:'\\Sent'}],
|
||||
async getMailboxLock(folder,options){assert.equal(folder,'Sent');assert.equal(options.readOnly,true);locked=true;return {release(){locked=false;released++;}};},
|
||||
async search(query,options){assert.equal(locked,true);assert.equal(options.uid,true);return [3,8,5,10,2];},
|
||||
fetchOne(){assert.fail('search must not fetch each message separately');},
|
||||
async *fetch(range,query,options){batches++;assert.equal(locked,true);assert.equal(range,'10,8,5');assert.deepEqual(query,{uid:true,envelope:true,size:true});assert.deepEqual(options,{uid:true});
|
||||
// An expunged UID is absent; sequence order need not equal the requested order.
|
||||
yield {uid:5,size:100,envelope:{subject:'Five'}};yield {uid:10,envelope:{subject:'Ten'}};
|
||||
yield {uid:10,size:200};yield {uid:99,size:1,envelope:{subject:'Unrequested'}};yield {seq:1,flags:new Set()};
|
||||
}
|
||||
};
|
||||
const result=await readHistory(client,{action:'search',folder:'Sent',since:'2026-10-01',before:'2026-10-02',limit:3});
|
||||
assert.deepEqual(result.messages.map(item=>[item.uid,item.subject,item.size]),[[10,'Ten',200],[5,'Five',100]]);assert.equal(result.uidValidity,'42');assert.equal(result.truncated,true);assert.equal(batches,1);assert.equal(released,1);assert.equal(locked,false);
|
||||
});
|
||||
|
||||
test('empty history search never FETCHes and an interrupted batch always releases its lock',async()=>{
|
||||
let released=0;
|
||||
const client={mailbox:{uidValidity:9n},list:async()=>[{path:'Sent',specialUse:'\\Sent'}],getMailboxLock:async()=>({release(){released++;}}),search:async()=>[],
|
||||
fetchOne:()=>assert.fail('no per-message fetch'),async *fetch(){assert.fail('empty results must not become a wildcard fetch');}
|
||||
};
|
||||
const input={action:'search',folder:'Sent',since:'2026-10-01',before:'2026-10-02'};
|
||||
assert.deepEqual((await readHistory(client,input)).messages,[]);assert.equal(released,1);
|
||||
client.search=async()=>[4];client.fetch=async function*(){throw Error('batch interrupted');};
|
||||
await assert.rejects(readHistory(client,input),/batch interrupted/);assert.equal(released,2);
|
||||
});
|
||||
test('an IMAP-only history account stores no SMTP credential and rejects all sending or template polling',async t=>{
|
||||
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'history-account-')),store=new MailStore(dir);
|
||||
const service=new MailService({store,smtpFactory:()=>{throw Error('SMTP should be unreachable');}});
|
||||
|
||||
Reference in new issue
Block a user