Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Admin/Nodes/SyncNodeCommandHandlerTests.cs
T
mrleo1nidandClaude Opus 5.5 bb5e59904f
CI / Backend (build + test) (push) Failing after 1m36s
CI / Frontend (lint + typecheck + build) (push) Successful in 41s
Add inbound country flags and node load in location picker
- Inbound.CountryCode (ISO alpha-2), set on publish; flag shown before
  the location name (SVG via country-flag-icons on the site, emoji in bot)
- Node.Capacity (optional, not a limit); available inbounds report load
  percent of capacity or active config count, sorted least loaded first

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 19:45:17 +03:00

80 lines
3.0 KiB
C#

using NSubstitute;
using PnvPanel.Application.Admin.Nodes;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Models;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Configs;
using PnvPanel.Domain.Inbounds;
using PnvPanel.Domain.Nodes;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Nodes;
public class SyncNodeCommandHandlerTests
{
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
private static Node CreateNode() =>
Node.Register(
"node-1",
new Uri("https://node1.example.com"),
new NodeCredentials("admin", "protected"),
null
);
[Fact]
public async Task Handle_WhenInboundGoneFromPanelAndHasNoConfigs_RemovesIt()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = CreateNode();
var inbound = Inbound.FromRemote(node.Id, "old-id", VpnProtocol.Vless, "old", 443);
dbContext.Nodes.Add(node);
dbContext.Inbounds.Add(inbound);
await dbContext.SaveChangesAsync(CancellationToken.None);
_gateway
.ListInboundsAsync(Arg.Any<Node>(), Arg.Any<CancellationToken>())
.Returns(
Result.Success<IReadOnlyList<RemoteInboundInfo>>(
[new RemoteInboundInfo("new-id", VpnProtocol.Vless, "new", 8443)]
)
);
var handler = new SyncNodeCommandHandler(dbContext, _gateway);
var result = await handler.Handle(new SyncNodeCommand(node.Id), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.DoesNotContain(dbContext.Inbounds.Local, i => i.RemoteInboundId == "old-id");
Assert.Contains(dbContext.Inbounds.Local, i => i.RemoteInboundId == "new-id");
}
[Fact]
public async Task Handle_WhenInboundGoneFromPanelButHasConfigs_MarksUnavailableInsteadOfRemoving()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = CreateNode();
var inbound = Inbound.FromRemote(node.Id, "old-id", VpnProtocol.Vless, "old", 443);
inbound.Publish("Old", null, [Guid.NewGuid()]);
var config = VpnConfig.Create(Guid.NewGuid(), inbound.Id, VpnProtocol.Vless, null);
config.AssignRemoteClient("external-id");
dbContext.Nodes.Add(node);
dbContext.Inbounds.Add(inbound);
dbContext.VpnConfigs.Add(config);
await dbContext.SaveChangesAsync(CancellationToken.None);
_gateway
.ListInboundsAsync(Arg.Any<Node>(), Arg.Any<CancellationToken>())
.Returns(Result.Success<IReadOnlyList<RemoteInboundInfo>>([]));
var handler = new SyncNodeCommandHandler(dbContext, _gateway);
var result = await handler.Handle(new SyncNodeCommand(node.Id), CancellationToken.None);
Assert.True(result.IsSuccess);
var stored = Assert.Single(dbContext.Inbounds.Local, i => i.RemoteInboundId == "old-id");
Assert.False(stored.IsAvailable);
Assert.False(stored.IsPublished);
}
}