package p.studio.controls.forms;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

final class StudioFormSessionTest {
    @Test
    void resetDraftKeepsEditingModeAndClearsDirtyState() {
        final StudioFormSession<String> session = new StudioFormSession<>("source");

        session.beginEdit();
        session.setDraft("draft");

        session.resetDraft();

        assertEquals(StudioFormMode.EDITING, session.mode());
        assertEquals("source", session.draft());
        assertFalse(session.isDirty());
    }

    @Test
    void cancelEditResetsDraftAndReturnsToReadOnly() {
        final StudioFormSession<String> session = new StudioFormSession<>("source");

        session.beginEdit();
        session.setDraft("draft");

        session.cancelEdit();

        assertEquals(StudioFormMode.READ_ONLY, session.mode());
        assertEquals("source", session.draft());
        assertFalse(session.isDirty());
    }

    @Test
    void replaceSourceResetsModeAndDraft() {
        final StudioFormSession<String> session = new StudioFormSession<>("source");

        session.beginEdit();
        session.setDraft("draft");

        session.replaceSource("next");

        assertEquals(StudioFormMode.READ_ONLY, session.mode());
        assertEquals("next", session.source());
        assertEquals("next", session.draft());
        assertFalse(session.isDirty());
    }

    @Test
    void updateDraftMarksSessionDirty() {
        final StudioFormSession<String> session = new StudioFormSession<>("source");

        session.beginEdit();
        session.updateDraft(current -> current + "-edited");

        assertTrue(session.isDirty());
        assertEquals("source-edited", session.draft());
    }
}
