using System.Text.Json; namespace UCVL.VPNPlus; internal static class ConnectionSelectionSelfTest { internal static int Run() { var checks = new Dictionary(); bool Rejected(Action action) { try { action(); return false; } catch (InvalidOperationException) { return true; } } string[] authorized = ["office-test", "remote-test"]; var auto = ConnectionSelection.Create(false, "office-test", authorized); checks["default_auto_ignores_previous_manual_choice"] = auto.NodeSlug == "auto" && !auto.Manual; checks["auto_attempts_are_bounded"] = auto.Attempts(12) == 3 && auto.Attempts(2) == 2; var manual = ConnectionSelection.Create(true, "office-test", authorized); checks["manual_uses_exact_authorized_node"] = manual.Manual && manual.NodeSlug == "office-test"; checks["manual_has_no_fallback_attempt"] = manual.Attempts(12) == 1; checks["missing_choice_rejected"] = Rejected(() => ConnectionSelection.Create(true, null, authorized)); checks["removed_permission_rejected"] = Rejected(() => ConnectionSelection.Create(true, "removed", authorized)); checks["manual_auto_slug_rejected"] = Rejected(() => ConnectionSelection.Create(true, "auto", ["auto"])); checks["different_server_node_rejected"] = Rejected(() => manual.ValidateResponse("remote-test")); checks["empty_server_node_rejected"] = Rejected(() => auto.ValidateResponse("")); checks["manual_exact_response_accepted"] = !Rejected(() => manual.ValidateResponse("office-test")); checks["auto_concrete_response_accepted"] = !Rejected(() => auto.ValidateResponse("remote-test")); var siteName = NodePresentation.Name(NodePresentation.JinanSlug, "济南(博翠明湖)", true); checks["private_connected_name_follows_registered_site"] = siteName == "济南(博翠明湖)"; checks["legacy_private_empty_name_keeps_zero3_fallback"] = NodePresentation.Name(NodePresentation.JinanSlug, "", true) == NodePresentation.JinanName; checks["public_node_not_relabelled_as_zero3"] = NodePresentation.Name(NodePresentation.JinanSlug, "VPN002", false) == "VPN002"; checks["fallback_shows_actual_public_node"] = NodePresentation.Name("aliyun-tokyo-002", "VPN002", false) == "VPN002"; checks["learning_keeps_connected_node_name"] = NodePresentation.LearningDetail(true, siteName, "规则已更新") == siteName + " · 规则已更新"; checks["learning_after_fallback_names_public_node"] = NodePresentation.LearningDetail(true, "VPN002", "规则已更新") == "VPN002 · 规则已更新"; checks["unconnected_selection_does_not_claim_actual_node"] = NodePresentation.LearningDetail(false, siteName, "正在验证") == "正在验证"; checks["public_transport_has_no_suffix"] = NodePresentation.WithTransport("VPN002", false) == "VPN002"; checks["tailnet_transport_has_explicit_suffix"] = NodePresentation.WithTransport("VPN002", true) == "VPN002 + Tailscale"; checks["second_node_uses_same_transport_format"] = NodePresentation.WithTransport("VPN003", true) == "VPN003 + Tailscale"; using (var form = new MainForm(preview: true, uiOnly: true)) foreach (var check in form.CheckSelectionUi()) checks.Add(check.Key, check.Value); var passed = checks.Values.All(v => v); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "connection-selection-self-test.json"), JsonSerializer.Serialize(new { passed, network_modified = false, local_office_gateway_tested = false, checks }, new JsonSerializerOptions { WriteIndented = true })); return passed ? 0 : 18; } } internal sealed partial class MainForm { internal Dictionary CheckSelectionUi() { var result = new Dictionary(); var profile = new Profile { Nodes = [new() { Slug = "office-test", Name = "优创未来·济南办" }, new() { Slug = "remote-test", Name = "远程测试" }], RouteModes = [new() { Id = "smart", Name = "AI 智能代理" }], }; _availableNodeCount = 2; UpdateSelectionNodes(profile, false); result["ui_defaults_to_auto"] = !ManualSelection; _connectionModeCombo.SelectedIndex = 1; _manualNodeCombo.SelectedIndex = 0; result["ui_manual_has_authorized_choices"] = ManualSelection && _manualNodeCombo.Items.Count == 2; SetBusy(true); result["ui_locked_during_connect"] = !_connectionModeCombo.Enabled && !_manualNodeCombo.Enabled; SetBusy(false); _wantConnected = true; RefreshSelectionControls(); result["ui_locked_during_connection_or_recovery"] = !_connectionModeCombo.Enabled && !_manualNodeCombo.Enabled; _wantConnected = false; RefreshSelectionControls(); result["ui_editable_after_disconnect"] = _connectionModeCombo.Enabled && _manualNodeCombo.Enabled; UpdateSelectionNodes(profile, true); result["profile_refresh_keeps_exact_manual_choice"] = (_manualNodeCombo.SelectedItem as NodeInfo)?.Slug == "office-test"; UpdateSelectionNodes(new Profile { Nodes = [profile.Nodes[1]] }, true); result["removed_node_not_silently_replaced"] = ManualSelection && _manualNodeCombo.SelectedItem is null; UpdateSelectionNodes(profile, false); result["new_login_resets_to_auto"] = !ManualSelection && _manualNodeCombo.SelectedItem is null; var nodeCount = _manualNodeCombo.Items.Count; _currentNodeSlug = "office-test"; _currentNodeBaseName = "VPN002"; _currentNodeIsPrivateAuxiliary = false; UpdateConnectedTransportPresentation(false); result["connected_public_name_is_plain_node"] = _currentNodeName == "VPN002" && !_currentNodeUsesTailnet; UpdateConnectedTransportPresentation(true); result["connected_tailnet_name_marks_actual_transport"] = _currentNodeName == "VPN002 + Tailscale" && _currentNodeUsesTailnet; UpdateConnectedTransportPresentation(true); result["tailnet_refresh_does_not_duplicate_suffix"] = _currentNodeName == "VPN002 + Tailscale"; UpdateConnectedTransportPresentation(false); result["tailnet_fallback_removes_suffix_keeps_node"] = _currentNodeName == "VPN002" && _currentNodeSlug == "office-test" && !_currentNodeUsesTailnet; result["transport_does_not_duplicate_node_choices"] = _manualNodeCombo.Items.Count == nodeCount && nodeCount == 2; _currentNodeIsPrivateAuxiliary = true; _currentNodeBaseName = NodePresentation.JinanName; UpdateConnectedTransportPresentation(true); result["private_tailnet_connection_shows_actual_transport"] = _currentNodeName == NodePresentation.JinanName + " + Tailscale" && _currentNodeUsesTailnet; _currentNodeBaseName = null; UpdateConnectedTransportPresentation(false); result["cleared_connection_does_not_claim_tailnet"] = _currentNodeName is null && !_currentNodeUsesTailnet; // Exercise real dashboard layout, not the hidden login page's stale bounds. Opacity = 0; ShowInTaskbar = false; Show(); ShowDashboard(profile); _refreshTimer.Stop(); PerformLayout(); Application.DoEvents(); _currentNodeName = NodePresentation.JinanName; _currentNodeLabel.Text = _currentNodeName; SetConnectionState(true, "已安全连接", _currentNodeName); LayoutDashboard(); var allConnectedLayoutsFit = true; foreach (var (size, label) in new[] { (new Size(480, 360), "compact"), (new Size(840, 480), "minimum_wide"), (new Size(960, 720), "wide") }) { ClientSize = new Size(ScaleLogical(size.Width), ScaleLogical(size.Height)); PerformLayout(); LayoutDashboard(); Application.DoEvents(); var isWide = WideDashboardPresentation; var rowFits = _currentNodeLabel.Visible && _modeCard.ClientRectangle.Contains(_currentNodeLabel.Bounds) && _nodeHeading.Right <= _currentNodeLabel.Left && _currentNodeLabel.Bottom <= _routeReadOnly.Top && (!isWide || !_wideSummaryState.Visible || _currentNodeLabel.Right <= _wideSummaryState.Left); result["connected_node_does_not_overlap_mode_or_range_" + label] = rowFits; result["connected_node_name_fits_" + label] = _currentNodeLabel.Text == NodePresentation.JinanName && FitsNodeText(); allConnectedLayoutsFit &= rowFits && FitsNodeText(); if (isWide) { _currentNodeBaseName = NodePresentation.JinanName; UpdateConnectedTransportPresentation(true); LayoutDashboard(); result["long_tailnet_name_keeps_node_and_transport_visible_" + label] = FitsNodeText() && (_currentNodeLabel.Text == _currentNodeName || _currentNodeLabel.Text == _currentNodeBaseName) && _wideSummaryState.Text.Contains("Tailscale", StringComparison.Ordinal); UpdateConnectedTransportPresentation(false); LayoutDashboard(); } } result["connected_node_has_dedicated_actual_state_space"] = allConnectedLayoutsFit; result["connected_mode_badge_does_not_cover_name"] = !_scheduleBadge.Visible; Size = MinimumSize; PerformLayout(); LayoutDashboard(); Application.DoEvents(); result["zero3_name_fits_compact_connected_row"] = FitsNodeText(); RequestExit(); return result; bool FitsNodeText() { using var graphics = _currentNodeLabel.CreateGraphics(); return TextRenderer.MeasureText(graphics, _currentNodeLabel.Text, _currentNodeLabel.Font, Size.Empty, TextFormatFlags.SingleLine).Width <= _currentNodeLabel.ClientSize.Width; } } }