UNPKG

@ritas-inc/hanaqueryapi-client

Version:

TypeScript client for HANA Query API with full type safety and error handling

163 lines (136 loc) โ€ข 5.92 kB
/** * Test script to verify the new plan validation behavior * * This script tests the updated client methods and API behavior where: * - 404 is returned only when the plan doesn't exist * - 200 with empty arrays is returned when plan exists but has no products/work orders * * Usage: node --experimental-strip-types test-plan-validation.ts */ import { HanaQueryClient } from './hana-query-client.ts'; import { NotFoundError } from './errors.ts'; // Configuration const TEST_CONFIG = { baseUrl: 'http://192.168.15.28:3001', timeout: 30000, enableLogging: true, logLevel: 'info' as const }; // Test plan IDs const EXISTING_PLAN_ID = 5; // Adjust based on your data const NON_EXISTENT_PLAN_ID = 99999; async function testPlanValidation(): Promise<void> { console.log('๐Ÿงช Testing Plan Validation Behavior'); console.log('=' .repeat(50)); const client = new HanaQueryClient(TEST_CONFIG); // Test 1: Plan existence check console.log('\n๐Ÿ“‹ Test 1: Plan Existence Check'); console.log('-'.repeat(30)); try { const existsTrue = await client.planExists(EXISTING_PLAN_ID); console.log(`โœ… Plan ${EXISTING_PLAN_ID} exists: ${existsTrue}`); const existsFalse = await client.planExists(NON_EXISTENT_PLAN_ID); console.log(`โœ… Plan ${NON_EXISTENT_PLAN_ID} exists: ${existsFalse}`); } catch (error) { console.log(`โŒ Plan existence check failed:`, error); } // Test 2: Products endpoint - existing plan console.log('\n๐Ÿ“ฆ Test 2: Products for Existing Plan'); console.log('-'.repeat(30)); try { const result = await client.getPlanProducts(EXISTING_PLAN_ID); console.log(`โœ… Products retrieved successfully:`); console.log(` Plan ID: ${result.metadata.planId}`); console.log(` Product count: ${result.metadata.count}`); console.log(` Array length: ${result.data.products.length}`); if (result.data.products.length === 0) { console.log(` ๐Ÿ“ Note: Plan exists but has no products (this is normal)`); } else { console.log(` ๐Ÿ“ Sample product: ${result.data.products[0].itemcode}`); } } catch (error) { console.log(`โŒ Products test failed:`, error); } // Test 3: Products endpoint - non-existent plan console.log('\n๐Ÿ“ฆ Test 3: Products for Non-Existent Plan'); console.log('-'.repeat(30)); try { const result = await client.getPlanProducts(NON_EXISTENT_PLAN_ID); console.log(`โŒ Should not reach here - expected 404 error`); } catch (error) { if (error instanceof NotFoundError) { console.log(`โœ… Correctly received 404 for non-existent plan: ${error.message}`); } else { console.log(`โŒ Unexpected error:`, error); } } // Test 4: Work orders endpoint - existing plan console.log('\n๐Ÿญ Test 4: Work Orders for Existing Plan'); console.log('-'.repeat(30)); try { const result = await client.getPlanWorkOrders(EXISTING_PLAN_ID); console.log(`โœ… Work orders retrieved successfully:`); console.log(` Plan ID: ${result.metadata.planId}`); console.log(` Work order count: ${result.metadata.count}`); console.log(` Array length: ${result.data.workOrders.length}`); if (result.data.workOrders.length === 0) { console.log(` ๐Ÿ“ Note: Plan exists but has no work orders (this is normal)`); } else { console.log(` ๐Ÿ“ Sample work order: ${result.data.workOrders[0].order_id}`); } } catch (error) { console.log(`โŒ Work orders test failed:`, error); } // Test 5: Work orders endpoint - non-existent plan console.log('\n๐Ÿญ Test 5: Work Orders for Non-Existent Plan'); console.log('-'.repeat(30)); try { const result = await client.getPlanWorkOrders(NON_EXISTENT_PLAN_ID); console.log(`โŒ Should not reach here - expected 404 error`); } catch (error) { if (error instanceof NotFoundError) { console.log(`โœ… Correctly received 404 for non-existent plan: ${error.message}`); } else { console.log(`โŒ Unexpected error:`, error); } } // Test 6: Safe methods - existing plan console.log('\n๐Ÿ›ก๏ธ Test 6: Safe Methods for Existing Plan'); console.log('-'.repeat(30)); try { const productsResult = await client.getPlanProductsSafe(EXISTING_PLAN_ID); console.log(`โœ… Safe products method:`); console.log(` Plan exists: ${productsResult.planExists}`); console.log(` Product count: ${productsResult.products.length}`); const workOrdersResult = await client.getPlanWorkOrdersSafe(EXISTING_PLAN_ID); console.log(`โœ… Safe work orders method:`); console.log(` Plan exists: ${workOrdersResult.planExists}`); console.log(` Work order count: ${workOrdersResult.workOrders.length}`); } catch (error) { console.log(`โŒ Safe methods test failed:`, error); } // Test 7: Safe methods - non-existent plan console.log('\n๐Ÿ›ก๏ธ Test 7: Safe Methods for Non-Existent Plan'); console.log('-'.repeat(30)); try { const productsResult = await client.getPlanProductsSafe(NON_EXISTENT_PLAN_ID); console.log(`โœ… Safe products method:`); console.log(` Plan exists: ${productsResult.planExists}`); console.log(` Product count: ${productsResult.products.length}`); const workOrdersResult = await client.getPlanWorkOrdersSafe(NON_EXISTENT_PLAN_ID); console.log(`โœ… Safe work orders method:`); console.log(` Plan exists: ${workOrdersResult.planExists}`); console.log(` Work order count: ${workOrdersResult.workOrders.length}`); } catch (error) { console.log(`โŒ Safe methods test failed:`, error); } console.log('\nโœจ Plan validation tests completed!'); } // Run tests if (import.meta.url === `file://${process.argv[1]}`) { testPlanValidation().catch(error => { console.error('โŒ Test execution failed:', error); process.exit(1); }); } export { testPlanValidation };