// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FrameLedger.Infrastructure.Telemetry;
namespace FrameLedger.Infrastructure.Tests.Telemetry;
/// A scripted : which LUIDs have a counter, and what each read returns.
internal sealed class FakeMemoryCounter : IAdapterMemoryCounter
{
private readonly Func?> _bind;
private Func? _read;
/// Scripted per LUID.
/// Per LUID: a read function, or null when the counter does not exist there.
public FakeMemoryCounter(Func?> bind) => _bind = bind;
public List Opened { get; } = [];
public int Reads { get; private set; }
public bool Disposed { get; private set; }
public bool TryOpen(ulong luid)
{
Opened.Add(luid);
_read = _bind(luid);
return _read is not null;
}
public ulong ReadDedicatedUsageBytes()
{
Reads++;
return _read is null ? throw new InvalidOperationException("no counter is open") : _read();
}
public void Dispose() => Disposed = true;
public static FakeMemoryCounter Constant(ulong bytes) => new(_ => () => bytes);
public static FakeMemoryCounter Absent() => new(_ => null);
}