web: reset links to the default mode, and keep CN agents dial-only by default [CI]
ci/woodpecker/push/web-build Pipeline was successful
ci/woodpecker/push/verify Pipeline was successful
ci/woodpecker/push/dev-image Pipeline was successful
ci/woodpecker/push/dev-backend-image Pipeline was successful

POST /api/admin/links/reset clears every manual link setting and turns automatic linking on. In the default rules a listening node in CN ranks below a listening node elsewhere, so the CN side dials; the links are pushed again when a node's country is first resolved or changes.
This commit is contained in:
justme committed 2026-10-21 16:35:58 +08:00
1 parent 35207a70ea
commit 7fe9775f97
8 files changed
+222 -15

No files matched your search

@@ -1,5 +1,7 @@
using System.Net;
using System.Text.Json.Nodes;
using Fleet.Server;
using MoShiro.Backend.Application;
using MoShiro.TestSupport;
namespace MoShiro.Backend.Tests;
@@ -60,6 +62,56 @@ public sealed class LinkTests : IClassFixture<BackendFixture>
Assert.True((await LinkAsync(a, b))!["linked"]!.GetValue<bool>());
}
private static AgentRecord Node(string id, bool listens) =>
new(new Fleet.Core.NodeId(id), id, NodeStatus.Active, [], [], "", 0, [], [], true, [], null, null, 0, 0, null, 0, null, 0, Listens: listens);
[Fact]
public void DefaultDialer_NonListeningDials_ThenDomesticDialsForeign_ThenSmallerId()
{
AgentRecord home = Node("agt_a", listens: false);
AgentRecord cn = Node("agt_b", listens: true);
AgentRecord abroad = Node("agt_c", listens: true);
AgentRecord abroad2 = Node("agt_d", listens: true);
var snapshot = new LinkSnapshot(true, [], ["agt_b"]);
Assert.True(snapshot.IsLinked(home, cn)); // 不监听的拨监听的
Assert.False(snapshot.IsLinked(cn, home));
Assert.True(snapshot.IsLinked(cn, abroad)); // 国内的拨国外的,哪怕它的 id 更小
Assert.False(snapshot.IsLinked(abroad, cn));
Assert.True(snapshot.IsLinked(abroad, abroad2)); // 一样的按 id,小的拨
Assert.False(snapshot.IsLinked(abroad2, abroad));
Assert.True(snapshot.IsLinked(home, cn)); // 只有国内的在监听时,国内的也得当监听方
// 手动指定的方向不受国家影响:让国外的拨国内的
var manual = new LinkSnapshot(true, new() { [("agt_c", "agt_b")] = true }, ["agt_b"]);
Assert.True(manual.IsLinked(abroad, cn));
Assert.False(manual.IsLinked(cn, abroad));
}
[Fact]
public async Task Links_ResetDropsManualSettingsAndTurnsAutomaticLinkingOn()
{
await using AgentSimulator first = await _backend.ConnectAgentAsync();
await using AgentSimulator second = await _backend.ConnectAgentAsync();
string[] ids = [.. new[] { first.NodeId.Value, second.NodeId.Value }.Order(StringComparer.Ordinal)];
await SetLinkAsync(ids[1], ids[0], true); // 手动让大 id 的拨小 id 的
await SetAutoLinkAsync(false);
try
{
using HttpResponseMessage reset = await _backend.Admin.PostAsync("/api/admin/links/reset", BackendFixture.Json(new JsonObject()));
reset.EnsureSuccessStatusCode();
JsonNode matrix = JsonNode.Parse(await reset.Content.ReadAsStringAsync())!;
Assert.True(matrix["auto_link"]!.GetValue<bool>());
JsonNode link = (await LinkAsync(ids[0], ids[1]))!; // 回到默认方向:小 id 拨大 id
Assert.True(link["linked"]!.GetValue<bool>());
Assert.Null(link["override"]);
}
finally
{
await SetAutoLinkAsync(true);
}
}
[Fact]
public async Task Links_FollowTheAutomaticSwitch_AndManualChoicesAlwaysWin()
{