Onboarding: unify hook context and share adapter test helper

This commit is contained in:
Gustavo Madeira Santana
2026-02-26 01:04:11 -05:00
parent 197e720b62
commit 2205dac7d1
4 changed files with 37 additions and 29 deletions

View File

@@ -91,7 +91,7 @@ export type ChannelOnboardingAdapter = {
ctx: ChannelOnboardingInteractiveContext,
) => Promise<ChannelOnboardingConfiguredResult>;
configureWhenConfigured?: (
ctx: ChannelOnboardingConfigureContext,
ctx: ChannelOnboardingInteractiveContext,
) => Promise<ChannelOnboardingConfiguredResult>;
dmPolicy?: ChannelOnboardingDmPolicy;
onAccountRecorded?: (accountId: string, options?: SetupChannelsOptions) => void;

View File

@@ -6,6 +6,9 @@ import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import type { ChannelChoice } from "./onboard-types.js";
import { getChannelOnboardingAdapter } from "./onboarding/registry.js";
import type { ChannelOnboardingAdapter } from "./onboarding/types.js";
export function setDefaultChannelPluginRegistryForTests(): void {
const channels = [
@@ -18,3 +21,24 @@ export function setDefaultChannelPluginRegistryForTests(): void {
] as unknown as Parameters<typeof createTestRegistry>[0];
setActivePluginRegistry(createTestRegistry(channels));
}
export function patchChannelOnboardingAdapter<K extends keyof ChannelOnboardingAdapter>(
channel: ChannelChoice,
patch: Pick<ChannelOnboardingAdapter, K>,
): () => void {
const adapter = getChannelOnboardingAdapter(channel);
if (!adapter) {
throw new Error(`missing onboarding adapter for ${channel}`);
}
const keys = Object.keys(patch) as K[];
const previous = {} as Pick<ChannelOnboardingAdapter, K>;
for (const key of keys) {
previous[key] = adapter[key];
adapter[key] = patch[key];
}
return () => {
for (const key of keys) {
adapter[key] = previous[key];
}
};
}

View File

@@ -3,11 +3,11 @@ import type { OpenClawConfig } from "../config/config.js";
import { createEmptyPluginRegistry } from "../plugins/registry.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import type { WizardPrompter } from "../wizard/prompts.js";
import { setDefaultChannelPluginRegistryForTests } from "./channel-test-helpers.js";
import {
patchChannelOnboardingAdapter,
setDefaultChannelPluginRegistryForTests,
} from "./channel-test-helpers.js";
import { setupChannels } from "./onboard-channels.js";
import type { ChannelChoice } from "./onboard-types.js";
import { getChannelOnboardingAdapter } from "./onboarding/registry.js";
import type { ChannelOnboardingAdapter } from "./onboarding/types.js";
import { createExitThrowingRuntime, createWizardPrompter } from "./test-wizard-helpers.js";
function createPrompter(overrides: Partial<WizardPrompter>): WizardPrompter {
@@ -31,27 +31,6 @@ function createUnexpectedPromptGuards() {
};
}
function patchOnboardingAdapter<K extends keyof ChannelOnboardingAdapter>(
channel: ChannelChoice,
patch: Pick<ChannelOnboardingAdapter, K>,
): () => void {
const adapter = getChannelOnboardingAdapter(channel);
if (!adapter) {
throw new Error(`missing onboarding adapter for ${channel}`);
}
const keys = Object.keys(patch) as K[];
const previous = {} as Pick<ChannelOnboardingAdapter, K>;
for (const key of keys) {
previous[key] = adapter[key];
adapter[key] = patch[key];
}
return () => {
for (const key of keys) {
adapter[key] = previous[key];
}
};
}
vi.mock("node:fs/promises", () => ({
default: {
access: vi.fn(async () => {
@@ -284,7 +263,7 @@ describe("setupChannels", () => {
const selection = vi.fn();
const onAccountId = vi.fn();
const configureInteractive = vi.fn(async () => "skip" as const);
const restore = patchOnboardingAdapter("telegram", {
const restore = patchChannelOnboardingAdapter("telegram", {
getStatus: vi.fn(async ({ cfg }) => ({
channel: "telegram",
configured: Boolean(cfg.channels?.telegram?.botToken),
@@ -342,7 +321,7 @@ describe("setupChannels", () => {
const configure = vi.fn(async () => {
throw new Error("configure should not be called when configureInteractive is present");
});
const restore = patchOnboardingAdapter("telegram", {
const restore = patchChannelOnboardingAdapter("telegram", {
getStatus: vi.fn(async ({ cfg }) => ({
channel: "telegram",
configured: Boolean(cfg.channels?.telegram?.botToken),
@@ -402,7 +381,7 @@ describe("setupChannels", () => {
"configure should not be called when configureWhenConfigured handles updates",
);
});
const restore = patchOnboardingAdapter("telegram", {
const restore = patchChannelOnboardingAdapter("telegram", {
getStatus: vi.fn(async ({ cfg }) => ({
channel: "telegram",
configured: Boolean(cfg.channels?.telegram?.botToken),
@@ -441,6 +420,9 @@ describe("setupChannels", () => {
);
expect(configureWhenConfigured).toHaveBeenCalledTimes(1);
expect(configureWhenConfigured).toHaveBeenCalledWith(
expect.objectContaining({ configured: true, label: expect.any(String) }),
);
expect(configure).not.toHaveBeenCalled();
expect(selection).toHaveBeenCalledWith(["telegram"]);
expect(onAccountId).toHaveBeenCalledWith("telegram", "acct-2");

View File

@@ -540,6 +540,8 @@ export async function setupChannels(
accountOverrides,
shouldPromptAccountIds,
forceAllowFrom: forceAllowFromChannels.has(channel),
configured: true,
label,
});
if (!(await applyCustomOnboardingResult(channel, custom))) {
return;