2020-03-20 20:28:31 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as path from 'path';
|
2021-01-23 01:37:51 +00:00
|
|
|
import { session, webContents, WebContents } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import { v4 } from 'uuid';
|
2023-02-20 11:30:57 +00:00
|
|
|
import { listen } from './lib/spec-helpers';
|
2023-02-23 23:53:53 +00:00
|
|
|
import { on, once } from 'events';
|
2020-02-20 23:19:06 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const partition = 'service-workers-spec';
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
describe('session.serviceWorkers', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let ses: Electron.Session;
|
|
|
|
let server: http.Server;
|
|
|
|
let baseUrl: string;
|
2023-02-19 09:24:24 +00:00
|
|
|
let w: WebContents;
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
before(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ses = session.fromPartition(partition);
|
|
|
|
await ses.clearStorageData();
|
2021-01-23 01:37:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
const uuid = v4();
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
// /{uuid}/{file}
|
2020-03-20 20:28:31 +00:00
|
|
|
const file = req.url!.split('/')[2]!;
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
if (file.endsWith('.js')) {
|
2020-03-20 20:28:31 +00:00
|
|
|
res.setHeader('Content-Type', 'application/javascript');
|
2020-02-20 23:19:06 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
res.end(fs.readFileSync(path.resolve(__dirname, 'fixtures', 'api', 'service-workers', file)));
|
|
|
|
});
|
2023-02-20 11:30:57 +00:00
|
|
|
const { port } = await listen(server);
|
|
|
|
baseUrl = `http://localhost:${port}/${uuid}`;
|
2020-02-20 23:19:06 +00:00
|
|
|
|
2023-02-16 14:41:41 +00:00
|
|
|
w = (webContents as typeof ElectronInternal.WebContents).create({ session: ses });
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
afterEach(async () => {
|
2022-12-14 21:07:38 +00:00
|
|
|
w.destroy();
|
2020-03-20 20:28:31 +00:00
|
|
|
server.close();
|
2021-01-23 01:37:51 +00:00
|
|
|
await ses.clearStorageData();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
describe('getAllRunning()', () => {
|
|
|
|
it('should initially report none are running', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(ses.serviceWorkers.getAllRunning()).to.deep.equal({});
|
|
|
|
});
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
it('should report one as running once you load a page with a service worker', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
w.loadURL(`${baseUrl}/index.html`);
|
|
|
|
await once(ses.serviceWorkers, 'console-message');
|
2020-03-20 20:28:31 +00:00
|
|
|
const workers = ses.serviceWorkers.getAllRunning();
|
|
|
|
const ids = Object.keys(workers) as any[] as number[];
|
|
|
|
expect(ids).to.have.lengthOf(1, 'should have one worker running');
|
|
|
|
});
|
|
|
|
});
|
2020-02-20 23:19:06 +00:00
|
|
|
|
2021-01-23 01:37:51 +00:00
|
|
|
describe('getFromVersionID()', () => {
|
2020-02-20 23:19:06 +00:00
|
|
|
it('should report the correct script url and scope', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
w.loadURL(`${baseUrl}/index.html`);
|
|
|
|
const eventInfo = await once(ses.serviceWorkers, 'console-message');
|
2020-03-20 20:28:31 +00:00
|
|
|
const details: Electron.MessageDetails = eventInfo[1];
|
|
|
|
const worker = ses.serviceWorkers.getFromVersionID(details.versionId);
|
|
|
|
expect(worker).to.not.equal(null);
|
|
|
|
expect(worker).to.have.property('scope', baseUrl + '/');
|
|
|
|
expect(worker).to.have.property('scriptUrl', baseUrl + '/sw.js');
|
|
|
|
});
|
|
|
|
});
|
2020-02-20 23:19:06 +00:00
|
|
|
|
|
|
|
describe('console-message event', () => {
|
|
|
|
it('should correctly keep the source, message and level', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const messages: Record<string, Electron.MessageDetails> = {};
|
2023-02-23 23:53:53 +00:00
|
|
|
w.loadURL(`${baseUrl}/logs.html`);
|
|
|
|
for await (const [, details] of on(ses.serviceWorkers, 'console-message')) {
|
|
|
|
messages[details.message] = details;
|
|
|
|
expect(details).to.have.property('source', 'console-api');
|
|
|
|
if (Object.keys(messages).length >= 4) break;
|
2020-02-20 23:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(messages).to.have.property('log log');
|
|
|
|
expect(messages).to.have.property('info log');
|
|
|
|
expect(messages).to.have.property('warn log');
|
|
|
|
expect(messages).to.have.property('error log');
|
|
|
|
expect(messages['log log']).to.have.property('level', 1);
|
|
|
|
expect(messages['info log']).to.have.property('level', 1);
|
|
|
|
expect(messages['warn log']).to.have.property('level', 2);
|
|
|
|
expect(messages['error log']).to.have.property('level', 3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|