|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 1 | +import { describe, expect, it, vi } from "vitest"; |
2 | 2 | import type { OpenClawConfig } from "../config/config.js"; |
3 | | -import { setActivePluginRegistry } from "../plugins/runtime.js"; |
4 | | -import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js"; |
5 | | -import type { WizardPrompter } from "../wizard/prompts.js"; |
6 | | -import { patchChannelSetupWizardAdapter } from "./channel-test-helpers.js"; |
7 | 3 | import { |
| 4 | + createChannelOnboardingPostWriteHook, |
8 | 5 | createChannelOnboardingPostWriteHookCollector, |
9 | 6 | runCollectedChannelOnboardingPostWriteHooks, |
10 | | - setupChannels, |
11 | 7 | } from "./onboard-channels.js"; |
12 | | -import { createExitThrowingRuntime, createWizardPrompter } from "./test-wizard-helpers.js"; |
13 | | - |
14 | | -function setMinimalTelegramOnboardingRegistryForTests(): void { |
15 | | - setActivePluginRegistry( |
16 | | - createTestRegistry([ |
17 | | - { |
18 | | - pluginId: "telegram", |
19 | | - source: "test", |
20 | | - plugin: { |
21 | | - ...createChannelTestPluginBase({ |
22 | | - id: "telegram", |
23 | | - label: "Telegram", |
24 | | - capabilities: { chatTypes: ["direct", "group"] }, |
25 | | - }), |
26 | | - setup: { |
27 | | - applyAccountConfig: ({ cfg }: { cfg: OpenClawConfig }) => cfg, |
28 | | - }, |
29 | | - setupWizard: { |
30 | | - channel: "telegram", |
31 | | - status: { |
32 | | - configuredLabel: "Configured", |
33 | | - unconfiguredLabel: "Not configured", |
34 | | - resolveConfigured: ({ cfg }: { cfg: OpenClawConfig }) => |
35 | | - Boolean(cfg.channels?.telegram?.botToken), |
36 | | - }, |
37 | | - credentials: [], |
38 | | - }, |
39 | | - }, |
40 | | - }, |
41 | | - ]), |
42 | | - ); |
43 | | -} |
44 | | - |
45 | | -function createPrompter(overrides: Partial<WizardPrompter>): WizardPrompter { |
46 | | - return createWizardPrompter( |
47 | | - { |
48 | | - progress: vi.fn(() => ({ update: vi.fn(), stop: vi.fn() })), |
49 | | - ...overrides, |
50 | | - }, |
51 | | - { defaultSelect: "__done__" }, |
52 | | - ); |
53 | | -} |
54 | | - |
55 | | -function createQuickstartTelegramSelect() { |
56 | | - return vi.fn(async ({ message }: { message: string }) => { |
57 | | - if (message === "Select channel (QuickStart)") { |
58 | | - return "telegram"; |
59 | | - } |
60 | | - return "__done__"; |
61 | | - }); |
62 | | -} |
63 | | - |
64 | | -function createUnexpectedQuickstartPrompter(select: WizardPrompter["select"]) { |
65 | | - return createPrompter({ |
66 | | - select, |
67 | | - multiselect: vi.fn(async () => { |
68 | | - throw new Error("unexpected multiselect"); |
69 | | - }), |
70 | | - text: vi.fn(async ({ message }: { message: string }) => { |
71 | | - throw new Error(`unexpected text prompt: ${message}`); |
72 | | - }) as unknown as WizardPrompter["text"], |
73 | | - }); |
74 | | -} |
| 8 | +import { createExitThrowingRuntime } from "./test-wizard-helpers.js"; |
75 | 9 |
|
76 | 10 | describe("setupChannels post-write hooks", () => { |
77 | | - beforeEach(() => { |
78 | | - setMinimalTelegramOnboardingRegistryForTests(); |
79 | | - }); |
80 | | - |
81 | 11 | it("collects onboarding post-write hooks and runs them against the final config", async () => { |
82 | | - const select = createQuickstartTelegramSelect(); |
83 | 12 | const afterConfigWritten = vi.fn(async () => {}); |
84 | | - const configureInteractive = vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({ |
85 | | - cfg: { |
86 | | - ...cfg, |
87 | | - channels: { |
88 | | - ...cfg.channels, |
89 | | - telegram: { ...cfg.channels?.telegram, botToken: "new-token" }, |
90 | | - }, |
91 | | - } as OpenClawConfig, |
92 | | - accountId: "acct-1", |
93 | | - })); |
94 | | - const restore = patchChannelSetupWizardAdapter("telegram", { |
95 | | - configureInteractive, |
| 13 | + const previousCfg = {} as OpenClawConfig; |
| 14 | + const cfg = { |
| 15 | + channels: { |
| 16 | + telegram: { botToken: "new-token" }, |
| 17 | + }, |
| 18 | + } as OpenClawConfig; |
| 19 | + const adapter = { |
96 | 20 | afterConfigWritten, |
97 | | - getStatus: vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({ |
98 | | - channel: "telegram", |
99 | | - configured: Boolean(cfg.channels?.telegram?.botToken), |
100 | | - statusLines: [], |
101 | | - })), |
102 | | - }); |
103 | | - const prompter = createUnexpectedQuickstartPrompter( |
104 | | - select as unknown as WizardPrompter["select"], |
105 | | - ); |
| 21 | + }; |
106 | 22 | const collector = createChannelOnboardingPostWriteHookCollector(); |
107 | 23 | const runtime = createExitThrowingRuntime(); |
| 24 | + const hook = createChannelOnboardingPostWriteHook({ |
| 25 | + accountId: "acct-1", |
| 26 | + adapter, |
| 27 | + channel: "telegram", |
| 28 | + previousCfg, |
| 29 | + }); |
108 | 30 |
|
109 | | - try { |
110 | | - const cfg = await setupChannels({} as OpenClawConfig, runtime, prompter, { |
111 | | - quickstartDefaults: true, |
112 | | - skipConfirm: true, |
113 | | - onPostWriteHook: (hook) => { |
114 | | - collector.collect(hook); |
115 | | - }, |
116 | | - }); |
| 31 | + if (!hook) { |
| 32 | + throw new Error("expected post-write hook"); |
| 33 | + } |
| 34 | + collector.collect(hook); |
117 | 35 |
|
118 | | - expect(afterConfigWritten).not.toHaveBeenCalled(); |
| 36 | + expect(afterConfigWritten).not.toHaveBeenCalled(); |
119 | 37 |
|
120 | | - await runCollectedChannelOnboardingPostWriteHooks({ |
121 | | - hooks: collector.drain(), |
122 | | - cfg, |
123 | | - runtime, |
124 | | - }); |
| 38 | + await runCollectedChannelOnboardingPostWriteHooks({ |
| 39 | + hooks: collector.drain(), |
| 40 | + cfg, |
| 41 | + runtime, |
| 42 | + }); |
125 | 43 |
|
126 | | - expect(afterConfigWritten).toHaveBeenCalledWith({ |
127 | | - previousCfg: {} as OpenClawConfig, |
128 | | - cfg, |
129 | | - accountId: "acct-1", |
130 | | - runtime, |
131 | | - }); |
132 | | - } finally { |
133 | | - restore(); |
134 | | - } |
| 44 | + expect(afterConfigWritten).toHaveBeenCalledWith({ |
| 45 | + previousCfg, |
| 46 | + cfg, |
| 47 | + accountId: "acct-1", |
| 48 | + runtime, |
| 49 | + }); |
135 | 50 | }); |
136 | 51 |
|
137 | 52 | it("logs onboarding post-write hook failures without aborting", async () => { |
|
0 commit comments