File size: 2,365 Bytes
ffe328b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import test from 'node:test';
import assert from 'node:assert/strict';
import { parseMarkdownCodeFiles, inferLanguage, assemblePreviewHtml } from './codeParser.ts';
import { ProjectFile } from '../types/index.ts';

test('inferLanguage accurately identifies file types', () => {
  assert.equal(inferLanguage('index.html'), 'html');
  assert.equal(inferLanguage('style.css'), 'css');
  assert.equal(inferLanguage('app.js'), 'javascript');
  assert.equal(inferLanguage('component.tsx'), 'typescript');
  assert.equal(inferLanguage('package.json'), 'json');
  assert.equal(inferLanguage('README.md'), 'markdown');
});

test('parseMarkdownCodeFiles extracts multi-file blocks with comments', () => {
  const markdown = `
Here is your application:

\`\`\`html <!-- file: index.html -->
<!DOCTYPE html>
<html>
  <body><h1>Hello World</h1></body>
</html>
\`\`\`

And the styling:
\`\`\`css /* file: style.css */
h1 { color: cyan; }
\`\`\`

And the logic:
\`\`\`javascript // file: app.js
console.log("RiShre Vibe Loaded");
\`\`\`
  `;

  const files = parseMarkdownCodeFiles(markdown);
  assert.equal(files.length, 3);
  
  const html = files.find(f => f.name === 'index.html');
  const css = files.find(f => f.name === 'style.css');
  const js = files.find(f => f.name === 'app.js');

  assert.ok(html);
  assert.ok(css);
  assert.ok(js);
  assert.ok(html.content.includes('Hello World'));
  assert.ok(css.content.includes('color: cyan'));
  assert.ok(js.content.includes('RiShre Vibe Loaded'));
});

test('assemblePreviewHtml injects styles, scripts, and virtual console', () => {
  const files: ProjectFile[] = [
    {
      id: '1',
      name: 'index.html',
      path: '/index.html',
      language: 'html',
      content: '<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Content</h1></body></html>'
    },
    {
      id: '2',
      name: 'style.css',
      path: '/style.css',
      language: 'css',
      content: 'h1 { font-size: 24px; }'
    },
    {
      id: '3',
      name: 'app.js',
      path: '/app.js',
      language: 'javascript',
      content: 'console.log("Ready");'
    }
  ];

  const html = assemblePreviewHtml(files);
  assert.ok(html.includes('RISHRE_CONSOLE_EVENT'));
  assert.ok(html.includes('h1 { font-size: 24px; }'));
  assert.ok(html.includes('console.log("Ready");'));
  assert.ok(html.includes('<h1>Content</h1>'));
});