Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/cli/src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ const accent = chalk.rgb(131, 127, 255);
const args = process.argv.slice(3);
let limit = 20;
const tailIdx = args.indexOf("--tail");
if (tailIdx !== -1 && args[tailIdx + 1]) {
limit = parseInt(args[tailIdx + 1], 10) || 20;
if (tailIdx !== -1) {
const rawLimit = args[tailIdx + 1];
if (!rawLimit || !/^\d+$/.test(rawLimit) || Number(rawLimit) < 1) {
console.log(chalk.red("Invalid --tail value. Expected a positive integer."));
process.exit(1);
}
limit = Number(rawLimit);
}

const config = loadConfig();
Expand Down
50 changes: 50 additions & 0 deletions src/__tests__/cli-logs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const originalArgv = process.argv.slice();
const originalExit = process.exit;

describe("automaton-cli logs validation", () => {
beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();
});

afterEach(() => {
process.argv = originalArgv.slice();
process.exit = originalExit;
vi.restoreAllMocks();
vi.resetModules();
});

it.each(["0", "-1", "abc"])("rejects invalid --tail value %s", async (value) => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi
.spyOn(process, "exit")
.mockImplementation(((code?: number) => {
throw new Error(`process.exit:${code ?? 0}`);
}) as typeof process.exit);

process.argv = ["node", "automaton-cli", "logs", "--tail", value];

await expect(import("../../packages/cli/src/commands/logs.ts")).rejects.toThrow("process.exit:1");

expect(exitSpy).toHaveBeenCalledWith(1);
expect(logSpy).toHaveBeenCalledWith("Invalid --tail value. Expected a positive integer.");
});

it("rejects missing --tail value", async () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi
.spyOn(process, "exit")
.mockImplementation(((code?: number) => {
throw new Error(`process.exit:${code ?? 0}`);
}) as typeof process.exit);

process.argv = ["node", "automaton-cli", "logs", "--tail"];

await expect(import("../../packages/cli/src/commands/logs.ts")).rejects.toThrow("process.exit:1");

expect(exitSpy).toHaveBeenCalledWith(1);
expect(logSpy).toHaveBeenCalledWith("Invalid --tail value. Expected a positive integer.");
});
});