using CodeRemoteManager.SetupApp;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CodeRemoteManager.SetupApp.Tests;

[TestClass]
public sealed class DevicePairingLauncherTests
{
    [TestMethod]
    public void ParsesOnlyThePairingValuesNeededByTheLocalSetupWindow()
    {
        var result = DevicePairingLauncher.ParseResult(
            """{"code":"ABCD-EFGH-JKMN","expiresAt":"2099-01-01T00:00:00.000Z","qrPayload":"crm-pair:v1:TEST_PAYLOAD"}""");

        Assert.AreEqual("ABCD-EFGH-JKMN", result.Code);
        Assert.AreEqual(DateTimeOffset.Parse("2099-01-01T00:00:00.000Z"), result.ExpiresAt);
        Assert.AreEqual("crm-pair:v1:TEST_PAYLOAD", result.QrPayload);
    }

    [TestMethod]
    public void RejectsMalformedOutputWithOneSafeErrorCode()
    {
        var error = Assert.ThrowsException<DevicePairingException>(() =>
            DevicePairingLauncher.ParseResult("{\"code\":\"invalid\",\"localPath\":\"private\"}"));

        Assert.AreEqual("pairing_creation_failed", error.Message);
        Assert.IsFalse(error.ToString().Contains("localPath", StringComparison.Ordinal));
    }
}
