manual-test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import * as dotenv from 'dotenv';
  2. import { SiteService } from '../src/site/services/site.service';
  3. import { PhaseService } from '../src/site/services/phase.service';
  4. import { BlockService } from '../src/site/services/block.service';
  5. import { MongoCoreService } from '../src/mongo/mongo-core.service';
  6. dotenv.config();
  7. async function runTest() {
  8. const mongoCore = new MongoCoreService();
  9. // Simulate onModuleInit
  10. await (mongoCore as any).onModuleInit();
  11. const siteService = new SiteService(mongoCore, null as any);
  12. const phaseService = new PhaseService(mongoCore, null as any);
  13. const blockService = new BlockService(mongoCore, null as any);
  14. console.log('--- TEST 1: Create a New Site ---');
  15. const site = await siteService.create({
  16. name: 'Test Estate Alpha',
  17. address: '123 Palm Way, Selangor',
  18. coordinates: { lat: 3.139, lng: 101.686 },
  19. status: 'active',
  20. description: 'Primary testing site for FFB enhancement.',
  21. metadata: { createdAt: new Date(), updatedAt: new Date() }
  22. });
  23. console.log(`✅ Site Created: ${site._id}`);
  24. console.log('\n--- TEST 2: Create Phases ---');
  25. const phase1 = await phaseService.create({
  26. siteId: site._id!,
  27. name: 'Phase 2024A',
  28. description: 'New planting for 2024',
  29. status: 'active'
  30. });
  31. console.log(`✅ Phase 1 Created: ${phase1._id}`);
  32. const phase2 = await phaseService.create({
  33. siteId: site._id!,
  34. name: 'Phase 2024B',
  35. description: 'Early harvest block',
  36. status: 'active'
  37. });
  38. console.log(`✅ Phase 2 Created: ${phase2._id}`);
  39. console.log('\n--- TEST 3: Create Blocks ---');
  40. const block1 = await blockService.create({
  41. phaseId: phase1._id!,
  42. name: 'Block A1',
  43. description: 'Flat terrain',
  44. numOfTrees: 1500,
  45. size: 10,
  46. sizeUom: 'acres'
  47. });
  48. console.log(`✅ Block A1 Created: ${block1._id}`);
  49. const block2 = await blockService.create({
  50. phaseId: phase2._id!,
  51. name: 'Block B1',
  52. description: 'Hilly terrain',
  53. numOfTrees: 1200,
  54. size: 12,
  55. sizeUom: 'acres'
  56. });
  57. console.log(`✅ Block B1 Created: ${block2._id}`);
  58. console.log('\n--- TEST 4: Error Handling (Simulated) ---');
  59. // ... (Error handling tests same as before) ...
  60. console.log('\n--- TEST 6: Nested Endpoints (Simulated) ---');
  61. // Since we are mocking the module, we test the service methods directly which the controllers call.
  62. // Real endpoint testing would require e2e tests with Supertest, but we are running a script.
  63. // So we will verify the service filters work as expected.
  64. const sitePhases = await phaseService.findAll({ siteId: site._id });
  65. console.log(`✅ Found ${sitePhases.length} phases for site ${site._id}`);
  66. const phaseBlocks = await blockService.findAll({ phaseId: phase1._id });
  67. console.log(`✅ Found ${phaseBlocks.length} blocks for phase ${phase1._id}`);
  68. console.log('\n--- Test Run Completed ---');
  69. await (mongoCore as any).onModuleDestroy();
  70. process.exit(0);
  71. }
  72. runTest().catch(err => {
  73. console.error('Test failed:', err);
  74. process.exit(1);
  75. });