-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlib.tests.ts
More file actions
125 lines (104 loc) · 3.87 KB
/
lib.tests.ts
File metadata and controls
125 lines (104 loc) · 3.87 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
import { join, resolve } from "node:path";
import { initSimnet } from "@stacks/clarinet-sdk";
import type { ClarityValue } from "@stacks/transactions";
import fc from "fast-check";
import { getContractFunction, strategyFor } from "./lib";
const manifestPath = join(resolve(__dirname, "example"), "Clarinet.toml");
describe("getContractFunction", () => {
it("returns the function interface for a known contract and function", async () => {
const simnet = await initSimnet(manifestPath);
const fn = getContractFunction(simnet, "counter", "increment");
expect(fn.name).toBe("increment");
expect(fn.access).toBe("public");
});
it("returns a function with arguments", async () => {
const simnet = await initSimnet(manifestPath);
const fn = getContractFunction(simnet, "counter", "add");
expect(fn.name).toBe("add");
expect(fn.args.length).toBe(1);
});
it("throws when the contract does not exist", async () => {
const simnet = await initSimnet(manifestPath);
expect(() => getContractFunction(simnet, "nonexistent", "foo")).toThrow(
'Contract "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.nonexistent" not found.',
);
});
it("throws when the function does not exist", async () => {
const simnet = await initSimnet(manifestPath);
expect(() => getContractFunction(simnet, "counter", "nonexistent")).toThrow(
'Function "nonexistent" not found in contract "counter".',
);
});
it("accepts a custom deployer address", async () => {
const simnet = await initSimnet(manifestPath);
// Using the actual deployer explicitly should work the same.
const fn = getContractFunction(
simnet,
"counter",
"increment",
simnet.deployer,
);
expect(fn.name).toBe("increment");
});
});
describe("strategyFor", () => {
it("produces ClarityValue arrays for a function with arguments", async () => {
const simnet = await initSimnet(manifestPath);
const fn = getContractFunction(simnet, "counter", "add");
const arb = strategyFor(simnet, fn);
fc.assert(
fc.property(arb, (args: ClarityValue[]) => {
expect(Array.isArray(args)).toBe(true);
expect(args.length).toBe(fn.args.length);
args.forEach((arg) => {
expect(arg).toHaveProperty("type");
});
}),
{ numRuns: 10 },
);
});
it("produces an empty array for a function with no arguments", async () => {
const simnet = await initSimnet(manifestPath);
const fn = getContractFunction(simnet, "counter", "increment");
const arb = strategyFor(simnet, fn);
fc.assert(
fc.property(arb, (args: ClarityValue[]) => {
expect(args).toEqual([]);
}),
{ numRuns: 1 },
);
});
it("produces arguments usable with simnet.callPublicFn", async () => {
const simnet = await initSimnet(manifestPath);
const fn = getContractFunction(simnet, "counter", "add");
const arb = strategyFor(simnet, fn);
fc.assert(
fc.property(arb, (args: ClarityValue[]) => {
// Should not throw — arguments are valid Clarity values.
const { result } = simnet.callPublicFn(
`${simnet.deployer}.counter`,
"add",
args,
simnet.deployer,
);
expect(result).toBeDefined();
}),
{ numRuns: 5 },
);
});
it("honors allAddresses override for principal-typed arguments", async () => {
const simnet = await initSimnet(manifestPath);
const fn = getContractFunction(simnet, "rendezvous-token", "mint");
const restrictedAddress = [...simnet.getAccounts().values()][0];
const arb = strategyFor(simnet, fn, [restrictedAddress]);
fc.assert(
fc.property(arb, (args: ClarityValue[]) => {
const [recipientArg] = args;
expect(recipientArg).toEqual(
expect.objectContaining({ value: restrictedAddress }),
);
}),
{ numRuns: 10 },
);
});
});