package cn.nianxx.thhotel.integrations.document.libreoffice.adapter;

import cn.nianxx.thhotel.platform.documentconversion.service.DocumentConversionException;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class ProcessBuilderLibreOfficeProcessRunnerTest {

    @TempDir
    private Path tempDir;

    @Test
    void shouldRestoreInterruptFlagWhenInterruptedDuringLibreOfficeWait() {
        ProcessBuilderLibreOfficeProcessRunner runner = new ProcessBuilderLibreOfficeProcessRunner();
        LibreOfficeProcessRequest request = new LibreOfficeProcessRequest(
                List.of("/bin/sh", "-c", "sleep 5"),
                tempDir,
                tempDir.resolve("source.xlsx"),
                tempDir,
                tempDir.resolve("lo-profile"),
                Duration.ofSeconds(30));

        Thread.currentThread().interrupt();
        try {
            org.assertj.core.api.Assertions.assertThatThrownBy(() -> runner.run(request))
                    .isInstanceOf(DocumentConversionException.class)
                    .satisfies(exception -> org.assertj.core.api.Assertions.assertThat(
                                    ((DocumentConversionException) exception).getErrorCode())
                            .isEqualTo("DOCUMENT_CONVERSION_FAILED"));
            org.assertj.core.api.Assertions.assertThat(Thread.currentThread().isInterrupted()).isTrue();
        } finally {
            Thread.interrupted();
        }
    }
}
