basant307/AI_Governance_Project
045
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { describe, it, expect, beforeAll, afterAll } from 'vitest';8import { promises as fs } from 'node:fs';9import path from 'node:path';10import os from 'node:os';11import { FileCommandLoader } from './FileCommandLoader.js';12 13describe('FileCommandLoader - Markdown support', () => {14 let tempDir: string;15 16 beforeAll(async () => {17 // Create a temporary directory for test commands18 tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'qwen-md-test-'));19 });20 21 afterAll(async () => {22 // Clean up23 await fs.rm(tempDir, { recursive: true, force: true });24 });25 26 it('should load markdown commands with frontmatter', async () => {27 // Create a test markdown command file28 const mdContent = `---29description: Test markdown command30argument-hint: "[issue-number]"31---32 33This is a test prompt from markdown.`;34 35 const commandPath = path.join(tempDir, 'test-command.md');36 await fs.writeFile(commandPath, mdContent, 'utf-8');37 38 // Create loader with temp dir as command source39 const loader = new FileCommandLoader(null);40 41 // Mock the getCommandDirectories to return our temp dir42 const originalMethod = loader['getCommandDirectories'];43 loader['getCommandDirectories'] = () => [{ path: tempDir }];44 45 try {46 const commands = await loader.loadCommands(new AbortController().signal);47 48 expect(commands).toHaveLength(1);49 expect(commands[0].name).toBe('test-command');50 expect(commands[0].description).toBe('Test markdown command');51 expect(commands[0].argumentHint).toBe('[issue-number]');52 } finally {53 // Restore original method54 loader['getCommandDirectories'] = originalMethod;55 }56 });57 58 it('should load markdown commands without frontmatter', async () => {59 // Create a test markdown command file without frontmatter60 const mdContent = 'This is a simple prompt without frontmatter.';61 62 const commandPath = path.join(tempDir, 'simple-command.md');63 await fs.writeFile(commandPath, mdContent, 'utf-8');64 65 const loader = new FileCommandLoader(null);66 const originalMethod = loader['getCommandDirectories'];67 loader['getCommandDirectories'] = () => [{ path: tempDir }];68 69 try {70 const commands = await loader.loadCommands(new AbortController().signal);71 72 const simpleCommand = commands.find(73 (cmd) => cmd.name === 'simple-command',74 );75 expect(simpleCommand).toBeDefined();76 expect(simpleCommand?.description).toContain('Custom command from');77 } finally {78 loader['getCommandDirectories'] = originalMethod;79 }80 });81 82 it('should load markdown commands with BOM and CRLF frontmatter', async () => {83 const mdContent =84 '\uFEFF---\r\ndescription: Windows markdown command\r\n---\r\n\r\nPrompt from windows markdown.\r\n';85 86 const commandPath = path.join(tempDir, 'windows-command.md');87 await fs.writeFile(commandPath, mdContent, 'utf-8');88 89 const loader = new FileCommandLoader(null);90 const originalMethod = loader['getCommandDirectories'];91 loader['getCommandDirectories'] = () => [{ path: tempDir }];92 93 try {94 const commands = await loader.loadCommands(new AbortController().signal);95 const windowsCommand = commands.find(96 (cmd) => cmd.name === 'windows-command',97 );98 99 expect(windowsCommand).toBeDefined();100 expect(windowsCommand?.description).toBe('Windows markdown command');101 } finally {102 loader['getCommandDirectories'] = originalMethod;103 }104 });105 106 it('should load both toml and markdown commands', async () => {107 // Create both TOML and Markdown files108 const tomlContent = `prompt = "TOML prompt"109description = "TOML command"`;110 111 const mdContent = `---112description: Markdown command113---114 115Markdown prompt`;116 117 await fs.writeFile(118 path.join(tempDir, 'toml-cmd.toml'),119 tomlContent,120 'utf-8',121 );122 await fs.writeFile(path.join(tempDir, 'md-cmd.md'), mdContent, 'utf-8');123 124 const loader = new FileCommandLoader(null);125 const originalMethod = loader['getCommandDirectories'];126 loader['getCommandDirectories'] = () => [{ path: tempDir }];127 128 try {129 const commands = await loader.loadCommands(new AbortController().signal);130 131 const tomlCommand = commands.find((cmd) => cmd.name === 'toml-cmd');132 const mdCommand = commands.find((cmd) => cmd.name === 'md-cmd');133 134 expect(tomlCommand).toBeDefined();135 expect(tomlCommand?.description).toBe('TOML command');136 137 expect(mdCommand).toBeDefined();138 expect(mdCommand?.description).toBe('Markdown command');139 } finally {140 loader['getCommandDirectories'] = originalMethod;141 }142 });143});144 