Skip to content

Commit 156d2e6

Browse files
authored
refactor(channels): own account config mutations (#115970)
1 parent 9991f49 commit 156d2e6

6 files changed

Lines changed: 729 additions & 142 deletions

File tree

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import type { OpenClawConfig } from "../../config/types.openclaw.js";
3+
import { createChannelTestPluginBase } from "../../test-utils/channel-plugins.js";
4+
import {
5+
applyPreparedChannelAccountConfiguration,
6+
applyPreparedChannelAccountRemoval,
7+
prepareChannelAccountConfiguration,
8+
prepareChannelAccountRemoval,
9+
} from "./account-config-mutation.js";
10+
import { defineChannelSetupContract } from "./setup-contract.js";
11+
import type { ChannelPlugin } from "./types.plugin.js";
12+
13+
const runtime = {
14+
log: vi.fn(),
15+
error: vi.fn(),
16+
exit: vi.fn(),
17+
} as never;
18+
19+
describe("channel account config mutations", () => {
20+
it("prepares, validates, applies, and reports lifecycle changes in order", async () => {
21+
const callOrder: string[] = [];
22+
const beforePersistentEffect = vi.fn(async () => {
23+
callOrder.push("authority");
24+
});
25+
const cfg = {
26+
channels: {
27+
"test-chat": {
28+
enabled: true,
29+
token: "old-token",
30+
},
31+
},
32+
} satisfies OpenClawConfig;
33+
const plugin = {
34+
...createChannelTestPluginBase({ id: "test-chat" }),
35+
setup: {
36+
singleAccountKeysToMove: ["token"],
37+
prepareAccountConfigInput: ({ input }: { input: Record<string, unknown> }) => {
38+
callOrder.push("prepare");
39+
return { ...input, token: "prepared-token" };
40+
},
41+
validateInput: ({ input }: { input: Record<string, unknown> }) => {
42+
callOrder.push("validate");
43+
return input.token === "prepared-token" ? null : "input was not prepared";
44+
},
45+
applyAccountConfig: ({ cfg: inputCfg, accountId, input }) => {
46+
callOrder.push("apply");
47+
const channel = inputCfg.channels?.["test-chat"] as
48+
| {
49+
enabled?: boolean;
50+
accounts?: Record<string, Record<string, unknown>>;
51+
}
52+
| undefined;
53+
return {
54+
...inputCfg,
55+
channels: {
56+
...inputCfg.channels,
57+
"test-chat": {
58+
...channel,
59+
accounts: {
60+
...channel?.accounts,
61+
[accountId]: { token: (input as { token: string }).token },
62+
},
63+
},
64+
},
65+
};
66+
},
67+
},
68+
lifecycle: {
69+
onAccountConfigChanged: ({ prevCfg, nextCfg, accountId }) => {
70+
callOrder.push("lifecycle");
71+
expect(prevCfg).toBe(cfg);
72+
expect(accountId).toBe("work");
73+
expect(nextCfg.channels?.["test-chat"]).toMatchObject({
74+
accounts: {
75+
default: { token: "old-token" },
76+
work: { token: "prepared-token" },
77+
},
78+
});
79+
},
80+
},
81+
} as ChannelPlugin;
82+
83+
const prepared = await prepareChannelAccountConfiguration({
84+
cfg,
85+
plugin,
86+
requestedAccountId: "Work",
87+
resolveInput: () => ({ token: "raw-token" }),
88+
runtime,
89+
beforePersistentEffect,
90+
});
91+
expect(prepared.ok).toBe(true);
92+
if (!prepared.ok) {
93+
return;
94+
}
95+
96+
const applied = await applyPreparedChannelAccountConfiguration({
97+
cfg,
98+
channel: "test-chat",
99+
prepared: prepared.value,
100+
runtime,
101+
beforePersistentEffect,
102+
});
103+
104+
expect(callOrder).toEqual([
105+
"authority",
106+
"prepare",
107+
"validate",
108+
"apply",
109+
"authority",
110+
"lifecycle",
111+
]);
112+
expect(applied.accountId).toBe("work");
113+
expect(applied.input).toEqual({ token: "prepared-token" });
114+
});
115+
116+
it("returns channel-owned setup parse errors before config mutation", async () => {
117+
const applyAccountConfig = vi.fn(({ cfg }) => cfg);
118+
const plugin = {
119+
...createChannelTestPluginBase({ id: "typed-chat" }),
120+
setupContract: defineChannelSetupContract({
121+
fields: {
122+
token: {
123+
kind: "string",
124+
cli: { flags: "--token <token>", description: "Bot token" },
125+
},
126+
},
127+
adapter: { applyAccountConfig },
128+
}),
129+
} as ChannelPlugin;
130+
131+
const prepared = await prepareChannelAccountConfiguration({
132+
cfg: {},
133+
plugin,
134+
resolveInput: () => ({ unknownOption: true }),
135+
runtime,
136+
});
137+
138+
expect(prepared).toEqual({
139+
ok: false,
140+
error: {
141+
kind: "invalid-input",
142+
message: "Unsupported setup option: unknownOption",
143+
},
144+
});
145+
expect(applyAccountConfig).not.toHaveBeenCalled();
146+
});
147+
148+
it("normalizes plugin-resolved account IDs only at the config mutation boundary", async () => {
149+
const applyAccountConfig = vi.fn(({ cfg }) => cfg);
150+
const onAccountConfigChanged = vi.fn();
151+
const plugin = {
152+
...createChannelTestPluginBase({ id: "test-chat" }),
153+
setup: {
154+
resolveAccountId: () => "Work",
155+
applyAccountConfig,
156+
},
157+
lifecycle: { onAccountConfigChanged },
158+
} as ChannelPlugin;
159+
160+
const prepared = await prepareChannelAccountConfiguration({
161+
cfg: {},
162+
plugin,
163+
requestedAccountId: "ignored",
164+
resolveInput: () => ({ token: "token-1" }),
165+
runtime,
166+
});
167+
expect(prepared.ok).toBe(true);
168+
if (!prepared.ok) {
169+
return;
170+
}
171+
172+
const applied = await applyPreparedChannelAccountConfiguration({
173+
cfg: {},
174+
channel: "test-chat",
175+
prepared: prepared.value,
176+
runtime,
177+
});
178+
179+
expect(applyAccountConfig).toHaveBeenCalledWith({
180+
cfg: {},
181+
accountId: "work",
182+
input: { token: "token-1" },
183+
});
184+
expect(onAccountConfigChanged).toHaveBeenCalledWith({
185+
prevCfg: {},
186+
nextCfg: {},
187+
accountId: "Work",
188+
runtime,
189+
});
190+
expect(applied.accountId).toBe("Work");
191+
});
192+
193+
it("does not resolve input when the channel has no account setup capability", async () => {
194+
const resolveInput = vi.fn(() => {
195+
throw new Error("input should stay lazy");
196+
});
197+
const plugin = createChannelTestPluginBase({ id: "read-only-chat" }) as ChannelPlugin;
198+
199+
const prepared = await prepareChannelAccountConfiguration({
200+
cfg: {},
201+
plugin,
202+
resolveInput,
203+
runtime,
204+
});
205+
206+
expect(prepared).toEqual({
207+
ok: false,
208+
error: { kind: "unsupported" },
209+
});
210+
expect(resolveInput).not.toHaveBeenCalled();
211+
});
212+
213+
it("deletes an account and runs its owner lifecycle hook", async () => {
214+
const onAccountRemoved = vi.fn();
215+
const cfg = {
216+
channels: {
217+
"test-chat": {
218+
accounts: {
219+
default: { token: "default-token" },
220+
work: { token: "work-token" },
221+
},
222+
},
223+
},
224+
} satisfies OpenClawConfig;
225+
const plugin = {
226+
...createChannelTestPluginBase({
227+
id: "test-chat",
228+
config: {
229+
deleteAccount: ({ cfg: inputCfg, accountId }) => {
230+
const channel = inputCfg.channels?.["test-chat"] as {
231+
accounts?: Record<string, Record<string, unknown>>;
232+
};
233+
const accounts = { ...channel.accounts };
234+
delete accounts[accountId];
235+
return {
236+
...inputCfg,
237+
channels: {
238+
...inputCfg.channels,
239+
"test-chat": { ...channel, accounts },
240+
},
241+
};
242+
},
243+
},
244+
}),
245+
gateway: { startAccount: vi.fn() },
246+
lifecycle: { onAccountRemoved },
247+
} as ChannelPlugin;
248+
const prepared = prepareChannelAccountRemoval({
249+
plugin,
250+
accountId: "Work",
251+
action: "delete",
252+
});
253+
254+
expect(prepared).toMatchObject({
255+
accountId: "work",
256+
accountKey: "work",
257+
shouldStopRuntime: true,
258+
});
259+
const result = await applyPreparedChannelAccountRemoval({
260+
cfg,
261+
prepared,
262+
runtime,
263+
});
264+
265+
expect(result.ok).toBe(true);
266+
if (result.ok) {
267+
expect(result.value.nextConfig.channels?.["test-chat"]).toMatchObject({
268+
accounts: { default: { token: "default-token" } },
269+
});
270+
}
271+
expect(onAccountRemoved).toHaveBeenCalledWith({
272+
prevCfg: cfg,
273+
accountId: "work",
274+
runtime,
275+
});
276+
});
277+
278+
it("reports unsupported removal actions without running lifecycle hooks", async () => {
279+
const onAccountConfigChanged = vi.fn();
280+
const plugin = {
281+
...createChannelTestPluginBase({ id: "test-chat" }),
282+
lifecycle: { onAccountConfigChanged },
283+
} as ChannelPlugin;
284+
const prepared = prepareChannelAccountRemoval({
285+
plugin,
286+
accountId: "default",
287+
action: "disable",
288+
});
289+
290+
const result = await applyPreparedChannelAccountRemoval({
291+
cfg: {},
292+
prepared,
293+
runtime,
294+
});
295+
296+
expect(result).toEqual({
297+
ok: false,
298+
error: { kind: "unsupported-action", action: "disable" },
299+
});
300+
expect(onAccountConfigChanged).not.toHaveBeenCalled();
301+
});
302+
});

0 commit comments

Comments
 (0)