155 lines
5.0 KiB
JavaScript
155 lines
5.0 KiB
JavaScript
/**
|
||
* 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 };
|