package com.foxx.androidcast.network.transport.fec;

/*********************************************************************
 * FecShardWireTest.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 FecShardWireTest
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (3 commits, 77 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	553e2d1952d29aa8573f4dd55b5e83a33bb7283593b6d87247437f8aa69b5f16
**********************************************************************/
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import org.junit.Test;

public class FecShardWireTest {
    @Test
    public void largeShard_roundTrip() throws Exception {
        byte[] payload = new byte[54_000];
        for (int i = 0; i < payload.length; i++) {
            payload[i] = (byte) (i & 0xFF);
        }
        ReedSolomonFec fec = new ReedSolomonFec(4, 3);
        byte[][] shards = fec.encode(payload);
        byte[] shard = shards[0];
        assertTrue(shard.length > 255);
        byte[] wire = FecShardWire.encodeShard(
                ProtectionEnvelope.MODE_FEC_34, 99, 1000, 0, shards.length,
                payload.length, 4, 3, shard);
        FecShardWire.Parsed p = FecShardWire.tryParse(wire);
        assertNotNull(p);
        assertArrayEquals(shard, p.shardData);
    }

    @Test
    public void v2SmallShard_stillParses() throws Exception {
        byte[] shard = new byte[200];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.write(new byte[] {'A', 'C', 'P', 'F'});
        dos.writeByte(FecShardWire.VERSION_SHARD_V2);
        dos.writeByte(ProtectionEnvelope.MODE_FEC_34);
        dos.writeInt(1);
        dos.writeInt(10);
        dos.writeByte(0);
        dos.writeByte(7);
        dos.writeInt(800);
        dos.writeByte(4);
        dos.writeByte(3);
        dos.writeByte(shard.length);
        dos.write(shard);
        FecShardWire.Parsed p = FecShardWire.tryParse(bos.toByteArray());
        assertNotNull(p);
        assertArrayEquals(shard, p.shardData);
    }

    @Test
    public void encodeParse_roundTrip() throws Exception {
        byte[] shard = new byte[64];
        for (int i = 0; i < shard.length; i++) {
            shard[i] = (byte) i;
        }
        byte[] wire = FecShardWire.encodeShard(
                ProtectionEnvelope.MODE_FEC_34, 42, 1000, 2, 7, 200, 4, 3, shard);
        FecShardWire.Parsed p = FecShardWire.tryParse(wire);
        assertNotNull(p);
        assertEquals(42, p.groupId);
        assertEquals(1000, p.baseMessageId);
        assertEquals(2, p.shardIndex);
        assertArrayEquals(shard, p.shardData);
        assertTrue(FecShardWire.isShardPacket(wire));
    }
}
