// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FrameLedger.App.Update;
namespace FrameLedger.App.Tests.Update;
/// An a test scripts: what the next check finds, what each step throws, and what was asked of it.
internal sealed class FakeUpdateClient : IUpdateClient
{
public bool IsInstalled { get; set; } = true;
public string? CurrentVersion { get; set; } = "0.1.0";
/// A package an earlier run left downloaded (beta.14), or null.
public UpdateCandidate? Pending { get; set; }
/// What the next check returns; null is "current".
public UpdateCandidate? Next { get; set; }
public UpdateException? CheckThrows { get; set; }
/// One failure per download attempt, in order; an empty queue downloads.
public Queue DownloadThrows { get; } = new();
public int DownloadAttempts { get; private set; }
/// The includePrereleases flag of every check, in order.
public List Checks { get; } = [];
public List Downloaded { get; } = [];
public List Applied { get; } = [];
public UpdateCandidate? FindPendingRestart() => Pending;
public Task CheckAsync(bool includePrereleases, CancellationToken ct = default)
{
Checks.Add(includePrereleases);
return CheckThrows is { } ex ? Task.FromException(ex) : Task.FromResult(Next);
}
public Task DownloadAsync(UpdateCandidate candidate, IProgress? progress, CancellationToken ct = default)
{
DownloadAttempts++;
if (DownloadThrows.TryDequeue(out UpdateException? ex))
{
return Task.FromException(ex);
}
progress?.Report(50);
progress?.Report(100);
Downloaded.Add(candidate.Version);
return Task.CompletedTask;
}
public void ApplyOnExit(UpdateCandidate candidate) => Applied.Add(candidate.Version);
}