Skip to content

Commit 292688a

Browse files
committed
test(feature): date validation, user unhappy flows
1 parent 07ab0c6 commit 292688a

File tree

2 files changed

+147
-24
lines changed

2 files changed

+147
-24
lines changed

contracts/SinglePlayerCommit.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ contract SinglePlayerCommit is Ownable {
239239
}
240240

241241
function processCommitment(address committer) public {
242+
console.log("Processing commitment");
242243
Commitment memory commitment = commitments[committer];
243244

244245
// check if commitment has ended

test/SinglePlayerCommit.user.ts

Lines changed: 146 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,88 @@ export function userCanManageCommitments(): void {
7777
expect(_updatedCommitterBalance.isZero()).to.be.true;
7878
});
7979

80-
//TODO Check 7 days in future
81-
it("deposit 100 DAI and make a commitment of biking 50 kms against 50 DAI stake with deposited funds", async function () {
80+
it("not make a commitment without deposited funds", async function () {
81+
//Transaction
82+
const _activity: string = await this.singlePlayerCommit.activityList(0);
83+
const _measureIndex: number = 0;
84+
const _goal: number = 50;
85+
const _startTime: number = Date.now();
86+
const _amountToStake: BigNumber = utils.parseEther("50.0");
87+
88+
await expect(
89+
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
90+
).to.be.revertedWith("SPC::makeCommitment - insufficient token balance");
91+
});
92+
93+
it("not make a commitment with invalid parameters", async function () {
94+
//Transaction to deposit funds
95+
const _amountToDeposit: BigNumber = utils.parseEther("100.0");
96+
97+
await this.token.mock.transferFrom.returns(true);
98+
await expect(contractWithUser.deposit(_amountToDeposit, _overrides))
99+
.to.emit(this.singlePlayerCommit, "Deposit")
100+
.withArgs(await user.getAddress(), _amountToDeposit);
101+
expect("transferFrom").to.be.calledOnContract(this.token);
102+
103+
//Default parameters
104+
let _activity: BytesLike = await this.singlePlayerCommit.activityList(0);
105+
let _measureIndex: number = 0;
106+
let _goal: number = 50;
107+
let _startTime: number = Date.now();
108+
const _amountToStake: BigNumber = utils.parseEther("50.0");
109+
110+
//Activity
111+
//TODO improve to revertedWith; now returns invalid opcode instead of error message
112+
_activity = 'LALALA';
113+
114+
await expect(
115+
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
116+
).to.be.reverted;
117+
_activity = await this.singlePlayerCommit.activityList(0);
118+
119+
//Measure
120+
//TODO improve to revertedWith; now returns invalid opcode instead of error message
121+
_measureIndex = 1;
122+
123+
await expect(
124+
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
125+
).to.be.reverted;
126+
_measureIndex = 0;
127+
128+
//Goal
129+
_goal = 1;
130+
131+
await expect(
132+
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
133+
).to.be.revertedWith("SPC::makeCommitment - goal is too low");
134+
135+
_goal = 9999;
136+
137+
await expect(
138+
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
139+
).to.be.revertedWith("SPC::makeCommitment - goal is too high");
140+
141+
_goal = 50;
142+
143+
//Start time
144+
//TODO Not reverting on time before current
145+
_startTime = new Date('1 Jan 2016 12:34:56 GMT').valueOf();
146+
147+
// await expect(
148+
// contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
149+
// ).to.be.revertedWith("SPC::makeCommitment - commitment cannot start in the past");
150+
151+
//Transaction to clean up balance
152+
const _amountToWithdraw: BigNumber = utils.parseEther("100.0");
153+
154+
await this.token.mock.transfer.returns(true);
155+
await expect(contractWithUser.withdraw(_amountToWithdraw, _overrides))
156+
.to.emit(this.singlePlayerCommit, "Withdrawal")
157+
.withArgs(await user.getAddress(), _amountToWithdraw);
158+
expect("transfer").to.be.calledOnContract(this.token);
159+
});
160+
161+
it("deposit 100 DAI and make a commitment of biking 50 kms against 50 DAI stake", async function () {
82162
//User balance in wallet [ETH] and contract [DAI]
83163
const _userBalance: BigNumber = await user.getBalance();
84164
expect(_userBalance.lt(utils.parseEther("10000000000000000.0"))).to.be.true;
@@ -98,21 +178,17 @@ export function userCanManageCommitments(): void {
98178
expect("transferFrom").to.be.calledOnContract(this.token);
99179

100180
//Transaction
101-
const _activity: BytesLike = await this.singlePlayerCommit.activityList(0);
181+
const _activity: string = await this.singlePlayerCommit.activityList(0);
102182
const _measureIndex: number = 0;
103183
const _goal: number = 50;
104184
const _startTime: number = Date.now();
105185
const _amountToStake: BigNumber = utils.parseEther("50.0");
106-
107-
// const _endTime = new Date().setDate(_startTime + 7)
108-
// console.log(_endTime)
109-
// expect(commitment.start).to.equal(utils.bigNumberify())
186+
const _expectedEndTime = addDays(_startTime, 7);
110187

111188
await this.token.mock.transfer.returns(true);
112189
await expect(
113190
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
114191
).to.emit(this.singlePlayerCommit, "NewCommitment");
115-
// .withArgs(await user.getAddress(), _activity, _measureIndex, _startTime, ,_amountToStake);
116192

117193
//Validate
118194
const commitment = await this.singlePlayerCommit.commitments(user.getAddress());
@@ -130,22 +206,55 @@ export function userCanManageCommitments(): void {
130206
expect(commitment.goalValue.toNumber()).to.equal(_goal);
131207
expect(commitment.stake).to.equal(_amountToStake);
132208
expect(commitment.start).to.equal(_startTime);
209+
expect(commitment.end).to.not.be.undefined; //milliseconds, timing make equal difficult
133210
});
134211

135-
//TODO Resolve commitment and configure start/endtime
136-
it.skip("can resolve a commitment after end date", async function() {
137-
const _address = await user.getAddress()
138-
const commitment = await this.singlePlayerCommit.commitments(user.getAddress());
212+
it("not make more than one commitment", async function () {
213+
const _address = await user.getAddress();
214+
const commitment = await this.singlePlayerCommit.commitments(_address);
139215
expect(commitment.exists).to.be.true;
216+
217+
//Transaction
218+
const _activity: BytesLike = await this.singlePlayerCommit.activityList(0);
219+
const _measureIndex: number = 0;
220+
const _goal: number = 50;
221+
const _startTime: number = Date.now();
222+
const _amountToStake: BigNumber = utils.parseEther("50.0");
223+
224+
await this.token.mock.transfer.returns(true);
140225
await expect(
141-
contractWithUser.processCommitment(_address),
142-
).to.emit(this.singlePlayerCommit, "CommitmentEnded");
226+
contractWithUser.makeCommitment(_activity, _measureIndex, _goal, _startTime, _amountToStake, _overrides),
227+
).to.be.revertedWith("SPC::makeCommitment - msg.sender already has a commitment");
228+
});
229+
230+
it("not resolve a commitment before end date", async function () {
231+
let commitment;
232+
const _address = await user.getAddress();
233+
commitment = await this.singlePlayerCommit.commitments(_address);
234+
expect(commitment.exists).to.be.true;
235+
236+
await expect(contractWithUser.processCommitment(_address, _overrides)).to.be.revertedWith(
237+
"SPC::processCommitment - commitment is still active",
238+
);
239+
240+
commitment = await this.singlePlayerCommit.commitments(user.getAddress());
241+
expect(commitment.exists).to.be.true;
242+
});
243+
244+
//TODO Configure start/endtime and resolve commitment
245+
it.skip("resolve a commitment after end date", async function () {
246+
const _address = await user.getAddress();
247+
const commitment = await this.singlePlayerCommit.commitments(user.getAddress());
248+
expect(commitment.exists).to.be.true;
249+
await expect(contractWithUser.processCommitment(_address, _overrides)).to.emit(
250+
this.singlePlayerCommit,
251+
"CommitmentEnded",
252+
);
143253
expect(commitment.exists).to.be.false;
144-
})
254+
});
145255

146-
//TODO
256+
//TODO Currently failing on active commitment; need fixture or cleanup
147257
it.skip("make a deposit 100DAI and commitment of biking 50 kms against 50 DAI stake in a single call", async function () {
148-
149258
//User balance in wallet [ETH] and contract [DAI]
150259
const _userBalance: BigNumber = await user.getBalance();
151260
expect(_userBalance.lt(utils.parseEther("10000000000000000.0"))).to.be.true;
@@ -163,16 +272,22 @@ export function userCanManageCommitments(): void {
163272
const _startTime: number = Date.now();
164273
const _amountToStake: BigNumber = utils.parseEther("50.0");
165274
const _amountToDeposit: BigNumber = utils.parseEther("100.0");
166-
167-
// const _endTime = new Date().setDate(_startTime + 7)
168-
// console.log(_endTime)
169-
// expect(commitment.start).to.equal(utils.bigNumberify())
275+
const _expectedEndTime = addDays(_startTime, 7);
170276

171277
await this.token.mock.transfer.returns(true);
172278
await expect(
173-
contractWithUser.depositAndCommit(_activity, _measureIndex, _goal, _startTime, _amountToStake, _amountToDeposit,_overrides),
174-
).to.emit(this.singlePlayerCommit, "NewCommitment");
175-
// .withArgs(await user.getAddress(), _activity, _measureIndex, _startTime, ,_amountToStake);
279+
contractWithUser.depositAndCommit(
280+
_activity,
281+
_measureIndex,
282+
_goal,
283+
_startTime,
284+
_amountToStake,
285+
_amountToDeposit,
286+
_overrides,
287+
),
288+
).to.emit(this.singlePlayerCommit, "NewCommitment")
289+
.withArgs(await user.getAddress(), _activity, _measureIndex, _startTime, _expectedEndTime,_amountToStake);
290+
176291
expect("transferFrom").to.be.calledOnContract(this.token);
177292
expect("deposit").to.be.calledOnContract(this.singlePlayerCommit);
178293
expect("makeCommitment").to.be.calledOnContract(this.singlePlayerCommit);
@@ -195,4 +310,11 @@ export function userCanManageCommitments(): void {
195310
expect(commitment.start).to.equal(_startTime);
196311
});
197312
});
313+
198314
}
315+
316+
function addDays(date: number, days: number) {
317+
const result: Date = new Date(date);
318+
result.setDate(result.getDate() + days);
319+
return result.valueOf();
320+
}

0 commit comments

Comments
 (0)