package tv.tivilux.nativeplayer.data;

public final class LiveStreamTraceTest {
    private static long time;
    private static void check(boolean result, String message) {
        if (!result) throw new AssertionError(message);
    }
    public static void main(String[] args) throws Exception {
        LiveStreamTrace.clock = () -> time;
        time = 1000;
        LiveStreamTrace.start();
        LiveStreamTrace.selected("1", "A&E");
        LiveStreamTrace.begin("a", "HLS");
        LiveStreamTrace.Session old = LiveStreamTrace.find("a");
        LiveStreamTrace.Request read = old.open("TS_RESOURCE");
        read.opened(); read.read(100);
        time += 200;
        LiveStreamTrace.selected("2", "Catchy Comedy");
        LiveStreamTrace.begin("b", "HLS");
        LiveStreamTrace.Request current = LiveStreamTrace.find("b").open("PLAYLIST");
        current.opened(); current.read(25);
        time += 7;
        read.read(40); read.closing();
        time += 3;
        read.closed(true);
        String text = LiveStreamTrace.snapshot();
        check(text.contains("latest close 10 ms after switch/stop"), "close timing");
        check(text.contains("read bytes after switch/stop=40; last at +7 ms"), "late-read timing");
        check(text.contains("S2 [Catchy Comedy] HLS\n  requests=1 unclosed=1"), "current stays separate");
        LiveStreamTrace.Request late = old.open("PLAYLIST");
        check(late != null, "old HLS source still tracked after switch");
        check(LiveStreamTrace.snapshot().contains("late opens=1"), "late open detected");
        late.closing(); late.closed(false);
        check(LiveStreamTrace.snapshot().contains("closeErrors=1"), "failure cannot look successful");
        LiveStreamTrace.stop();
        String stopped = LiveStreamTrace.snapshot();
        current.read(500); current.closing(); current.closed(true);
        check(stopped.equals(LiveStreamTrace.snapshot()), "stopped capture stays frozen");
        LiveStreamTrace.start();
        read.read(99); old.open("PLAYLIST");
        check(!LiveStreamTrace.snapshot().contains("A&E"), "stale callbacks cannot enter new capture");
        LiveStreamTrace.selected("3", "https://secret.invalid/user/password");
        LiveStreamTrace.begin("c", "TS/OTHER");
        check(!LiveStreamTrace.snapshot().contains("password"), "URL-like names redacted");
        // Stress a snapshot concurrent with a loader returning data.
        LiveStreamTrace.Request parallel = LiveStreamTrace.find("c").open("TS_RESOURCE");
        Thread loader = new Thread(() -> { for (int i=0;i<10000;i++) parallel.read(1); });
        loader.start();
        for(int i=0;i<100;i++) LiveStreamTrace.snapshot();
        loader.join();
        check(parallel.bytes == 10000, "loader byte counter intact");
        for(int i=0;i<20;i++) LiveStreamTrace.begin("limit"+i,"HLS");
        check(!LiveStreamTrace.isRecording(), "capture source cap");
        LiveStreamTrace.clear();
        check(!LiveStreamTrace.snapshot().contains("SOURCE ASSIGNED"), "clear removes log");
        System.out.println("PASS: close timing, late reads/opens, source attribution, errors, stop/reset, privacy, concurrent snapshots and capture bound");
    }
}
