package com.gzzm.lobster.storage;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.*;

/**
 * 验证 FileSystemContentStore 的写入/读取/删除闭环。
 */
public class FileSystemContentStoreTest {

    @Rule
    public TemporaryFolder tmp = new TemporaryFolder();

    private FileSystemContentStore store;

    @Before
    public void setup() {
        store = new FileSystemContentStore(tmp.getRoot().getAbsolutePath());
    }

    @Test
    public void writeAndReadRoundTrip() {
        String content = "你好，大龙虾 / Hello Lobster";
        String ref = store.write("artifact", "u1", content, "txt");
        assertTrue("path should follow convention", ref.startsWith("artifact/"));
        assertTrue(ref.contains("u1"));
        assertEquals(content, store.read(ref));
    }

    @Test
    public void paginatedReadRespectsBounds() {
        String ref = store.write("artifact", "u1", "0123456789ABCDEF", "txt");
        assertEquals("56789", store.read(ref, 5, 5));
        assertEquals("", store.read(ref, 100, 5));
    }

    @Test
    public void deleteRemovesFile() {
        String ref = store.write("artifact", "u1", "X", "txt");
        assertTrue(store.exists(ref));
        store.delete(ref);
        assertFalse(store.exists(ref));
    }

    @Test
    public void sizeIsReported() {
        String ref = store.write("artifact", "u1", "12345", "txt");
        assertTrue(store.getSize(ref) >= 5);
    }

    @Test(expected = RuntimeException.class)
    public void pathTraversalIsRejected() {
        store.read("../../etc/passwd");
    }
}
