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
43 changes: 43 additions & 0 deletions src/services/command/__tests__/frontmatter-commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,49 @@ describe("Command loading with frontmatter", () => {
vi.clearAllMocks()
})

describe.each(["lookup", "discovery"] as const)("%s engine restrictions", (entryPoint) => {
async function load(content: string) {
mockFs.stat = vi.fn().mockResolvedValue({ isDirectory: () => true })
mockFs.readFile = vi.fn().mockResolvedValue(content)
mockFs.readdir = vi.fn().mockResolvedValue([{ name: "test.md", isFile: () => true }])
return entryPoint === "lookup"
? getCommand("/test/cwd", "test")
: (await getCommands("/test/cwd")).find((command) => command.name === "test")
}

it.each(["js", "javascript", "JavaScript", " js", "javascript\r", "python"])(
"treats %s frontmatter as text without executing it",
async (language) => {
const executed = vi.fn()
vi.stubGlobal("__commandFrontmatterExecuted", executed)
try {
// Include a BOM to exercise gray-matter's input normalization.
const content = `\uFEFF---${language}\n(globalThis.__commandFrontmatterExecuted(), {description: "unsafe"})\n---\nBody`
const command = await load(content)
expect(executed).not.toHaveBeenCalled()
expect(command).toMatchObject({ content: content.trim() })
expect(command?.description).toBeUndefined()
} finally {
vi.unstubAllGlobals()
}
},
)

it.each(["", "yaml", "yml", "json"])("accepts data-only %s frontmatter", async (language) => {
const metadata =
language === "json"
? '{"description":"Safe", "argument-hint":"input", "mode":"code"}'
: "description: Safe\nargument-hint: input\nmode: code"
const command = await load(`---${language}\n${metadata}\n---\nBody`)
expect(command).toMatchObject({
content: "Body",
description: "Safe",
argumentHint: "input",
mode: "code",
})
})
})

describe("getCommand with frontmatter", () => {
it("should load command with description from frontmatter", async () => {
const commandContent = `---
Expand Down
17 changes: 15 additions & 2 deletions src/services/command/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
*/
const MAX_DEPTH = 5

/** Parse command metadata only after rejecting document-selected executable engines. */
function parseCommandFrontmatter(content: string) {
// gray-matter strips a leading BOM before detecting the opening delimiter.
const input = content.replace(/^\uFEFF/, "")

Check warning on line 16 in src/services/command/commands.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/services/command/commands.ts:16: Survived Regex mutant (replacement: /\uFEFF/). See the job summary for the complete list and resolution guidance.
if (matter.test(input)) {

Check warning on line 17 in src/services/command/commands.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/services/command/commands.ts:17: Survived ConditionalExpression mutant (replacement: true). See the job summary for the complete list and resolution guidance.
const { name: language } = matter.language(input)
if (language && !["yaml", "yml", "json"].includes(language.toLowerCase())) {
throw new Error(`Unsupported command frontmatter language: ${language}`)

Check warning on line 20 in src/services/command/commands.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/services/command/commands.ts:20: Survived StringLiteral mutant (replacement: ``). See the job summary for the complete list and resolution guidance.
}
}
return matter(content)
}

export interface Command {
name: string
content: string
Expand Down Expand Up @@ -221,7 +234,7 @@

try {
// Try to parse frontmatter with gray-matter
parsed = matter(content)
parsed = parseCommandFrontmatter(content)
description =
typeof parsed.data.description === "string" && parsed.data.description.trim()
? parsed.data.description.trim()
Expand Down Expand Up @@ -306,7 +319,7 @@

try {
// Try to parse frontmatter with gray-matter
parsed = matter(content)
parsed = parseCommandFrontmatter(content)
description =
typeof parsed.data.description === "string" && parsed.data.description.trim()
? parsed.data.description.trim()
Expand Down
Loading