File size: 8,015 Bytes
34810d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | // Exercise Commander, auth dispatch, and actual owner resolution with local I/O seams.
import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelPluginCatalogEntry } from "../channels/plugins/catalog.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { registerChannelsCli } from "./channels-cli.js";
const fixture = vi.hoisted(() => ({
config: {} as OpenClawConfig,
registered: true,
login: vi.fn(),
logout: vi.fn(async () => ({ cleared: false })),
catalog: vi.fn<(...args: unknown[]) => ChannelPluginCatalogEntry[]>(() => []),
loadScoped: vi.fn(),
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
}));
vi.mock("../config/config.js", () => ({ getRuntimeConfig: () => fixture.config }));
vi.mock("../commands/config-validation.js", () => ({
requireValidConfigForWrite: async () => ({
snapshot: { sourceConfig: fixture.config, hash: "fixture" },
writeOptions: {},
}),
}));
vi.mock("../config/plugin-auto-enable.js", () => ({
applyPluginAutoEnable: ({ config }: { config: OpenClawConfig }) => ({ config, changes: [] }),
}));
vi.mock("../channels/plugins/catalog.js", () => ({
listRawChannelPluginCatalogEntries: fixture.catalog,
getChannelPluginCatalogEntry: () => undefined,
}));
vi.mock("../channels/plugins/index.js", () => ({
normalizeChannelId: (id: string) => id,
getLoadedChannelPlugin: () => (fixture.registered ? plugin : undefined),
listChannelPlugins: () => [plugin],
}));
vi.mock("../commands/channel-setup/plugin-install.js", () => ({
loadChannelSetupPluginRegistrySnapshotForChannel: fixture.loadScoped,
ensureChannelSetupPluginInstalled: vi.fn(),
}));
vi.mock("../gateway/call.js", () => ({
callGateway: async () => {
throw new Error("isolated fixture has no Gateway");
},
}));
vi.mock("../runtime.js", () => ({
defaultRuntime: fixture.runtime,
ExitError: class extends Error {},
}));
const plugin = {
id: "fixture-chat",
auth: { login: fixture.login },
gateway: { logoutAccount: fixture.logout },
config: {
listAccountIds: () => ["work"],
resolveAccount: () => ({ accountId: "work" }),
},
};
async function runAuth(mode: string, parent: string[] = [], leaf: string[] = []) {
const args = ["channels", ...parent, mode, ...leaf, "--channel", plugin.id, "--account", "work"];
const program = new Command()
.name("openclaw")
.enablePositionalOptions()
.exitOverride()
.configureOutput({ writeErr: () => undefined });
await registerChannelsCli(program, ["node", "openclaw", ...args]);
await program.parseAsync(args, { from: "user" });
}
describe.each(["login", "logout"])("channels %s owner", (mode) => {
beforeEach(() => {
vi.clearAllMocks();
fixture.registered = true;
fixture.config = {
agents: {
ownership: "explicit",
entries: {
research: { workspace: "/tmp/research-workspace" },
ops: { workspace: "/tmp/ops-workspace" },
},
},
};
fixture.catalog.mockReturnValue([
{
id: plugin.id,
pluginId: plugin.id,
origin: "bundled",
meta: {
id: plugin.id,
label: "Fixture",
selectionLabel: "Fixture",
docsPath: "",
blurb: "",
},
install: { npmSpec: "fixture-chat" },
},
]);
fixture.loadScoped.mockReturnValue({ channels: [{ plugin }], channelSetups: [] });
});
it.each([
{ name: "parent", parent: ["--agent", "ops"], leaf: [], owner: "ops" },
{ name: "leaf", parent: [], leaf: ["--agent", "ops"], owner: "ops" },
{
name: "leaf overrides parent",
parent: ["--agent", "research"],
leaf: ["--agent", "ops"],
owner: "ops",
},
{ name: "System Agent", parent: [], leaf: [], owner: "research" },
{ name: "trimmed explicit", parent: ["--agent", " ops "], leaf: [], owner: "ops" },
{ name: "explicit before System Agent", parent: ["--agent", "ops"], leaf: [], owner: "ops" },
])("retains the $name owner through plugin discovery", async ({ parent, leaf, owner, name }) => {
if (name === "System Agent") {
fixture.config.agents!.defaults = { systemAgent: { agentId: "research" } };
} else if (name === "explicit before System Agent") {
fixture.config.agents!.defaults = { systemAgent: { agentId: "???" } };
}
fixture.registered = false;
await runAuth(mode, parent, leaf);
expect(fixture.runtime.error).not.toHaveBeenCalled();
expect(fixture.loadScoped).toHaveBeenCalledWith(
expect.objectContaining({ workspaceDir: `/tmp/${owner}-workspace` }),
);
expect(mode === "login" ? fixture.login : fixture.logout).toHaveBeenCalledWith(
expect.objectContaining({ accountId: "work" }),
);
});
it("does not require discovery to reload an already registered plugin", async () => {
await runAuth(mode, ["--agent", "ops"]);
expect(fixture.runtime.error).not.toHaveBeenCalled();
expect(fixture.catalog).toHaveBeenCalledWith(
expect.objectContaining({ workspaceDir: "/tmp/ops-workspace" }),
);
expect(fixture.loadScoped).not.toHaveBeenCalled();
expect(mode === "login" ? fixture.login : fixture.logout).toHaveBeenCalled();
});
it.each([undefined, "missing"])(
"keeps an ownerless fleet from auth with designation %s",
async (agentId) => {
fixture.config.agents!.defaults = { systemAgent: { agentId } };
// Agent selection is an expected CLI condition rendered by the root failure
// owner; the command rethrows instead of printing its own copy.
await expect(runAuth(mode)).rejects.toMatchObject({
name: "AgentSelectionRequiredError",
message: expect.stringContaining("no explicit owner"),
});
expect(fixture.runtime.error).not.toHaveBeenCalled();
expect(fixture.runtime.exit).not.toHaveBeenCalled();
expect(fixture.catalog).not.toHaveBeenCalled();
expect(fixture.loadScoped).not.toHaveBeenCalled();
expect(fixture.login).not.toHaveBeenCalled();
expect(fixture.logout).not.toHaveBeenCalled();
},
);
it.each([
{ agent: "missing", error: 'Unknown agent id "missing"' },
{ agent: " ", error: "--agent must not be blank" },
])("rejects invalid explicit agent '$agent' before discovery", async ({ agent, error }) => {
fixture.config.agents!.defaults = { systemAgent: { agentId: "research" } };
await runAuth(mode, ["--agent", agent]);
expect(fixture.runtime.error).toHaveBeenCalledWith(expect.stringContaining(error));
expect(fixture.catalog).not.toHaveBeenCalled();
expect(fixture.login).not.toHaveBeenCalled();
expect(fixture.logout).not.toHaveBeenCalled();
});
it.each(["???", "OPS"])(
"rejects explicit owner %s instead of normalizing it into another agent",
async (agent) => {
fixture.config.agents!.entries = {
main: { workspace: "/tmp/main-workspace" },
ops: { workspace: "/tmp/ops-workspace" },
};
await runAuth(mode, ["--agent", agent]);
expect(fixture.runtime.error).toHaveBeenCalledWith(
expect.stringContaining(`Unknown agent id "${agent}"`),
);
expect(fixture.runtime.exit).toHaveBeenCalledWith(1);
expect(fixture.catalog).not.toHaveBeenCalled();
expect(fixture.login).not.toHaveBeenCalled();
expect(fixture.logout).not.toHaveBeenCalled();
},
);
it("keeps the legacy owner when the configured System Agent is unused", async () => {
fixture.config.agents!.ownership = undefined;
fixture.config.agents!.entries!.research!.default = true;
fixture.config.agents!.defaults = { systemAgent: { agentId: "???" } };
await runAuth(mode);
expect(fixture.runtime.error).not.toHaveBeenCalled();
expect(fixture.catalog).toHaveBeenCalledWith(
expect.objectContaining({ workspaceDir: "/tmp/research-workspace" }),
);
expect(mode === "login" ? fixture.login : fixture.logout).toHaveBeenCalled();
});
});
|