77 setRuntimeConfigSnapshot,
88} from "openclaw/plugin-sdk/runtime-config-snapshot";
99import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
10- import { nativeCommandRuntime } from "./native-command.runtime.js";
1110
1211const { loadModelCatalogMock, logVerboseMock } = vi.hoisted(() => ({
1312 loadModelCatalogMock: vi.fn(),
@@ -435,78 +434,73 @@ describe("createDiscordNativeCommand option wiring", () => {
435434 });
436435
437436 it("keeps plugin command autocomplete aligned with dispatch owner checks", async () => {
438- const restoreMatchPluginCommand = nativeCommandRuntime.matchPluginCommand;
439- nativeCommandRuntime.matchPluginCommand = (prompt) =>
440- prompt === "/pair" ? ({ command: { name: "pair" }, args: "" } as never) : null;
441- try {
442- const command = createDiscordNativeCommand({
443- command: {
444- name: "pair",
445- description : "Pair ",
446- acceptsArgs: true ,
447- args: [
448- {
449- name: "mode",
450- description : "Pairing mode" ,
451- type : "string" ,
452- preferAutocomplete: true ,
453- choices: () => [
454- { label: "fast", value: "fast" } ,
455- { label: "secure", value: "secure" } ,
456- ],
457- } ,
458- ],
459- },
460- cfg: createAllowedGuildAutocompleteConfig({
461- ownerAllowFrom: ["user:owner-user"] ,
437+ const command = createDiscordNativeCommand({
438+ command: {
439+ name: "pair",
440+ description: "Pair",
441+ acceptsArgs: true,
442+ args: [
443+ {
444+ name : "mode ",
445+ description: "Pairing mode" ,
446+ type: "string",
447+ preferAutocomplete: true,
448+ choices: () => [
449+ { label : "fast", value: "fast" } ,
450+ { label : "secure", value: "secure" } ,
451+ ] ,
452+ },
453+ ] ,
454+ requireAuth: true ,
455+ prepareDispatch: () => ({
456+ kind: "plugin" as const ,
457+ invocation: {
458+ runtime: { execute: vi.fn() },
459+ selection: Object.freeze({}),
460+ } ,
462461 }),
463- discordConfig: {
464- groupPolicy: "allowlist",
465- guilds: {
466- "guild-1": {
467- channels: {
468- "channel-1": {
469- enabled: true,
470- requireMention: false,
471- },
462+ } as never,
463+ cfg: createAllowedGuildAutocompleteConfig({
464+ ownerAllowFrom: ["user:owner-user"],
465+ }),
466+ discordConfig: {
467+ groupPolicy: "allowlist",
468+ guilds: {
469+ "guild-1": {
470+ channels: {
471+ "channel-1": {
472+ enabled: true,
473+ requireMention: false,
472474 },
473475 },
474476 },
475477 },
476- accountId: "default",
477- sessionPrefix: "discord:slash",
478- ephemeralDefault: true,
479- threadBindings: createNoopThreadBindingManager("default"),
480- });
481- const mode = requireOption(command, "mode");
482- const autocomplete = requireAutocomplete(
483- mode,
484- "plugin mode option did not wire autocomplete",
485- );
486- const respond = await runAutocomplete(autocomplete, {
487- userId: "blocked-user",
488- username: "blocked",
489- globalName: "Blocked",
490- channelType: ChannelType.GuildText,
491- channelId: "channel-1",
492- channelName: "general",
493- guildId: "guild-1",
494- focusedValue: "",
495- });
496-
497- expect(respond).toHaveBeenCalledWith([
498- { name: "fast", value: "fast" },
499- { name: "secure", value: "secure" },
500- ]);
501- } finally {
502- nativeCommandRuntime.matchPluginCommand = restoreMatchPluginCommand;
503- }
478+ },
479+ accountId: "default",
480+ sessionPrefix: "discord:slash",
481+ ephemeralDefault: true,
482+ threadBindings: createNoopThreadBindingManager("default"),
483+ });
484+ const mode = requireOption(command, "mode");
485+ const autocomplete = requireAutocomplete(mode, "plugin mode option did not wire autocomplete");
486+ const respond = await runAutocomplete(autocomplete, {
487+ userId: "blocked-user",
488+ username: "blocked",
489+ globalName: "Blocked",
490+ channelType: ChannelType.GuildText,
491+ channelId: "channel-1",
492+ channelName: "general",
493+ guildId: "guild-1",
494+ focusedValue: "",
495+ });
496+
497+ expect(respond).toHaveBeenCalledWith([
498+ { name: "fast", value: "fast" },
499+ { name: "secure", value: "secure" },
500+ ]);
504501 });
505502
506503 it("refreshes autocomplete authorization and dynamic choices between invocations", async () => {
507- const restoreMatchPluginCommand = nativeCommandRuntime.matchPluginCommand;
508- nativeCommandRuntime.matchPluginCommand = (prompt) =>
509- prompt === "/scope" ? ({ command: { name: "scope" }, args: "" } as never) : null;
510504 const sourceCfg = {
511505 session: { dmScope: "main" },
512506 channels: {
@@ -526,56 +520,57 @@ describe("createDiscordNativeCommand option wiring", () => {
526520 },
527521 },
528522 } as OpenClawConfig;
529- try {
530- const command = createDiscordNativeCommand({
531- command: {
532- name: "scope",
533- description: "Scope",
534- acceptsArgs: true,
535- args: [
536- {
537- name: "value",
538- description: "Scope value",
539- type: "string",
540- preferAutocomplete: true,
541- choices: ({ cfg }) => {
542- const dmScope = cfg?.session?.dmScope ?? "missing";
543- return [{ label: dmScope, value: dmScope }];
544- },
523+ const command = createDiscordNativeCommand({
524+ command: {
525+ name: "scope",
526+ description: "Scope",
527+ acceptsArgs: true,
528+ args: [
529+ {
530+ name: "value",
531+ description: "Scope value",
532+ type: "string",
533+ preferAutocomplete: true,
534+ choices: ({ cfg }: { cfg?: OpenClawConfig }) => {
535+ const dmScope = cfg?.session?.dmScope ?? "missing";
536+ return [{ label: dmScope, value: dmScope }];
545537 },
546- ],
547- },
548- cfg: sourceCfg,
549- discordConfig: sourceCfg.channels?.discord ?? {},
550- accountId: "default",
551- sessionPrefix: "discord:slash",
552- ephemeralDefault: true,
553- threadBindings: createNoopThreadBindingManager("default"),
554- });
555- const value = requireOption(command, "value");
556- const autocomplete = requireAutocomplete(
557- value,
558- "scope value option did not wire autocomplete",
559- );
560- const autocompleteParams = {
561- userId: "owner",
562- channelType: ChannelType.DM,
563- channelId: "dm-1",
564- channelName: "dm-1",
565- focusedValue: "",
566- } as const;
567-
568- const blockedRespond = await runAutocomplete(autocomplete, autocompleteParams);
569- expect(blockedRespond).toHaveBeenCalledWith([]);
570-
571- setRuntimeConfigSnapshot(runtimeCfg, runtimeCfg);
572- const refreshedRespond = await runAutocomplete(autocomplete, autocompleteParams);
573- expect(refreshedRespond).toHaveBeenCalledWith([
574- { name: "per-channel-peer", value: "per-channel-peer" },
575- ]);
576- } finally {
577- nativeCommandRuntime.matchPluginCommand = restoreMatchPluginCommand;
578- }
538+ },
539+ ],
540+ requireAuth: true,
541+ prepareDispatch: () => ({
542+ kind: "plugin" as const,
543+ invocation: {
544+ runtime: { execute: vi.fn() },
545+ selection: Object.freeze({}),
546+ },
547+ }),
548+ } as never,
549+ cfg: sourceCfg,
550+ discordConfig: sourceCfg.channels?.discord ?? {},
551+ accountId: "default",
552+ sessionPrefix: "discord:slash",
553+ ephemeralDefault: true,
554+ threadBindings: createNoopThreadBindingManager("default"),
555+ });
556+ const value = requireOption(command, "value");
557+ const autocomplete = requireAutocomplete(value, "scope value option did not wire autocomplete");
558+ const autocompleteParams = {
559+ userId: "owner",
560+ channelType: ChannelType.DM,
561+ channelId: "dm-1",
562+ channelName: "dm-1",
563+ focusedValue: "",
564+ } as const;
565+
566+ const blockedRespond = await runAutocomplete(autocomplete, autocompleteParams);
567+ expect(blockedRespond).toHaveBeenCalledWith([]);
568+
569+ setRuntimeConfigSnapshot(runtimeCfg, runtimeCfg);
570+ const refreshedRespond = await runAutocomplete(autocomplete, autocompleteParams);
571+ expect(refreshedRespond).toHaveBeenCalledWith([
572+ { name: "per-channel-peer", value: "per-channel-peer" },
573+ ]);
579574 });
580575
581576 it("returns no autocomplete choices outside the Discord allowlist when commands.useAccessGroups is false and commands.allowFrom is not configured", async () => {
0 commit comments