test-vl-generate.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os from 'os';
  2. import path from 'path';
  3. import { isTestWorkspacePath, resolveGenerationProjectDir, inferProjectName, sanitizeProjectName, shouldDefaultToTestProject } from './src/tools/vl-generate.js';
  4. let passed = 0;
  5. let failed = 0;
  6. function test(name, fn) {
  7. return Promise.resolve()
  8. .then(fn)
  9. .then(() => {
  10. console.log(` ✓ ${name}`);
  11. passed++;
  12. })
  13. .catch((err) => {
  14. console.log(` ✗ ${name}: ${err.message}`);
  15. failed++;
  16. });
  17. }
  18. function assertEqual(actual, expected, msg) {
  19. if (actual !== expected) {
  20. throw new Error(msg || `Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
  21. }
  22. }
  23. console.log('\n── VLGenerate Targeting Regression ──');
  24. const home = os.homedir();
  25. const testWorkspace = path.join(home, 'Documents', 'VLProjects', '_tests', 'CampusApp');
  26. const prodWorkspace = path.join(home, 'Documents', 'VLProjects', 'CampusApp');
  27. await test('test workspace detection recognizes _tests paths', async () => {
  28. assertEqual(isTestWorkspacePath(testWorkspace), true);
  29. assertEqual(isTestWorkspacePath(prodWorkspace), false);
  30. });
  31. await test('generation stays inside the current test workspace when projectName matches', async () => {
  32. const resolved = resolveGenerationProjectDir({
  33. currentWorkDir: testWorkspace,
  34. projectName: 'CampusApp',
  35. isTest: null,
  36. });
  37. assertEqual(resolved, testWorkspace);
  38. });
  39. await test('generation inherits _tests parent for new projects when current workspace is a test project', async () => {
  40. const resolved = resolveGenerationProjectDir({
  41. currentWorkDir: testWorkspace,
  42. projectName: 'NewCampusApp',
  43. isTest: null,
  44. });
  45. assertEqual(resolved, path.join(home, 'Documents', 'VLProjects', '_tests', 'NewCampusApp'));
  46. });
  47. await test('explicit production target overrides inferred test parent', async () => {
  48. const resolved = resolveGenerationProjectDir({
  49. currentWorkDir: testWorkspace,
  50. projectName: 'CampusApp',
  51. isTest: false,
  52. });
  53. assertEqual(resolved, prodWorkspace);
  54. });
  55. await test('sanitizeProjectName converts mixed text into PascalCase English-only names', async () => {
  56. assertEqual(sanitizeProjectName('campus app demo'), 'CampusAppDemo');
  57. assertEqual(sanitizeProjectName('Campus-App'), 'CampusApp');
  58. });
  59. await test('inferProjectName extracts English names from mixed-language prompts', async () => {
  60. assertEqual(inferProjectName('做一个 CampusFlow 项目用于测试'), 'CampusFlow');
  61. assertEqual(inferProjectName('Create a StudyHub app for teachers'), 'StudyHub');
  62. });
  63. await test('shouldDefaultToTestProject detects demo/test requests', async () => {
  64. assertEqual(shouldDefaultToTestProject('做一个测试 demo 应用', ''), true);
  65. assertEqual(shouldDefaultToTestProject('创建正式生产项目', ''), false);
  66. });
  67. console.log(`\n── Results ──\n\n ${passed} passed, ${failed} failed\n`);
  68. process.exit(failed > 0 ? 1 : 0);