irongit
defi_property/test/Factory.test.js
180 lines6.5 KBJavaScript
1const { expect } = require("chai");
2const { ethers } = require("hardhat");
3
4describe("Factory", function () {
5 let Factory, HomeTransaction;
6 let factory;
7 let realtor, seller, buyer, realtor2;
8
9 const homeAddress = "456 Oak Avenue";
10 const zip = "10001";
11 const city = "New York";
12 const price = ethers.utils.parseEther("5");
13 const realtorFee = ethers.utils.parseEther("0.25");
14
15 beforeEach(async function () {
16 [realtor, seller, buyer, realtor2] = await ethers.getSigners();
17
18 Factory = await ethers.getContractFactory("Factory");
19 HomeTransaction = await ethers.getContractFactory("HomeTransaction");
20
21 factory = await Factory.deploy();
22 await factory.deployed();
23 });
24
25 describe("Contract Creation", function () {
26 it("should create a new HomeTransaction contract", async function () {
27 const tx = await factory.connect(realtor).create(
28 homeAddress, zip, city, realtorFee, price, seller.address, buyer.address
29 );
30
31 const receipt = await tx.wait();
32 const event = receipt.events.find(e => e.event === "ContractCreated");
33
34 expect(event).to.not.be.undefined;
35 expect(event.args.realtor).to.equal(realtor.address);
36 expect(event.args.seller).to.equal(seller.address);
37 expect(event.args.buyer).to.equal(buyer.address);
38 expect(event.args.price).to.equal(price);
39 });
40
41 it("should increment instance count", async function () {
42 expect(await factory.getInstanceCount()).to.equal(0);
43
44 await factory.connect(realtor).create(
45 homeAddress, zip, city, realtorFee, price, seller.address, buyer.address
46 );
47
48 expect(await factory.getInstanceCount()).to.equal(1);
49 });
50
51 it("should set caller as realtor", async function () {
52 await factory.connect(realtor).create(
53 homeAddress, zip, city, realtorFee, price, seller.address, buyer.address
54 );
55
56 const instance = await factory.getInstance(0);
57 const homeContract = HomeTransaction.attach(instance);
58
59 expect(await homeContract.realtor()).to.equal(realtor.address);
60 });
61
62 it("should create functional contract", async function () {
63 await factory.connect(realtor).create(
64 homeAddress, zip, city, realtorFee, price, seller.address, buyer.address
65 );
66
67 const instance = await factory.getInstance(0);
68 const homeContract = HomeTransaction.attach(instance);
69
70 // Verify contract works
71 await homeContract.connect(seller).sellerSignContract();
72 expect(await homeContract.contractState()).to.equal(1); // WaitingBuyerSignature
73 });
74 });
75
76 describe("Instance Retrieval", function () {
77 beforeEach(async function () {
78 // Create 3 contracts
79 await factory.connect(realtor).create(
80 "Address 1", "11111", "City 1", realtorFee, price, seller.address, buyer.address
81 );
82 await factory.connect(realtor).create(
83 "Address 2", "22222", "City 2", realtorFee, price, seller.address, buyer.address
84 );
85 await factory.connect(realtor2).create(
86 "Address 3", "33333", "City 3", realtorFee, price, seller.address, buyer.address
87 );
88 });
89
90 it("should get instance by index", async function () {
91 const instance0 = await factory.getInstance(0);
92 const instance1 = await factory.getInstance(1);
93
94 expect(instance0).to.not.equal(instance1);
95 expect(instance0).to.not.equal(ethers.constants.AddressZero);
96 });
97
98 it("should reject out of range index", async function () {
99 await expect(factory.getInstance(99)).to.be.revertedWith("Index out of range");
100 });
101
102 it("should get all instances", async function () {
103 const instances = await factory.getInstances();
104 expect(instances.length).to.equal(3);
105 });
106
107 it("should get correct instance count", async function () {
108 expect(await factory.getInstanceCount()).to.equal(3);
109 });
110 });
111
112 describe("Contracts by Role", function () {
113 beforeEach(async function () {
114 // Realtor 1 creates 2 contracts
115 await factory.connect(realtor).create(
116 "Address 1", "11111", "City 1", realtorFee, price, seller.address, buyer.address
117 );
118 await factory.connect(realtor).create(
119 "Address 2", "22222", "City 2", realtorFee, price, seller.address, buyer.address
120 );
121
122 // Realtor 2 creates 1 contract
123 await factory.connect(realtor2).create(
124 "Address 3", "33333", "City 3", realtorFee, price, seller.address, buyer.address
125 );
126 });
127
128 it("should track contracts by realtor", async function () {
129 const realtor1Contracts = await factory.getContractsByRealtor(realtor.address);
130 const realtor2Contracts = await factory.getContractsByRealtor(realtor2.address);
131
132 expect(realtor1Contracts.length).to.equal(2);
133 expect(realtor2Contracts.length).to.equal(1);
134 });
135
136 it("should track contracts by seller", async function () {
137 const sellerContracts = await factory.getContractsBySeller(seller.address);
138 expect(sellerContracts.length).to.equal(3);
139 });
140
141 it("should track contracts by buyer", async function () {
142 const buyerContracts = await factory.getContractsByBuyer(buyer.address);
143 expect(buyerContracts.length).to.equal(3);
144 });
145
146 it("should return empty array for address with no contracts", async function () {
147 const [, , , , stranger] = await ethers.getSigners();
148 const strangerContracts = await factory.getContractsByRealtor(stranger.address);
149 expect(strangerContracts.length).to.equal(0);
150 });
151 });
152
153 describe("Multiple Transactions", function () {
154 it("should handle concurrent transactions independently", async function () {
155 // Create two contracts
156 await factory.connect(realtor).create(
157 "House A", "11111", "City A", realtorFee, price, seller.address, buyer.address
158 );
159 await factory.connect(realtor).create(
160 "House B", "22222", "City B", realtorFee, price, seller.address, buyer.address
161 );
162
163 const contractA = HomeTransaction.attach(await factory.getInstance(0));
164 const contractB = HomeTransaction.attach(await factory.getInstance(1));
165
166 // Progress contract A further than B
167 await contractA.connect(seller).sellerSignContract();
168 await contractA.connect(buyer).buyerSignContractAndPayDeposit({
169 value: price.mul(10).div(100)
170 });
171
172 // Only sign contract B with seller
173 await contractB.connect(seller).sellerSignContract();
174
175 // Verify independent states
176 expect(await contractA.contractState()).to.equal(2); // WaitingRealtorReview
177 expect(await contractB.contractState()).to.equal(1); // WaitingBuyerSignature
178 });
179 });
180});