package com.foxx.androidcast.stats;

/*********************************************************************
 * SessionStatsStoreTest.java
 * Created at: Sat 16 May 2026 18:50:36 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class SessionStatsStoreTest
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (3 commits, 77 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	eef9a1af2345924c4aecd68e533fc2e36fa044726f9bac38bac34c620f390e77
**********************************************************************/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.foxx.androidcast.CastConfig;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;

public class SessionStatsStoreTest {

    @Test
    public void pruneDirectory_keepsAtMostMaxMinusOne() throws IOException {
        File dir = Files.createTempDirectory("session_stats_test").toFile();
        int max = CastConfig.SESSION_STATS_MAX_FILES;
        for (int i = 0; i < max + 5; i++) {
            File f = new File(dir, String.format("session_20260101_%06d.json", i));
            assertTrue(f.createNewFile());
            Files.write(f.toPath(), "{}".getBytes(StandardCharsets.UTF_8));
        }
        assertEquals(max + 5, dir.list((d, n) -> n.endsWith(".json")).length);

        SessionStatsStore.pruneDirectory(dir, max);

        String[] remaining = dir.list((d, n) -> n.endsWith(".json"));
        Arrays.sort(remaining);
        assertEquals(max - 1, remaining.length);
        assertEquals("session_20260101_000006.json", remaining[0]);
        assertEquals("session_20260101_000024.json", remaining[remaining.length - 1]);

        for (File f : dir.listFiles()) {
            assertTrue(f.delete());
        }
        assertTrue(dir.delete());
    }

    @Test
    public void prefixForDirection() {
        assertEquals(SessionStatsStore.PREFIX_SEND, SessionStatsStore.prefixForDirection("send"));
        assertEquals(SessionStatsStore.PREFIX_RECV, SessionStatsStore.prefixForDirection("recv"));
        assertEquals(SessionStatsStore.PREFIX_SEND, SessionStatsStore.prefixForDirection(null));
    }

    @Test
    public void pruneDirectory_includesSendAndRecvPrefixes() throws IOException {
        File dir = Files.createTempDirectory("session_stats_prefix").toFile();
        new File(dir, "session_send_20260101_000001.json").createNewFile();
        new File(dir, "session_recv_20260101_000002.json").createNewFile();
        new File(dir, "session_20260101_000003.json").createNewFile();
        new File(dir, "other.txt").createNewFile();

        SessionStatsStore.pruneDirectory(dir, 2);

        String[] remaining = dir.list((d, n) -> n.endsWith(".json"));
        Arrays.sort(remaining);
        assertEquals(1, remaining.length);
        assertEquals("session_send_20260101_000001.json", remaining[0]);

        for (File f : dir.listFiles()) {
            assertTrue(f.delete());
        }
        assertTrue(dir.delete());
    }
}
