using ClaudeDo.Worker.Usage;

namespace ClaudeDo.Worker.Tests.Usage;

public sealed class UsageStateTests
{
    private static UsageSnapshot MakeSnapshot(double fiveHourUtilization) => new(
        new UsageBucket(fiveHourUtilization, null),
        null,
        [],
        DateTime.UtcNow);

    [Fact]
    public void ReportSuccess_SetsSnapshotAndClearsError()
    {
        var state = new UsageState();
        state.ReportFailure("boom", DateTime.UtcNow);

        state.ReportSuccess(MakeSnapshot(42));

        Assert.NotNull(state.Snapshot);
        Assert.Equal(42, state.Snapshot!.FiveHour!.Utilization);
        Assert.Null(state.LastError);
    }

    [Fact]
    public void ReportFailure_AfterSuccess_KeepsOldSnapshotButSetsError()
    {
        var state = new UsageState();
        state.ReportSuccess(MakeSnapshot(10));

        state.ReportFailure("network down", DateTime.UtcNow);

        Assert.NotNull(state.Snapshot);
        Assert.Equal(10, state.Snapshot!.FiveHour!.Utilization);
        Assert.Equal("network down", state.LastError);
    }

    [Fact]
    public void NoAttemptYet_SnapshotAndErrorAreNull()
    {
        var state = new UsageState();

        Assert.Null(state.Snapshot);
        Assert.Null(state.LastError);
        Assert.Null(state.LastAttemptUtc);
    }
}
