using ClaudeDo.Worker.Online;

namespace ClaudeDo.Worker.Tests.Online;

public sealed class OnlineTokenStoreTests : IDisposable
{
    private readonly string _tokenPath = Path.Combine(Path.GetTempPath(), $"online_token_{Guid.NewGuid():N}.bin");

    public void Dispose()
    {
        try { File.Delete(_tokenPath); } catch { }
    }

    [Fact]
    public void Save_Read_RoundTrips()
    {
        if (!OperatingSystem.IsWindows()) return; // DPAPI is Windows-only

        var store = new OnlineTokenStore(_tokenPath);
        store.Save("my-refresh-token");
        var result = store.Read();
        Assert.Equal("my-refresh-token", result);
    }

    [Fact]
    public void Clear_Removes_Token()
    {
        if (!OperatingSystem.IsWindows()) return;

        var store = new OnlineTokenStore(_tokenPath);
        store.Save("token");
        store.Clear();
        Assert.Null(store.Read());
    }

    [Fact]
    public void Read_WhenFileAbsent_Returns_Null()
    {
        if (!OperatingSystem.IsWindows()) return;

        var store = new OnlineTokenStore(_tokenPath);
        Assert.Null(store.Read());
    }

    [Fact]
    public void Clear_WhenFileAbsent_DoesNotThrow()
    {
        if (!OperatingSystem.IsWindows()) return;

        var store = new OnlineTokenStore(_tokenPath);
        store.Clear(); // no exception expected
    }
}
