-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitizen.tests.ts
323 lines (271 loc) · 9.31 KB
/
citizen.tests.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import fc from "fast-check";
import {
buildRendezvousData,
getContractSource,
getTestContractSource,
groupContractsByEpochFromSimnetPlan,
issueFirstClassCitizenship,
scheduleRendezvous,
} from "./citizen";
import { initSimnet } from "@hirosystems/clarinet-sdk";
import { join } from "path";
import { cpSync, existsSync, mkdtempSync, readFileSync, rmSync } from "fs";
import { tmpdir } from "os";
import yaml from "yaml";
describe("Simnet deployment plan operations", () => {
const manifestDir = "example";
const manifestFileName = "Clarinet.toml";
const simnetPlanFileName = "default.simnet-plan.yaml";
const context = `(define-map context (string-ascii 100) {
called: uint
;; other data
})
(define-public (update-context (function-name (string-ascii 100)) (called uint))
(ok (map-set context function-name {called: called})))`;
it("adds context between contract and invariants", () => {
fc.assert(
// Arrange
fc.property(fc.string(), fc.string(), (contract, invariants) => {
// Act
const actual = scheduleRendezvous(contract, invariants);
// Assert
const expected = `${contract}\n\n${context}\n\n${invariants}`;
expect(actual).toBe(expected);
})
);
});
it("retrieves the simnet deployment plan", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
rmSync(simnetPlanPath, { force: true });
if (existsSync(simnetPlanPath)) {
throw new Error("Simnet plan file already exists");
}
const manifestPath = join(tempDir, manifestFileName);
await initSimnet(manifestPath);
// Exercise
const simnetPlanContent = readFileSync(simnetPlanPath, {
encoding: "utf-8",
}).toString();
// Verify
expect(simnetPlanContent).toBeDefined();
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("groups the contracts by epoch", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
await initSimnet(manifestPath);
const parsedSimnetPlan = yaml.parse(
readFileSync(simnetPlanPath, { encoding: "utf-8" }).toString()
);
// Exercise
const actual = groupContractsByEpochFromSimnetPlan(parsedSimnetPlan);
// Verify
const expected = {
"3.0": [
{
cargo: {
path: "contracts/cargo.clar",
clarity_version: 3,
},
},
{
counter: {
path: "contracts/counter.clar",
clarity_version: 3,
},
},
{
reverse: {
path: "contracts/reverse.clar",
clarity_version: 3,
},
},
{
slice: {
path: "contracts/slice.clar",
clarity_version: 3,
},
},
],
};
expect(actual).toEqual(expected);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("retrieves the test contract source from the simnet deployment plan", () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const parsedSimnetPlan = yaml.parse(
readFileSync(simnetPlanPath, { encoding: "utf-8" }).toString()
);
// Exercise
const actual = getTestContractSource(parsedSimnetPlan, "counter", tempDir);
// Verify
const expected = readFileSync(
join("example", "contracts", "counter.tests.clar"),
{ encoding: "utf-8" }
);
expect(actual).toBe(expected);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("builds the rendezvous data", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
await initSimnet(manifestPath);
const parsedSimnetPlan = yaml.parse(
readFileSync(simnetPlanPath, { encoding: "utf-8" }).toString()
);
// Exercise
const rendezvousSources = new Map(
["counter"]
.map((contractName) =>
buildRendezvousData(parsedSimnetPlan, contractName, manifestDir)
)
.map((rendezvousContractData) => [
rendezvousContractData.rendezvousContractName,
rendezvousContractData.rendezvousSource,
])
);
// Verify
const counterSrc = readFileSync(
join("example", "contracts", "counter.clar"),
{ encoding: "utf-8" }
);
const counterTestsSrc = readFileSync(
join("example", "contracts", "counter.tests.clar"),
{ encoding: "utf-8" }
);
const rendezvousSrc = scheduleRendezvous(counterSrc, counterTestsSrc);
const expected = new Map([["counter", rendezvousSrc]]);
expect(rendezvousSources).toEqual(expected);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("retrieves the contract source for a target contract", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
await initSimnet(manifestPath);
const parsedSimnetPlan = yaml.parse(
readFileSync(simnetPlanPath, { encoding: "utf-8" }).toString()
);
const rendezvousSources = new Map(
["counter"]
.map((contractName) =>
buildRendezvousData(parsedSimnetPlan, contractName, manifestDir)
)
.map((rendezvousContractData) => [
rendezvousContractData.rendezvousContractName,
rendezvousContractData.rendezvousSource,
])
);
const contractsByEpoch =
groupContractsByEpochFromSimnetPlan(parsedSimnetPlan);
const counterContractData = contractsByEpoch["3.0"].find(
(contract) => contract.counter
)!.counter;
// Exercise
const actual = getContractSource(
["counter"],
rendezvousSources,
"counter",
{
path: counterContractData.path,
clarity_version: counterContractData.clarity_version,
},
tempDir
);
// Verify
const counterSrc = readFileSync(
join("example", "contracts", "counter.clar"),
{ encoding: "utf-8" }
);
const counterTestsSrc = readFileSync(
join("example", "contracts", "counter.tests.clar"),
{ encoding: "utf-8" }
);
const expected = scheduleRendezvous(counterSrc, counterTestsSrc);
expect(actual).toBe(expected);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("retrieves the contract source for a non-target contract", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
const simnetPlanPath = join(tempDir, "deployments", simnetPlanFileName);
const manifestPath = join(tempDir, manifestFileName);
await initSimnet(manifestPath);
const parsedSimnetPlan = yaml.parse(
readFileSync(simnetPlanPath, { encoding: "utf-8" }).toString()
);
const rendezvousSources = new Map(
["counter"]
.map((contractName) =>
buildRendezvousData(parsedSimnetPlan, contractName, manifestDir)
)
.map((rendezvousContractData) => [
rendezvousContractData.rendezvousContractName,
rendezvousContractData.rendezvousSource,
])
);
const contractsByEpoch =
groupContractsByEpochFromSimnetPlan(parsedSimnetPlan);
const cargoContractData = contractsByEpoch["3.0"].find(
(contract) => contract.cargo
)!.cargo;
// Exercise
const actual = getContractSource(
["counter"],
rendezvousSources,
"cargo",
{
path: cargoContractData.path,
clarity_version: cargoContractData.clarity_version,
},
tempDir
);
// Verify
const expected = readFileSync(join("example", "contracts", "cargo.clar"), {
encoding: "utf-8",
});
expect(actual).toBe(expected);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("issues first-class citizenship", async () => {
// Setup
const tempDir = mkdtempSync(join(tmpdir(), "simnet-test-"));
cpSync(manifestDir, tempDir, { recursive: true });
// Exercise
const firstClassSimnet = await issueFirstClassCitizenship(tempDir, "cargo");
const actual = firstClassSimnet.getContractSource("cargo");
// Verify
const cargoSrc = readFileSync(join("example", "contracts", "cargo.clar"), {
encoding: "utf-8",
});
const cargoTestsSrc = readFileSync(
join("example", "contracts", "cargo.tests.clar"),
{ encoding: "utf-8" }
);
const expected = scheduleRendezvous(cargoSrc, cargoTestsSrc);
expect(actual).toBe(expected);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
});