You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

36 lines
1.4 KiB

package wechat
import (
"context"
"errors"
"testing"
)
// 事故回归:wechat 初始化失败时(如商户私钥加载失败),
// 1. 库不得 Fatal/panic(原来 newSys 里是 log.Fatalf,直接杀掉 home 进程 → 容器连坐 → 全站 502);
// 2. defsys 必须保持 nil(不能把 (*WeChat)(nil) 装进接口,否则守卫失效、调用即 panic);
// 3. 包级函数返回 ErrNotInited 而不是 panic。
func TestOnInitFailure_NoFatal_GuardsHold(t *testing.T) {
defsys = nil // 与同包其它测试隔离
t.Cleanup(func() { defsys = nil })
// 空配置 → 私钥路径为空 → 加载失败
err := OnInit(map[string]interface{}{})
if err == nil {
t.Fatal("空配置下 OnInit 应返回错误")
}
if defsys != nil {
t.Fatal("OnInit 失败后 defsys 必须保持 nil,否则是「非 nil 接口 + nil 指针」,调用即 panic")
}
// 以下调用都不能 panic
if got := GenerateOrderNo("WxAPP"); got != "" {
t.Errorf("未初始化时 GenerateOrderNo 应返回空串,实际=%q", got)
}
if _, e := CreateAppOrder(context.Background(), "no1", 1, "d", "u"); !errors.Is(e, ErrNotInited) {
t.Errorf("未初始化时 CreateAppOrder 应返回 ErrNotInited,实际=%v", e)
}
if _, e := DecryptWxPayNotify("a", "n", "c"); !errors.Is(e, ErrNotInited) {
t.Errorf("未初始化时 DecryptWxPayNotify 应返回 ErrNotInited,实际=%v", e)
}
}