mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 08:58:37 +00:00
Onboarding: unify hook context and share adapter test helper
This commit is contained in:
@@ -91,7 +91,7 @@ export type ChannelOnboardingAdapter = {
|
|||||||
ctx: ChannelOnboardingInteractiveContext,
|
ctx: ChannelOnboardingInteractiveContext,
|
||||||
) => Promise<ChannelOnboardingConfiguredResult>;
|
) => Promise<ChannelOnboardingConfiguredResult>;
|
||||||
configureWhenConfigured?: (
|
configureWhenConfigured?: (
|
||||||
ctx: ChannelOnboardingConfigureContext,
|
ctx: ChannelOnboardingInteractiveContext,
|
||||||
) => Promise<ChannelOnboardingConfiguredResult>;
|
) => Promise<ChannelOnboardingConfiguredResult>;
|
||||||
dmPolicy?: ChannelOnboardingDmPolicy;
|
dmPolicy?: ChannelOnboardingDmPolicy;
|
||||||
onAccountRecorded?: (accountId: string, options?: SetupChannelsOptions) => void;
|
onAccountRecorded?: (accountId: string, options?: SetupChannelsOptions) => void;
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
|
|||||||
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
|
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
|
||||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||||
import { createTestRegistry } from "../test-utils/channel-plugins.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 {
|
export function setDefaultChannelPluginRegistryForTests(): void {
|
||||||
const channels = [
|
const channels = [
|
||||||
@@ -18,3 +21,24 @@ export function setDefaultChannelPluginRegistryForTests(): void {
|
|||||||
] as unknown as Parameters<typeof createTestRegistry>[0];
|
] as unknown as Parameters<typeof createTestRegistry>[0];
|
||||||
setActivePluginRegistry(createTestRegistry(channels));
|
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];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ import type { OpenClawConfig } from "../config/config.js";
|
|||||||
import { createEmptyPluginRegistry } from "../plugins/registry.js";
|
import { createEmptyPluginRegistry } from "../plugins/registry.js";
|
||||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||||
import type { WizardPrompter } from "../wizard/prompts.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 { 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";
|
import { createExitThrowingRuntime, createWizardPrompter } from "./test-wizard-helpers.js";
|
||||||
|
|
||||||
function createPrompter(overrides: Partial<WizardPrompter>): WizardPrompter {
|
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", () => ({
|
vi.mock("node:fs/promises", () => ({
|
||||||
default: {
|
default: {
|
||||||
access: vi.fn(async () => {
|
access: vi.fn(async () => {
|
||||||
@@ -284,7 +263,7 @@ describe("setupChannels", () => {
|
|||||||
const selection = vi.fn();
|
const selection = vi.fn();
|
||||||
const onAccountId = vi.fn();
|
const onAccountId = vi.fn();
|
||||||
const configureInteractive = vi.fn(async () => "skip" as const);
|
const configureInteractive = vi.fn(async () => "skip" as const);
|
||||||
const restore = patchOnboardingAdapter("telegram", {
|
const restore = patchChannelOnboardingAdapter("telegram", {
|
||||||
getStatus: vi.fn(async ({ cfg }) => ({
|
getStatus: vi.fn(async ({ cfg }) => ({
|
||||||
channel: "telegram",
|
channel: "telegram",
|
||||||
configured: Boolean(cfg.channels?.telegram?.botToken),
|
configured: Boolean(cfg.channels?.telegram?.botToken),
|
||||||
@@ -342,7 +321,7 @@ describe("setupChannels", () => {
|
|||||||
const configure = vi.fn(async () => {
|
const configure = vi.fn(async () => {
|
||||||
throw new Error("configure should not be called when configureInteractive is present");
|
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 }) => ({
|
getStatus: vi.fn(async ({ cfg }) => ({
|
||||||
channel: "telegram",
|
channel: "telegram",
|
||||||
configured: Boolean(cfg.channels?.telegram?.botToken),
|
configured: Boolean(cfg.channels?.telegram?.botToken),
|
||||||
@@ -402,7 +381,7 @@ describe("setupChannels", () => {
|
|||||||
"configure should not be called when configureWhenConfigured handles updates",
|
"configure should not be called when configureWhenConfigured handles updates",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
const restore = patchOnboardingAdapter("telegram", {
|
const restore = patchChannelOnboardingAdapter("telegram", {
|
||||||
getStatus: vi.fn(async ({ cfg }) => ({
|
getStatus: vi.fn(async ({ cfg }) => ({
|
||||||
channel: "telegram",
|
channel: "telegram",
|
||||||
configured: Boolean(cfg.channels?.telegram?.botToken),
|
configured: Boolean(cfg.channels?.telegram?.botToken),
|
||||||
@@ -441,6 +420,9 @@ describe("setupChannels", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(configureWhenConfigured).toHaveBeenCalledTimes(1);
|
expect(configureWhenConfigured).toHaveBeenCalledTimes(1);
|
||||||
|
expect(configureWhenConfigured).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ configured: true, label: expect.any(String) }),
|
||||||
|
);
|
||||||
expect(configure).not.toHaveBeenCalled();
|
expect(configure).not.toHaveBeenCalled();
|
||||||
expect(selection).toHaveBeenCalledWith(["telegram"]);
|
expect(selection).toHaveBeenCalledWith(["telegram"]);
|
||||||
expect(onAccountId).toHaveBeenCalledWith("telegram", "acct-2");
|
expect(onAccountId).toHaveBeenCalledWith("telegram", "acct-2");
|
||||||
|
|||||||
@@ -540,6 +540,8 @@ export async function setupChannels(
|
|||||||
accountOverrides,
|
accountOverrides,
|
||||||
shouldPromptAccountIds,
|
shouldPromptAccountIds,
|
||||||
forceAllowFrom: forceAllowFromChannels.has(channel),
|
forceAllowFrom: forceAllowFromChannels.has(channel),
|
||||||
|
configured: true,
|
||||||
|
label,
|
||||||
});
|
});
|
||||||
if (!(await applyCustomOnboardingResult(channel, custom))) {
|
if (!(await applyCustomOnboardingResult(channel, custom))) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user