Repository navigation
Expand file tree
/
Copy pathsession-context.test.ts
More file actions
190 lines (175 loc) · 5.14 KB
/
Copy pathsession-context.test.ts
File metadata and controls
190 lines (175 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/core";
import { beforeEach, describe, expect, it, vi } from "vitest";
const { resolveSessionIdentityMock } = vi.hoisted(() => ({
resolveSessionIdentityMock: vi.fn(),
}));
vi.mock("../src/session-store.ts", () => ({
resolveSessionIdentity: resolveSessionIdentityMock,
}));
import { buildEvaluationContext } from "../src/session-context.ts";
function createApi(): OpenClawPluginApi {
return {
id: "agent-control-openclaw-plugin",
version: "test-version",
config: {},
logger: {
info: vi.fn(),
warn: vi.fn(),
},
on: vi.fn(),
};
}
beforeEach(() => {
resolveSessionIdentityMock.mockReset().mockResolvedValue({
provider: null,
type: "unknown",
channelName: null,
dmUserName: null,
label: null,
from: null,
to: null,
accountId: null,
source: "unknown",
});
});
describe("buildEvaluationContext", () => {
it("derives channel details from the session key when store metadata is unknown", async () => {
// Given a request with a Discord channel session key and no session-store identity
const request = {
api: createApi(),
sourceAgentId: "worker-1",
state: {
sourceAgentId: "worker-1",
agentName: "base-agent:worker-1",
steps: [],
stepsHash: "hash-1",
lastSyncedStepsHash: null,
controlObservabilityById: new Map(),
syncPromise: null,
},
event: {
runId: "event-run",
toolCallId: "event-call",
},
ctx: {
sessionKey: "agent:worker-1:discord:guild-1:channel-2",
},
failClosed: false,
};
// When the evaluation context is built
const context = await buildEvaluationContext(request);
// Then channel metadata is derived directly from the session key
expect(context).toMatchObject({
openclawAgentId: "worker-1",
channelType: "channel",
runId: "event-run",
toolCallId: "event-call",
channel: {
provider: "discord",
type: "channel",
scope: "discord:guild-1:channel-2",
source: "sessionKey",
},
});
});
it("prefers session-store provider and type while retaining the key scope", async () => {
// Given direct-message identity from the session store and a group-scoped session key
resolveSessionIdentityMock.mockResolvedValueOnce({
provider: "slack",
type: "direct",
channelName: null,
dmUserName: "Alice",
label: "Alice",
from: "alice@example.com",
to: "bot@example.com",
accountId: "acct-1",
source: "sessionStore",
});
const request = {
api: createApi(),
sourceAgentId: "worker-1",
state: {
sourceAgentId: "worker-1",
agentName: "base-agent:worker-1",
steps: [{ type: "tool" as const, name: "shell" }],
stepsHash: "hash-1",
lastSyncedStepsHash: "hash-0",
controlObservabilityById: new Map(),
syncPromise: null,
},
event: {},
ctx: {
sessionKey: "agent:worker-1:discord:group:team-room",
runId: "ctx-run",
toolCallId: "ctx-call",
},
failClosed: true,
configuredAgentVersion: "2026.03.20",
pluginVersion: "test-version",
};
// When the evaluation context is built
const context = await buildEvaluationContext(request);
// Then provider and channel type come from the store while scope stays from the key
expect(context).toMatchObject({
runId: "ctx-run",
toolCallId: "ctx-call",
channelType: "direct",
dmUserName: "Alice",
senderFrom: "alice@example.com",
policy: {
failClosed: true,
configuredAgentVersion: "2026.03.20",
},
sync: {
agentName: "base-agent:worker-1",
stepCount: 1,
stepsHash: "hash-1",
lastSyncedStepsHash: "hash-0",
},
channel: {
provider: "slack",
type: "direct",
scope: "discord:group:team-room",
source: "sessionStore+sessionKey",
dmUserName: "Alice",
from: "alice@example.com",
},
});
});
it("falls back to unknown channel information for an unparseable session key", async () => {
// Given a request whose session key does not match the expected agent format
const request = {
api: createApi(),
sourceAgentId: "worker-1",
state: {
sourceAgentId: "worker-1",
agentName: "base-agent:worker-1",
steps: [],
stepsHash: "hash-1",
lastSyncedStepsHash: null,
controlObservabilityById: new Map(),
syncPromise: null,
},
event: {},
ctx: {
sessionKey: "not-an-agent-session-key",
},
failClosed: false,
};
// When the evaluation context is built
const context = await buildEvaluationContext(request);
// Then channel-related fields fall back to unknown values
expect(context).toMatchObject({
channelType: "unknown",
channelName: null,
dmUserName: null,
senderFrom: null,
channel: {
provider: null,
type: "unknown",
scope: null,
source: "unknown",
},
});
});
});