From a232dcbfbb93e6953989c980b5e189faac118b3b Mon Sep 17 00:00:00 2001 From: w287346141 <57440615+w287346141@users.noreply.github.com> Date: Mon, 1 Jun 2026 11:46:11 +0800 Subject: [PATCH] Validate prune session days --- src/cli/commands/prune-sessions.ts | 3 ++- tests/prune-sessions-command.test.ts | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/prune-sessions-command.test.ts diff --git a/src/cli/commands/prune-sessions.ts b/src/cli/commands/prune-sessions.ts index 9a9ee8e..14f9c8e 100644 --- a/src/cli/commands/prune-sessions.ts +++ b/src/cli/commands/prune-sessions.ts @@ -7,9 +7,10 @@ export interface PruneSessionsOptions { export function pruneSessionsCommand(opts: PruneSessionsOptions): void { const days = opts.days ?? 90; - if (!Number.isFinite(days) || days < 1) { + if (!Number.isInteger(days) || days < 1) { console.error(`--days must be a positive integer (got ${days}).`); process.exit(1); + return; } if (opts.dryRun) { const cutoff = Date.now() - days * 24 * 60 * 60 * 1000; diff --git a/tests/prune-sessions-command.test.ts b/tests/prune-sessions-command.test.ts new file mode 100644 index 0000000..157bf1a --- /dev/null +++ b/tests/prune-sessions-command.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it, vi } from "vitest"; +import { pruneSessionsCommand } from "../src/cli/commands/prune-sessions.js"; + +describe("pruneSessionsCommand", () => { + it("rejects fractional --days values", () => { + const err = vi.spyOn(console, "error").mockImplementation(() => {}); + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + const exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never); + + pruneSessionsCommand({ days: 1.5, dryRun: true }); + + expect(err).toHaveBeenCalledWith("--days must be a positive integer (got 1.5)."); + expect(log).not.toHaveBeenCalled(); + expect(exit).toHaveBeenCalledWith(1); + + err.mockRestore(); + log.mockRestore(); + exit.mockRestore(); + }); +});