testchimp-runner-core 0.0.87 → 0.0.88

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { PlaywrightExecutionRequest, PlaywrightExecutionResponse, ScriptExecutionRequest, ScriptExecutionResponse } from './types';
1
+ import { PlaywrightExecutionRequest, PlaywrightExecutionResponse, ScriptExecutionRequest, ScriptExecutionResponse, TestFileExecutionRequest, TestFileExecutionResponse } from './types';
2
2
  import { AuthConfig } from './auth-config';
3
3
  import { LLMProvider } from './llm-provider';
4
4
  import { ProgressReporter } from './progress-reporter';
@@ -84,12 +84,15 @@ export declare class ExecutionService {
84
84
  */
85
85
  private initializeBrowser;
86
86
  /**
87
- * Execute step with command-level tracking
88
- * Parses step code into individual commands and executes them one-by-one
89
- * Maintains execution context so variables defined in earlier commands/steps are accessible to later ones
90
- * Returns successful commands, the failing command, remaining commands, and error
91
- * This allows us to preserve successful commands and continue with remaining ones after repair
87
+ * Execute a test file with hooks support
88
+ * Parses the test file, extracts hooks and tests, executes them in correct order
89
+ * Each test is reported separately with its own jobId
92
90
  */
93
- private executeStepWithTracking;
91
+ runTestFile(request: TestFileExecutionRequest): Promise<TestFileExecutionResponse>;
92
+ /**
93
+ * Execute a hook (beforeAll, afterAll, beforeEach, afterEach)
94
+ * Uses the same execution context as tests
95
+ */
96
+ private executeHook;
94
97
  }
95
98
  //# sourceMappingURL=execution-service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"execution-service.d.ts","sourceRoot":"","sources":["../src/execution-service.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,0BAA0B,EAC1B,2BAA2B,EAE3B,sBAAsB,EACtB,uBAAuB,EAKxB,MAAM,SAAS,CAAC;AAKjB,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAW3C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM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,GAAG,OAAO,GAAG,MAAM,KAAK,IAAI,GAAG,IAAI;IAKpF;;OAEG;IACH,OAAO,CAAC,GAAG;IAOX,OAAO,CAAC,WAAW;IAanB;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC;;;OAGG;IACH,aAAa,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI;IAQ3C;;OAEG;IACG,aAAa,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,uBAAuB,CAAC;IAkBtF;;OAEG;YACW,qBAAqB;IAmBnC;;OAEG;IACG,gBAAgB,CAAC,OAAO,EAAE,0BAA0B,GAAG,OAAO,CAAC,2BAA2B,CAAC;IAgCjG;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAsC7B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACH,OAAO,IAAI,OAAO;IAIlB;;;;OAIG;YACW,+BAA+B;IA2iB7C;;;OAGG;YACW,iBAAiB;YAiGjB,UAAU;YAkLV,eAAe;YAoKf,oBAAoB;YAiBpB,eAAe;IAuC7B,OAAO,CAAC,qBAAqB;IAK7B;;OAEG;YACW,iBAAiB;IAI/B;;;;OAIG;IACG,WAAW,CAAC,OAAO,EAAE,wBAAwB,GAAG,OAAO,CAAC,yBAAyB,CAAC;IAgaxF;;;OAGG;YACW,WAAW;CAuB1B"}
@@ -36,6 +36,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.ExecutionService = void 0;
37
37
  const vm = __importStar(require("vm"));
38
38
  const path = __importStar(require("path"));
39
+ const crypto = __importStar(require("crypto"));
40
+ const fs = __importStar(require("fs"));
39
41
  const async_hooks_1 = require("async_hooks");
40
42
  const playwright_mcp_service_1 = require("./playwright-mcp-service");
41
43
  const types_1 = require("./types");
@@ -48,11 +50,11 @@ const playwright_runner_1 = require("./utils/playwright-runner");
48
50
  const import_utils_1 = require("./utils/import-utils");
49
51
  const initialization_code_utils_1 = require("./utils/initialization-code-utils");
50
52
  const script_parser_utils_1 = require("./utils/script-parser-utils");
51
- const step_execution_utils_1 = require("./utils/step-execution-utils");
52
53
  const script_generator_utils_1 = require("./utils/script-generator-utils");
53
54
  const backend_proxy_llm_provider_1 = require("./providers/backend-proxy-llm-provider");
54
55
  const build_info_1 = require("./build-info");
55
56
  const orchestrator_1 = require("./orchestrator");
57
+ const test_file_parser_1 = require("./utils/test-file-parser");
56
58
  class ModuleResolutionManager {
57
59
  /**
58
60
  * Install the global hook (only once, shared by all instances)
@@ -1471,108 +1473,396 @@ class ExecutionService {
1471
1473
  return (0, browser_utils_1.initializeBrowser)(playwrightConfig, headless, playwrightConfigFilePath, this.logger);
1472
1474
  }
1473
1475
  /**
1474
- * Execute step with command-level tracking
1475
- * Parses step code into individual commands and executes them one-by-one
1476
- * Maintains execution context so variables defined in earlier commands/steps are accessible to later ones
1477
- * Returns successful commands, the failing command, remaining commands, and error
1478
- * This allows us to preserve successful commands and continue with remaining ones after repair
1476
+ * Execute a test file with hooks support
1477
+ * Parses the test file, extracts hooks and tests, executes them in correct order
1478
+ * Each test is reported separately with its own jobId
1479
1479
  */
1480
- async executeStepWithTracking(stepCode, page, priorStepsContext = '') {
1481
- const successfulCommands = [];
1482
- let failingCommand;
1483
- let remainingCommands = [];
1484
- let error;
1485
- // Parse step code into individual statements (handles multi-line await blocks)
1486
- const statements = step_execution_utils_1.StepExecutionUtils.splitIntoExecutableStatements(stepCode);
1487
- // Separate variable declarations and Playwright commands
1488
- const declarations = []; // const/let/var declarations
1489
- const commands = []; // executable statements (await/page/expect/ai/control flow)
1490
- for (const statement of statements) {
1491
- const trimmed = statement.trim();
1492
- if (trimmed.length === 0) {
1493
- continue;
1494
- }
1495
- // Skip comment-only statements
1496
- const withoutComments = trimmed.replace(/^\s*\/\/.*$/gm, '').trim();
1497
- if (withoutComments.length === 0) {
1498
- continue;
1499
- }
1500
- const isDeclaration = /^(const|let|var)\b/.test(trimmed) && !/\bawait\b/.test(trimmed);
1501
- const isExecutable = /\bawait\b/.test(statement) ||
1502
- /\bpage\./.test(statement) ||
1503
- /\bexpect\(/.test(statement) ||
1504
- /\bai\./.test(statement) ||
1505
- /^(if|for|while|switch|try|do)\b/.test(trimmed);
1506
- if (isDeclaration) {
1507
- declarations.push(statement);
1480
+ async runTestFile(request) {
1481
+ const startTime = Date.now();
1482
+ const testResults = [];
1483
+ try {
1484
+ // Read script content if scriptFilePath is provided
1485
+ let script;
1486
+ if (request.script) {
1487
+ script = request.script;
1508
1488
  }
1509
- else if (isExecutable) {
1510
- commands.push(statement);
1489
+ else if (request.scriptFilePath) {
1490
+ try {
1491
+ script = fs.readFileSync(request.scriptFilePath, 'utf8');
1492
+ this.log(`Read test file from: ${request.scriptFilePath}`);
1493
+ }
1494
+ catch (error) {
1495
+ throw new Error(`Failed to read test file: ${error instanceof Error ? error.message : String(error)}`);
1496
+ }
1511
1497
  }
1512
1498
  else {
1513
- // Default to executing the statement to preserve behaviour
1514
- commands.push(statement);
1499
+ throw new Error('Either script or scriptFilePath must be provided');
1515
1500
  }
1516
- }
1517
- if (commands.length === 0 && declarations.length === 0) {
1518
- // No parseable commands - try to execute whole code as-is with prior context
1519
- try {
1520
- const fullCode = priorStepsContext ? priorStepsContext + '\n' + stepCode : stepCode;
1521
- await this.executeStepCode(fullCode, page);
1522
- return { successfulCommands: [stepCode], failingCommand: undefined, remainingCommands: [], error: undefined };
1501
+ // Validate tempDir is provided
1502
+ if (!request.tempDir) {
1503
+ throw new Error('tempDir is required for test file execution');
1523
1504
  }
1524
- catch (err) {
1505
+ // Auto-discover playwright.config.js if not provided
1506
+ let playwrightConfig = request.playwrightConfig;
1507
+ let playwrightConfigFilePath = request.playwrightConfigFilePath;
1508
+ if (!playwrightConfig && !playwrightConfigFilePath) {
1509
+ const configExtensions = ['.js', '.ts', '.mjs'];
1510
+ for (const ext of configExtensions) {
1511
+ const configPath = path.join(request.tempDir, `playwright.config${ext}`);
1512
+ if (fs.existsSync(configPath)) {
1513
+ playwrightConfigFilePath = configPath;
1514
+ this.log(`Auto-discovered Playwright config: ${configPath}`);
1515
+ break;
1516
+ }
1517
+ }
1518
+ }
1519
+ // Extract imports from the original script once (they'll be needed for hooks and tests)
1520
+ // Store the full original script for passing to test executions
1521
+ const originalScript = script;
1522
+ // Parse test file to extract hooks and tests
1523
+ this.log('Parsing test file to extract hooks and tests...');
1524
+ const parsed = test_file_parser_1.TestFileParser.parseTestFile(script, request.testNames);
1525
+ // Flatten structure for execution
1526
+ const flattened = test_file_parser_1.TestFileParser.flattenForExecution(parsed);
1527
+ this.log(`Found ${flattened.fileLevelHooks.beforeAll.length} file-level beforeAll hooks, ${flattened.fileLevelHooks.afterAll.length} file-level afterAll hooks`);
1528
+ this.log(`Found ${flattened.fileLevelHooks.beforeEach.length} file-level beforeEach hooks, ${flattened.fileLevelHooks.afterEach.length} file-level afterEach hooks`);
1529
+ this.log(`Found ${flattened.suites.length} suite(s) with suite-level hooks`);
1530
+ this.log(`Found ${flattened.tests.length} test(s)`);
1531
+ if (flattened.tests.length === 0) {
1532
+ this.log('No tests found in file', 'warn');
1525
1533
  return {
1526
- successfulCommands: [],
1527
- failingCommand: stepCode,
1528
- remainingCommands: [],
1529
- error: step_execution_utils_1.StepExecutionUtils.safeSerializeError(err)
1534
+ success: true,
1535
+ testResults: [],
1536
+ executionTime: Date.now() - startTime
1530
1537
  };
1531
1538
  }
1532
- }
1533
- // Build accumulated context: start with prior steps context, then add current declarations
1534
- const accumulatedContext = [];
1535
- if (priorStepsContext) {
1536
- accumulatedContext.push(priorStepsContext);
1537
- }
1538
- accumulatedContext.push(...declarations);
1539
- successfulCommands.push(...declarations); // Declarations are part of successful commands
1540
- // Execute commands one by one, maintaining variable context only
1541
- // We accumulate executed commands separately to avoid re-executing them
1542
- const executedCommands = [];
1543
- for (let cmdIdx = 0; cmdIdx < commands.length; cmdIdx++) {
1544
- const cmd = commands[cmdIdx];
1545
- try {
1546
- // Execute ONLY this command (not re-executing prior ones)
1547
- // Include accumulated context for variable access only
1548
- const codeToExecute = accumulatedContext.length > 0
1549
- ? [...accumulatedContext, cmd].join('\n')
1550
- : cmd;
1551
- await this.executeStepCode(codeToExecute, page);
1552
- // Success - track the command
1553
- executedCommands.push(cmd);
1554
- successfulCommands.push(cmd);
1555
- // Only add declarations to accumulated context (not action commands)
1556
- if (cmd.startsWith('const ') || cmd.startsWith('let ') || cmd.startsWith('var ')) {
1557
- accumulatedContext.push(cmd);
1539
+ // Initialize browser/context/page (shared across all hooks and tests)
1540
+ const useExistingBrowser = !!(request.existingBrowser && request.existingContext && request.existingPage);
1541
+ let browser;
1542
+ let context;
1543
+ let page;
1544
+ let browserCreated = false;
1545
+ if (useExistingBrowser) {
1546
+ this.log('Using existing browser/page provided by caller');
1547
+ browser = request.existingBrowser;
1548
+ context = request.existingContext;
1549
+ page = request.existingPage;
1550
+ }
1551
+ else {
1552
+ this.log('Initializing browser for test file execution...');
1553
+ const browserInstance = await this.initializeBrowser(playwrightConfig, request.headless !== false, playwrightConfigFilePath);
1554
+ browser = browserInstance.browser;
1555
+ context = browserInstance.context;
1556
+ page = browserInstance.page;
1557
+ browserCreated = true;
1558
+ }
1559
+ // Apply file upload overrides if tempDir is provided
1560
+ if (request.tempDir) {
1561
+ applyFileUploadOverrides(page, request.tempDir, request.testFolderPath, (msg, level) => this.log(msg, level));
1562
+ this.log(`Applied file upload overrides with tempDir: ${request.tempDir}`);
1563
+ }
1564
+ // Execute file-level beforeAll hooks
1565
+ if (flattened.fileLevelHooks.beforeAll.length > 0) {
1566
+ this.log(`Executing ${flattened.fileLevelHooks.beforeAll.length} file-level beforeAll hook(s)...`);
1567
+ try {
1568
+ for (const hook of flattened.fileLevelHooks.beforeAll) {
1569
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1570
+ }
1571
+ this.log('File-level beforeAll hooks executed successfully');
1572
+ }
1573
+ catch (error) {
1574
+ const errorMsg = error instanceof Error ? error.message : String(error);
1575
+ this.log(`File-level beforeAll hook failed: ${errorMsg}`, 'error');
1576
+ // Cleanup browser if we created it
1577
+ if (browserCreated && browser) {
1578
+ try {
1579
+ await browser.close();
1580
+ }
1581
+ catch (closeError) {
1582
+ // Ignore close errors
1583
+ }
1584
+ }
1585
+ return {
1586
+ success: false,
1587
+ testResults: [],
1588
+ executionTime: Date.now() - startTime,
1589
+ error: `File-level beforeAll hook failed: ${errorMsg}`
1590
+ };
1558
1591
  }
1559
- this.log(` ✓ Command succeeded: ${cmd.substring(0, 60)}...`);
1560
1592
  }
1561
- catch (err) {
1562
- // This command failed - it's the failing one
1563
- failingCommand = cmd;
1564
- error = step_execution_utils_1.StepExecutionUtils.safeSerializeError(err);
1565
- // Capture remaining commands that were not executed
1566
- remainingCommands = commands.slice(cmdIdx + 1);
1567
- this.log(` ✗ Command failed: ${cmd.substring(0, 60)}...`);
1568
- this.log(` Error: ${error}`);
1569
- if (remainingCommands.length > 0) {
1570
- this.log(` ⏭ ${remainingCommands.length} remaining commands not executed`);
1593
+ // Helper function to create a unique key from suite path (avoids collisions if suite names contain '__')
1594
+ const getSuiteKey = (suitePath) => {
1595
+ // Use JSON.stringify to safely serialize the array, avoiding collisions
1596
+ return JSON.stringify(suitePath);
1597
+ };
1598
+ // Build a Map of suites by key for O(1) lookups (instead of O(n) .find() calls)
1599
+ const suiteMap = new Map();
1600
+ for (const suite of flattened.suites) {
1601
+ const suiteKey = getSuiteKey(suite.suitePath);
1602
+ suiteMap.set(suiteKey, suite);
1603
+ }
1604
+ // Track suite execution state (which suites have had beforeAll executed)
1605
+ const suiteBeforeAllExecuted = new Set();
1606
+ // Track test counts per suite (for determining last test in suite)
1607
+ const suiteTestCounts = new Map();
1608
+ const suiteTotalTests = new Map();
1609
+ // Initialize suite test counts
1610
+ for (const suite of flattened.suites) {
1611
+ const suiteKey = getSuiteKey(suite.suitePath);
1612
+ suiteTotalTests.set(suiteKey, suite.testIndices.length);
1613
+ suiteTestCounts.set(suiteKey, 0);
1614
+ }
1615
+ // Execute each test
1616
+ for (let testIndex = 0; testIndex < flattened.tests.length; testIndex++) {
1617
+ const testData = flattened.tests[testIndex];
1618
+ const test = testData.test;
1619
+ const testStartTime = Date.now();
1620
+ const jobId = crypto.randomUUID();
1621
+ let testStatus = 'passed';
1622
+ let testError;
1623
+ try {
1624
+ // Before test execution: Execute suite-level beforeAll hooks (if first test in suite)
1625
+ if (testData.suitePath.length > 0) {
1626
+ // Execute beforeAll for each suite in the path (in order: parent → child)
1627
+ for (let i = 0; i < testData.suitePath.length; i++) {
1628
+ const suitePath = testData.suitePath.slice(0, i + 1);
1629
+ const suiteKey = getSuiteKey(suitePath);
1630
+ // Look up suite in Map (O(1) instead of O(n) .find())
1631
+ const suite = suiteMap.get(suiteKey);
1632
+ if (suite && suite.beforeAll.length > 0 && !suiteBeforeAllExecuted.has(suiteKey)) {
1633
+ // Use human-readable suite name for logging
1634
+ const suiteDisplayName = suitePath.join('__');
1635
+ this.log(`Executing suite-level beforeAll hooks for suite: ${suiteDisplayName}`);
1636
+ try {
1637
+ for (const hook of suite.beforeAll) {
1638
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1639
+ }
1640
+ suiteBeforeAllExecuted.add(suiteKey);
1641
+ this.log(`Suite-level beforeAll hooks executed for suite: ${suiteDisplayName}`);
1642
+ }
1643
+ catch (error) {
1644
+ const errorMsg = error instanceof Error ? error.message : String(error);
1645
+ this.log(`Suite-level beforeAll hook failed for suite ${suiteDisplayName}: ${errorMsg}`, 'error');
1646
+ testStatus = 'failed';
1647
+ testError = `Suite-level beforeAll hook failed: ${errorMsg}`;
1648
+ }
1649
+ }
1650
+ }
1651
+ }
1652
+ // Call onStartTest callback (use fullName)
1653
+ if (this.progressReporter?.onStartTest) {
1654
+ await this.progressReporter.onStartTest(test.fullName, jobId, page, browser, context);
1655
+ }
1656
+ // Execute file-level beforeEach hooks
1657
+ if (flattened.fileLevelHooks.beforeEach.length > 0 && testStatus === 'passed') {
1658
+ this.log(`Executing ${flattened.fileLevelHooks.beforeEach.length} file-level beforeEach hook(s) for test: ${test.fullName}`);
1659
+ try {
1660
+ for (const hook of flattened.fileLevelHooks.beforeEach) {
1661
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1662
+ }
1663
+ }
1664
+ catch (error) {
1665
+ const errorMsg = error instanceof Error ? error.message : String(error);
1666
+ this.log(`File-level beforeEach hook failed for test ${test.fullName}: ${errorMsg}`, 'error');
1667
+ testStatus = 'failed';
1668
+ testError = `File-level beforeEach hook failed: ${errorMsg}`;
1669
+ }
1670
+ }
1671
+ // Execute suite-level beforeEach hooks (from all parent suites, in order: parent → child)
1672
+ if (testData.suiteBeforeEachHooks.length > 0 && testStatus === 'passed') {
1673
+ this.log(`Executing ${testData.suiteBeforeEachHooks.length} suite-level beforeEach hook(s) for test: ${test.fullName}`);
1674
+ try {
1675
+ for (const hook of testData.suiteBeforeEachHooks) {
1676
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1677
+ }
1678
+ }
1679
+ catch (error) {
1680
+ const errorMsg = error instanceof Error ? error.message : String(error);
1681
+ this.log(`Suite-level beforeEach hook failed for test ${test.fullName}: ${errorMsg}`, 'error');
1682
+ testStatus = 'failed';
1683
+ testError = `Suite-level beforeEach hook failed: ${errorMsg}`;
1684
+ }
1685
+ }
1686
+ // Run the test if hooks didn't fail
1687
+ if (testStatus === 'passed') {
1688
+ try {
1689
+ // Execute test using existing infrastructure
1690
+ // Construct a script with imports + this test using AST (more robust than string interpolation)
1691
+ const testScript = test_file_parser_1.TestFileParser.constructTestScriptWithImports(originalScript, test.fullName, // Use fullName for test execution
1692
+ test.code);
1693
+ const result = await this.executeStepsInPersistentContext([], // No pre-parsed steps - AST will extract from test body
1694
+ page, browser, context, {
1695
+ mode: request.mode || types_1.ExecutionMode.RUN_EXACTLY,
1696
+ jobId: jobId,
1697
+ tempDir: request.tempDir,
1698
+ originalScript: testScript // Full script with imports + test
1699
+ });
1700
+ if (!result.success) {
1701
+ testStatus = 'failed';
1702
+ testError = result.error || 'Test execution failed';
1703
+ }
1704
+ }
1705
+ catch (error) {
1706
+ testStatus = 'failed';
1707
+ testError = error instanceof Error ? error.message : String(error);
1708
+ }
1709
+ }
1710
+ // Execute suite-level afterEach hooks (from all parent suites, in reverse order: child → parent)
1711
+ if (testData.suiteAfterEachHooks.length > 0) {
1712
+ this.log(`Executing ${testData.suiteAfterEachHooks.length} suite-level afterEach hook(s) for test: ${test.fullName}`);
1713
+ try {
1714
+ for (const hook of testData.suiteAfterEachHooks) {
1715
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1716
+ }
1717
+ }
1718
+ catch (error) {
1719
+ const errorMsg = error instanceof Error ? error.message : String(error);
1720
+ this.log(`Suite-level afterEach hook failed for test ${test.fullName}: ${errorMsg}`, 'warn');
1721
+ // Log but continue - don't fail the test because of afterEach failure
1722
+ }
1723
+ }
1724
+ // Execute file-level afterEach hooks (even if test failed)
1725
+ if (flattened.fileLevelHooks.afterEach.length > 0) {
1726
+ this.log(`Executing ${flattened.fileLevelHooks.afterEach.length} file-level afterEach hook(s) for test: ${test.fullName}`);
1727
+ try {
1728
+ for (const hook of flattened.fileLevelHooks.afterEach) {
1729
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1730
+ }
1731
+ }
1732
+ catch (error) {
1733
+ const errorMsg = error instanceof Error ? error.message : String(error);
1734
+ this.log(`File-level afterEach hook failed for test ${test.fullName}: ${errorMsg}`, 'warn');
1735
+ // Log but continue - don't fail the test because of afterEach failure
1736
+ }
1737
+ }
1738
+ // After test execution: Increment test counts and execute suite-level afterAll hooks (if last test in suite)
1739
+ if (testData.suitePath.length > 0) {
1740
+ // Increment test count for each suite in the path
1741
+ for (let i = 0; i < testData.suitePath.length; i++) {
1742
+ const suitePath = testData.suitePath.slice(0, i + 1);
1743
+ const suiteKey = getSuiteKey(suitePath);
1744
+ const currentCount = suiteTestCounts.get(suiteKey) || 0;
1745
+ suiteTestCounts.set(suiteKey, currentCount + 1);
1746
+ }
1747
+ // Execute afterAll for each suite in the path (in reverse order: child → parent)
1748
+ for (let i = testData.suitePath.length - 1; i >= 0; i--) {
1749
+ const suitePath = testData.suitePath.slice(0, i + 1);
1750
+ const suiteKey = getSuiteKey(suitePath);
1751
+ // Look up suite in Map (O(1) instead of O(n) .find())
1752
+ const suite = suiteMap.get(suiteKey);
1753
+ if (suite && suite.afterAll.length > 0) {
1754
+ const totalTests = suiteTotalTests.get(suiteKey) || 0;
1755
+ const currentCount = suiteTestCounts.get(suiteKey) || 0;
1756
+ // If this is the last test in the suite, execute afterAll
1757
+ if (currentCount >= totalTests) {
1758
+ // Use human-readable suite name for logging
1759
+ const suiteDisplayName = suitePath.join('__');
1760
+ this.log(`Executing suite-level afterAll hooks for suite: ${suiteDisplayName}`);
1761
+ try {
1762
+ for (const hook of suite.afterAll) {
1763
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1764
+ }
1765
+ this.log(`Suite-level afterAll hooks executed for suite: ${suiteDisplayName}`);
1766
+ }
1767
+ catch (error) {
1768
+ const errorMsg = error instanceof Error ? error.message : String(error);
1769
+ this.log(`Suite-level afterAll hook failed for suite ${suiteDisplayName}: ${errorMsg}`, 'warn');
1770
+ // Log but continue - don't fail the test because of afterAll failure
1771
+ }
1772
+ }
1773
+ }
1774
+ }
1775
+ }
1776
+ // Call onEndTest callback (use fullName)
1777
+ if (this.progressReporter?.onEndTest) {
1778
+ await this.progressReporter.onEndTest(test.fullName, jobId, testStatus, testError, page, browser, context);
1779
+ }
1780
+ }
1781
+ catch (error) {
1782
+ // Unexpected error during test execution
1783
+ testStatus = 'failed';
1784
+ testError = error instanceof Error ? error.message : String(error);
1785
+ this.log(`Unexpected error during test ${test.name}: ${testError}`, 'error');
1786
+ // Still call onEndTest if possible
1787
+ if (this.progressReporter?.onEndTest) {
1788
+ try {
1789
+ await this.progressReporter.onEndTest(test.fullName, jobId, testStatus, testError, page, browser, context);
1790
+ }
1791
+ catch (callbackError) {
1792
+ this.log(`onEndTest callback failed: ${callbackError}`, 'warn');
1793
+ }
1794
+ }
1795
+ }
1796
+ // Record test result (use fullName)
1797
+ testResults.push({
1798
+ testName: test.fullName,
1799
+ jobId: jobId,
1800
+ status: testStatus,
1801
+ error: testError,
1802
+ executionTime: Date.now() - testStartTime
1803
+ });
1804
+ this.log(`Test ${test.fullName} completed with status: ${testStatus}`);
1805
+ }
1806
+ // Execute file-level afterAll hooks
1807
+ if (flattened.fileLevelHooks.afterAll.length > 0) {
1808
+ this.log(`Executing ${flattened.fileLevelHooks.afterAll.length} file-level afterAll hook(s)...`);
1809
+ try {
1810
+ for (const hook of flattened.fileLevelHooks.afterAll) {
1811
+ await this.executeHook(hook.code, page, context, browser, request.tempDir);
1812
+ }
1813
+ this.log('File-level afterAll hooks executed successfully');
1814
+ }
1815
+ catch (error) {
1816
+ const errorMsg = error instanceof Error ? error.message : String(error);
1817
+ this.log(`File-level afterAll hook failed: ${errorMsg}`, 'warn');
1818
+ // Log but don't fail overall execution
1571
1819
  }
1572
- break; // Stop at first failure
1573
1820
  }
1821
+ // Cleanup browser if we created it
1822
+ if (browserCreated && browser) {
1823
+ try {
1824
+ await browser.close();
1825
+ this.log('Browser closed');
1826
+ }
1827
+ catch (closeError) {
1828
+ this.log(`Error closing browser: ${closeError}`, 'warn');
1829
+ }
1830
+ }
1831
+ // Determine overall success (all tests passed)
1832
+ const allPassed = testResults.every(result => result.status === 'passed');
1833
+ return {
1834
+ success: allPassed,
1835
+ testResults: testResults,
1836
+ executionTime: Date.now() - startTime
1837
+ };
1838
+ }
1839
+ catch (error) {
1840
+ const errorMsg = error instanceof Error ? error.message : String(error);
1841
+ this.log(`Test file execution failed: ${errorMsg}`, 'error');
1842
+ return {
1843
+ success: false,
1844
+ testResults: testResults,
1845
+ executionTime: Date.now() - startTime,
1846
+ error: errorMsg
1847
+ };
1848
+ }
1849
+ }
1850
+ /**
1851
+ * Execute a hook (beforeAll, afterAll, beforeEach, afterEach)
1852
+ * Uses the same execution context as tests
1853
+ */
1854
+ async executeHook(hookCode, page, context, browser, tempDir) {
1855
+ const { expect, test } = require('@playwright/test');
1856
+ const { ai } = require('ai-wright');
1857
+ // Create execution context for the hook
1858
+ const hookContext = new PersistentExecutionContext(page, expect, test, ai, browser, context, tempDir);
1859
+ try {
1860
+ // Execute hook code
1861
+ await hookContext.executeCode(hookCode);
1862
+ }
1863
+ finally {
1864
+ hookContext.dispose();
1574
1865
  }
1575
- return { successfulCommands, failingCommand, remainingCommands, error };
1576
1866
  }
1577
1867
  }
1578
1868
  exports.ExecutionService = ExecutionService;