package p.studio.workspaces.editor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Files;
import java.nio.file.Path;

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

final class EditorFileBufferLoaderTest {
    @TempDir
    Path tempDir;

    @Test
    void loadReturnsReadOnlyTextBufferForUtf8TextFile() throws Exception {
        final var file = tempDir.resolve("main.pbs");
        Files.writeString(file, "fn main(): void\n");

        final var result = new EditorFileBufferLoader().load(file);

        assertTrue(result.isPresent());
        assertEquals("main.pbs", result.get().tabLabel());
        assertEquals("LF", result.get().lineSeparator());
        assertTrue(result.get().content().contains("fn main()"));
    }

    @Test
    void loadRejectsBinaryLikeFile() throws Exception {
        final var file = tempDir.resolve("sprite.bin");
        Files.write(file, new byte[]{0x01, 0x00, 0x02});

        final var result = new EditorFileBufferLoader().load(file);

        assertTrue(result.isEmpty());
    }
}
