using ClaudeDo.Worker;

namespace ClaudeDo.Worker.Tests.Tickets;

public sealed class DpapiTokenStoreTests : IDisposable
{
    private readonly string _dir = Path.Combine(Path.GetTempPath(), "cdo-tok-" + Guid.NewGuid().ToString("N"));

    [Fact]
    public void Save_and_read_round_trip()
    {
        var store = new DpapiTokenStore(Path.Combine(_dir, "ticket.pat"));
        store.Save("tsp_secret");
        Assert.Equal("tsp_secret", store.Read());
    }

    [Fact]
    public void Read_returns_null_when_missing()
        => Assert.Null(new DpapiTokenStore(Path.Combine(_dir, "nope.pat")).Read());

    [Fact]
    public void Two_stores_with_different_files_do_not_share_state()
    {
        var a = new DpapiTokenStore(Path.Combine(_dir, "a.token"));
        var b = new DpapiTokenStore(Path.Combine(_dir, "b.token"));
        a.Save("aaa");
        b.Save("bbb");
        Assert.Equal("aaa", a.Read());
        Assert.Equal("bbb", b.Read());
    }

    [Fact]
    public void Clear_removes_the_file()
    {
        var store = new DpapiTokenStore(Path.Combine(_dir, "c.token"));
        store.Save("x");
        store.Clear();
        Assert.Null(store.Read());
    }

    public void Dispose()
    {
        if (Directory.Exists(_dir)) Directory.Delete(_dir, recursive: true);
    }
}
