diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 1b3f8547e..cf3906e4b 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -1412,6 +1412,57 @@ const registerNetworkIpc = (mainWindow) => { folder = collection; } + // Create a map to store HookManagers for this collection/folder run + // Key format: 'collection:', 'folder:', 'request:' + const hookManagersMap = new Map(); + + // Register collection-level hooks immediately + const collectionHookManagerOptions = { + request: {}, // Placeholder request for hook registration + envVars, + runtimeVariables, + collectionPath, + processEnvVars, + scriptingConfig, + runRequestByItemPathname, + collectionName: collection?.name, + onConsoleLog: (type, args) => { + console[type](...args); + mainWindow.webContents.send('main:console-log', { + type, + args + }); + } + }; + + const collectionRoot = collection?.draft?.root || collection?.root || {}; + const collectionHooks = get(collectionRoot, 'request.script.hooks', ''); + const collectionHookManagerKey = `collection:${collectionPath}`; + await getOrCreateHookManager(hookManagersMap, collectionHookManagerKey, collectionHooks, collectionHookManagerOptions); + const isCollectionRun = folder?.uid === collection?.uid; + + // If a folder is being run (not the collection itself), register folder hooks along the folder path tree + if (folder && !isCollectionRun) { + const folderTreePath = getTreePathFromCollectionToItem(collection, folder); + + // Extract folder hooks from the folder tree path + for (const pathItem of folderTreePath) { + if (pathItem.type === 'folder') { + const folderRoot = pathItem?.draft || pathItem?.root; + const folderHooks = get(folderRoot, 'request.script.hooks', ''); + if (folderHooks && folderHooks.trim() !== '') { + const folderHookManagerKey = `folder:${pathItem.pathname}`; + await getOrCreateHookManager(hookManagersMap, folderHookManagerKey, folderHooks, collectionHookManagerOptions); + } + } + } + } + + if (isCollectionRun) { + const collectionHookManager = hookManagersMap.get(collectionHookManagerKey); + await collectionHookManager.call(HOOK_EVENTS.RUNNER_BEFORE_COLLECTION_RUN, { collection, collectionUid }); + } + mainWindow.webContents.send('main:run-folder-event', { type: 'testrun-started', isRecursive: recursive, diff --git a/packages/bruno-tests/hooks-comprehensive-tests/collection-run-start-setup.bru b/packages/bruno-tests/hooks-comprehensive-tests/collection-run-start-setup.bru index 66e0b9b3f..2f845427d 100644 --- a/packages/bruno-tests/hooks-comprehensive-tests/collection-run-start-setup.bru +++ b/packages/bruno-tests/hooks-comprehensive-tests/collection-run-start-setup.bru @@ -20,10 +20,9 @@ tests { const apiToken = bru.getEnvVar('api-token'); expect(apiToken).to.equal('mock-token-12345'); }); - + test("request should have access to setup vars", function() { const setupComplete = bru.getVar('setup-complete'); expect(setupComplete).to.equal('true'); }); } -