forked from dfinity/pic-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.spec.ts
More file actions
98 lines (78 loc) · 2.78 KB
/
clock.spec.ts
File metadata and controls
98 lines (78 loc) · 2.78 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
import { resolve } from 'node:path';
import { Principal } from '@dfinity/principal';
import { Actor, generateRandomIdentity, PocketIc } from 'pic-js-mops';
import { Identity } from '@dfinity/agent';
import { _SERVICE, idlFactory } from '../../declarations/clock.did';
const WASM_PATH = resolve(
__dirname,
'..',
'..',
'..',
'..',
'.dfx',
'local',
'canisters',
'clock',
'clock.wasm.gz',
);
describe('Clock', () => {
let actor: Actor<_SERVICE>;
let pic: PocketIc;
let canisterId: Principal;
let controllerIdentity: Identity;
let otherControllerIdentity: Identity;
beforeEach(async () => {
controllerIdentity = generateRandomIdentity();
otherControllerIdentity = generateRandomIdentity();
pic = await PocketIc.create(process.env.PIC_URL);
const fixture = await pic.setupCanister<_SERVICE>({
idlFactory,
wasm: WASM_PATH,
sender: controllerIdentity.getPrincipal(),
controllers: [
controllerIdentity.getPrincipal(),
otherControllerIdentity.getPrincipal(),
],
});
actor = fixture.actor;
canisterId = fixture.canisterId;
});
afterEach(async () => {
await pic.tearDown();
});
it('should create the correct canister', async () => {
const canisterExists = await pic.getCanisterSubnetId(canisterId);
expect(canisterExists).toBeTruthy();
});
it('should assign the correct controllers', async () => {
const controllers = await pic.getControllers(canisterId);
expect(controllers).toContainEqual(controllerIdentity.getPrincipal());
expect(controllers).toContainEqual(otherControllerIdentity.getPrincipal());
});
it('should not create any other canister', async () => {
const otherCanisterId = Principal.fromUint8Array(new Uint8Array([0]));
const canisterExists = await pic.getCanisterSubnetId(otherCanisterId);
expect(canisterExists).toBeFalsy();
});
it('should set and get canister cycles', async () => {
const cycles = await pic.getCyclesBalance(canisterId);
const cyclesToAdd = 1_000_000_000;
const updatedCyclesBalance = await pic.addCycles(canisterId, cyclesToAdd);
const fetchUpdatedCyclesBalance = await pic.getCyclesBalance(canisterId);
expect(updatedCyclesBalance).toEqual(cycles + cyclesToAdd);
expect(fetchUpdatedCyclesBalance).toEqual(updatedCyclesBalance);
});
it.each([1, 10, 1_000, 100_000])(
'should return the correct time after %d second(s)',
async timeToAdvanceS => {
await pic.resetTime();
await pic.tick();
const timeToAdvanceMs = timeToAdvanceS * 1_000;
const initialTime = await actor.get();
await pic.advanceTime(timeToAdvanceMs);
await pic.tick();
const finalTime = await actor.get();
expect(finalTime).toEqual(initialTime + BigInt(timeToAdvanceMs));
},
);
});