-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
blockchain_integration/pi_network/tests/test_EscrowContract.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// tests/test_EscrowContract.js | ||
const { expect } = require("chai"); | ||
const { ethers } = require("hardhat"); | ||
|
||
describe("EscrowContract", function () { | ||
let Escrow; | ||
let escrow; | ||
let owner; | ||
let seller; | ||
let buyer; | ||
let arbiter; | ||
|
||
beforeEach(async function () { | ||
Escrow = await ethers.getContractFactory("EscrowContract"); | ||
[owner, seller, buyer, arbiter] = await ethers.getSigners(); | ||
escrow = await Escrow.deploy(); | ||
await escrow.deployed(); | ||
}); | ||
|
||
it("Should create an escrow", async function () { | ||
const tx = await escrow.createEscrow(seller.address, arbiter.address, { value: ethers.utils.parseEther("1.0") }); | ||
await tx.wait(); | ||
|
||
const escrowDetails = await escrow.getEscrow(0); | ||
expect(escrowDetails.seller).to.equal(seller.address); | ||
expect(escrowDetails.arbiter).to.equal(arbiter.address); | ||
expect(escrowDetails.amount.toString()).to.equal(ethers.utils.parseEther("1.0").toString()); | ||
expect(escrowDetails.isCompleted).to.be.false; | ||
}); | ||
|
||
it("Should release funds to the seller", async function () { | ||
await escrow.createEscrow(seller.address, arbiter.address, { value: ethers.utils.parseEther("1.0") }); | ||
await escrow.connect(arbiter).releaseFunds(0); | ||
|
||
const escrowDetails = await escrow.getEscrow(0); | ||
expect(escrowDetails.isCompleted).to.be.true; | ||
expect(await ethers.provider.getBalance(seller.address)).to.be.above(0); | ||
}); | ||
|
||
it("Should not allow non-arbiter to release funds", async function () { | ||
await escrow.createEscrow(seller.address, arbiter.address, { value: ethers.utils.parseEther("1.0") }); | ||
await expect(escrow.connect(buyer).releaseFunds(0)).to.be.revertedWith("Only arbiter can release funds"); | ||
}); | ||
|
||
it("Should allow arbiter to refund the buyer", async function () { | ||
await escrow.createEscrow(seller.address, arbiter.address, { value: ethers.utils.parseEther("1.0") }); | ||
await escrow.connect(arbiter).refund(0); | ||
|
||
const escrowDetails = await escrow.getEscrow(0); | ||
expect(escrowDetails.isCompleted).to.be.true; | ||
expect(await ethers.provider.getBalance(buyer.address)).to.be.above(0); | ||
}); | ||
|
||
it("Should not allow non-arbiter to refund", async function () { | ||
await escrow.createEscrow(seller.address, arbiter.address, { value: ethers.utils.parseEther("1.0") }); | ||
await expect(escrow.connect(seller).refund(0)).to.be.revertedWith("Only arbiter can refund"); | ||
}); | ||
|
||
it("Should not allow releasing funds for an already completed escrow", async function () { | ||
await escrow.createEscrow(seller.address, arbiter.address, { value: ethers.utils.parseEther("1.0") }); | ||
await escrow.connect(arbiter).releaseFunds(0); | ||
|
||
await expect(escrow.connect(arbiter).releaseFunds(0)).to.be.revertedWith("Escrow already completed"); | ||
}); | ||
}); |