Files
-Fonu/internal/frp/service_diagnostics_test.go
T
mrlee 278f8267b7 chore: 初始提交 Fonu 1.0.2
- 公网反代:Nginx 状态卡片(版本 / 运行状态 / 配置校验 / 端口监听)
- 部署设置第一、二梯队(HTTPS 开关、安全头、限流、连接数、仅中国大陆 IP)
- 隧道检测兼容 frps v0.68 / v0.71(V2 与旧版端点)
- 恢复被清空的 internal/frp/store.go、internal/api/ddns_handler.go、web/src/views/DdnsView.vue
2026-09-23 09:40:59 +08:00

65 lines
2.4 KiB
Go

package frp
import (
"context"
"errors"
"strings"
"testing"
)
func TestDiagnoseDialError(t *testing.T) {
err := errors.New("connectex: An attempt was made to access a socket in a way forbidden by its access permissions.")
message := diagnoseDialError(err)
if !strings.HasPrefix(message, "本机网络策略阻止连接:") {
t.Fatalf("unexpected message: %s", message)
}
}
// 23.3:诊断拆分为独立检查项,登录状态从 frpc 日志推断,unknown 不等于失败。
func TestSummarizeFRPCLogin(t *testing.T) {
logLines := strings.Join([]string{
"2026/09/17 10:00:00 [I] [root.go:139] frpc service start",
"2026/09/17 10:00:01 [W] [control.go:167] login to the server failed: token error",
"2026/09/17 10:01:00 [I] [control.go:164] login to server success, get run id [abc]",
}, "\n")
state, detail := summarizeFRPCLogin(logLines)
if state != diagStateOK {
t.Fatalf("login success should win over earlier failure, got %s (%s)", state, detail)
}
failedLog := "2026/09/17 10:00:01 [W] [control.go:167] login to the server failed: token [wrong] error: authentication failed"
state, detail = summarizeFRPCLogin(failedLog)
if state != diagStateFailed || !strings.Contains(detail, "登录失败") {
t.Fatalf("login failure should be reported, got %s (%s)", state, detail)
}
state, _ = summarizeFRPCLogin("2026/09/17 10:00:00 [I] [root.go:139] frpc service start")
if state != diagStateUnknown {
t.Fatalf("no login evidence should be unknown, got %s", state)
}
}
func TestDiagnoseDNSFormatsRecords(t *testing.T) {
check := diagnoseDNS(context.Background(), "192.168.1.10")
if check.State != diagStateOK || !strings.Contains(check.Detail, "IP 地址") {
t.Fatalf("literal IP should skip resolution: %+v", check)
}
}
func TestSummarizeServerSamplesUsesLatestStatus(t *testing.T) {
detail := summarizeServerSamples([]ServerSample{
{CheckedAt: "2026-09-15 18:00:00", Available: true, LatencyMS: 20},
{CheckedAt: "2026-09-15 18:01:00", Available: false, Error: "连接失败"},
})
if detail.Available {
t.Fatal("latest failed sample should make current status unavailable")
}
if detail.Availability != 50 {
t.Fatalf("unexpected availability: %v", detail.Availability)
}
if detail.AverageLatency != 20 || detail.PeakLatency != 20 {
t.Fatalf("unexpected latency summary: average=%d peak=%d", detail.AverageLatency, detail.PeakLatency)
}
if detail.LastError != "连接失败" {
t.Fatalf("unexpected last error: %s", detail.LastError)
}
}