test-compile-report-persistence.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import fs from 'fs';
  2. import os from 'os';
  3. import path from 'path';
  4. import { analyzeCompileResult, persistLastCompileReport } from './src/server/routes/compile.js';
  5. function assert(condition, message) {
  6. if (!condition) throw new Error(message);
  7. }
  8. console.log('\n── Compile Report Persistence ──');
  9. const workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vlcode-compile-report-'));
  10. try {
  11. const rawResult = {
  12. code: 0,
  13. data: {
  14. gid: 1653,
  15. errList: [
  16. { level: 'warning', file: 'Apps/Demo.vx', line: 3, message: 'deprecated prop' },
  17. { level: 'error', file: 'Apps/Demo.vx', line: 12, message: 'Unexpected token' },
  18. ],
  19. appPreviewUrlMap: {
  20. 'Apps/Demo.vx': 'https://example.test/demo',
  21. },
  22. },
  23. };
  24. const analysis = analyzeCompileResult(rawResult, { action: 'parsePjt' });
  25. assert(analysis.success === false, 'hard errors should keep compile result unsuccessful');
  26. assert(analysis.errCount === 1, 'hard error count should exclude warnings');
  27. assert(analysis.warningCount === 1, 'warning count should be preserved');
  28. assert(Array.isArray(analysis.errList) && analysis.errList.length === 1, 'errList should only contain hard errors');
  29. assert(Array.isArray(analysis.warningList) && analysis.warningList.length === 1, 'warningList should only contain warnings');
  30. assert(analysis.previewUrls['Apps/Demo.vx'] === 'https://example.test/demo', 'preview URLs should be extracted');
  31. const report = persistLastCompileReport(workDir, rawResult, { action: 'parsePjt' });
  32. const savedPath = path.join(workDir, '.vl-code', 'last-compile.json');
  33. const saved = JSON.parse(fs.readFileSync(savedPath, 'utf-8'));
  34. assert(report.errCount === 1, 'persisted report should keep hard error count');
  35. assert(saved.warningCount === 1, 'persisted report should keep warning count');
  36. assert(saved.errList.length === 1, 'persisted errList should only contain hard errors');
  37. assert(saved.warningList.length === 1, 'persisted warningList should only contain warnings');
  38. assert(saved.previewUrls['Apps/Demo.vx'] === 'https://example.test/demo', 'persisted preview URLs should be preserved');
  39. assert(typeof saved.timestamp === 'string' && saved.timestamp.length > 0, 'persisted report should include timestamp');
  40. console.log('PASS test-compile-report-persistence.js');
  41. } finally {
  42. fs.rmSync(workDir, { recursive: true, force: true });
  43. }