1// Tests of the install block of static/js/ermite.js (ermiteInstall): the
2// service worker registration and the "install app" button. Run with
3// node --test 'internal/adapters/web/jstest/*.test.mjs'
4import assert from 'node:assert/strict';
5import { readFileSync } from 'node:fs';
6import { test } from 'node:test';
7import { dom, el, installDom } from './dom.mjs';
8
9const doc = installDom();
10const button = el('button', { attrs: { 'data-install': '' } });
11button.hidden = true;
12document.body.append(button);
13
14const setNavigator = (v) => Object.defineProperty(globalThis, 'navigator', { value: v, configurable: true, writable: true });
15const listeners = {};
16globalThis.addEventListener = (type, fn) => { listeners[type] = fn; };
17const fire = (type, ev) => listeners[type](ev);
18let standalone = false;
19globalThis.matchMedia = () => ({ matches: standalone });
20doc.readyState = 'loading';
21const registered = [];
22setNavigator({ serviceWorker: { register: async (url, opts) => { registered.push([url, opts]); return {}; } } });
23
24// Only the ermiteInstall block is evaluated: the rest needs a page.
25const src = readFileSync(new URL('../static/js/ermite.js', import.meta.url), 'utf8');
26const start = src.indexOf(' window.ermiteInstall = ');
27const end = src.indexOf(' })();', start) + ' })();'.length;
28new Function('$$', src.slice(start, end))((sel) => document.querySelectorAll(sel));
29const I = window.ermiteInstall;
30
31// promptEvent is a beforeinstallprompt event.
32function promptEvent(outcome = 'accepted') {
33 const e = { prevented: false, prompts: 0, preventDefault() { this.prevented = true; } };
34 e.prompt = async () => { e.prompts++; };
35 e.userChoice = Promise.resolve({ outcome });
36 return e;
37}
38
39test('the service worker is registered at the root scope once the page has loaded', async () => {
40 assert.equal(registered.length, 0, 'not before load');
41 await listeners.load();
42 assert.deepEqual(registered, [['/-/sw.js', { scope: '/' }]]);
43});
44
45test('a refused registration is not an error', async () => {
46 setNavigator({ serviceWorker: { register: () => Promise.reject(new Error('insecure')) } });
47 assert.equal(await I.register(), null);
48 setNavigator({});
49 assert.equal(await I.register(), null);
50});
51
52test('the button shows on beforeinstallprompt, and a click prompts once', async () => {
53 const e = promptEvent();
54 assert.equal(button.hidden, true);
55 fire('beforeinstallprompt', e);
56 assert.equal(e.prevented, true, 'the browser banner is replaced by the button');
57 assert.equal(button.hidden, false);
58 const done = listeners.click({ target: { closest: (s) => (s === '[data-install]' ? button : null) } });
59 assert.equal(done, undefined);
60 assert.equal(await I.pending, 'accepted');
61 assert.equal(await I.prompt(), '', 'the event was used by the click');
62 assert.equal(e.prompts, 1);
63 assert.equal(button.hidden, true);
64});
65
66test('the button stays while the dialog is up; its focus then moves to the search', async () => {
67 const search = el('button', { attrs: { 'aria-controls': 'panel-cmd' } });
68 document.body.append(search);
69 const e = promptEvent('dismissed');
70 let shownDuring = null;
71 e.prompt = async () => { shownDuring = !button.hidden; };
72 fire('beforeinstallprompt', e);
73 button.focus();
74 dom.focused = [];
75 assert.equal(await I.prompt(), 'dismissed');
76 assert.equal(shownDuring, true, 'not hidden under the dialog');
77 assert.equal(button.hidden, true);
78 assert.deepEqual(dom.focused, [search]);
79 dom.active = null;
80 search.remove();
81});
82
83test('prompt without an event does nothing; a dismissal reports its outcome', async () => {
84 assert.equal(await I.prompt(), '');
85 const e = promptEvent('dismissed');
86 fire('beforeinstallprompt', e);
87 assert.equal(await I.prompt(), 'dismissed');
88 assert.equal(button.hidden, true);
89});
90
91test('appinstalled hides the button', () => {
92 fire('beforeinstallprompt', promptEvent());
93 assert.equal(button.hidden, false);
94 fire('appinstalled', {});
95 assert.equal(button.hidden, true);
96});
97
98test('an app that runs installed never shows the button', () => {
99 standalone = true;
100 const e = promptEvent();
101 fire('beforeinstallprompt', e);
102 assert.equal(button.hidden, true);
103 assert.equal(e.prevented, false);
104 standalone = false;
105});