Skip to content

Commit 9755241

Browse files
committed
fix(cli): reject partial numeric options
1 parent 163df25 commit 9755241

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

src/commands/channels.add.test.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ describe("channelsAddCommand", () => {
880880
expect(runtime.exit).not.toHaveBeenCalled();
881881
});
882882

883-
it("drops malformed numeric channel setup options before plugin setup", async () => {
883+
it("rejects malformed numeric channel setup options before plugin setup", async () => {
884884
const applyAccountConfig = vi.fn(({ cfg, input }: ApplyAccountConfigParams) => ({
885885
...cfg,
886886
channels: {
@@ -898,21 +898,19 @@ describe("channelsAddCommand", () => {
898898
configMocks.readConfigFileSnapshot.mockResolvedValue({ ...baseConfigSnapshot });
899899
setActivePluginRegistry(createTestRegistry([{ pluginId: "matrix", plugin, source: "test" }]));
900900

901-
await channelsAddCommand(
902-
{
903-
channel: "matrix",
904-
initialSyncLimit: "10x",
905-
},
906-
runtime,
907-
{ hasFlags: true },
908-
);
901+
await expect(
902+
channelsAddCommand(
903+
{
904+
channel: "matrix",
905+
initialSyncLimit: "10x",
906+
},
907+
runtime,
908+
{ hasFlags: true },
909+
),
910+
).rejects.toThrow("--initial-sync-limit must be a non-negative integer.");
909911

910-
expect(applyAccountConfig).toHaveBeenCalledTimes(1);
911-
expect(
912-
(applyAccountConfig.mock.calls[0]?.[0] as ApplyAccountConfigParams | undefined)?.input
913-
.initialSyncLimit,
914-
).toBeUndefined();
915-
expect(writtenChannel("matrix").initialSyncLimit).toBeUndefined();
912+
expect(applyAccountConfig).not.toHaveBeenCalled();
913+
expect(configMocks.writeConfigFile).not.toHaveBeenCalled();
916914
});
917915

918916
it("falls back from untrusted workspace catalog shadows when adding by alias", async () => {

src/commands/channels/add.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ async function resolveCatalogChannelEntry(raw: string, cfg: OpenClawConfig | nul
8080
});
8181
}
8282

83-
function parseOptionalInt(value: unknown): number | undefined {
84-
return parseStrictNonNegativeInteger(value);
83+
function parseOptionalInt(value: unknown, flag: string): number | undefined {
84+
if (value === undefined || value === null || value === "") {
85+
return undefined;
86+
}
87+
const parsed = parseStrictNonNegativeInteger(value);
88+
if (parsed === undefined) {
89+
throw new Error(`${flag} must be a non-negative integer.`);
90+
}
91+
return parsed;
8592
}
8693

8794
function parseOptionalDelimitedInput(value: unknown): string[] | undefined {
@@ -111,7 +118,7 @@ function buildChannelSetupInput(opts: ChannelsAddOptions): ChannelSetupInput {
111118
input.secretFile ??= readOptionalString(input.tokenFile);
112119
}
113120

114-
input.initialSyncLimit = parseOptionalInt(opts.initialSyncLimit);
121+
input.initialSyncLimit = parseOptionalInt(opts.initialSyncLimit, "--initial-sync-limit");
115122
input.groupChannels = parseOptionalDelimitedInput(opts.groupChannels);
116123
input.dmAllowlist = parseOptionalDelimitedInput(opts.dmAllowlist);
117124
return input as ChannelSetupInput;

0 commit comments

Comments
 (0)