"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const experimental_ct_react_1 = require("@playwright/experimental-ct-react");
const AudioAnswer_story_1 = require("../stories/exam/AudioAnswer.story");
const exam_engine_mastering_1 = require("@digabi/exam-engine-mastering");
const AudioPlayer_page_1 = require("../pages/AudioPlayer.page");
const RECORD = 'Äänitä vastaus';
const STOP = 'Lopeta äänitys';
const DELETE = 'Poista äänitteesi';
experimental_ct_react_1.test.describe('AudioAnswer', () => {
    let component;
    experimental_ct_react_1.test.beforeEach(async ({ mount }) => {
        const results = await (0, exam_engine_mastering_1.masterExam)(audioXml, () => '', getMediaMetadata);
        const examXml = results[0].xml;
        component = await mount(react_1.default.createElement(AudioAnswer_story_1.AudioAnswerStory, { examXml: examXml }));
    });
    (0, experimental_ct_react_1.test)('contains only button to start recording', async () => {
        await expectStartRecordingButtonToBeVisible();
    });
    (0, experimental_ct_react_1.test)('clicking start button starts recording', async () => {
        await startRecording();
        await expectStopRecordingButtonToBeVisible();
    });
    (0, experimental_ct_react_1.test)('clicking stop button after start stops recording and shows player for the recording', async () => {
        await startRecording();
        await expectTimeElapsed('0:02');
        await stopRecording();
        await expectAudioPlayerToBeVisible();
    });
    (0, experimental_ct_react_1.test)('recording shows time elapsed', async () => {
        await startRecording();
        await expectTimeElapsed('0:01');
        await expectTimeElapsed('0:02');
        await expectTimeElapsed('0:03');
        await stopRecording();
    });
    (0, experimental_ct_react_1.test)('audio player shows same duration as elapsed recording time', async ({ browserName }) => {
        experimental_ct_react_1.test.skip(browserName === 'firefox', 'Does not work in Firefox for some reason in test browser, in real browser it works');
        await startRecording();
        await expectTimeElapsed('0:03');
        await stopRecording();
        const audioPlayer = new AudioPlayer_page_1.AudioPlayerPage(component);
        await (0, experimental_ct_react_1.expect)(audioPlayer.durationDisplay).toHaveText('00:03');
        await (0, experimental_ct_react_1.expect)(audioPlayer.playButton).toBeVisible();
    });
    (0, experimental_ct_react_1.test)('recorded audio can be played', async ({ page, browserName }) => {
        experimental_ct_react_1.test.skip(browserName === 'firefox', 'Does not work in Firefox for some reason in test browser, in real browser it works');
        await startRecording();
        await expectTimeElapsed('0:02');
        await stopRecording();
        const audioPlayer = new AudioPlayer_page_1.AudioPlayerPage(component);
        await (0, experimental_ct_react_1.expect)(audioPlayer.currentTimeDisplay).toHaveText('00:00');
        await audioPlayer.playButton.click();
        await page.waitForTimeout(3000);
        await (0, experimental_ct_react_1.expect)(audioPlayer.currentTimeDisplay).toHaveText('00:02');
    });
    (0, experimental_ct_react_1.test)('recorded audio can be deleted', async () => {
        await startRecording();
        await expectTimeElapsed('0:03');
        await stopRecording();
        await expectDeleteRecordingButtonToBeVisible();
        await deleteRecording();
        await expectStartRecordingButtonToBeVisible();
    });
    (0, experimental_ct_react_1.test)('recording is available for one audio recorder at the time', async () => {
        await startRecording();
        await expectTimeElapsed('0:01');
        await expectAlreadyRecordingError(1);
    });
    async function expectButtonToBeVisible(name, nth) {
        await (0, experimental_ct_react_1.expect)(component.getByTestId('audio-answer').nth(nth).getByRole('button', { name })).toBeVisible();
    }
    async function expectStartRecordingButtonToBeVisible(nth = 0) {
        await expectButtonToBeVisible(RECORD, nth);
    }
    async function expectStopRecordingButtonToBeVisible(nth = 0) {
        await expectButtonToBeVisible(STOP, nth);
    }
    async function expectDeleteRecordingButtonToBeVisible(nth = 0) {
        await expectButtonToBeVisible(DELETE, nth);
    }
    async function expectAudioPlayerToBeVisible(nth = 0) {
        const audioPlayer = new AudioPlayer_page_1.AudioPlayerPage(component, nth);
        await (0, experimental_ct_react_1.expect)(audioPlayer.baseLocator).toBeVisible();
    }
    async function expectAlreadyRecordingError(nth = 0) {
        await (0, experimental_ct_react_1.expect)(component.getByTestId('audio-answer').nth(nth)).toContainText('Äänitys on käynnissä toisessa tehtävässä');
    }
    async function expectTimeElapsed(text, nth = 0) {
        await (0, experimental_ct_react_1.expect)(component.getByTestId('audio-answer-time-elapsed').nth(nth)).toHaveText(text);
    }
    async function pushButton(name, nth) {
        await component.getByTestId('audio-answer').nth(nth).getByRole('button', { name }).click();
    }
    async function startRecording(nth = 0) {
        await expectButtonToBeVisible(RECORD, nth);
        await pushButton(RECORD, nth);
        await expectButtonToBeVisible(STOP, nth);
    }
    async function stopRecording(nth = 0) {
        await expectButtonToBeVisible(STOP, nth);
        await pushButton(STOP, nth);
    }
    async function deleteRecording(nth = 0) {
        await expectButtonToBeVisible(DELETE, nth);
        await pushButton(DELETE, nth);
    }
});
const getMediaMetadata = (_src, _type) => Promise.resolve({ duration: 999 });
const audioXml = `<e:exam xmlns:e="http://ylioppilastutkinto.fi/exam.xsd" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ylioppilastutkinto.fi/exam.xsd https://abitti.net/schema/exam.xsd" exam-schema-version="0.5">
    <e:exam-versions>
        <e:exam-version lang="fi-FI"/>
    </e:exam-versions>
    <e:exam-title>Exam</e:exam-title>
    <e:exam-instruction/>
    <e:table-of-contents/>
    <e:section>
        <e:section-title>Section</e:section-title>
        <e:section-instruction/>
        <e:question>
            <e:question-title>Audiotehtävä</e:question-title>
            <e:question-instruction />
            <e:audio-answer max-score="5" />
            <e:audio-answer max-score="5" />
        </e:question>
    </e:section>
</e:exam>`;
//# sourceMappingURL=AudioAnswer.test.js.map