mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
产品名早已是 ModelRouter,但仓库标识符仍沿用旧名。这次统一,读写两侧都做兼容, 已部署的机器不会因为顺序问题立刻失效。 - go module `llmsproxy` → `modelrouter`;全仓 import 与 `cmd/modelrouter` 同步。 - `Makefile BINARY=modelrouter`;发行包名、systemd unit、/usr/share 资源、 `/usr/local/bin` 目标全部改为 modelrouter。 - 默认配置目录 `/etc/llmsproxy` → `/etc/modelrouter`。**读取侧回退**:插件与 `storeDir` 仍会检查旧路径,所以"先换二进制、后改 unit"不会立即失效。 - 环境变量 `LLMSPROXY_*`(PLUGIN_DIR / XDISPLAY_BASE / ARCH / LIVE_ZEN / BUILD_DIR)改为 `MODELROUTER_*`;`LLMS_PROXY_MASTER_KEY` → `MODELROUTER_MASTER_KEY`。旧名在读取侧继续接受。 - 桌面端内嵌核心 `bin/llmsproxy` → `bin/modelrouter`,并保留旧文件名回退, 避免升级到一半的 profile 被判成"core binary missing"。 - 新增 `packaging/migrate-to-modelrouter.sh`:**默认 dry-run**,复制而非移动、 旧目录永不删除、支持 `--rollback`;随 release tar 分发并由 core-dist 校验。 - `deploy.sh` 增加改名保护:发现旧 `/etc/llmsproxy` 而新配置不存在时**中止**, 否则会部署出一个"能启动但没有 key、没有源"的空实例。 - 临时目录前缀 `/tmp/llmsproxy-*` → `/tmp/modelrouter-*`;清理逻辑同时回收两代。 兼容别名(`__llmsproxy_*` 全局、`llmsproxy` SDK 别名、旧路径/旧 env)刻意保留, 它们是升级路径而非遗留;docs/plugins.md 已标注为废弃。 本提交未包含:`adapters/opencodezen.lua`、`adapter_opencodezen_test.go`、 `adapter_opencodezen_live_test.go`(OpenCode UA/免费池的既有改动,另批提交)。
178 lines
5.8 KiB
Go
178 lines
5.8 KiB
Go
package core
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"sync/atomic"
|
||
"testing"
|
||
|
||
"modelrouter/internal/lua"
|
||
)
|
||
|
||
// loomyHarness:Loomy 微信扫码长轮询的测试台。
|
||
//
|
||
// 参考语义(loomy-wechat.ts 点名,早期版本**读反了**导致"扫码后卡在已扫码"):
|
||
//
|
||
// 408 = 等待扫码;404 = 已扫码待确认(继续轮询);
|
||
// 405 = **已确认**(wx_code 就在这一帧);403 = 取消;402 = 失效
|
||
type loomyHarness struct {
|
||
t *testing.T
|
||
ps *lua.Plugins
|
||
ts *httptest.Server
|
||
polls int32
|
||
confirmAt int32
|
||
gotAuth string
|
||
}
|
||
|
||
func newLoomyHarness(t *testing.T) *loomyHarness {
|
||
t.Helper()
|
||
h := &loomyHarness{t: t, confirmAt: 3}
|
||
h.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
switch {
|
||
case strings.HasPrefix(r.URL.Path, "/connect/qrconnect"):
|
||
w.Header().Set("Content-Type", "text/html")
|
||
// 夹具必须用**真实微信页面的形态**:uuid 内嵌在
|
||
// <img class="js_qrcode_img" src="/connect/qrcode/<uuid>"/>。
|
||
// 用别的形态(如 var uuid = "...")测出来的正则不是生产要用的那条。
|
||
fmt.Fprint(w, `<html><img class="js_qrcode_img" src="/connect/qrcode/UUID-TEST-1"/></html>`)
|
||
case strings.HasPrefix(r.URL.Path, "/connect/l/qrconnect"):
|
||
n := atomic.AddInt32(&h.polls, 1)
|
||
w.Header().Set("Content-Type", "text/plain")
|
||
if n == 1 {
|
||
fmt.Fprint(w, "window.wx_errcode=408;") // 等待扫码
|
||
return
|
||
}
|
||
if n < atomic.LoadInt32(&h.confirmAt) {
|
||
fmt.Fprint(w, "window.wx_errcode=404;") // 已扫码待确认
|
||
return
|
||
}
|
||
fmt.Fprint(w, `window.wx_errcode=405;window.wx_code='WX-CODE-1';`) // 已确认
|
||
case r.URL.Path == "/login/thirdAccount/bind/auth":
|
||
h.gotAuth = r.Header.Get("Authorization")
|
||
w.Header().Set("Content-Type", "application/json")
|
||
fmt.Fprint(w, `{"code":0,"data":{"rcode":"RCODE-1","bind":1,"nickname":"loomy-dev","userid":"u5"}}`)
|
||
default:
|
||
w.WriteHeader(404)
|
||
}
|
||
}))
|
||
t.Cleanup(h.ts.Close)
|
||
|
||
c := kernelTestCore(t)
|
||
h.ps = c.Plugins()
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := h.ps.LoadSource("channels", src); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "list_sources", "status"}, false)
|
||
if _, err := h.ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||
"platform": "loomy",
|
||
"wechat": map[string]interface{}{
|
||
"auth_url": h.ts.URL + "/connect/qrconnect?state={state}",
|
||
"qr_url": h.ts.URL + "/connect/qrcode/{uuid}",
|
||
"poll_url": h.ts.URL + "/connect/l/qrconnect",
|
||
"account_base": h.ts.URL,
|
||
},
|
||
}); err != nil {
|
||
t.Fatalf("set_endpoints: %v", err)
|
||
}
|
||
return h
|
||
}
|
||
|
||
func (h *loomyHarness) begin() (map[string]interface{}, string) {
|
||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||
Method: "POST", Body: `{"platform":"loomy"}`, Role: "admin",
|
||
})
|
||
if err != nil {
|
||
h.t.Fatal(err)
|
||
}
|
||
j := out.JSON.(map[string]interface{})
|
||
return j, j["session"].(map[string]interface{})["id"].(string)
|
||
}
|
||
|
||
func (h *loomyHarness) pollOnce(sid string) map[string]interface{} {
|
||
out, err := h.ps.InvokeHandler("channels", "poll_login", &lua.HandlerRequest{
|
||
Method: "GET", Query: map[string]string{"id": sid}, Role: "admin",
|
||
})
|
||
if err != nil {
|
||
h.t.Fatal(err)
|
||
}
|
||
return out.JSON.(map[string]interface{})
|
||
}
|
||
|
||
// TestLoomyWechatPollBehavior 逐行锁定微信长轮询的状态判据。
|
||
func TestLoomyWechatPollBehavior(t *testing.T) {
|
||
h := newLoomyHarness(t)
|
||
|
||
t.Run("行1 授权页抽出uuid并给出二维码", func(t *testing.T) {
|
||
atomic.StoreInt32(&h.polls, 0)
|
||
j, _ := h.begin()
|
||
qr, _ := j["qr_url"].(string)
|
||
if !strings.Contains(qr, "UUID-TEST-1") {
|
||
t.Fatalf("二维码 URL 应含抽出的 uuid, 实得 %q", qr)
|
||
}
|
||
})
|
||
|
||
t.Run("行2 408等待_404已扫码但不算完成", func(t *testing.T) {
|
||
atomic.StoreInt32(&h.polls, 0)
|
||
atomic.StoreInt32(&h.confirmAt, 99)
|
||
_, sid := h.begin()
|
||
// 第一拍 408
|
||
if st := h.pollOnce(sid); st["state"] != "pending" {
|
||
t.Fatalf("408 应 pending, 实得 %v", st["state"])
|
||
}
|
||
// 第二拍 404 —— **关键**:已扫码但**不是**完成(早期读反会卡这里)
|
||
st := h.pollOnce(sid)
|
||
if st["state"] != "pending" {
|
||
t.Fatalf("404(已扫码待确认) 必须是 pending 而非 completed, 实得 %v", st["state"])
|
||
}
|
||
})
|
||
|
||
t.Run("行3 405才算确认_并换到凭据", func(t *testing.T) {
|
||
atomic.StoreInt32(&h.polls, 0)
|
||
atomic.StoreInt32(&h.confirmAt, 3)
|
||
_, sid := h.begin()
|
||
h.pollOnce(sid) // 408
|
||
h.pollOnce(sid) // 404
|
||
st := h.pollOnce(sid) // 405
|
||
if st["state"] != "completed" {
|
||
t.Fatalf("405 应 completed, 实得 %v (msg=%v)", st["state"], st["message"])
|
||
}
|
||
// 账号端点必须用讯飞签名头(account ak:sig),不是 Bearer
|
||
if !strings.HasPrefix(h.gotAuth, "account ") {
|
||
t.Fatalf("账号端点应用 account ak:sig 签名, 实得 %q", h.gotAuth)
|
||
}
|
||
if !strings.Contains(h.gotAuth, ":") {
|
||
t.Fatalf("签名头应含冒号分隔的 ak:sig, 实得 %q", h.gotAuth)
|
||
}
|
||
v, ok, _ := h.ps.GetSecret("channels", "channels:loomy")
|
||
if !ok || !strings.Contains(v, "RCODE-1") {
|
||
t.Fatalf("凭据应含 rcode, 实得 %q", v)
|
||
}
|
||
})
|
||
|
||
t.Run("行4 403取消_402失效_都是终态失败", func(t *testing.T) {
|
||
for _, tc := range []struct{ errcode, want string }{
|
||
{"403", "取消"}, {"402", "失效"},
|
||
} {
|
||
hh := newLoomyHarness(t)
|
||
hh.ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
if strings.HasPrefix(r.URL.Path, "/connect/qrconnect") {
|
||
fmt.Fprint(w, `<img src="/connect/qrcode/U">`)
|
||
return
|
||
}
|
||
fmt.Fprintf(w, "window.wx_errcode=%s;", tc.errcode)
|
||
})
|
||
_, sid := hh.begin()
|
||
st := hh.pollOnce(sid)
|
||
if st["state"] != "failed" {
|
||
t.Fatalf("%s 应 failed, 实得 %v", tc.errcode, st["state"])
|
||
}
|
||
}
|
||
})
|
||
}
|