-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpassingtime.test.ts
More file actions
175 lines (152 loc) · 5.47 KB
/
Copy pathpassingtime.test.ts
File metadata and controls
175 lines (152 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Chain, Account } from "../src/index";
import { expectAction, expectThrow } from "../src/assertion";
import { blockTimeToMs } from "../src/utils";
describe("account test", () => {
let chain: Chain;
let passingtimeContract;
let chainName = process.env.CHAIN_NAME || "WAX";
let contractAccount: Account;
beforeAll(async () => {
chain = await Chain.setupChain(chainName);
contractAccount = chain.accounts[0];
await contractAccount.addCode("active");
passingtimeContract = await contractAccount.setContract({
abi: "./contracts/build/passingtime.abi",
wasm: "./contracts/build/passingtime.wasm",
});
}, 60000);
afterAll(async () => {
await chain.clear();
}, 10000);
describe("Passing time", function () {
it("should create new item and add time to buy item", async () => {
let rsp = await passingtimeContract.action.newepoch(
{
epoch_id: 0,
period: 24 * 60 * 60, // 1 day
},
[{ actor: contractAccount.name, permission: "active" }]
);
let epochs = await passingtimeContract.table.epochs.get({
scope: contractAccount.name,
});
expect(epochs.rows.length).toBe(1);
expect(epochs.rows[0].epoch_id).toBe(0);
expect(epochs.rows[0].start_at).toBe(rsp.processed.block_time);
expect(epochs.rows[0].period).toBe(24 * 60 * 60);
await expectThrow(
passingtimeContract.action.newepoch(
{
epoch_id: 1,
period: 60 * 60, // 1 hour
},
[{ actor: contractAccount.name, permission: "active" }]
),
"only start next epoch once the previous epoch has done"
);
await chain.time.increase(2 * 60 * 60); // add 2 hours
await expectThrow(
passingtimeContract.action.newepoch(
{
epoch_id: 1,
period: 60 * 60, // 1 hour
},
[{ actor: contractAccount.name, permission: "active" }]
),
"only start next epoch once the previous epoch has done"
);
await chain.time.increase(22 * 60 * 60); // add more 20 hours
rsp = await passingtimeContract.action.newepoch(
{
epoch_id: 1,
period: 60 * 60, // 1 hour
},
[{ actor: contractAccount.name, permission: "active" }]
);
epochs = await passingtimeContract.table.epochs.get({
scope: contractAccount.name,
});
expect(epochs.rows.length).toBe(2);
expect(epochs.rows[epochs.rows.length - 1].epoch_id).toBe(1);
expect(epochs.rows[epochs.rows.length - 1].start_at).toBe(
rsp.processed.block_time
);
expect(epochs.rows[epochs.rows.length - 1].period).toBe(60 * 60);
}, 40000);
});
describe("Increase time to specific time point", function () {
it("should increase time to able to add new epoch", async () => {
const currentBlockTimeMilliSecond = blockTimeToMs(
(await chain.getInfo()).head_block_time
);
const twoHoursAhead = new Date(
currentBlockTimeMilliSecond + 2 * 60 * 60 * 1000
);
const oneDayAhead = new Date(
currentBlockTimeMilliSecond + 1 * 24 * 60 * 60 * 1000
);
const twoDaysAhead = new Date(
currentBlockTimeMilliSecond + 2 * 24 * 60 * 60 * 1000
);
const oneMonthAhead = new Date(
currentBlockTimeMilliSecond + 30 * 24 * 60 * 60 * 1000
);
let rsp = await passingtimeContract.action.newepochdate(
{
epoch_id: 0,
end_at: oneDayAhead.toISOString().slice(0, -1),
},
[{ actor: contractAccount.name, permission: "active" }]
);
let epochs = await passingtimeContract.table.epochdates.get({
scope: contractAccount.name,
});
expect(epochs.rows.length).toBe(1);
expect(epochs.rows[0].epoch_id).toBe(0);
expect(epochs.rows[0].start_at).toBe(rsp.processed.block_time);
expect(epochs.rows[0].end_at).toBe(
oneDayAhead.toISOString().slice(0, -1)
);
await expectThrow(
passingtimeContract.action.newepochdate(
{
epoch_id: 1,
end_at: twoDaysAhead.toISOString().slice(0, -1),
},
[{ actor: contractAccount.name, permission: "active" }]
),
"only start next epoch once the previous epoch has done"
);
await chain.time.increaseTo(twoHoursAhead.getTime()); // add 2 hours
await expectThrow(
passingtimeContract.action.newepochdate(
{
epoch_id: 1,
end_at: twoDaysAhead.toISOString().slice(0, -1),
},
[{ actor: contractAccount.name, permission: "active" }]
),
"only start next epoch once the previous epoch has done"
);
await chain.time.increaseTo(oneDayAhead.getTime()); // add more 20 hours
rsp = await passingtimeContract.action.newepochdate(
{
epoch_id: 1,
end_at: twoDaysAhead.toISOString().slice(0, -1),
},
[{ actor: contractAccount.name, permission: "active" }]
);
epochs = await passingtimeContract.table.epochdates.get({
scope: contractAccount.name,
});
expect(epochs.rows.length).toBe(2);
expect(epochs.rows[epochs.rows.length - 1].epoch_id).toBe(1);
expect(epochs.rows[epochs.rows.length - 1].start_at).toBe(
rsp.processed.block_time
);
expect(epochs.rows[epochs.rows.length - 1].end_at).toBe(
twoDaysAhead.toISOString().slice(0, -1)
);
}, 50000);
});
});