UNPKG

kleros-escrow-data-service

Version:

Data service for interacting with Kleros Escrow

157 lines (156 loc) 7.19 kB
// Basic TypeScript test for Kleros Escrow services const assert = require("assert"); import { ethers } from "ethers"; import { TransactionService, IPFSService, createKlerosEscrowClient, ArbitratorService, } from "../index"; import { TransactionStatus } from "../types/transaction"; // Mock configuration const mockConfig = { provider: { url: "https://mainnet.infura.io/v3/your-api-key", networkId: 1, }, multipleArbitrableTransaction: { address: "0xEscrowAddress", abi: [], }, ipfsGateway: "https://ipfs.example.com", }; // Mock provider const mockProvider = { getNetwork: async () => ({ chainId: 1 }), getBlockNumber: async () => 1000000, getBlock: async () => ({ timestamp: Math.floor(Date.now() / 1000) }), getLogs: async () => [], on: () => { }, removeListener: () => { }, }; // Mock signer const mockSigner = { getAddress: async () => "0xMockAddress", signMessage: async () => "0xMockSignature", _isSigner: true, provider: mockProvider, connect: () => mockSigner, }; // Simple test runner async function runTests() { var _a, _b, _c; console.log("Running Kleros Escrow Services Tests"); // Test 1: Transaction Service console.log("\n--- Testing Transaction Service ---"); try { // Create the service with our mock config const transactionService = new TransactionService(mockConfig); // Mock the internal methods that would call the blockchain transactionService.getTransaction = async (id) => { return { id, sender: "0xSender", receiver: "0xReceiver", amount: ethers.utils.parseEther("1.0").toString(), status: TransactionStatus.NoDispute, timeoutPayment: Math.floor(Date.now() / 1000) + 86400, lastInteraction: Math.floor(Date.now() / 1000), createdAt: Math.floor(Date.now() / 1000) - 3600, senderFee: "0", receiverFee: "0", }; }; // Test the service const result = await transactionService.getTransaction("1"); assert.equal(result.id, "1", "Transaction ID should match"); assert.equal(result.sender, "0xSender", "Sender should match"); assert.equal(result.receiver, "0xReceiver", "Receiver should match"); console.log("✅ Transaction Service test passed"); } catch (error) { console.error("❌ Transaction Service test failed:", error); } // Test 2: IPFS Service console.log("\n--- Testing IPFS Service ---"); try { // Create the service with our mock config const ipfsService = new IPFSService(mockConfig.ipfsGateway); // Mock the fetch method global.fetch = async () => { return { ok: true, json: async () => ({ title: "Test", description: "Test Description" }), }; }; // Test the service const result = await ipfsService.fetchFromIPFS("QmTest"); assert.equal(result.title, "Test", "Title should match"); assert.equal(result.description, "Test Description", "Description should match"); console.log("✅ IPFS Service test passed"); } catch (error) { console.error("❌ IPFS Service test failed:", error); } // Test 3: Arbitrator Service console.log("\n--- Testing Arbitrator Service ---"); try { // Create the service with our mock config const arbitratorService = new ArbitratorService(mockConfig); // Mock the internal contract methods arbitratorService["escrowContract"] = { arbitrator: async () => "0x988b3a538b618c7a603e1c11ab82cd16dbe28069", arbitratorExtraData: async () => "0x00", }; arbitratorService["arbitratorContract"] = { arbitrationCost: async () => ethers.utils.parseEther("0.1"), }; // Test the service const result = await arbitratorService.getArbitrator(); assert.equal(result.address, "0x988b3a538b618c7a603e1c11ab82cd16dbe28069", "Arbitrator address should match"); assert.equal(result.arbitrationCost, ethers.utils.parseEther("0.1").toString(), "Arbitration cost should match"); console.log("✅ Arbitrator Service test passed"); } catch (error) { console.error("❌ Arbitrator Service test failed:", error); } // Test 4: Client Creation (Read-only) console.log("\n--- Testing Read-only Client Creation ---"); try { // Create the client without a signer (read-only) const readOnlyClient = createKlerosEscrowClient(mockConfig); // Verify the client has all expected services assert.ok(readOnlyClient.services.transaction, "Should have transaction service"); assert.ok(readOnlyClient.services.dispute, "Should have dispute service"); assert.ok(readOnlyClient.services.arbitrator, "Should have arbitrator service"); assert.ok(readOnlyClient.services.event, "Should have event service"); assert.ok(readOnlyClient.services.ipfs, "Should have IPFS service"); // Verify the client does not have actions assert.equal(readOnlyClient.canWrite(), false, "Should not have write capabilities"); assert.equal(readOnlyClient.actions, undefined, "Should not have actions"); console.log("✅ Read-only Client Creation test passed"); } catch (error) { console.error("❌ Read-only Client Creation test failed:", error); } // Test 5: Client Creation (With Write Capabilities) console.log("\n--- Testing Client Creation With Write Capabilities ---"); try { // Create the client with a signer const writeClient = createKlerosEscrowClient(mockConfig, mockSigner); // Verify the client has all expected services assert.ok(writeClient.services.transaction, "Should have transaction service"); assert.ok(writeClient.services.dispute, "Should have dispute service"); assert.ok(writeClient.services.arbitrator, "Should have arbitrator service"); assert.ok(writeClient.services.event, "Should have event service"); assert.ok(writeClient.services.ipfs, "Should have IPFS service"); // Verify the client has actions assert.equal(writeClient.canWrite(), true, "Should have write capabilities"); assert.ok(writeClient.actions, "Should have actions"); assert.ok((_a = writeClient.actions) === null || _a === void 0 ? void 0 : _a.transaction, "Should have transaction actions"); assert.ok((_b = writeClient.actions) === null || _b === void 0 ? void 0 : _b.dispute, "Should have dispute actions"); assert.ok((_c = writeClient.actions) === null || _c === void 0 ? void 0 : _c.evidence, "Should have evidence actions"); console.log("✅ Client With Write Capabilities test passed"); } catch (error) { console.error("❌ Client With Write Capabilities test failed:", error); } console.log("\n--- All Tests Completed ---"); } // Run the tests runTests().catch(console.error);