package com.foxx.androidcast.remoteaccess;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import com.wireguard.config.Config;

import org.json.JSONObject;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

/** Ensures BE credential → wg-quick text parses with wireguard-android Config. */
public class WireGuardConfigParseTest {

    @Test
    public void builderOutput_parsesWithWireGuardConfig() throws Exception {
        JSONObject creds = new JSONObject();
        creds.put("interface_private_key", "YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
        creds.put("interface_address", "10.66.66.2/32");
        creds.put("peer_public_key", "YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
        creds.put("peer_endpoint", "ra.apps.f0xx.org:51820");
        creds.put("peer_allowed_ips", "10.66.66.1/32");

        String quick = WireGuardConfigBuilder.fromConnectCredentials(creds);
        assertFalse(quick.isEmpty());

        try (ByteArrayInputStream in = new ByteArrayInputStream(quick.getBytes(StandardCharsets.UTF_8))) {
            Config config = Config.parse(in);
            assertEquals("10.66.66.2/32", config.getInterface().getAddresses().iterator().next().toString());
        }
    }
}
