| 1 | package bot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/control" |
| 11 | "reasonix/internal/event" |
| 12 | "reasonix/internal/tool" |
| 13 | ) |
| 14 | |
| 15 | // TestGatewayNewSessionPinsRotatedPathAgainstFallback: /new 旋转出新会话后, |
| 16 | // 下一条消息的 profile 必须解析到旋转后的新路径(而不是确定性 fallback 的旧 |
| 17 | // 路径),否则 sessionStateMatchesRuntime 会重建旧会话、静默撤销 /new。 |
| 18 | func TestGatewayNewSessionPinsRotatedPathAgainstFallback(t *testing.T) { |
| 19 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 20 | gw := NewGateway(GatewayConfig{}, nil, logger) |
| 21 | adapter := newFakeAdapter(PlatformWeixin, "fake-weixin") |
| 22 | msg := InboundMessage{Platform: PlatformWeixin, ConnectionID: "weixin-weixin", Domain: "weixin", ChatType: ChatDM, ChatID: "chat", UserID: "user", Text: "/new"} |
| 23 | key := BuildSessionKey(msg.Session()) |
| 24 | sessionDir := t.TempDir() |
| 25 | oldPath := agent.NewSessionPath(sessionDir, "old-model") |
| 26 | exec := agent.New(gatewayFakeProvider{}, tool.NewRegistry(), agent.NewSession("system"), agent.Options{}, event.Discard) |
| 27 | ctrl := control.New(control.Options{Executor: exec, SessionDir: sessionDir, SessionPath: oldPath, Label: "fake-model"}) |
| 28 | leases := control.NewSessionLeaseKeeper() |
| 29 | if err := leases.Rebind(oldPath); err != nil { |
| 30 | t.Fatalf("bind old session lease: %v", err) |
| 31 | } |
| 32 | gw.controllers[key] = &sessionState{ctrl: ctrl, leases: leases, sessionPath: oldPath} |
| 33 | |
| 34 | gw.handleSlashCommand(context.Background(), adapter, key, msg) |
| 35 | |
| 36 | rotated := ctrl.SessionPath() |
| 37 | if rotated == oldPath { |
| 38 | t.Fatalf("controller session path was not rotated") |
| 39 | } |
| 40 | profile := gw.sessionProfileForMessage(msg) |
| 41 | if canonicalBotPath(profile.sessionPath) != canonicalBotPath(rotated) { |
| 42 | t.Fatalf("next-message profile session path = %q, want rotated %q", profile.sessionPath, rotated) |
| 43 | } |
| 44 | if !sessionStateMatchesRuntime(gw.controllers[key], profile) { |
| 45 | t.Fatalf("rotated controller should match the next-message profile") |
| 46 | } |
| 47 | gw.closeSessions() |
| 48 | } |
| 49 |