"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_1 = require("node:test");
const node_fs_1 = require("node:fs");
const node_assert_1 = __importDefault(require("node:assert"));
const _1 = require(".");
const node_path_1 = require("node:path");
const node_child_process_1 = require("node:child_process");
function isResolcInPath() {
    try {
        (0, node_child_process_1.execSync)('resolc --version', { stdio: 'ignore' });
        return true;
    }
    catch {
        return false;
    }
}
const compileOptions = [{}];
if (isResolcInPath()) {
    compileOptions.push({ bin: 'resolc' });
}
for (const options of compileOptions) {
    (0, node_test_1.test)(`check Ok output with option ${JSON.stringify(options)}`, async () => {
        const contract = 'fixtures/token.sol';
        const sources = {
            [contract]: {
                content: (0, node_fs_1.readFileSync)('fixtures/storage.sol', 'utf8'),
            },
        };
        const out = await (0, _1.compile)(sources, options);
        (0, node_assert_1.default)(out.contracts[contract].Storage.abi != null);
        (0, node_assert_1.default)(out.contracts[contract].Storage.evm.bytecode != null);
    });
}
(0, node_test_1.test)('check Err output', async () => {
    const contract = 'contracts/1_Storage.sol';
    const sources = {
        [contract]: {
            content: (0, node_fs_1.readFileSync)('fixtures/storage_bad.sol', 'utf8'),
        },
    };
    const out = await (0, _1.compile)(sources);
    (0, node_assert_1.default)(out?.errors?.[0].message.includes('SPDX license identifier not provided in source file'));
    (0, node_assert_1.default)(out?.errors?.[1].message.includes('Source file does not specify required compiler version'));
});
(0, node_test_1.test)('resolve import', () => {
    const cases = [
        // local
        {
            file: './fixtures/storage.sol',
            expected: (0, node_path_1.resolve)('fixtures/storage.sol'),
        },
        // scopped module with version
        {
            file: '@openzeppelin/contracts@5.1.0/token/ERC20/ERC20.sol',
            expected: (0, node_path_1.resolve)('node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol'),
        },
        // scopped module without version
        {
            file: '@openzeppelin/contracts/token/ERC20/ERC20.sol',
            expected: (0, node_path_1.resolve)('node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol'),
        },
        // scopped module with wrong version
        {
            file: '@openzeppelin/contracts@4.8.3/token/ERC20/ERC20.sol',
            expected: `Error: Version mismatch: Specified @openzeppelin/contracts@4.8.3, but installed version is 5.1.0`,
        },
        // module without version
        {
            file: 'acorn/package.json',
            expected: (0, node_path_1.resolve)('node_modules/acorn/package.json'),
        },
        // scopped module with version
        {
            file: 'acorn@8.14.0/package.json',
            expected: (0, node_path_1.resolve)('node_modules/acorn/package.json'),
        },
        {
            file: 'acorn@8.14.1/package.json',
            expected: `Error: Version mismatch: Specified acorn@8.14.1, but installed version is 8.14.0`,
        },
    ];
    for (const { file, expected } of cases) {
        try {
            const resolved = (0, _1.tryResolveImport)(file);
            (0, node_assert_1.default)(resolved === expected, `\nGot:\n${resolved}\nExpected:\n${expected}`);
        }
        catch (error) {
            (0, node_assert_1.default)(String(error) == expected, `\nGot:\n${String(error)}\nExpected:\n${expected}`);
        }
    }
});
//# sourceMappingURL=index.test.js.map