refactor: install optional channels for directory
This commit is contained in:
parent
06845a1974
commit
ba1bb8505f
105
src/cli/directory-cli.test.ts
Normal file
105
src/cli/directory-cli.test.ts
Normal file
@ -0,0 +1,105 @@
|
||||
import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { registerDirectoryCli } from "./directory-cli.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
loadConfig: vi.fn(),
|
||||
writeConfigFile: vi.fn(),
|
||||
resolveInstallableChannelPlugin: vi.fn(),
|
||||
resolveMessageChannelSelection: vi.fn(),
|
||||
getChannelPlugin: vi.fn(),
|
||||
resolveChannelDefaultAccountId: vi.fn(),
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../config/config.js", () => ({
|
||||
loadConfig: mocks.loadConfig,
|
||||
writeConfigFile: mocks.writeConfigFile,
|
||||
}));
|
||||
|
||||
vi.mock("../commands/channel-setup/channel-plugin-resolution.js", () => ({
|
||||
resolveInstallableChannelPlugin: mocks.resolveInstallableChannelPlugin,
|
||||
}));
|
||||
|
||||
vi.mock("../infra/outbound/channel-selection.js", () => ({
|
||||
resolveMessageChannelSelection: mocks.resolveMessageChannelSelection,
|
||||
}));
|
||||
|
||||
vi.mock("../channels/plugins/index.js", () => ({
|
||||
getChannelPlugin: mocks.getChannelPlugin,
|
||||
}));
|
||||
|
||||
vi.mock("../channels/plugins/helpers.js", () => ({
|
||||
resolveChannelDefaultAccountId: mocks.resolveChannelDefaultAccountId,
|
||||
}));
|
||||
|
||||
vi.mock("../runtime.js", () => ({
|
||||
defaultRuntime: {
|
||||
log: (...args: unknown[]) => mocks.log(...args),
|
||||
error: (...args: unknown[]) => mocks.error(...args),
|
||||
exit: (...args: unknown[]) => mocks.exit(...args),
|
||||
},
|
||||
}));
|
||||
|
||||
describe("registerDirectoryCli", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.loadConfig.mockReturnValue({ channels: {} });
|
||||
mocks.writeConfigFile.mockResolvedValue(undefined);
|
||||
mocks.resolveChannelDefaultAccountId.mockReturnValue("default");
|
||||
mocks.resolveMessageChannelSelection.mockResolvedValue({
|
||||
channel: "slack",
|
||||
configured: ["slack"],
|
||||
source: "explicit",
|
||||
});
|
||||
mocks.exit.mockImplementation((code?: number) => {
|
||||
throw new Error(`exit:${code ?? 0}`);
|
||||
});
|
||||
});
|
||||
|
||||
it("installs an explicit optional directory channel on demand", async () => {
|
||||
const self = vi.fn().mockResolvedValue({ id: "self-1", name: "Family Phone" });
|
||||
mocks.resolveInstallableChannelPlugin.mockResolvedValue({
|
||||
cfg: {
|
||||
channels: {},
|
||||
plugins: { entries: { whatsapp: { enabled: true } } },
|
||||
},
|
||||
channelId: "whatsapp",
|
||||
plugin: {
|
||||
id: "whatsapp",
|
||||
directory: { self },
|
||||
},
|
||||
configChanged: true,
|
||||
});
|
||||
|
||||
const program = new Command().name("openclaw");
|
||||
registerDirectoryCli(program);
|
||||
|
||||
await program.parseAsync(["directory", "self", "--channel", "whatsapp", "--json"], {
|
||||
from: "user",
|
||||
});
|
||||
|
||||
expect(mocks.resolveInstallableChannelPlugin).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
rawChannel: "whatsapp",
|
||||
allowInstall: true,
|
||||
}),
|
||||
);
|
||||
expect(mocks.writeConfigFile).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
plugins: { entries: { whatsapp: { enabled: true } } },
|
||||
}),
|
||||
);
|
||||
expect(self).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
accountId: "default",
|
||||
}),
|
||||
);
|
||||
expect(mocks.log).toHaveBeenCalledWith(
|
||||
JSON.stringify({ id: "self-1", name: "Family Phone" }, null, 2),
|
||||
);
|
||||
expect(mocks.error).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@ -1,7 +1,8 @@
|
||||
import type { Command } from "commander";
|
||||
import { resolveChannelDefaultAccountId } from "../channels/plugins/helpers.js";
|
||||
import { getChannelPlugin } from "../channels/plugins/index.js";
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import { resolveInstallableChannelPlugin } from "../commands/channel-setup/channel-plugin-resolution.js";
|
||||
import { loadConfig, writeConfigFile } from "../config/config.js";
|
||||
import { danger } from "../globals.js";
|
||||
import { resolveMessageChannelSelection } from "../infra/outbound/channel-selection.js";
|
||||
import { defaultRuntime } from "../runtime.js";
|
||||
@ -96,13 +97,32 @@ export function registerDirectoryCli(program: Command) {
|
||||
.option("--json", "Output JSON", false);
|
||||
|
||||
const resolve = async (opts: { channel?: string; account?: string }) => {
|
||||
const cfg = loadConfig();
|
||||
const selection = await resolveMessageChannelSelection({
|
||||
cfg,
|
||||
channel: opts.channel ?? null,
|
||||
});
|
||||
let cfg = loadConfig();
|
||||
const explicitChannel = opts.channel?.trim();
|
||||
const resolvedExplicit = explicitChannel
|
||||
? await resolveInstallableChannelPlugin({
|
||||
cfg,
|
||||
runtime: defaultRuntime,
|
||||
rawChannel: explicitChannel,
|
||||
allowInstall: true,
|
||||
supports: (plugin) => Boolean(plugin.directory),
|
||||
})
|
||||
: null;
|
||||
if (resolvedExplicit?.configChanged) {
|
||||
cfg = resolvedExplicit.cfg;
|
||||
await writeConfigFile(cfg);
|
||||
}
|
||||
const selection = explicitChannel
|
||||
? {
|
||||
channel: resolvedExplicit?.channelId,
|
||||
}
|
||||
: await resolveMessageChannelSelection({
|
||||
cfg,
|
||||
channel: opts.channel ?? null,
|
||||
});
|
||||
const channelId = selection.channel;
|
||||
const plugin = getChannelPlugin(channelId);
|
||||
const plugin =
|
||||
resolvedExplicit?.plugin ?? (channelId ? getChannelPlugin(channelId) : undefined);
|
||||
if (!plugin) {
|
||||
throw new Error(`Unsupported channel: ${String(channelId)}`);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user