fix: address feedback

This commit is contained in:
Nate 2025-08-23 11:23:58 -07:00
parent 314758d040
commit 826182ceec
7 changed files with 42 additions and 26 deletions

View file

@ -1,5 +1,5 @@
import { vi } from "vitest";
export const processPromptOrRule = vi.fn<(spec: string) => Promise<string>>();
export const processRule = vi.fn<(rule: string) => Promise<string>>();
export const processPrompt = vi.fn<(prompt: string) => Promise<string>>();

View file

@ -1,13 +1,20 @@
import { describe, it, expect, beforeEach } from "vitest";
import { describe, it, expect, beforeEach, afterAll } from "vitest";
import { parseArgs, processPrompt } from "./args.js";
import { parseArgs, processPromptOrRule } from "./args.js";
describe("args --prompt flag", () => {
const originalArgv = process.argv;
beforeEach(() => {
// Reset process.argv for each test
process.argv = ["node", "script.js"];
});
afterAll(() => {
// Restore original process.argv after all tests
process.argv = originalArgv;
});
it("should parse single prompt from --prompt flag", () => {
process.argv = ["node", "script.js", "--prompt", "my-prompt"];
const result = parseArgs();
@ -43,7 +50,7 @@ describe("args --prompt flag", () => {
});
it("should process direct string content", async () => {
const result = await processPrompt("This is a direct prompt");
const result = await processPromptOrRule("This is a direct prompt");
expect(result).toBe("This is a direct prompt");
});
});

View file

@ -1,6 +1,6 @@
import { vi } from "vitest";
import { parseArgs, processRule } from "./args.js";
import { parseArgs, processPromptOrRule } from "./args.js";
describe("parseArgs", () => {
const originalArgv = process.argv;
@ -217,7 +217,7 @@ describe("parseArgs", () => {
});
});
describe("processRule (loadRuleFromHub integration)", () => {
describe("processPromptOrRule (loadRuleFromHub integration)", () => {
// Mock fetch for hub tests
const originalFetch = global.fetch;
const mockFetch = vi.fn() as any;
@ -251,7 +251,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(zipBuffer),
} as Response);
const result = await processRule("continuedev/sentry-nextjs");
const result = await processPromptOrRule("continuedev/sentry-nextjs");
expect(result).toBe(ruleContent);
expect(mockFetch).toHaveBeenCalledWith(
@ -269,7 +269,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
statusText: "Not Found",
} as Response);
await expect(processRule("continuedev/nonexistent-rule")).rejects.toThrow(
await expect(processPromptOrRule("continuedev/nonexistent-rule")).rejects.toThrow(
'Failed to load rule from hub "continuedev/nonexistent-rule": HTTP 404: Not Found',
);
});
@ -277,7 +277,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
it("should handle network errors when loading from hub", async () => {
mockFetch.mockRejectedValue(new Error("Network error"));
await expect(processRule("continuedev/sentry-nextjs")).rejects.toThrow(
await expect(processPromptOrRule("continuedev/sentry-nextjs")).rejects.toThrow(
'Failed to load rule from hub "continuedev/sentry-nextjs": Network error',
);
});
@ -285,7 +285,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
it("should handle invalid slug format", async () => {
// "invalid-slug" doesn't contain "/" so it's treated as direct content, not a hub slug
// Let's test with a slug that has wrong format but contains "/"
await expect(processRule("invalid/slug/too/many/parts")).rejects.toThrow(
await expect(processPromptOrRule("invalid/slug/too/many/parts")).rejects.toThrow(
'Invalid hub slug format. Expected "owner/package", got: invalid/slug/too/many/parts',
);
});
@ -300,7 +300,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(zipBuffer),
} as Response);
await expect(processRule("continuedev/empty-rule")).rejects.toThrow(
await expect(processPromptOrRule("continuedev/empty-rule")).rejects.toThrow(
'Failed to load rule from hub "continuedev/empty-rule": No rule content found in downloaded zip file',
);
});
@ -316,7 +316,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(zipBuffer),
} as Response);
await expect(processRule("continuedev/directory-only")).rejects.toThrow(
await expect(processPromptOrRule("continuedev/directory-only")).rejects.toThrow(
'Failed to load rule from hub "continuedev/directory-only": No rule content found in downloaded zip file',
);
});
@ -338,7 +338,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(zipBuffer),
} as Response);
const result = await processRule("continuedev/mixed-files");
const result = await processPromptOrRule("continuedev/mixed-files");
expect(result).toBe(mdContent);
});
@ -359,7 +359,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(zipBuffer),
} as Response);
const result = await processRule("continuedev/multiple-rules");
const result = await processPromptOrRule("continuedev/multiple-rules");
// Should use the first markdown file found (alphabetically)
expect(result).toBe(rule1Content);
@ -373,13 +373,13 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(invalidZipBuffer),
} as Response);
await expect(processRule("continuedev/corrupted-zip")).rejects.toThrow(
await expect(processPromptOrRule("continuedev/corrupted-zip")).rejects.toThrow(
'Failed to load rule from hub "continuedev/corrupted-zip"',
);
});
});
describe("processRule logic", () => {
describe("processPromptOrRule logic", () => {
it("should identify hub slugs correctly", async () => {
const JSZip = (await import("jszip")).default;
const zip = new JSZip();
@ -391,14 +391,14 @@ describe("processRule (loadRuleFromHub integration)", () => {
arrayBuffer: () => Promise.resolve(zipBuffer),
} as Response);
const result = await processRule("owner/package");
const result = await processPromptOrRule("owner/package");
expect(result).toBe("# Hub Rule");
expect(mockFetch).toHaveBeenCalled();
});
it("should treat direct strings as content", async () => {
const directContent = "This is direct rule content";
const result = await processRule(directContent);
const result = await processPromptOrRule(directContent);
expect(result).toBe(directContent);
expect(mockFetch).not.toHaveBeenCalled();
});
@ -406,7 +406,7 @@ describe("processRule (loadRuleFromHub integration)", () => {
it("should distinguish between hub slugs and file paths", async () => {
// This would be treated as a file path, not a hub slug
// since it starts with "."
await expect(processRule("./owner/package")).rejects.toThrow(
await expect(processPromptOrRule("./owner/package")).rejects.toThrow(
'Failed to read rule file "./owner/package": Rule file not found',
);
expect(mockFetch).not.toHaveBeenCalled();

View file

@ -183,9 +183,18 @@ export function parseArgs(): CommandLineArgs {
if (promptIndices.length > 0) {
result.prompts = [];
for (const promptIndex of promptIndices) {
if (promptIndex + 1 < args.length) {
result.prompts.push(args[promptIndex + 1]);
if (promptIndex + 1 >= args.length) {
throw new Error("--prompt flag requires a value");
}
const promptValue = args[promptIndex + 1];
// Check if the next argument is actually another flag
if (promptValue.startsWith("--")) {
throw new Error("--prompt flag requires a value, found flag instead");
}
result.prompts.push(promptValue);
}
}

View file

@ -73,7 +73,7 @@ describe("ConfigService", () => {
config: mockConfig as any,
source: { type: "cli-flag", path: "/config.yaml" } as any,
});
vi.mocked(args.processRule).mockImplementation(
vi.mocked(args.processPromptOrRule).mockImplementation(
async (rule) => `Processed: ${rule}`,
);

View file

@ -11,11 +11,11 @@ vi.mock("./services/ServiceContainer.js", () => ({
},
}));
// Mock processRule to avoid file system operations in tests
// Mock processPromptOrRule to avoid file system operations in tests
vi.mock("./args.js", () => ({
processRule: vi
processPromptOrRule: vi
.fn()
.mockImplementation((rule: string) => Promise.resolve(rule)),
.mockImplementation((spec: string) => Promise.resolve(spec)),
}));
describe("constructSystemMessage", () => {

View file

@ -170,7 +170,7 @@ export function useChat({
const finalMessage = await processAndCombinePrompts(additionalPrompts, initialPrompt);
if (finalMessage) {
handleUserMessage(finalMessage);
await handleUserMessage(finalMessage);
}
};