-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvariant.tests.ts
95 lines (83 loc) · 2.92 KB
/
invariant.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
import { initSimnet } from "@hirosystems/clarinet-sdk";
import { initializeClarityContext, initializeLocalContext } from "./invariant";
import {
getContractNameFromContractId,
getFunctionsFromContractInterfaces,
getSimnetDeployerContractsInterfaces,
} from "./shared";
import { join } from "path";
import { issueFirstClassCitizenship } from "./citizen";
import { Cl } from "@stacks/transactions";
describe("Simnet contracts operations", () => {
it("correctly initializes the local context for a given functions map", async () => {
// Arrange
const manifestPath = join("example", "Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const sutContractsInterfaces = getSimnetDeployerContractsInterfaces(simnet);
const sutContractsAllFunctions = getFunctionsFromContractInterfaces(
sutContractsInterfaces
);
const expectedInitialContext = Object.fromEntries(
Array.from(sutContractsAllFunctions.entries()).map(
([contractId, functions]) => [
contractId,
Object.fromEntries(functions.map((f) => [f.name, 0])),
]
)
);
// Act
const actualInitialContext = initializeLocalContext(
sutContractsAllFunctions
);
// Assert
expect(actualInitialContext).toEqual(expectedInitialContext);
});
it("correctly initializes the Clarity context", async () => {
// Arrange
const simnet = await issueFirstClassCitizenship("example", "counter");
const rendezvousList = Array.from(
getSimnetDeployerContractsInterfaces(simnet).keys()
).filter((deployedContract) =>
["counter"].includes(getContractNameFromContractId(deployedContract))
);
const rendezvousAllFunctions = getFunctionsFromContractInterfaces(
new Map(
Array.from(getSimnetDeployerContractsInterfaces(simnet)).filter(
([contractId]) => rendezvousList.includes(contractId)
)
)
);
// Act
initializeClarityContext(simnet, rendezvousAllFunctions);
const actualContext = Array.from(rendezvousAllFunctions).flatMap(
([contractId, functions]) =>
functions.map((f) => {
const actualValue = simnet.getMapEntry(
contractId,
"context",
Cl.stringAscii(f.name)
);
return {
contractId,
functionName: f.name,
called: actualValue,
};
})
);
// Assert
// 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 = Array.from(rendezvousAllFunctions).flatMap(
([contractId, functions]) =>
functions.map((f) => {
return {
contractId,
functionName: f.name,
called: expectedClarityValue,
};
})
);
expect(actualContext).toEqual(expectedContext);
});
});