Skip to content

Commit

Permalink
Fix tests and build warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisreimann committed Nov 7, 2024
1 parent 1143f28 commit e862992
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 52 deletions.
6 changes: 3 additions & 3 deletions BTCPayApp.Desktop/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DesktopSecureConfigProvider: ISecureConfigProvider
{
private readonly IDataProtector _dataProtector;

public DesktopSecureConfigProvider(IDataDirectoryProvider directoryProvider, IDataProtectionProvider dataProtectionProvider)
public DesktopSecureConfigProvider(IDataDirectoryProvider directoryProvider, IDataProtectionProvider dataProtectionProvider)
{
_dataProtector = dataProtectionProvider.CreateProtector("SecureConfig");
_configDir = directoryProvider.GetAppDataDirectory().ContinueWith(task =>
Expand All @@ -38,7 +38,7 @@ public DesktopSecureConfigProvider(IDataDirectoryProvider directoryProvider, IDa
return res;
});
}

private readonly Task<string> _configDir;

public async Task<T?> Get<T>(string key)
Expand Down Expand Up @@ -118,7 +118,7 @@ public DesktopDataDirectoryProvider(IConfiguration configuration)
}
public virtual Task<string> GetAppDataDirectory()
{
var dirName = _configuration.GetValue<string>("BTCPAYAPP_DIRNAME", "BTCPayApp");
var dirName = _configuration.GetValue<string>("BTCPAYAPP_DIRNAME", "BTCPayApp")!;
return Task.FromResult(GetDirectory(dirName));
}

Expand Down
75 changes: 36 additions & 39 deletions BTCPayApp.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task CanStartAppCore()
TestUtils.Eventually(() =>
Assert.Equal(BTCPayConnectionState.Connecting, node.ConnectionManager.ConnectionState));
TestUtils.Eventually(() =>
Assert.Equal(BTCPayConnectionState.ConnectedAsMaster, node.ConnectionManager.ConnectionState));
Assert.Equal(BTCPayConnectionState.ConnectedAsMaster, node.ConnectionManager.ConnectionState));
TestUtils.Eventually(() => Assert.Equal(OnChainWalletState.NotConfigured, node.OnChainWalletManager.State));

TestUtils.Eventually(() => Assert.Equal(LightningNodeState.WaitingForConnection, node.LNManager.State));
Expand Down Expand Up @@ -135,10 +135,10 @@ public async Task CanStartAppCore()
TestUtils.Eventually(() =>
Assert.Equal(BTCPayConnectionState.ConnectedAsMaster, node2.ConnectionManager.ConnectionState));


TestUtils.Eventually(() => Assert.Equal(OnChainWalletState.Loaded, node2.OnChainWalletManager.State));
TestUtils.Eventually(() => Assert.Equal(LightningNodeState.Loaded, node2.LNManager.State));


await node2.LNManager.Node.UpdateConfig(async config =>
{
Expand All @@ -148,11 +148,11 @@ await node2.LNManager.Node.UpdateConfig(async config =>
await TestUtils.EventuallyAsync(async () =>
Assert.True((await node2.LNManager.Node.GetConfig()).AcceptInboundConnection));
TestUtils.Eventually(() => Assert.NotNull(node2.LNManager.Node?.PeerHandler.Endpoint));

//test onchain wallet
var address = await node.OnChainWalletManager.DeriveScript(WalletDerivation.NativeSegwit);
var address2 = await node2.OnChainWalletManager.DeriveScript(WalletDerivation.NativeSegwit);

var network = node.ConnectionManager.ReportedNetwork;
Assert.NotNull(network);
var rpc = new RPCClient(RPCCredentialString.Parse("server=http://localhost:43782;ceiwHEbqWI83:DwubwWsoo3"), network);
Expand All @@ -165,7 +165,7 @@ await TestUtils.EventuallyAsync(async () =>
Assert.Equal(Money.Coins(2).Satoshi, utxos.Sum(coin => (Money) coin.Amount));
});

Assert.Null( node2.AccountManager.GetCurrentStore());
var store = await node2.AccountManager.GetClient().CreateStore(new CreateStoreRequest()
{
Expand All @@ -187,15 +187,15 @@ await TestUtils.EventuallyAsync(async () =>
});
var pms = await node2.AccountManager.GetClient().GetInvoicePaymentMethods(store.Id, invoice.Id);
Assert.Equal(2, pms.Length);

var pmOnchain = pms.First(p => p.PaymentMethodId == "BTC-CHAIN");
var pmLN = pms.First(p => p.PaymentMethodId == "BTC-LN");

var requestOfInvoice =
Assert.Single(await node2.LNManager.Node.PaymentsManager.List(payments => payments));
Assert.Equal(pmLN.Destination, requestOfInvoice.PaymentRequest.ToString());
Assert.True(requestOfInvoice.Inbound);

//let's pay onchain for now
await FundWallet(rpc, BitcoinAddress.Create(pmOnchain.Destination, network), Money.Coins(1));
await TestUtils.EventuallyAsync(async () =>
Expand All @@ -215,17 +215,17 @@ await TestUtils.EventuallyAsync(async () =>
Assert.Equal(LightningPaymentStatus.Pending, requestOfInvoice.Status);
});

//artificial self payment
invoice = await node2.AccountManager.GetClient().CreateInvoice(store.Id, new CreateInvoiceRequest()
{
Amount = 1m,
Currency = "BTC",
});

pms = await node2.AccountManager.GetClient().GetInvoicePaymentMethods(store.Id, invoice.Id);
Assert.Equal(2, pms.Length);

pmLN = pms.First(p => p.PaymentMethodId == "BTC-LN");
// AppLightningPayment node2PaymentUpdate = null;
// node2.LNManager.Node.PaymentsManager.OnPaymentUpdate += (sender, payment) =>
Expand All @@ -248,10 +248,10 @@ await TestUtils.EventuallyAsync(async () =>
Assert.Equal(3, payments.Count);
Assert.Contains(payments, payment => payment.Inbound && payment.PaymentRequest.ToString() == pmLN.Destination && payment.Status == LightningPaymentStatus.Complete);
Assert.Contains(payments, payment => !payment.Inbound && payment.PaymentRequest.ToString() == pmLN.Destination && payment.Status == LightningPaymentStatus.Complete);




using var node3 = await HeadlessTestNode.Create("Node3", _output);
var username2 = Guid.NewGuid() + "@gg.com";
Assert.True((await node3.AccountManager.Register(btcpayUri.AbsoluteUri, username2, username2)).Succeeded);
Expand Down Expand Up @@ -295,7 +295,7 @@ await TestUtils.EventuallyAsync(async () =>
var node2Channel = Assert.Single( await node2.LNManager.Node.GetChannels());
var node3Channel= Assert.Single( await node3.LNManager.Node.GetChannels());
Assert.Equal(node2Channel.Key, node3Channel.Key);
Assert.Equal(
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node3Channel.Value.channelDetails.get_confirmations()).some,
Expand All @@ -304,41 +304,41 @@ await TestUtils.EventuallyAsync(async () =>
Assert.Equal(0,
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node3Channel.Value.channelDetails.get_confirmations())
.some);
Assert.False(node3Channel.Value.channelDetails.get_is_channel_ready());
Assert.False(node2Channel.Value.channelDetails.get_is_channel_ready());
Assert.False(node3Channel.Value.channelDetails.get_is_usable());
Assert.False(node2Channel.Value.channelDetails.get_is_usable());
Assert.Equal(1,
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node3Channel.Value.channelDetails.get_confirmations_required())
.some);
Assert.Equal(1,
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node2Channel.Value.channelDetails.get_confirmations_required())
.some);
});


await rpc.GenerateAsync(1);

await TestUtils.EventuallyAsync(async () =>
{
var node2Channel = Assert.Single( await node2.LNManager.Node.GetChannels());
var node3Channel= Assert.Single( await node3.LNManager.Node.GetChannels());
Assert.Equal(node2Channel.Key, node3Channel.Key);
Assert.Equal(
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node3Channel.Value.channelDetails.get_confirmations()).some,
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node2Channel.Value.channelDetails.get_confirmations()).some);
Assert.True(node3Channel.Value.channelDetails.get_is_channel_ready());
Assert.True(node2Channel.Value.channelDetails.get_is_channel_ready());
Assert.True(node3Channel.Value.channelDetails.get_is_usable());
Assert.True(node2Channel.Value.channelDetails.get_is_usable());
Assert.Equal(1,
Assert.IsType<Option_u32Z.Option_u32Z_Some>(node3Channel.Value.channelDetails.get_confirmations_required())
.some);
Expand All @@ -350,15 +350,15 @@ await TestUtils.EventuallyAsync(async () =>
Assert.Equal(0, node3Channel.Value.channelDetails.get_balance_msat());
Assert.Equal(LightMoney.Coins(0.5m).MilliSatoshi, node2Channel.Value.channelDetails.get_balance_msat());
Assert.Equal(LightMoney.Coins(0.5m).MilliSatoshi, node2Channel.Value.channelDetails.get_balance_msat());
});
var node3PR = await node3.LNManager.Node.PaymentsManager.RequestPayment(LightMoney.Coins(0.01m), TimeSpan.FromHours(2),
uint256.Zero);



await TestUtils.EventuallyAsync(async () =>
{
var node2PR = await node2.LNManager.Node.PaymentsManager.PayInvoice(node3PR.PaymentRequest);
Expand All @@ -372,21 +372,18 @@ await TestUtils.EventuallyAsync(async () =>
))).Single();
await Task.Delay(500);
}
Assert.Equal(node2PR.Status, LightningPaymentStatus.Complete);
Assert.Equal(LightningPaymentStatus.Complete, node2PR.Status);
Assert.Equal(node2PR.Preimage, node3PR.Preimage);
Assert.NotNull(node2PR.Preimage);
Assert.Single(await node3.LNManager.Node.PaymentsManager.List(payments =>
payments.Where(payment => payment.PaymentHash == node3PR.PaymentHash && payment.Inbound && payment.Status == LightningPaymentStatus.Complete)));
});
}

private async Task<uint256?> FundWallet(RPCClient rpc, BitcoinAddress address, Money m)
{

var attempts = 0;
while (attempts < 101)
{
Expand All @@ -403,4 +400,4 @@ await TestUtils.EventuallyAsync(async () =>

return null;
}
}
}
13 changes: 7 additions & 6 deletions BTCPayApp.Tests/TestSecureConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ public class TestSecureConfigProvider : ISecureConfigProvider
{
private readonly ConcurrentDictionary<string, object> _data = new();

public async Task<T?> Get<T>(string key)
public Task<T?> Get<T>(string key)
{
if (_data.TryGetValue(key, out var value)) return (T?) value;

return default;
return _data.TryGetValue(key, out var value)
? Task.FromResult((T?) value)
: Task.FromResult<T?>(default);
}

public async Task Set<T>(string key, T? value)
public Task Set<T>(string key, T? value)
{
if (value is null)
_data.TryRemove(key, out _);
else
_data.AddOrReplace(key, value);
return Task.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions BTCPayApp.Tests/WebApplicationFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class XunitLogger : ILogger
private const string Warn = "warn";
private const string Error = "fail";
private const string Critical = "crit";

private readonly string _categoryName;
private readonly bool _useScopes;
private readonly ITestOutputHelper _output;
Expand All @@ -47,7 +47,7 @@ public IDisposable BeginScope<TState>(TState state)
return _scopes.Push(state);
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception, string> formatter)
{
var sb = new StringBuilder();
Expand Down Expand Up @@ -167,4 +167,4 @@ public static bool UsesScopes(this ILoggingBuilder builder)
}

#endregion
}
}

0 comments on commit e862992

Please sign in to comment.