api-service-workers-spec.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { session, webContents, WebContents } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { v4 } from 'uuid';
  4. import { on, once } from 'node:events';
  5. import * as fs from 'node:fs';
  6. import * as http from 'node:http';
  7. import * as path from 'node:path';
  8. import { listen } from './lib/spec-helpers';
  9. const partition = 'service-workers-spec';
  10. describe('session.serviceWorkers', () => {
  11. let ses: Electron.Session;
  12. let server: http.Server;
  13. let baseUrl: string;
  14. let w: WebContents;
  15. before(async () => {
  16. ses = session.fromPartition(partition);
  17. await ses.clearStorageData();
  18. });
  19. beforeEach(async () => {
  20. const uuid = v4();
  21. server = http.createServer((req, res) => {
  22. const url = new URL(req.url!, `http://${req.headers.host}`);
  23. // /{uuid}/{file}
  24. const file = url.pathname!.split('/')[2]!;
  25. if (file.endsWith('.js')) {
  26. res.setHeader('Content-Type', 'application/javascript');
  27. }
  28. res.end(fs.readFileSync(path.resolve(__dirname, 'fixtures', 'api', 'service-workers', file)));
  29. });
  30. const { port } = await listen(server);
  31. baseUrl = `http://localhost:${port}/${uuid}`;
  32. w = (webContents as typeof ElectronInternal.WebContents).create({ session: ses });
  33. });
  34. afterEach(async () => {
  35. w.destroy();
  36. server.close();
  37. await ses.clearStorageData();
  38. });
  39. describe('getAllRunning()', () => {
  40. it('should initially report none are running', () => {
  41. expect(ses.serviceWorkers.getAllRunning()).to.deep.equal({});
  42. });
  43. it('should report one as running once you load a page with a service worker', async () => {
  44. w.loadURL(`${baseUrl}/index.html`);
  45. await once(ses.serviceWorkers, 'console-message');
  46. const workers = ses.serviceWorkers.getAllRunning();
  47. const ids = Object.keys(workers) as any[] as number[];
  48. expect(ids).to.have.lengthOf(1, 'should have one worker running');
  49. });
  50. });
  51. describe('getFromVersionID()', () => {
  52. it('should report the correct script url and scope', async () => {
  53. w.loadURL(`${baseUrl}/index.html`);
  54. const eventInfo = await once(ses.serviceWorkers, 'console-message');
  55. const details: Electron.MessageDetails = eventInfo[1];
  56. const worker = ses.serviceWorkers.getFromVersionID(details.versionId);
  57. expect(worker).to.not.equal(null);
  58. expect(worker).to.have.property('scope', baseUrl + '/');
  59. expect(worker).to.have.property('scriptUrl', baseUrl + '/sw.js');
  60. });
  61. });
  62. describe('console-message event', () => {
  63. it('should correctly keep the source, message and level', async () => {
  64. const messages: Record<string, Electron.MessageDetails> = {};
  65. w.loadURL(`${baseUrl}/index.html?scriptUrl=sw-logs.js`);
  66. for await (const [, details] of on(ses.serviceWorkers, 'console-message')) {
  67. messages[details.message] = details;
  68. expect(details).to.have.property('source', 'console-api');
  69. if (Object.keys(messages).length >= 4) break;
  70. }
  71. expect(messages).to.have.property('log log');
  72. expect(messages).to.have.property('info log');
  73. expect(messages).to.have.property('warn log');
  74. expect(messages).to.have.property('error log');
  75. expect(messages['log log']).to.have.property('level', 1);
  76. expect(messages['info log']).to.have.property('level', 1);
  77. expect(messages['warn log']).to.have.property('level', 2);
  78. expect(messages['error log']).to.have.property('level', 3);
  79. });
  80. });
  81. });