basant307/AI_Governance_Project
045
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { describe, it, expect } from 'vitest';8import {9 parseMarkdownCommand,10 MarkdownCommandDefSchema,11} from './markdown-command-parser.js';12 13describe('parseMarkdownCommand', () => {14 it('should parse markdown with YAML frontmatter', () => {15 const content = `---16description: Test command17argument-hint: "[issue-number]"18---19 20This is the prompt content.`;21 22 const result = parseMarkdownCommand(content);23 24 expect(result).toEqual({25 frontmatter: {26 description: 'Test command',27 'argument-hint': '[issue-number]',28 },29 prompt: 'This is the prompt content.',30 });31 });32 33 it('should parse markdown without frontmatter', () => {34 const content = 'This is just a prompt without frontmatter.';35 36 const result = parseMarkdownCommand(content);37 38 expect(result).toEqual({39 prompt: 'This is just a prompt without frontmatter.',40 });41 });42 43 it('should handle multi-line prompts', () => {44 const content = `---45description: Multi-line test46---47 48First line of prompt.49Second line of prompt.50Third line of prompt.`;51 52 const result = parseMarkdownCommand(content);53 54 expect(result.prompt).toBe(55 'First line of prompt.\nSecond line of prompt.\nThird line of prompt.',56 );57 });58 59 it('should trim whitespace from prompt', () => {60 const content = `---61description: Whitespace test62---63 64 Prompt with leading and trailing spaces 65`;66 67 const result = parseMarkdownCommand(content);68 69 expect(result.prompt).toBe('Prompt with leading and trailing spaces');70 });71 72 it('should handle empty frontmatter', () => {73 const content = `---74---75 76Prompt content after empty frontmatter.`;77 78 const result = parseMarkdownCommand(content);79 80 // Empty YAML frontmatter returns undefined, not {}81 expect(result.frontmatter).toBeUndefined();82 expect(result.prompt).toBe('Prompt content after empty frontmatter.');83 });84 85 it('should handle invalid YAML frontmatter gracefully', () => {86 // The YAML parser we use is quite tolerant, so most "invalid" YAML87 // actually parses successfully. This test verifies that behavior.88 const content = `---89description: test90---91 92Prompt content.`;93 94 const result = parseMarkdownCommand(content);95 96 expect(result.frontmatter).toBeDefined();97 expect(result.prompt).toBe('Prompt content.');98 });99 100 it('should parse frontmatter in CRLF files', () => {101 const content =102 '---\r\ndescription: Windows command\r\n---\r\n\r\nLine 1\r\nLine 2\r\n';103 104 const result = parseMarkdownCommand(content);105 106 expect(result).toEqual({107 frontmatter: {108 description: 'Windows command',109 },110 prompt: 'Line 1\nLine 2',111 });112 });113 114 it('should parse frontmatter in CR-only files', () => {115 const content =116 '---\rdescription: Old mac command\r---\r\rLine 1\rLine 2\r';117 118 const result = parseMarkdownCommand(content);119 120 expect(result).toEqual({121 frontmatter: {122 description: 'Old mac command',123 },124 prompt: 'Line 1\nLine 2',125 });126 });127 128 it('should parse frontmatter when content starts with UTF-8 BOM', () => {129 const content = `\uFEFF---130description: BOM command131---132 133Prompt from BOM file.`;134 135 const result = parseMarkdownCommand(content);136 137 expect(result).toEqual({138 frontmatter: {139 description: 'BOM command',140 },141 prompt: 'Prompt from BOM file.',142 });143 });144});145 146describe('MarkdownCommandDefSchema', () => {147 it('should validate valid markdown command def', () => {148 const validDef = {149 frontmatter: {150 description: 'Test description',151 'argument-hint': '[issue-number]',152 },153 prompt: 'Test prompt',154 };155 156 const result = MarkdownCommandDefSchema.safeParse(validDef);157 158 expect(result.success).toBe(true);159 });160 161 it('should validate markdown command def without frontmatter', () => {162 const validDef = {163 prompt: 'Test prompt',164 };165 166 const result = MarkdownCommandDefSchema.safeParse(validDef);167 168 expect(result.success).toBe(true);169 });170 171 it('should reject command def without prompt', () => {172 const invalidDef = {173 frontmatter: {174 description: 'Test description',175 },176 };177 178 const result = MarkdownCommandDefSchema.safeParse(invalidDef);179 180 expect(result.success).toBe(false);181 });182 183 it('should reject command def with non-string prompt', () => {184 const invalidDef = {185 prompt: 123,186 };187 188 const result = MarkdownCommandDefSchema.safeParse(invalidDef);189 190 expect(result.success).toBe(false);191 });192});193 