-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinvariant.tests.ts
More file actions
105 lines (86 loc) · 3.22 KB
/
invariant.tests.ts
File metadata and controls
105 lines (86 loc) · 3.22 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
import { rmSync } from "node:fs";
import { join, resolve } from "node:path";
import { initSimnet } from "@stacks/clarinet-sdk";
import { Cl } from "@stacks/transactions";
import { initializeClarityContext, initializeLocalContext } from "./invariant";
import {
getContractNameFromContractId,
getFunctionsFromContractInterfaces,
getSimnetDeployerContractsInterfaces,
} from "./shared";
import { createIsolatedTestEnvironment } from "./test.utils";
const isolatedTestEnvPrefix = "rendezvous-test-invariant-";
describe("Simnet contracts operations", () => {
it("correctly initializes the local context for a given contract and functions", 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 sutContractsAllFunctions = getFunctionsFromContractInterfaces(
sutContractsInterfaces,
);
// Pick the first contract for testing.
const [contractId, functions] = [...sutContractsAllFunctions.entries()][0];
const expectedInitialContext = {
[contractId]: Object.fromEntries(functions.map((f) => [f.name, 0])),
};
// Exercise
const actualInitialContext = initializeLocalContext(contractId, functions);
// Verify
expect(actualInitialContext).toEqual(expectedInitialContext);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
it("correctly initializes the Clarity context", async () => {
// Setup
const tempDir = createIsolatedTestEnvironment(
resolve(__dirname, "example"),
isolatedTestEnvPrefix,
);
const simnet = await initSimnet(join(tempDir, "Clarinet.toml"));
const rendezvousList = [
...getSimnetDeployerContractsInterfaces(simnet).keys(),
].filter((deployedContract) =>
["counter"].includes(getContractNameFromContractId(deployedContract)),
);
const rendezvousAllFunctions = getFunctionsFromContractInterfaces(
new Map(
[...getSimnetDeployerContractsInterfaces(simnet)].filter(
([contractId]) => rendezvousList.includes(contractId),
),
),
);
// Pick the first contract for testing.
const [contractId, functions] = [...rendezvousAllFunctions.entries()][0];
// Exercise
initializeClarityContext(simnet, contractId, functions);
const actualContext = functions.map((f) => {
const actualValue = simnet.getMapEntry(
contractId,
"context",
Cl.stringAscii(f.name),
);
return {
contractId,
functionName: f.name,
called: actualValue,
};
});
// Verify
// The JS representation of Clarity `(some (tuple (called uint)))`, where
// `called` is initialized to 0.
const expectedClarityValue = Cl.some(Cl.tuple({ called: Cl.uint(0) }));
const expectedContext = functions.map((f) => ({
contractId,
functionName: f.name,
called: expectedClarityValue,
}));
expect(actualContext).toEqual(expectedContext);
// Teardown
rmSync(tempDir, { recursive: true, force: true });
});
});