cmvr-wms/mobile-app/tests/updates.test.mjs

54 lines
6.4 KiB
JavaScript
Raw Normal View History

import test from 'node:test';import assert from 'node:assert/strict';
import {createUpdateController} from '../src/core/updates.ts';
function fixture(overrides={},options={}){
const views=[],calls={checks:0,fetches:0,reloads:0};
const driver={isEnabled:true,checkForUpdateAsync:async()=>{calls.checks++;return {isAvailable:true};},fetchUpdateAsync:async()=>{calls.fetches++;return {isNew:true};},reloadAsync:async()=>{calls.reloads++;},...overrides};
const controller=createUpdateController(driver,v=>views.push(v),options);return {controller,views,calls,last:()=>views.at(-1)};
}
test('concurrent checks share one request, download then prompt, and never automatically reload',async()=>{
let finish;const f=fixture({checkForUpdateAsync:()=>{f.calls.checks++;return new Promise(r=>finish=r);}});
const one=f.controller.check(),two=f.controller.check(true);assert.equal(one,two);await Promise.resolve();finish({isAvailable:true});await one;await f.controller.check();
assert.deepEqual(f.calls,{checks:1,fetches:1,reloads:0});assert.deepEqual(f.views.map(v=>v.state),['checking','downloading','ready']);assert.equal(f.last().visible,true);
});
test('disabled, no update and offline retain the current app without a popup',async()=>{
for(const [enabled,result,expected]of [[false,false,['disabled']],[true,false,['checking','current']],[true,'error',['checking','failed']]]){
const f=fixture({isEnabled:enabled,checkForUpdateAsync:async()=>{if(result==='error')throw Error('offline');return {isAvailable:result};}});await f.controller.check();assert.deepEqual(f.views.map(v=>v.state),expected);assert.equal(f.last().visible,false);assert.equal(f.calls.fetches,0);assert.equal(f.calls.reloads,0);
}
});
test('foreground checks are throttled, while a manual check may retry immediately',async()=>{
let time=0;const f=fixture({checkForUpdateAsync:async()=>{f.calls.checks++;return {isAvailable:false};}},{now:()=>time});await f.controller.check();
time=100;f.controller.foreground(false);f.controller.foreground(true);await f.controller.check();assert.equal(f.calls.checks,1);
time=15*60_000;f.controller.foreground(true);await f.controller.check();assert.equal(f.calls.checks,2);await f.controller.check(true);assert.equal(f.calls.checks,3);
});
test('an offline launch can recover on returning to foreground after a minute',async()=>{
let time=0;const f=fixture({checkForUpdateAsync:async()=>{f.calls.checks++;if(f.calls.checks===1)throw Error('offline');return {isAvailable:true};}},{now:()=>time});await f.controller.check();assert.equal(f.last().state,'failed');
time=59_999;f.controller.foreground(true);await f.controller.check();assert.equal(f.calls.checks,1);time=60_000;f.controller.foreground(true);await f.controller.check();assert.equal(f.calls.checks,2);assert.equal(f.last().visible,true);
});
test('background downloads wait for foreground; postponing never prompts repeatedly or reloads',async()=>{
const f=fixture({},{active:false});await f.controller.check();assert.equal(f.last().state,'ready');assert.equal(f.last().visible,false);f.controller.foreground(true);assert.equal(f.last().visible,true);
f.controller.foreground(false);assert.equal(f.last().visible,false);f.controller.foreground(true);assert.equal(f.last().visible,true);
f.controller.later();f.controller.foreground(false);f.controller.foreground(true);assert.equal(f.last().visible,false);assert.equal(f.calls.reloads,0);await f.controller.restart();assert.equal(f.calls.reloads,0);
await f.controller.check(true);assert.equal(f.last().visible,true);assert.equal(f.calls.checks,1);
});
test('a download finishing while backgrounded does not show a hidden native dialog',async()=>{
let finish;const f=fixture({fetchUpdateAsync:()=>new Promise(r=>finish=r)});const pending=f.controller.check();await Promise.resolve();await Promise.resolve();f.controller.foreground(false);finish({isNew:true});await pending;assert.equal(f.last().visible,false);f.controller.foreground(true);assert.equal(f.last().visible,true);
});
test('only a confirmed ready visible prompt reloads, even on double taps',async()=>{
let finish;const f=fixture({reloadAsync:()=>{f.calls.reloads++;return new Promise(r=>finish=r);}});await f.controller.restart();assert.equal(f.calls.reloads,0);await f.controller.check();
const one=f.controller.restart(),two=f.controller.restart();assert.equal(one,two);assert.equal(f.last().restarting,true);f.controller.later();assert.equal(f.last().visible,true);await Promise.resolve();assert.equal(f.calls.reloads,1);finish();await one;
});
test('reload failure retains the downloaded update and allows a confirmed retry',async()=>{
const f=fixture({reloadAsync:async()=>{f.calls.reloads++;if(f.calls.reloads===1)throw Error('native error');}});await f.controller.check();await f.controller.restart();assert.equal(f.last().state,'ready');assert.equal(f.last().restarting,false);assert.match(f.last().error,/重试/);await f.controller.restart();assert.equal(f.calls.reloads,2);
});
test('a changed server response is not called a ready update and approved rollback works',async()=>{
const f=fixture({fetchUpdateAsync:async()=>({isNew:false})});await f.controller.check();assert.equal(f.last().state,'current');assert.equal(f.last().visible,false);
const rollback=fixture({checkForUpdateAsync:async()=>({isAvailable:false,isRollBackToEmbedded:true}),fetchUpdateAsync:async()=>({isNew:false,isRollBackToEmbedded:true})});await rollback.controller.check();assert.equal(rollback.last().state,'ready');assert.equal(rollback.calls.reloads,0);
});
test('failed downloads can retry without ever losing the current working app',async()=>{
const f=fixture({fetchUpdateAsync:async()=>{f.calls.fetches++;if(f.calls.fetches===1)throw Error('network');return {isNew:true};}});await f.controller.check();assert.equal(f.last().state,'failed');assert.equal(f.last().visible,false);await f.controller.check(true);assert.equal(f.last().state,'ready');assert.equal(f.calls.reloads,0);
});
test('unmount ignores late check/download callbacks and cannot start a reload',async()=>{
let finish;const f=fixture({checkForUpdateAsync:()=>new Promise(r=>finish=r)});const pending=f.controller.check();await Promise.resolve();f.controller.dispose();finish({isAvailable:true});await pending;assert.deepEqual(f.views.map(v=>v.state),['checking']);assert.equal(f.calls.fetches,0);
const ready=fixture();await ready.controller.check();const reload=ready.controller.restart();ready.controller.dispose();await reload;assert.equal(ready.calls.reloads,0);
});