-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathshared.tests.ts
More file actions
126 lines (108 loc) · 3.93 KB
/
shared.tests.ts
File metadata and controls
126 lines (108 loc) · 3.93 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
import { rmSync } from "node:fs";
import { join, resolve } from "node:path";
import { initSimnet } from "@stacks/clarinet-sdk";
import fc from "fast-check";
import {
getContractNameFromContractId,
getFunctionsFromContractInterfaces,
getFunctionsListForContract,
getSimnetDeployerContractsInterfaces,
} from "./shared";
import { createIsolatedTestEnvironment } from "./test.utils";
const isolatedTestEnvPrefix = "rendezvous-test-shared-";
describe("Simnet contracts operations", () => {
it("retrieves the contracts from the simnet", async () => {
// Setup
const tempDir = createIsolatedTestEnvironment(
resolve(__dirname, "example"),
isolatedTestEnvPrefix,
);
const manifestPath = join(tempDir, "Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const expectedDeployerContracts = new Map(
[...simnet.getContractsInterfaces()].filter(
([key]) => key.split(".")[0] === simnet.deployer,
),
);
// Exercise
const actualDeployerContracts =
getSimnetDeployerContractsInterfaces(simnet);
// Verify
expect(actualDeployerContracts).toEqual(expectedDeployerContracts);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("retrieves the contract functions from the simnet", async () => {
// Setup
const tempDir = createIsolatedTestEnvironment(
resolve(__dirname, "example"),
isolatedTestEnvPrefix,
);
const manifestPath = join(tempDir, "Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const sutContractsInterfaces = getSimnetDeployerContractsInterfaces(simnet);
const sutContractsList = [...sutContractsInterfaces.keys()];
const allFunctionsMap = new Map(
Array.from(sutContractsInterfaces, ([contractId, contractInterface]) => [
contractId,
contractInterface.functions,
]),
);
const expectedContractFunctionsList = sutContractsList.map(
(contractId) => allFunctionsMap.get(contractId) || [],
);
// Exercise
const actualContractFunctionsList = sutContractsList.map((contractId) =>
getFunctionsListForContract(allFunctionsMap, contractId),
);
// Verify
expect(actualContractFunctionsList).toEqual(expectedContractFunctionsList);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("extracts the functions from the contract interfaces", async () => {
// Setup
const tempDir = createIsolatedTestEnvironment(
resolve(__dirname, "example"),
isolatedTestEnvPrefix,
);
const manifestPath = join(tempDir, "Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const sutContractsInterfaces = getSimnetDeployerContractsInterfaces(simnet);
const expectedAllFunctionsMap = new Map(
Array.from(sutContractsInterfaces, ([contractId, contractInterface]) => [
contractId,
contractInterface.functions,
]),
);
// Exercise
const actualAllFunctionsMap = getFunctionsFromContractInterfaces(
sutContractsInterfaces,
);
// Verify
expect(actualAllFunctionsMap).toEqual(expectedAllFunctionsMap);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
});
describe("Contract identifier parsing", () => {
it("gets correct contract name from contract identifier", () => {
const addressCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const contractNameCharset =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
fc.assert(
// Arrange
fc.property(
fc.string({ unit: fc.constantFrom(...addressCharset) }),
fc.string({ unit: fc.constantFrom(...contractNameCharset) }),
(address, contractName) => {
const contractId = `${address}.${contractName}`;
// Act
const actual = getContractNameFromContractId(contractId);
// Assert
expect(actual).toBe(contractName);
},
),
);
});
});