chore: Add missing tests for dev mode CSP (#662)

This commit is contained in:
Aaron
2024-05-20 23:35:21 -05:00
committed by GitHub
parent 5d8efef02f
commit 87d9511b31
2 changed files with 102 additions and 1 deletions
@@ -10,6 +10,7 @@ import {
fakeOptionsEntrypoint,
fakePopupEntrypoint,
fakeSidepanelEntrypoint,
fakeWxtDevServer,
setFakeWxt,
} from '../testing/fake-objects';
import { Manifest } from 'webextension-polyfill';
@@ -1496,6 +1497,89 @@ describe('Manifest Utils', () => {
expect(actual.host_permissions).toBeUndefined();
});
});
describe('Dev mode', () => {
it('should not add any code for production builds', async () => {
setFakeWxt({
config: {
command: 'build',
},
server: {
hostname: 'localhost',
port: 3000,
origin: 'http://localhost:3000',
},
});
const output = fakeBuildOutput();
const entrypoints: Entrypoint[] = [];
const { manifest: actual } = await generateManifest(
entrypoints,
output,
);
expect(actual.permissions).toBeUndefined();
expect(actual.content_security_policy).toBeUndefined();
});
it('should add required permissions for dev mode to function for MV2', async () => {
setFakeWxt({
config: {
command: 'serve',
manifestVersion: 2,
},
server: fakeWxtDevServer({
port: 3000,
hostname: 'localhost',
origin: 'http://localhost:3000',
}),
});
const output = fakeBuildOutput();
const entrypoints: Entrypoint[] = [];
const { manifest: actual } = await generateManifest(
entrypoints,
output,
);
expect(actual).toMatchObject({
content_security_policy:
"script-src 'self' http://localhost:3000; object-src 'self';",
permissions: ['http://localhost/*', 'tabs'],
});
});
it('should add required permissions for dev mode to function for MV3', async () => {
setFakeWxt({
config: {
command: 'serve',
manifestVersion: 3,
browser: 'chrome',
},
server: fakeWxtDevServer({
hostname: 'localhost',
port: 3000,
origin: 'http://localhost:3000',
}),
});
const output = fakeBuildOutput();
const entrypoints: Entrypoint[] = [];
const { manifest: actual } = await generateManifest(
entrypoints,
output,
);
expect(actual).toMatchObject({
content_security_policy: {
extension_pages:
"script-src 'self' 'wasm-unsafe-eval' http://localhost:3000; object-src 'self';",
},
host_permissions: ['http://localhost/*'],
permissions: ['tabs', 'scripting'],
});
});
});
});
describe('stripPathFromMatchPattern', () => {
@@ -311,10 +311,27 @@ export const fakeWxt = fakeObjectCreator<Wxt>(() => ({
logger: mock(),
reloadConfig: vi.fn(),
pm: mock(),
server: faker.helpers.arrayElement([undefined, mock<WxtDevServer>()]),
server: faker.helpers.arrayElement([undefined, fakeWxtDevServer()]),
builder: mock(),
}));
export const fakeWxtDevServer = fakeObjectCreator<WxtDevServer>(() => ({
currentOutput: fakeBuildOutput(),
hostname: 'localhost',
origin: 'http://localhost:3000',
port: 3000,
reloadContentScript: vi.fn(),
reloadExtension: vi.fn(),
reloadPage: vi.fn(),
restart: vi.fn(),
restartBrowser: vi.fn(),
start: vi.fn(),
stop: vi.fn(),
transformHtml: vi.fn(),
watcher: mock(),
ws: mock(),
}));
export function setFakeWxt(overrides?: DeepPartial<Wxt>) {
const wxt = fakeWxt(overrides);
setWxtForTesting(wxt);