/** * Test script for Tryton RPC Client * Equivalent to the Python test_client.py */ const { TrytonClient } = require("../src/client"); async function main() { console.log("šŸš€ Testing Tryton RPC Client for JavaScript"); console.log("============================================\n"); // Create client instance (equivalent to Python version) const client = new TrytonClient({ hostname: "https://demo7.6.tryton.org", // Explicitly use HTTPS database: "demo7.6", username: "admin", password: "admin", port: 8000, // Keep original port but force HTTPS language: "en", options: { verbose: true, // Enable logging to see requests/responses cache: true, // Enable caching keepMax: 4, // Maximum pooled connections }, }); try { console.log("šŸ“” Connecting to Tryton server..."); await client.connect(); console.log("āœ… Connected successfully!\n"); console.log("šŸ“‹ Client configuration:"); console.log(JSON.stringify(client.getConfig(), null, 2)); console.log(); // Test 1: Read party record (equivalent to Python test) console.log("šŸ” Test 1: Reading party record..."); const readResult = await client.call("model.party.party.read", [ [1], ["id", "name", "code"], {}, ]); console.log("šŸ“„ Read result:", JSON.stringify(readResult, null, 2)); console.log(); // Test 2: Create party record (equivalent to Python test) console.log("āž• Test 2: Creating new party record..."); const createResult = await client.call("model.party.party.create", [ [{ name: "Desde JavaScript" }], {}, ]); console.log("šŸ†• Create result (new IDs):", createResult); console.log(); // Test 3: Using helper methods console.log("šŸ› ļø Test 3: Using helper methods..."); // Read using helper method const parties = await client.read( "party.party", [1], ["id", "name", "code"] ); console.log("šŸ‘„ Parties (helper method):", parties); // Search for parties const partyIds = await client.search( "party.party", [["name", "like", "%"]], 0, 5 ); console.log("šŸ”Ž Found party IDs:", partyIds); // Search and read in one call const partyRecords = await client.searchRead( "party.party", [["id", "in", partyIds.slice(0, 3)]], ["id", "name", "code"] ); console.log("šŸ“‹ Party records:", partyRecords); // Count parties const partyCount = await client.searchCount("party.party", []); console.log("šŸ“Š Total parties count:", partyCount); console.log(); // Test 4: Server information console.log("ā„¹ļø Test 4: Server information..."); try { const version = await client.getVersion(); console.log("šŸ·ļø Server version:", version); } catch (error) { console.log("āš ļø Could not get version:", error.message); } try { const databases = await client.listDatabases(); console.log("šŸ—„ļø Available databases:", databases); } catch (error) { console.log("āš ļø Could not list databases:", error.message); } console.log(); // Test 5: Error handling console.log("āŒ Test 5: Error handling..."); try { await client.call("invalid.method.name", []); } catch (error) { console.log("āœ… Correctly caught error:", error.message); } console.log(); // Test 6: Multiple calls console.log("šŸ”„ Test 6: Multiple calls..."); const multipleCalls = [ { method: "model.party.party.read", args: [[1], ["name"]] }, { method: "model.party.party.search_count", args: [[]] }, ]; const multiResults = await client.callMultiple(multipleCalls); console.log("šŸ”¢ Multiple call results:", multiResults); console.log(); console.log("āœ… All tests completed successfully!"); } catch (error) { console.error("āŒ Test failed with error:", error.message); console.error("Stack trace:", error.stack); } finally { console.log("\nšŸ”Œ Closing connection..."); client.close(); console.log("šŸ‘‹ Goodbye!"); } } // Handle unhandled promise rejections process.on("unhandledRejection", (reason, promise) => { console.error("Unhandled Rejection at:", promise, "reason:", reason); process.exit(1); }); // Handle uncaught exceptions process.on("uncaughtException", (error) => { console.error("Uncaught Exception:", error); process.exit(1); }); // Run the test if (require.main === module) { main().catch(console.error); } module.exports = { main };