package p.studio.workspaces.editor;

import org.junit.jupiter.api.Test;

import java.nio.file.Path;

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

final class EditorOpenFileSessionTest {

    @Test
    void openAddsNewFileAndMarksItActive() {
        final var session = new EditorOpenFileSession();
        final var file = new EditorOpenFileBuffer(Path.of("src/main.pbs"), "main.pbs", "fn main(): void\n", "LF");

        session.open(file);

        assertEquals(1, session.openFiles().size());
        assertEquals(file.path().toAbsolutePath().normalize(), session.activeFile().orElseThrow().path());
    }

    @Test
    void openDoesNotDuplicateExistingTab() {
        final var session = new EditorOpenFileSession();
        final var first = new EditorOpenFileBuffer(Path.of("src/main.pbs"), "main.pbs", "a", "LF");
        final var second = new EditorOpenFileBuffer(Path.of("src/main.pbs"), "main.pbs", "b", "LF");

        session.open(first);
        session.open(second);

        assertEquals(1, session.openFiles().size());
        assertEquals(first.path().toAbsolutePath().normalize(), session.activeFile().orElseThrow().path());
        assertEquals("b", session.activeFile().orElseThrow().content());
    }

    @Test
    void activateSwitchesTheActiveTabWithinTheCurrentSession() {
        final var session = new EditorOpenFileSession();
        final var first = new EditorOpenFileBuffer(Path.of("src/main.pbs"), "main.pbs", "a", "LF");
        final var second = new EditorOpenFileBuffer(Path.of("src/other.pbs"), "other.pbs", "b", "LF");

        session.open(first);
        session.open(second);
        session.activate(first.path());

        assertEquals(first.path().toAbsolutePath().normalize(), session.activeFile().orElseThrow().path());
    }
}
