Files
LokiStack/backend/routers/router_test.go
T
opsx 0644dccf2a
CI / Backend (go vet + test) (push) Waiting to run
CI / Frontend (lint + build + test) (push) Waiting to run
test(routers): 断言跟上上游重构——/ai/analyze 改为 /ai/chat,补 license 路由
上游 fbbd3a3/6923a12 把整页 AIOps 改成浮窗助手:
  - 删除 POST /ai/analyze(整页结构化分析)
  - 新增 POST /ai/chat(助手对话,AdminAuth + RequirePro)
  - 新增 GET /system/license、POST /system/license/activate
路由回归测试仍在断言已删除的 /ai/analyze,导致 TestCriticalRoutes 失败。
本次按实际路由更新断言(这正是这个测试的价值:路由被删/改名时立刻暴露)。

注:services.RunAIAnalysis / CollectAISignals 现在没有任何调用方(死代码),
是否保留待确认——见 PR 讨论。
2026-10-31 18:08:59 +00:00

99 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package routers
import (
"testing"
"github.com/gin-gonic/gin"
)
// 回归:接口的 **HTTP 方法与路径** 必须和前端调用一致。
//
// 线上事故:一次模糊匹配的 patch 把
//
// logs.POST("/histogram", ...) → logs.GET("/histogram", ...)
//
// 前端 logsApi.histogram 用的是 POST(body 里带 sourceId/query/start/end),
// 于是所有直方图请求 404:
// - 日志查询页:直方图不显示
// - 日志分析页:直接报 "API endpoint not found",整页不可用
//
// 这类"方法写错"编译期完全查不出来,只能靠路由表断言。
func TestCriticalRoutes(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := SetupRouter()
// 期望的 method + path(注意 gin 会把 :param 保留为 :id)
expect := []struct{ method, path string }{
// 日志查询 / 分析页的数据来源:必须是 POST(前端传 body)
{"POST", "/api/v1/logs/histogram"},
{"POST", "/api/v1/logs/stats"},
{"POST", "/api/v1/logs/metrics"},
{"POST", "/api/v1/logs/query"},
{"POST", "/api/v1/logs/context"},
// 查询辅助接口:GET + query string
{"GET", "/api/v1/logs/sources"},
{"GET", "/api/v1/logs/labels"},
{"GET", "/api/v1/logs/label/:name/values"},
// 批量 label 值(下拉联动一次请求)—— 路由写错就是前端全部下拉空
{"GET", "/api/v1/logs/label-values"},
{"GET", "/api/v1/logs/my-scope"},
{"GET", "/api/v1/logs/daily-volume"},
{"GET", "/api/v1/logs/health"},
// 告警
{"GET", "/api/v1/alerts/rules"},
{"POST", "/api/v1/alerts/rules"},
{"GET", "/api/v1/alerts/channels"},
{"POST", "/api/v1/alerts/check"},
{"GET", "/api/v1/alerts/deliveries"},
// 审计 / 自检
{"GET", "/api/v1/audit-logs"},
{"GET", "/api/v1/system/self-check"},
// 设置类:保留期必须同时有读和写(写要管理员)
{"GET", "/api/v1/settings/retention"},
{"PUT", "/api/v1/settings/retention"},
// AIOps:模型配置(管理员)+ 分析 + 审计历史
{"GET", "/api/v1/ai/config"},
{"PUT", "/api/v1/ai/config"},
{"POST", "/api/v1/ai/config/test"},
// 重构后:整页分析改为浮窗助手(POST /ai/chat,admin + Pro 闸门)
{"POST", "/api/v1/ai/chat"},
{"GET", "/api/v1/system/license"},
{"POST", "/api/v1/system/license/activate"},
{"GET", "/api/v1/ai/runs"},
{"GET", "/api/v1/ai/runs/:id"},
// 备份恢复接口已在上游被移除(commit f87fb4b),这里不再断言
}
registered := map[string]bool{}
for _, route := range engine.Routes() {
registered[route.Method+" "+route.Path] = true
}
for _, item := range expect {
key := item.method + " " + item.path
if !registered[key] {
t.Errorf("缺少路由: %s(前端按这个方法和路径调用,缺了就是 404)", key)
}
}
}
// 回归:前端用 POST 的接口不能只有 GET 版本。
// 单独断言一次,避免以后又有人"顺手"把 POST 改成 GET。
func TestHistogramAcceptsPost(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := SetupRouter()
methods := map[string]bool{}
for _, route := range engine.Routes() {
if route.Path == "/api/v1/logs/histogram" {
methods[route.Method] = true
}
}
if !methods["POST"] {
t.Fatal("POST /api/v1/logs/histogram 未注册:前端 logsApi.histogram 用 POST,会 404")
}
if !methods["GET"] {
t.Fatal("GET /api/v1/logs/histogram 未注册:curl 调试入口丢了")
}
}