test(browser): complete guest protection fixture contracts

This commit is contained in:
Brennan Benson
2026-11-09 22:39:40 -08:00
parent a43f790f42
commit 54549b00a7
8 changed files with 19 additions and 6 deletions
@@ -86,6 +86,7 @@ describe('browserManager', () => {
const guest = {
id: 1711,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: vi.fn(),
@@ -143,6 +144,7 @@ describe('browserManager', () => {
const makeGuest = (id: number) => ({
id,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: vi.fn(),
@@ -62,6 +62,7 @@ function createGuest(id: number) {
getURL: () => 'https://example.com/',
isCurrentlyAudible: () => false,
isDestroyed: () => destroyed,
isCrashed: () => false,
isDevToolsOpened: () => opened,
setWindowOpenHandler: vi.fn(),
setDevToolsWebContents: vi.fn((host: DevToolsHost) => {
@@ -178,6 +178,7 @@ function createGuest(id: number) {
return {
id,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: browserMocks.guestSetBackgroundThrottlingMock,
@@ -26,6 +26,7 @@ export function makeGuest(id: number, mocks: GrabTestMocks): Electron.WebContent
return {
id,
isDestroyed: mocks.guestIsDestroyedMock,
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: mocks.guestSetBackgroundThrottlingMock,
@@ -81,6 +81,7 @@ describe('browserManager', () => {
const guest = {
id: 144,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: guestSetBackgroundThrottlingMock,
@@ -168,6 +169,7 @@ describe('browserManager', () => {
const guest = {
id: 150,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: guestSetBackgroundThrottlingMock,
@@ -209,6 +211,7 @@ describe('browserManager', () => {
const child = {
id: 1500 + index,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
setBackgroundThrottling: vi.fn(),
@@ -71,6 +71,7 @@ describe('browserManager page prompt routing', () => {
const guest = {
id: 107,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
getURL: vi.fn(() => 'https://trusted.example/home'),
@@ -163,6 +164,7 @@ describe('browserManager page prompt routing', () => {
const guest = {
id: 109,
isDestroyed: vi.fn(() => false),
isCrashed: vi.fn(() => false),
isCurrentlyAudible: vi.fn(() => false),
getType: vi.fn(() => 'webview'),
getURL: vi.fn(() => ''),
@@ -80,11 +80,13 @@ function openTab(url: string): { tab: string; id: number; handle: ViewportGuestH
}
function navigate(url: string, { inPlace = false } = {}): void {
const didStartNavigation = mocks.guestOnMock.mock.calls.findLast(
const didStartNavigation = mocks.guestOnMock.mock.calls.filter(
([event]) => event === 'did-start-navigation'
)?.[1]
expect(didStartNavigation).toBeTypeOf('function')
didStartNavigation(null, url, inPlace, true)
)
expect(didStartNavigation.length).toBeGreaterThan(0)
for (const [, listener] of didStartNavigation) {
listener({ url, isSameDocument: inPlace, isMainFrame: true }, url, inPlace, true)
}
}
function failDeviceMetrics(handle: ViewportGuestHandle): void {
@@ -27,7 +27,7 @@ describe('browser window close guard', () => {
it.each([true, false])('routes a close only from the top frame: %s', (isMainFrame) => {
const sendToHost = vi.fn()
const nativeClose = vi.fn()
const pageWindow = { close: nativeClose }
const pageWindow = { close: nativeClose, addEventListener: vi.fn() }
const preload = buildSync({
entryPoints: [fileURLToPath(new URL('./browser-guest.ts', import.meta.url))],
bundle: true,
@@ -39,11 +39,12 @@ describe('browser window close guard', () => {
runInNewContext(preload, {
window: pageWindow,
document: { addEventListener: vi.fn() },
process: { isMainFrame },
require: (name: string) => {
expect(name).toBe('electron')
return {
ipcRenderer: { sendToHost },
ipcRenderer: { sendToHost, send: vi.fn(), on: vi.fn() },
contextBridge: {
executeInMainWorld: (script: {
func: (requestClose: () => void) => void