Skip to content
Merged
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
109 changes: 98 additions & 11 deletions packages/cli/src/__tests__/commands/docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ui } from "../../util/terminal-ui.js";

const mockGetDocsDir = vi.fn<() => Promise<string>>();
const mockGetPhases = vi.fn<() => Promise<string[]>>();
const mockCopyFeatureDocTemplates = vi.fn<(...args: unknown[]) => Promise<any>>();
const mockCopyFeatureDocTemplates =
vi.fn<(...args: unknown[]) => Promise<any>>();
const mockTemplateManagerConstructor = vi.fn();

vi.mock("../../lib/Config.js", () => ({
Expand All @@ -20,7 +21,8 @@ vi.mock("../../lib/TemplateManager.js", () => ({
TemplateManager: vi.fn(function (...args: unknown[]) {
mockTemplateManagerConstructor(...args);
return {
copyFeatureDocTemplates: (...copyArgs: unknown[]) => mockCopyFeatureDocTemplates(...copyArgs),
copyFeatureDocTemplates: (...copyArgs: unknown[]) =>
mockCopyFeatureDocTemplates(...copyArgs),
};
}),
}));
Expand All @@ -33,6 +35,15 @@ vi.mock("../../util/terminal-ui.js", () => ({
},
}));

vi.mock("chalk", () => ({
default: {
dim: (text: string) => `[dim]${text}[/dim]`,
green: (text: string) => text,
red: (text: string) => text,
yellow: (text: string) => text,
},
}));

describe("docs command", () => {
const mockedUi = vi.mocked(ui);

Expand Down Expand Up @@ -61,14 +72,61 @@ describe("docs command", () => {
const program = new Command();
registerDocsCommand(program);

await program.parseAsync(["node", "test", "docs", "init-feature", "sample"]);
await program.parseAsync([
"node",
"test",
"docs",
"init-feature",
"sample",
]);

expect(mockCopyFeatureDocTemplates).toHaveBeenCalledWith("sample", {
date: "2026-05-25",
phases: ["requirements", "design"],
});
expect(mockedUi.success).toHaveBeenCalledWith("Created 1 feature doc(s) for sample.");
expect(mockedUi.text).toHaveBeenCalledWith("docs/ai/requirements/2026-05-25-feature-sample.md");
expect(mockedUi.success).toHaveBeenCalledWith(
"Created 1 feature doc for sample.",
);
expect(mockedUi.text).toHaveBeenCalledWith(
"[dim] - docs/ai/requirements/2026-05-25-feature-sample.md[/dim]",
);
});

it("pluralizes the success headline for multiple generated feature docs", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 4, 25, 10, 30));
mockCopyFeatureDocTemplates.mockResolvedValue([
{
phase: "requirements",
path: "/repo/docs/ai/requirements/2026-05-25-feature-sample.md",
relativePath: "docs/ai/requirements/2026-05-25-feature-sample.md",
},
{
phase: "design",
path: "/repo/docs/ai/design/2026-05-25-feature-sample.md",
relativePath: "docs/ai/design/2026-05-25-feature-sample.md",
},
]);
const program = new Command();
registerDocsCommand(program);

await program.parseAsync([
"node",
"test",
"docs",
"init-feature",
"sample",
]);

expect(mockedUi.success).toHaveBeenCalledWith(
"Created 2 feature docs for sample.",
);
expect(mockedUi.text).toHaveBeenCalledWith(
"[dim] - docs/ai/requirements/2026-05-25-feature-sample.md[/dim]",
);
expect(mockedUi.text).toHaveBeenCalledWith(
"[dim] - docs/ai/design/2026-05-25-feature-sample.md[/dim]",
);
});

it("uses the current local date", async () => {
Expand All @@ -77,7 +135,13 @@ describe("docs command", () => {
const program = new Command();
registerDocsCommand(program);

await program.parseAsync(["node", "test", "docs", "init-feature", "sample"]);
await program.parseAsync([
"node",
"test",
"docs",
"init-feature",
"sample",
]);

expect(mockCopyFeatureDocTemplates).toHaveBeenCalledWith("sample", {
date: "2026-05-25",
Expand All @@ -91,7 +155,14 @@ describe("docs command", () => {
const program = new Command();
registerDocsCommand(program);

await program.parseAsync(["node", "test", "docs", "init-feature", "feature-sample", "--json"]);
await program.parseAsync([
"node",
"test",
"docs",
"init-feature",
"feature-sample",
"--json",
]);

expect(mockedUi.text).toHaveBeenCalledWith(
JSON.stringify(
Expand All @@ -117,21 +188,37 @@ describe("docs command", () => {
const program = new Command();
registerDocsCommand(program);

await program.parseAsync(["node", "test", "docs", "init-feature", "bad name"]);
await program.parseAsync([
"node",
"test",
"docs",
"init-feature",
"bad name",
]);

expect(process.exitCode).toBe(1);
expect(mockCopyFeatureDocTemplates).not.toHaveBeenCalled();
expect(mockedUi.error).toHaveBeenCalledWith("Invalid feature name: bad name");
expect(mockedUi.error).toHaveBeenCalledWith(
"Invalid feature name: bad name",
);
});

it("surfaces copy errors and sets a non-zero exit code", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 4, 25, 10, 30));
mockCopyFeatureDocTemplates.mockRejectedValue(new Error("Feature docs already exist"));
mockCopyFeatureDocTemplates.mockRejectedValue(
new Error("Feature docs already exist"),
);
const program = new Command();
registerDocsCommand(program);

await program.parseAsync(["node", "test", "docs", "init-feature", "sample"]);
await program.parseAsync([
"node",
"test",
"docs",
"init-feature",
"sample",
]);

expect(process.exitCode).toBe(1);
expect(mockedUi.error).toHaveBeenCalledWith("Feature docs already exist");
Expand Down
Loading
Loading