Add browser download confirmation and parameterized client startup

This commit is contained in:
tongtong committed 2026-11-20 18:41:05 +08:00
1 parent 028fec64be
commit 3cf197a98c
25 files changed
+568 -46

No files matched your search

+55 -4
View File
@@ -74,7 +74,10 @@ test('worker restart retries the same request id; disabling auto restores browse
for (const auto of [true,false]) {
const m=mock(); m.item.paused=true;
m.data.pending_1={id:1,request,phase:'sending',created:Date.now(),original:request.url};
await recover(m.api,async r=>{assert.equal(r.request_id,'test-1'); return {status:'accepted',request_id:r.request_id,task_id:'42'};},auto);
await recover(m.api,async r=>{
assert.equal(r.request_id,'test-1'); assert.equal(r.action,auto?'add':'cancel');
return auto?{status:'accepted',request_id:r.request_id,task_id:'42'}:{status:'canceled'};
},auto);
assert.equal(m.item.state,auto?'interrupted':'in_progress'); assert.equal(m.item.paused,false);
}
});
@@ -83,7 +86,8 @@ test('background worker wires observed GET to native acknowledgement before canc
const m=mock();
m.data.config={auto:true,sites:'example.com',extensions:'.zip'};
function event() { return {listeners:[], addListener(fn){this.listeners.push(fn);}, removeListener(fn){this.listeners=this.listeners.filter(x=>x!==fn);}, hasListener(fn){return this.listeners.includes(fn);}, async emit(...args){for(const fn of [...this.listeners]) await fn(...args);}}; }
const old=globalThis.chrome;
const old=globalThis.chrome, oldTimeout=globalThis.setTimeout, timers=[];
globalThis.setTimeout=(...args)=>{const timer=oldTimeout(...args); timers.push(timer); return timer;};
const api={...m.api, runtime:{id:'test-extension',getURL:()=> 'chrome-extension://test-extension/',onInstalled:event(),onStartup:event(),onMessage:event(),
connectNative(name){assert.equal(name,'com.ttstd.ttdownloader'); const port={onMessage:event(),onDisconnect:event(),disconnect(){},postMessage(r){m.events.push('ack');queueMicrotask(()=>port.onMessage.emit({status:'accepted',request_id:r.request_id,task_id:'42',message:'accepted'}));}}; return port;}},
contextMenus:{onClicked:event()},alarms:{onAlarm:event(),create(){}},permissions:{onAdded:event(),onRemoved:event(),async contains(){return true;}},
@@ -95,7 +99,7 @@ test('background worker wires observed GET to native acknowledgement before canc
await api.webRequest.onBeforeRequest.emit({url:request.url,method:'GET'});
await api.downloads.onCreated.emit(m.item);
assert.deepEqual(m.events,['pause','ack','cancel']); assert.equal(m.data.lastResult.status,'accepted');
} finally { globalThis.chrome=old; }
} finally { timers.forEach(clearTimeout); globalThis.setTimeout=oldTimeout; globalThis.chrome=old; }
});
test('zero task id is not an acknowledgement', async () => {
@@ -104,7 +108,7 @@ test('zero task id is not an acknowledgement', async () => {
});
test('restart never pauses a sending task that the user resumed', async () => {
const m=mock(); m.data.pending_1={id:1,request,phase:'sending',created:Date.now(),original:request.url};
await recover(m.api,async()=>assert.fail('do not resend'),true);
await recover(m.api,async r=>{assert.equal(r.action,'cancel'); return {status:'canceled'};},true);
assert.equal(m.item.paused,false); assert.deepEqual(m.events,[]);
});
test('storage failure during fallback must not strand a paused download', async () => {
@@ -119,3 +123,50 @@ test('manual cross-site handoff does not leak source query parameters', () => {
assert.equal(linkReferrer('https://source.test/page?secret=value','http://download.test/file'),'');
assert.equal(linkReferrer('https://source.test/page#fragment','https://source.test/file'),'https://source.test/page');
});
test('confirmation waits without canceling browser and queries the same request after restart', async () => {
const m=mock();
const pending=await handoff(m.api,m.item,request,async r=>({status:'awaiting_confirmation',request_id:r.request_id}));
assert.equal(pending.status,'awaiting_confirmation'); assert.deepEqual(m.events,['pause']);
assert.equal(m.data.pending_1.phase,'confirming');
const results=await recover(m.api,async r=>{
assert.deepEqual(r,{action:'query',request_id:request.request_id});
return {status:'accepted',request_id:r.request_id,task_id:'42'};
},true);
assert.equal(results[0].status,'accepted'); assert.deepEqual(m.events,['pause','cancel']);
});
test('closing the desktop dialog restores browser download', async () => {
const m=mock();
await handoff(m.api,m.item,request,async r=>({status:'awaiting_confirmation',request_id:r.request_id}));
await recover(m.api,async()=>({status:'canceled',message:'用户取消'}),true);
assert.deepEqual(m.events,['pause','resume']); assert.equal(m.data.pending_1,undefined);
});
test('expired confirmation and disabled capture close the pending desktop dialog before resuming', async () => {
for(const auto of [true,false]) {
const m=mock();
await handoff(m.api,m.item,request,async r=>({status:'awaiting_confirmation',request_id:r.request_id}));
if(auto) m.data.pending_1.created=Date.now()-121000;
await recover(m.api,async r=>{assert.equal(r.action,'cancel'); m.events.push('dismiss'); return {status:'canceled'};},auto);
assert.deepEqual(m.events,['pause','dismiss','resume']);
}
});
test('confirmation wins a simultaneous timeout only after a persisted acknowledgement', async () => {
const m=mock();
await handoff(m.api,m.item,request,async r=>({status:'awaiting_confirmation',request_id:r.request_id}));
await recover(m.api,async r=>{assert.equal(r.action,'cancel'); return {status:'accepted',request_id:r.request_id,task_id:'42'};},false);
assert.deepEqual(m.events,['pause','cancel']);
});
test('user resuming a waiting browser download dismisses the unconfirmed desktop dialog', async () => {
const m=mock();
await handoff(m.api,m.item,request,async r=>({status:'awaiting_confirmation',request_id:r.request_id}));
m.item.paused=false;
await recover(m.api,async r=>{assert.equal(r.action,'cancel'); return {status:'canceled'};},true);
assert.deepEqual(m.events,['pause']); assert.equal(m.data.pending_1,undefined);
});
test('storage failure after acceptance never resumes an already handed-off download', async () => {
const m=mock(), set=m.api.storage.local.set;
m.api.storage.local.set=async values=>{if(values.pending_1?.phase==='cancel') throw new Error('quota'); return set(values);};
const result=await handoff(m.api,m.item,request,async r=>({status:'accepted',request_id:r.request_id,task_id:'42'}));
assert.equal(result.status,'accepted'); assert.deepEqual(m.events,['pause','cancel']);
});