fix(core): serialize migrated command descriptions as YAML (#5321)

This commit is contained in:
tt-a1i 2026-06-19 03:36:59 +08:00 committed by GitHub
parent 3e4f35bfe7
commit fda32b6e66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 2 deletions

View file

@ -13,6 +13,10 @@ import {
migrateTomlCommands,
generateMigrationPrompt,
} from './command-migration-tool.js';
import {
parseMarkdownCommand,
MarkdownCommandDefSchema,
} from './markdown-command-parser.js';
describe('command-migration-tool', () => {
let tempDir: string;
@ -153,6 +157,33 @@ description = "Test description"`;
expect(backupExists).toBe(false);
});
it('should preserve YAML-like descriptions as strings', async () => {
const tomlContent = `prompt = "Test prompt"
description = "false"`;
await fs.writeFile(path.join(tempDir, 'test.toml'), tomlContent, 'utf-8');
const result = await migrateTomlCommands({
commandDir: tempDir,
createBackup: false,
});
expect(result.success).toBe(true);
const mdContent = await fs.readFile(
path.join(tempDir, 'test.md'),
'utf-8',
);
const parsed = parseMarkdownCommand(mdContent);
const validationResult = MarkdownCommandDefSchema.safeParse(parsed);
expect(validationResult.success).toBe(true);
if (!validationResult.success) {
throw new Error('expected migrated command to be valid markdown');
}
expect(validationResult.data.frontmatter?.description).toBe('false');
});
it('should fail if Markdown file already exists', async () => {
await fs.writeFile(
path.join(tempDir, 'existing.toml'),

View file

@ -9,6 +9,13 @@ import {
convertTomlToMarkdown,
isTomlFormat,
} from './toml-to-markdown-converter.js';
import { parse as parseYaml } from './yaml-parser.js';
function readDescriptionFromFrontmatter(markdown: string): unknown {
const match = markdown.match(/^---\n([\s\S]*?)\n---\n/);
expect(match).not.toBeNull();
return parseYaml(match![1])['description'];
}
describe('convertTomlToMarkdown', () => {
it('should convert TOML with description to Markdown', () => {
@ -69,7 +76,20 @@ description = "Command with: special, characters!"`;
const result = convertTomlToMarkdown(tomlContent);
expect(result).toContain('description: Command with: special, characters!');
expect(readDescriptionFromFrontmatter(result)).toBe(
'Command with: special, characters!',
);
});
it('should preserve YAML-like description strings as strings', () => {
for (const description of ['false', '123', 'null', 'line one\nline two']) {
const tomlContent = `prompt = "Test prompt"
description = ${JSON.stringify(description)}`;
const result = convertTomlToMarkdown(tomlContent);
expect(readDescriptionFromFrontmatter(result)).toBe(description);
}
});
});

View file

@ -9,6 +9,7 @@
*/
import toml from '@iarna/toml';
import { stringify as stringifyYaml } from './yaml-parser.js';
export interface TomlCommandFormat {
prompt: string;
@ -47,8 +48,13 @@ export function convertTomlToMarkdown(tomlContent: string): string {
// Generate Markdown
if (description) {
const frontmatter = stringifyYaml(
{ description },
{ lineWidth: 0 },
).trimEnd();
return `---
description: ${description}
${frontmatter}
---
${prompt}