|
| 1 | +// Verifies outbound base-session keys honor per-binding session-scope |
| 2 | +// overrides so outbound-only sends resolve to the same session as inbound. |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import type { OpenClawConfig } from "../../config/config.js"; |
| 5 | +import { buildOutboundBaseSessionKey } from "./base-session-key.js"; |
| 6 | + |
| 7 | +describe("buildOutboundBaseSessionKey per-binding session scope", () => { |
| 8 | + it("folds a group into main via a per-binding groupScope override against a per-group default", () => { |
| 9 | + const cfg: OpenClawConfig = { |
| 10 | + session: { groupScope: "per-group" }, |
| 11 | + bindings: [ |
| 12 | + { |
| 13 | + type: "route", |
| 14 | + agentId: "main", |
| 15 | + match: { |
| 16 | + channel: "telegram", |
| 17 | + accountId: "default", |
| 18 | + peer: { kind: "group", id: "-100folded" }, |
| 19 | + }, |
| 20 | + session: { groupScope: "main" }, |
| 21 | + }, |
| 22 | + ], |
| 23 | + }; |
| 24 | + |
| 25 | + // Bound group folds into main per its override. |
| 26 | + expect( |
| 27 | + buildOutboundBaseSessionKey({ |
| 28 | + cfg, |
| 29 | + agentId: "main", |
| 30 | + channel: "telegram", |
| 31 | + accountId: "default", |
| 32 | + peer: { kind: "group", id: "-100folded" }, |
| 33 | + }), |
| 34 | + ).toBe("agent:main:main"); |
| 35 | + |
| 36 | + // Other groups keep their own key under the per-group default. |
| 37 | + expect( |
| 38 | + buildOutboundBaseSessionKey({ |
| 39 | + cfg, |
| 40 | + agentId: "main", |
| 41 | + channel: "telegram", |
| 42 | + accountId: "default", |
| 43 | + peer: { kind: "group", id: "-100other" }, |
| 44 | + }), |
| 45 | + ).toBe("agent:main:telegram:group:-100other"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("keeps a group on its own key via a per-binding groupScope override against a main default", () => { |
| 49 | + const cfg: OpenClawConfig = { |
| 50 | + session: { groupScope: "main" }, |
| 51 | + bindings: [ |
| 52 | + { |
| 53 | + type: "route", |
| 54 | + agentId: "main", |
| 55 | + match: { |
| 56 | + channel: "telegram", |
| 57 | + accountId: "default", |
| 58 | + peer: { kind: "group", id: "-100separate" }, |
| 59 | + }, |
| 60 | + session: { groupScope: "per-group" }, |
| 61 | + }, |
| 62 | + ], |
| 63 | + }; |
| 64 | + |
| 65 | + // Bound group stays on its own key per its override. |
| 66 | + expect( |
| 67 | + buildOutboundBaseSessionKey({ |
| 68 | + cfg, |
| 69 | + agentId: "main", |
| 70 | + channel: "telegram", |
| 71 | + accountId: "default", |
| 72 | + peer: { kind: "group", id: "-100separate" }, |
| 73 | + }), |
| 74 | + ).toBe("agent:main:telegram:group:-100separate"); |
| 75 | + |
| 76 | + // Other groups fold into main under the global default. |
| 77 | + expect( |
| 78 | + buildOutboundBaseSessionKey({ |
| 79 | + cfg, |
| 80 | + agentId: "main", |
| 81 | + channel: "telegram", |
| 82 | + accountId: "default", |
| 83 | + peer: { kind: "group", id: "-100other" }, |
| 84 | + }), |
| 85 | + ).toBe("agent:main:main"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("honors a per-binding dmScope override for a direct peer", () => { |
| 89 | + const cfg: OpenClawConfig = { |
| 90 | + session: { dmScope: "main" }, |
| 91 | + bindings: [ |
| 92 | + { |
| 93 | + type: "route", |
| 94 | + agentId: "main", |
| 95 | + match: { |
| 96 | + channel: "telegram", |
| 97 | + accountId: "default", |
| 98 | + peer: { kind: "direct", id: "123" }, |
| 99 | + }, |
| 100 | + session: { dmScope: "per-channel-peer" }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }; |
| 104 | + |
| 105 | + // Bound DM peer keeps its own per-channel-peer key per its override. |
| 106 | + expect( |
| 107 | + buildOutboundBaseSessionKey({ |
| 108 | + cfg, |
| 109 | + agentId: "main", |
| 110 | + channel: "telegram", |
| 111 | + accountId: "default", |
| 112 | + peer: { kind: "direct", id: "123" }, |
| 113 | + }), |
| 114 | + ).toBe("agent:main:telegram:direct:123"); |
| 115 | + |
| 116 | + // Other DM peers fold into main under the global default. |
| 117 | + expect( |
| 118 | + buildOutboundBaseSessionKey({ |
| 119 | + cfg, |
| 120 | + agentId: "main", |
| 121 | + channel: "telegram", |
| 122 | + accountId: "default", |
| 123 | + peer: { kind: "direct", id: "999" }, |
| 124 | + }), |
| 125 | + ).toBe("agent:main:main"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("falls back to global session scope when no binding matches", () => { |
| 129 | + const perGroupCfg: OpenClawConfig = { session: { groupScope: "per-group" } }; |
| 130 | + expect( |
| 131 | + buildOutboundBaseSessionKey({ |
| 132 | + cfg: perGroupCfg, |
| 133 | + agentId: "main", |
| 134 | + channel: "telegram", |
| 135 | + accountId: "default", |
| 136 | + peer: { kind: "group", id: "-100plain" }, |
| 137 | + }), |
| 138 | + ).toBe("agent:main:telegram:group:-100plain"); |
| 139 | + |
| 140 | + const mainCfg: OpenClawConfig = { session: { groupScope: "main" } }; |
| 141 | + expect( |
| 142 | + buildOutboundBaseSessionKey({ |
| 143 | + cfg: mainCfg, |
| 144 | + agentId: "main", |
| 145 | + channel: "telegram", |
| 146 | + accountId: "default", |
| 147 | + peer: { kind: "group", id: "-100plain" }, |
| 148 | + }), |
| 149 | + ).toBe("agent:main:main"); |
| 150 | + |
| 151 | + // Preserves the caller's explicit agentId rather than the binding's agent. |
| 152 | + expect( |
| 153 | + buildOutboundBaseSessionKey({ |
| 154 | + cfg: perGroupCfg, |
| 155 | + agentId: "scribe", |
| 156 | + channel: "telegram", |
| 157 | + accountId: "default", |
| 158 | + peer: { kind: "group", id: "-100plain" }, |
| 159 | + }), |
| 160 | + ).toBe("agent:scribe:telegram:group:-100plain"); |
| 161 | + }); |
| 162 | +}); |
0 commit comments