package com.foxx.androidcast.network.transport;

/*********************************************************************
 * StreamProtectionCompatTest.java
 * Created at: Mon 18 May 2026 14:25:02 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class StreamProtectionCompatTest
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 43 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	5c17f919671c3b028feb186a4ac67ca4465d8050766c88c59e83e45b02ff49d1
**********************************************************************/
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.foxx.androidcast.network.transport.fec.FecEncodedGroup;
import com.foxx.androidcast.network.transport.fec.FecShardWire;
import com.foxx.androidcast.network.transport.fec.ProtectionEnvelope;

import org.junit.Test;

public class StreamProtectionCompatTest {
    @Test
    public void detectsFecShardAndMonolithic() throws Exception {
        byte[] payload = "compat-probe".getBytes();
        FecEncodedGroup group = new FecProtectionEngine(
                ProtectionEnvelope.MODE_FEC_34, 4, 3, null).encodeGroup(payload);
        byte[] shard = FecShardWire.encodeShard(
                group.mode, group.groupId, 10, 0, group.totalShards(),
                group.originalLen, group.dataShards, group.parityShards, group.shards[0]);
        assertTrue(StreamProtectionCompat.isForeignProtectionPayload(shard));
        byte[] mono = ProtectionEnvelope.encode(
                ProtectionEnvelope.MODE_FEC_34, 4, 3, payload);
        assertTrue(StreamProtectionCompat.isForeignProtectionPayload(mono));
        assertFalse(StreamProtectionCompat.isForeignProtectionPayload(payload));
    }

    @Test
    public void shouldIgnore_whenProtectionNotNegotiated() throws Exception {
        byte[] payload = "x".getBytes();
        byte[] mono = ProtectionEnvelope.encode(
                ProtectionEnvelope.MODE_FEC_34, 4, 3, payload);
        assertTrue(StreamProtectionCompat.shouldIgnoreInbound(mono, StreamProtectionMode.NONE));
        assertFalse(StreamProtectionCompat.shouldIgnoreInbound(mono, StreamProtectionMode.FEC_3_4));
    }
}
