test: activate extension

This commit is contained in:
Samuel Maddock
2025-02-11 17:51:35 -05:00
parent d848852381
commit 8425588129
5 changed files with 90 additions and 0 deletions

View File

@@ -6,3 +6,4 @@
* `path` string - The extension's file path.
* `version` string
* `url` string - The extension's `chrome-extension://` URL.
* `enabled` boolean - Whether the extension is enabled.

View File

@@ -3,6 +3,7 @@ import { app, session, BrowserWindow, ipcMain, WebContents, Extension, Session }
import { expect } from 'chai';
import * as WebSocket from 'ws';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import * as fs from 'node:fs/promises';
import * as http from 'node:http';
@@ -223,6 +224,57 @@ describe('chrome extensions', () => {
}
});
describe('enable/disable', () => {
it('disables an extension', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
const extension = await customSession.loadExtension(path.join(fixtures, 'extensions', 'mv3-service-worker'));
expect(extension.enabled).to.be.true();
customSession.disableExtension(extension.id);
expect(customSession.getExtension(extension.id)!.enabled).to.be.false();
});
it('enables a previously disabled extension', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
const extension = await customSession.loadExtension(path.join(fixtures, 'extensions', 'mv3-service-worker'));
expect(extension.enabled).to.be.true();
customSession.disableExtension(extension.id);
expect(customSession.getExtension(extension.id)!.enabled).to.be.false();
customSession.enableExtension(extension.id);
expect(customSession.getExtension(extension.id)!.enabled).to.be.true();
});
it('persists disabled extensions', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
const extension = await customSession.loadExtension(path.join(fixtures, 'extensions', 'mv3-service-worker'));
customSession.disableExtension(extension.id);
customSession.removeExtension(extension.id);
const extension2 = await customSession.loadExtension(path.join(fixtures, 'extensions', 'mv3-service-worker'));
expect(extension2.enabled).to.be.false();
});
it('activates extension when enabled', async () => {
const runActivateExtensionTest = async () => {
const appPath = path.join(fixtures, 'apps', 'activate-extension', 'main.js');
const appProcess = spawn(process.execPath, [appPath], {
cwd: path.join(fixtures, 'apps', 'activate-extension'),
stdio: 'inherit'
});
const [code] = await once(appProcess, 'close');
expect(code).to.equal(0);
};
console.log('running test 1');
await runActivateExtensionTest();
console.log('running test 2');
await runActivateExtensionTest();
});
});
it('emits extension lifecycle events', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);

View File

@@ -0,0 +1,27 @@
const { app, session } = require('electron');
const path = require('node:path');
const { setTimeout } = require('node:timers/promises');
app.whenReady().then(async () => {
const ses = session.defaultSession;
const fixtures = path.join(__dirname, '..', '..');
const extPath = path.join(fixtures, 'extensions', 'simple-background-sw');
const activatedPromise = new Promise((resolve) => {
ses.serviceWorkers.on('running-status-changed', ({ versionId, runningStatus }) => {
console.log(`service worker ${versionId} now ${runningStatus}`);
if (runningStatus === 'running') {
resolve();
}
});
});
await ses.loadExtension(extPath);
await activatedPromise;
// Need to wait for data to be flushed?
await setTimeout(1000);
app.exit(0);
});

View File

@@ -0,0 +1 @@
console.log('started');

View File

@@ -0,0 +1,9 @@
{
"name": "Simple background sw",
"description": "just a lil service worker",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
}
}