"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const showProblem_1 = __importDefault(require("./showProblem"));
const buildAssetsPath_1 = __importDefault(require("./utils/buildAssetsPath"));
const types_1 = require("./types");
jest.mock('./utils/buildAssetsPath', () => jest.fn());
jest.mock('child_process', () => ({
    execSync: jest.fn(),
}));
jest.mock('fs', () => ({
    readFileSync: jest.fn(),
}));
describe(('showProblem'), () => {
    let mockExecSync;
    let mockBuildAssetsPath;
    const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform');
    beforeEach(() => {
        mockExecSync = child_process_1.execSync;
        mockBuildAssetsPath = buildAssetsPath_1.default;
    });
    afterEach(() => {
        jest.resetAllMocks();
        Object.defineProperty(process, 'platform', originalPlatform);
    });
    it('should read the correct path of the file', () => {
        (0, showProblem_1.default)('problem 1');
        expect(buildAssetsPath_1.default).toHaveBeenCalledWith('code/problem-1.cpp');
    });
    it('should console log for non windows and unix-like platforms', () => {
        jest.spyOn(console, 'log').mockReturnValue(undefined);
        mockBuildAssetsPath.mockReturnValue('codePath');
        fs_1.readFileSync.mockReturnValue('file content');
        Object.defineProperty(process, 'platform', {
            value: 'other',
        });
        (0, showProblem_1.default)('problem 1');
        expect(console.log).toHaveBeenCalledWith('file content');
    });
    it('should handle errors', () => {
        mockBuildAssetsPath.mockImplementation(() => {
            throw new Error('error');
        });
        jest.spyOn(console, 'log').mockReturnValue(undefined);
        (0, showProblem_1.default)('problem 1');
        expect(console.log).toHaveBeenCalledWith('There was an error with showing the problem', new Error('error'));
    });
    it.each([
        ['more', types_1.Platform.Windows],
        ['less', types_1.Platform.Linux],
    ])('should call %s for %s', (expectedCommand, platform) => {
        mockBuildAssetsPath.mockReturnValue('codePath');
        Object.defineProperty(process, 'platform', {
            value: platform,
        });
        (0, showProblem_1.default)('problem 1');
        expect(mockExecSync.mock.calls[0][0]).toBe(`${expectedCommand} codePath`);
    });
});
