"use strict";
// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeExpectations = exports.parseExpectations = void 0;
exports.getExpectedResults = getExpectedResults;
exports.isExpectedResult = isExpectedResult;
exports.getSkippedTests = getSkippedTests;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const paths_js_1 = require("./paths.js");
const platform_js_1 = require("./platform.js");
const test_config_js_1 = require("./test_config.js");
const test_expectations_parser_js_1 = require("./test_expectations_parser.js");
exports.parseExpectations = test_expectations_parser_js_1.parseExpectations;
exports.serializeExpectations = test_expectations_parser_js_1.serializeExpectations;
const parsedExpectations = new Map();
function getExpectedResults(testId) {
    const expectationsPath = test_config_js_1.TestConfig.expectationsFile ? path.resolve(test_config_js_1.TestConfig.expectationsFile) :
        path.join(paths_js_1.SOURCE_ROOT, 'test', 'TestExpectations');
    if (!parsedExpectations.has(expectationsPath)) {
        if (fs.existsSync(expectationsPath)) {
            const content = fs.readFileSync(expectationsPath, 'utf8');
            parsedExpectations.set(expectationsPath, (0, exports.parseExpectations)(content).filter(e => !e.isCommentOrEmpty));
        }
        else {
            parsedExpectations.set(expectationsPath, []);
        }
    }
    const expectations = parsedExpectations.get(expectationsPath);
    const match = expectations.find(e => {
        if (!e.testName) {
            return false;
        }
        if (e.platforms && e.platforms.length > 0) {
            if (!e.platforms.includes(platform_js_1.platform)) {
                return false;
            }
        }
        if (paths_js_1.TEST_ID_REGEX.test(e.testName)) {
            return testId === e.testName;
        }
        return testId === e.testName || testId.startsWith(e.testName + ':');
    });
    return match?.results;
}
function isExpectedResult({ exactTestId, success, skipped }) {
    const expectedResults = getExpectedResults(exactTestId);
    if (!expectedResults) {
        return success || skipped;
    }
    if (skipped) {
        return expectedResults.includes('Skip');
    }
    if (success) {
        return expectedResults.includes('Pass');
    }
    return expectedResults.includes('Failure');
}
function getSkippedTests() {
    const expectationsPath = test_config_js_1.TestConfig.expectationsFile ? path.resolve(test_config_js_1.TestConfig.expectationsFile) :
        path.join(paths_js_1.SOURCE_ROOT, 'test', 'TestExpectations');
    if (!parsedExpectations.has(expectationsPath)) {
        if (fs.existsSync(expectationsPath)) {
            const content = fs.readFileSync(expectationsPath, 'utf8');
            parsedExpectations.set(expectationsPath, (0, exports.parseExpectations)(content).filter(e => !e.isCommentOrEmpty));
        }
        else {
            parsedExpectations.set(expectationsPath, []);
        }
    }
    const expectations = parsedExpectations.get(expectationsPath);
    return expectations
        .filter(e => {
        if (!e.testName) {
            return false;
        }
        if (e.results && !e.results.includes('Skip')) {
            return false;
        }
        if (e.platforms && e.platforms.length > 0 && !e.platforms.includes(platform_js_1.platform)) {
            return false;
        }
        return true;
    })
        .map(e => e.testName)
        .map(skipped => {
        const parts = skipped.split(':');
        const file = parts[0];
        const caseName = parts.slice(1).join(':');
        const jsFile = file.replace(/\.ts$/, '.js');
        const absoluteJsFile = path.isAbsolute(jsFile) ? jsFile : path.join(paths_js_1.GEN_DIR, jsFile);
        return parts.length > 1 ? `${absoluteJsFile}:${caseName}` : absoluteJsFile;
    });
}
//# sourceMappingURL=test_expectations.js.map