package tech.easyflow.ai.node;

import org.junit.Assert;
import org.junit.Test;

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

/**
 * 临时文件 MultipartFile 适配测试。
 */
public class TemporaryFileMultipartFileTest {

    /**
     * 验证文件流、大小及 transferTo 均复用磁盘内容。
     *
     * @throws Exception 临时文件读写失败时抛出
     */
    @Test
    public void shouldExposeTemporaryFileWithoutChangingContent() throws Exception {
        byte[] content = "workflow-streaming-file".getBytes(java.nio.charset.StandardCharsets.UTF_8);
        Path source = Files.createTempFile("temporary-file-multipart-source-", ".txt");
        Path target = Files.createTempFile("temporary-file-multipart-target-", ".txt");
        try {
            Files.write(source, content);
            TemporaryFileMultipartFile file =
                    new TemporaryFileMultipartFile("result.txt", source, "text/plain");

            Assert.assertEquals(content.length, file.getSize());
            Assert.assertEquals("text/plain", file.getContentType());
            Assert.assertArrayEquals(content, file.getInputStream().readAllBytes());

            file.transferTo(target.toFile());
            Assert.assertArrayEquals(content, Files.readAllBytes(target));
        } finally {
            Files.deleteIfExists(source);
            Files.deleteIfExists(target);
        }
    }
}
