/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import fs from 'fs';
import path from 'path';
export async function createSkipTestPredicate(projectName) {
    if (!process.env.PWTEST_USE_BIDI_EXPECTATIONS)
        return () => false;
    const expectationsMap = await parseBidiExpectations(projectName);
    return (info) => {
        const key = info.titlePath.join(' › ');
        const expectation = expectationsMap.get(key);
        return ['flaky', 'fail', 'timeout'].includes(expectation);
    };
}
export async function parseBidiExpectations(projectName) {
    const filePath = projectExpectationPath(projectName);
    try {
        await fs.promises.access(filePath);
    }
    catch (e) {
        return new Map();
    }
    const content = await fs.promises.readFile(filePath);
    const pairs = content.toString().split('\n').map(line => {
        const match = /(?<titlePath>.+) \[(?<expectation>[^\]]+)\]$/.exec(line);
        if (!match) {
            console.error('Bad expectation line: ' + line);
            return undefined;
        }
        return [match.groups.titlePath, match.groups.expectation];
    }).filter(Boolean);
    return new Map(pairs);
}
export function projectExpectationPath(project) {
    return path.join(__dirname, 'expectations', project + '.txt');
}
//# sourceMappingURL=expectationUtil.js.map