web: the offer to install is made once, and the answer is kept
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Install support is not visible in the source. It is the manifest the browser
|
||||
// reads, the worker it registers, and what that worker still holds once the network
|
||||
// is gone — so unlike the other checks here, which read a module and assert on it,
|
||||
// this one has to drive a real browser at a real build. Four assertions, one line
|
||||
// this one has to drive a real browser at a real build. Five assertions, one line
|
||||
// each, against a preview server:
|
||||
//
|
||||
// npx vite build && npx vite preview --port 4183 &
|
||||
@@ -25,7 +25,7 @@ const browser = await chromium.launch({
|
||||
});
|
||||
const context = await browser.newContext();
|
||||
|
||||
// Every assertion prints, rather than throwing on the first one: which of the four
|
||||
// Every assertion prints, rather than throwing on the first one: which of the five
|
||||
// works and which does not is the whole answer.
|
||||
let failed = 0;
|
||||
const check = (ok, label) => {
|
||||
@@ -78,9 +78,39 @@ try {
|
||||
}
|
||||
check(/RecipesCam/.test(brand ?? ''), `offline reload of /app paints the app's own header: ${brand}`);
|
||||
|
||||
// (e) The offer is asked once. The browser's sheet is stubbed, because a check cannot
|
||||
// click the real one: waved away it is a refusal, and a refusal is stored, so the next
|
||||
// visit renders no dialog at all. The stub goes in an init script so it is there on
|
||||
// both loads.
|
||||
await context.setOffline(false);
|
||||
await page.addInitScript(() => {
|
||||
window.__offerInstall = () => {
|
||||
const event = new Event('beforeinstallprompt');
|
||||
event.prompt = () => Promise.resolve();
|
||||
event.userChoice = Promise.resolve({ outcome: 'dismissed' });
|
||||
window.dispatchEvent(event);
|
||||
window.dispatchEvent(new Event('rc:install-offer'));
|
||||
};
|
||||
});
|
||||
const offer = async () => {
|
||||
await page.evaluate(() => window.__offerInstall());
|
||||
await page.waitForTimeout(150);
|
||||
return page.locator('[data-key="install-yes"]').count();
|
||||
};
|
||||
await page.reload();
|
||||
const first = await offer();
|
||||
await page.locator('[data-key="install-yes"]').click();
|
||||
const stored = await page.evaluate(() => localStorage.getItem('rc.install.dismissed.v1'));
|
||||
await page.reload();
|
||||
const again = await offer();
|
||||
check(
|
||||
first === 1 && stored === '1' && again === 0,
|
||||
`install dialog shown ${first} time(s), refused (localStorage '${stored}'), shown again after reload: ${again}`,
|
||||
);
|
||||
|
||||
await browser.close();
|
||||
if (failed) {
|
||||
console.error(`pwa-check: ${failed} of 4 failed`);
|
||||
console.error(`pwa-check: ${failed} of 5 failed`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('pwa-check ok');
|
||||
|
||||
Reference in New Issue
Block a user