Files
platforms/backend/api/internal/logic/common/staff_wiring_test.go
2026-08-11 13:37:44 +08:00

72 lines
2.1 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.
// 功能描述:验证三个管理后台的工作人员写入入口均接入公共错误转换链路。
// 版本v1.0
package common
import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"runtime"
"testing"
)
// TestStaffHandlersUseWriteErrorConversion 防止任一后台重新绕过工作人员错误转换器。
// 参数t 为 Go 测试上下文。
// 返回值:无。
func TestStaffHandlersUseWriteErrorConversion(t *testing.T) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("无法定位工作人员接线测试文件")
}
logicDirectory := filepath.Dir(filepath.Dir(currentFile))
targets := []struct {
name string
path string
}{
{name: "平台总后台", path: filepath.Join(logicDirectory, "platform", "staff", "staff.go")},
{name: "气站后台", path: filepath.Join(logicDirectory, "gas", "staff.go")},
{name: "配送点后台", path: filepath.Join(logicDirectory, "delivery", "staff.go")},
}
for _, target := range targets {
t.Run(target.name, func(t *testing.T) {
calls := commonCallsInFile(t, target.path)
if calls["CreateStaffRecord"] == 0 {
t.Fatalf("%s工作人员创建入口未接入 CreateStaffRecord", target.name)
}
if calls["UpdateAllowedByIdentityWithError"] == 0 {
t.Fatalf("%s工作人员编辑入口未接入错误转换更新函数", target.name)
}
})
}
}
// commonCallsInFile 统计源文件中调用 common 包函数的次数。
// 参数t 为测试上下文path 为待检查的 Go 源文件。
// 返回值:以函数名为键、调用次数为值的映射。
func commonCallsInFile(t *testing.T, path string) map[string]int {
t.Helper()
file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
if err != nil {
t.Fatalf("解析工作人员处理器失败:%v", err)
}
calls := map[string]int{}
ast.Inspect(file, func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
selector, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
packageName, ok := selector.X.(*ast.Ident)
if ok && packageName.Name == "common" {
calls[selector.Sel.Name]++
}
return true
})
return calls
}