From e5d720d98d55b1f23e49d3820df6957063a1d1a7 Mon Sep 17 00:00:00 2001 From: Fabrizio Duroni Date: Fri, 27 Feb 2026 07:22:16 -0800 Subject: [PATCH] Added missing tests for Standalone fusebox shell (#55460) Summary: This PR adds the missing test coverage for the standalone Fusebox debugger shell in `StandaloneFuseboxShell-test`. First, I added some missing expectation for the success case (`enableStandaloneFuseboxShell=true` and no errors) already present: - the assertion that `unstable_prepareFuseboxShell` is called just one time during middleware initialization - the assertion that `launchDebuggerAppWindow` is never called when the standalone shell is available Then, I added the missing test for the case when `unstable_prepareFuseboxShell` returns one of the error code like eg. `platform_not_supported` (as reported also in the TODO that I removed). In this way now the test is verifying the full behaviour for the new standalone mode for debugger/dev tools. ## Changelog: [GENERAL] [CHANGED] - Added missing tests for Standalone fusebox shell Pull Request resolved: https://github.com/facebook/react-native/pull/55460 Test Plan: - Ran `StandaloneFuseboxShell-test` and verify all tests cases passed - Ran the full React Native test suite to ensure all tests pass. - Verified test correctness by intentionally breaking production code: - removing the calls to the `DefaultBrowserLauncher` in `openDebuggerMiddleware` to verify that the tests were correctly failing Reviewed By: Abbondanzo Differential Revision: D93157076 Pulled By: huntie fbshipit-source-id: 51c12ad67dd929035f779b53add9163a6619cf41 --- .../__tests__/StandaloneFuseboxShell-test.js | 261 ++++++++++++------ 1 file changed, 182 insertions(+), 79 deletions(-) diff --git a/packages/dev-middleware/src/__tests__/StandaloneFuseboxShell-test.js b/packages/dev-middleware/src/__tests__/StandaloneFuseboxShell-test.js index 7453d2af21c..89385b56ceb 100644 --- a/packages/dev-middleware/src/__tests__/StandaloneFuseboxShell-test.js +++ b/packages/dev-middleware/src/__tests__/StandaloneFuseboxShell-test.js @@ -9,7 +9,10 @@ */ import type {JsonPagesListResponse} from '../inspector-proxy/types'; -import type {DevToolLauncher} from '../types/DevToolLauncher'; +import type { + DebuggerShellPreparationResult, + DevToolLauncher, +} from '../types/DevToolLauncher'; import {fetchJson, requestLocal} from './FetchUtils'; import {createDeviceMock} from './InspectorDeviceUtils'; @@ -21,112 +24,212 @@ const PAGES_POLLING_DELAY = 2100; jest.useFakeTimers(); -describe('enableStandaloneFuseboxShell experiment', () => { +async function setupDevice( + serverRef: {+serverBaseWsUrl: string, ...}, + signal: AbortSignal, +) { + const device = await createDeviceMock( + `${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`, + signal, + ); + device.getPages.mockImplementation(() => [ + { + app: 'bar-app', + id: 'page1', + title: 'bar-title', + vm: 'bar-vm', + capabilities: { + nativePageReloads: true, + }, + }, + ]); + jest.advanceTimersByTime(PAGES_POLLING_DELAY); + + return device; +} + +function setupToolLauncherWithFuseboxShell( + prepareDebuggerShell: () => Promise, +) { const ToolLauncherWithFuseboxShell: DevToolLauncher = { launchDebuggerAppWindow: async (url: string) => {}, launchDebuggerShell: () => { throw new Error('Not implemented'); }, - prepareDebuggerShell: async () => { - return {code: 'not_implemented'}; - }, + prepareDebuggerShell, }; - const serverRef = withServerForEachTest({ - logger: undefined, - unstable_toolLauncher: ToolLauncherWithFuseboxShell, - unstable_experiments: { - enableStandaloneFuseboxShell: true, - }, - }); + + const prepareDebuggerShellSpy = jest.spyOn( + ToolLauncherWithFuseboxShell, + 'prepareDebuggerShell', + ); + + return { + ToolLauncherWithFuseboxShell, + prepareDebuggerShellSpy, + }; +} + +describe('enableStandaloneFuseboxShell experiment', () => { const autoCleanup = withAbortSignalForEachTest(); + afterEach(() => { jest.clearAllMocks(); }); describe('/open-debugger endpoint', () => { - test('launches the shell with a frontend URL and stable window key', async () => { - // Connect a device to use when opening the debugger - const device = await createDeviceMock( - `${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`, - autoCleanup.signal, - ); - device.getPages.mockImplementation(() => [ - { - app: 'bar-app', - id: 'page1', - title: 'bar-title', - vm: 'bar-vm', - capabilities: { - // Ensure the device target can be found when launching the debugger - nativePageReloads: true, - }, + describe('success', () => { + const {ToolLauncherWithFuseboxShell, prepareDebuggerShellSpy} = + setupToolLauncherWithFuseboxShell(() => + Promise.resolve({code: 'success'}), + ); + const server = withServerForEachTest({ + logger: undefined, + unstable_toolLauncher: ToolLauncherWithFuseboxShell, + unstable_experiments: { + enableStandaloneFuseboxShell: true, }, - ]); - jest.advanceTimersByTime(PAGES_POLLING_DELAY); + }); - const launchDebuggerAppWindowSpy = jest - .spyOn(ToolLauncherWithFuseboxShell, 'launchDebuggerAppWindow') - .mockResolvedValue(); - const showFuseboxShellSpy = jest - .spyOn(ToolLauncherWithFuseboxShell, 'launchDebuggerShell') - .mockResolvedValue(); + test('launches the shell with a frontend URL and stable window key', async () => { + // Connect a device to use when opening the debugger + const device = await setupDevice(server, autoCleanup.signal); - try { - // Fetch the target information for the device - const pageListResponse = await fetchJson( - `${serverRef.serverBaseUrl}/json`, - ); - // Select the first target from the page list response - expect(pageListResponse.length).toBeGreaterThanOrEqual(1); - const firstPage = pageListResponse[0]; + const launchDebuggerAppWindowSpy = jest + .spyOn(ToolLauncherWithFuseboxShell, 'launchDebuggerAppWindow') + .mockResolvedValue(); + const showFuseboxShellSpy = jest + .spyOn(ToolLauncherWithFuseboxShell, 'launchDebuggerShell') + .mockResolvedValue(); - // Build the URL for the debugger - const openUrl = new URL('/open-debugger', serverRef.serverBaseUrl); - openUrl.searchParams.set('launchId', 'launch1'); - openUrl.searchParams.set( - 'device', - firstPage.reactNative.logicalDeviceId, - ); - openUrl.searchParams.set('target', firstPage.id); - // Request to open the debugger for the first device - { + try { + // Fetch the target information for the device + const pageListResponse = await fetchJson( + `${server.serverBaseUrl}/json`, + ); + // Select the first target from the page list response + expect(pageListResponse.length).toBeGreaterThanOrEqual(1); + const firstPage = pageListResponse[0]; + + // Build the URL for the debugger + const openUrl = new URL('/open-debugger', server.serverBaseUrl); + openUrl.searchParams.set('launchId', 'launch1'); + openUrl.searchParams.set( + 'device', + firstPage.reactNative.logicalDeviceId, + ); + openUrl.searchParams.set('target', firstPage.id); + // Request to open the debugger for the first device const response = await requestLocal(openUrl.toString(), { method: 'POST', }); + // Ensure the request was handled properly expect(response.statusCode).toBe(200); - } - openUrl.searchParams.set('launchId', 'launch1'); - // Ensure the debugger was launched using the standalone shell API - expect(showFuseboxShellSpy).toHaveBeenCalledWith( - expect.any(String), - expect.any(String), - ); - const firstWindowKey = showFuseboxShellSpy.mock.calls[0][1]; + // Debugger was launched but will fail to prepare standalone shell + expect(prepareDebuggerShellSpy).toHaveBeenCalled(); - showFuseboxShellSpy.mockClear(); - openUrl.searchParams.set('launchId', 'launch2'); + // Ensure the debugger was launched using the standalone shell API + expect(showFuseboxShellSpy).toHaveBeenCalledWith( + expect.any(String), + expect.any(String), + ); - { - const response = await requestLocal(openUrl.toString(), { + // No call to the regular browser launcher since standalone shell should be used + expect(launchDebuggerAppWindowSpy).not.toHaveBeenCalled(); + + const firstWindowKey = showFuseboxShellSpy.mock.calls[0][1]; + + showFuseboxShellSpy.mockClear(); + openUrl.searchParams.set('launchId', 'launch2'); + + const anotherResponse = await requestLocal(openUrl.toString(), { method: 'POST', }); - // Ensure the request was handled properly - expect(response.statusCode).toBe(200); - } - // Ensure the debugger was launched using the standalone shell API and the same window key - expect(showFuseboxShellSpy).toHaveBeenCalledWith( - expect.any(String), - firstWindowKey, - ); - expect(launchDebuggerAppWindowSpy).not.toHaveBeenCalled(); - } finally { - device.close(); - } + // Ensure the request was handled properly + expect(anotherResponse.statusCode).toBe(200); + + // Ensure the debugger was launched using the standalone shell API and the same window key + expect(showFuseboxShellSpy).toHaveBeenCalledWith( + expect.any(String), + firstWindowKey, + ); + + // Ensure the debugger preparation function was called, just one time, during middleware initialization + expect(showFuseboxShellSpy).toHaveBeenCalledTimes(1); + + // No fallback needed + expect(launchDebuggerAppWindowSpy).not.toHaveBeenCalled(); + } finally { + device.close(); + } + }); }); - // TODO(moti): Add tests around prepareDebuggerShell + describe('prepareDebuggerShell failures', () => { + const {ToolLauncherWithFuseboxShell, prepareDebuggerShellSpy} = + setupToolLauncherWithFuseboxShell(() => + Promise.resolve({code: 'platform_not_supported'}), + ); + const server = withServerForEachTest({ + logger: undefined, + unstable_toolLauncher: ToolLauncherWithFuseboxShell, + unstable_experiments: { + enableStandaloneFuseboxShell: true, + }, + }); + + test('falls back to browser window when preparation fails', async () => { + // Connect a device to use when opening the debugger + const device = await setupDevice(server, autoCleanup.signal); + + const launchDebuggerAppWindowSpy = jest + .spyOn(ToolLauncherWithFuseboxShell, 'launchDebuggerAppWindow') + .mockResolvedValue(); + const showFuseboxShellSpy = jest + .spyOn(ToolLauncherWithFuseboxShell, 'launchDebuggerShell') + .mockResolvedValue(); + + try { + // Fetch the target information for the device + const pageListResponse = await fetchJson( + `${server.serverBaseUrl}/json`, + ); + // Select the first target from the page list response + expect(pageListResponse.length).toBeGreaterThanOrEqual(1); + const firstPage = pageListResponse[0]; + + // Build the URL for the debugger + const openUrl = new URL('/open-debugger', server.serverBaseUrl); + openUrl.searchParams.set('launchId', 'launch1'); + openUrl.searchParams.set( + 'device', + firstPage.reactNative.logicalDeviceId, + ); + openUrl.searchParams.set('target', firstPage.id); + + // Request to open the debugger for the first device + const response = await requestLocal(openUrl.toString(), { + method: 'POST', + }); + + // Ensure the request was handled properly + expect(response.statusCode).toBe(200); + + // Debugger was launched but will fail to prepare standalone shell + expect(prepareDebuggerShellSpy).toHaveBeenCalled(); + + // Debugger is not launched with standalone shell since preparation failed + expect(showFuseboxShellSpy).not.toHaveBeenCalled(); + + // Debugger fallback + expect(launchDebuggerAppWindowSpy).toHaveBeenCalled(); + } finally { + device.close(); + } + }); + }); }); });