import { describe, expect, it } from 'vitest'; import { isResourceDocumentPreviewable, resourceDocumentPreviewMarkdown, } from '../src/features/resource-canvas/resourceDocumentPreviewModel'; import type { ProjectResource } from '../src/view/project-development/resourceProjectionModel'; function resource(path: string): ProjectResource { return { id: path, path, label: path, mediaType: 'text/plain', category: 'document', subtype: 'document', manifestAssetId: 'doc', sourceLabel: '', taskTitle: null, producerTaskId: null, externalResourceId: null, referenceResourceIds: [], dependencies: [], dependencyDepth: 0, }; } describe('resourceDocumentPreviewMarkdown', () => { it('文档与空行原样进入 Markdown 渲染', () => { const text = '# 标题\n\n\n|列|\n|-|\n|值|'; expect( resourceDocumentPreviewMarkdown(resource('docs/design.md'), text), ).toBe(text); expect(isResourceDocumentPreviewable(resource('docs/design.md'))).toBe( true, ); }); it.each([ ['game/main.ts', 'typescript'], ['game/main.js', 'javascript'], ['game/main.py', 'python'], ['assets/data.json', 'json'], ['assets/DESIGN.JSON', 'json'], ])('%s 包为 %s 代码块', (path, language) => { expect( resourceDocumentPreviewMarkdown(resource(path), ' source\n\n\n'), ).toBe(`\`\`\`${language}\n source\n\n\n\`\`\``); }); it('JSON 规格按原文代码块预览,不把字段值当 Markdown', () => { expect( resourceDocumentPreviewMarkdown( resource('assets/data.json'), '{ "text": "# 不应成为标题" }\n', ), ).toBe('```json\n{ "text": "# 不应成为标题" }\n```'); }); it('正文包含 Markdown 围栏时不会逃出代码块', () => { const text = 'const sample = "````";\n\n\n'; expect( resourceDocumentPreviewMarkdown(resource('game/main.js'), text), ).toBe(`\`\`\`\`\`javascript\n${text}\n\`\`\`\`\``); }); it('图片没有文档预览入口,内联文档不要求文件路径', () => { expect( isResourceDocumentPreviewable({ ...resource('assets/image.png'), mediaType: 'image/png', subtype: 'image', }), ).toBe(false); expect( isResourceDocumentPreviewable({ ...resource(''), content: '# 回执', subtype: 'agent-result', }), ).toBe(true); expect(isResourceDocumentPreviewable(resource(''))).toBe(false); }); });