-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathblockNumberBuilder.test.ts
More file actions
129 lines (110 loc) · 3.61 KB
/
Copy pathblockNumberBuilder.test.ts
File metadata and controls
129 lines (110 loc) · 3.61 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
127
128
129
import { concat, size, toHex } from 'viem';
import { expect, describe, it } from 'vitest';
import { blockNumberBuilder } from '../../src/caveatBuilder/blockNumberBuilder';
import type { DeleGatorEnvironment } from '../../src/types';
import { randomAddress } from '../utils';
describe('blockNumberBuilder()', () => {
const EXPECTED_TERMS_LENGTH = 32; // 16 bytes for blockAfterThreshold + 16 bytes for blockBeforeThreshold
const environment = {
caveatEnforcers: { BlockNumberEnforcer: randomAddress() },
} as any as DeleGatorEnvironment;
const buildWithThresholds = (
blockAfterThreshold: bigint,
blockBeforeThreshold: bigint,
) => {
return blockNumberBuilder(
environment,
blockAfterThreshold,
blockBeforeThreshold,
);
};
describe('validation', () => {
it('should fail when both thresholds are zero', () => {
expect(() => buildWithThresholds(0n, 0n)).to.throw(
'Invalid thresholds: At least one of blockAfterThreshold or blockBeforeThreshold must be specified',
);
});
it('should fail when blockAfterThreshold is greater than or equal to blockBeforeThreshold', () => {
expect(() => buildWithThresholds(10n, 5n)).to.throw(
'Invalid thresholds: blockAfterThreshold must be less than blockBeforeThreshold if both are specified',
);
expect(() => buildWithThresholds(10n, 10n)).to.throw(
'Invalid thresholds: blockAfterThreshold must be less than blockBeforeThreshold if both are specified',
);
});
});
describe('builds a caveat', () => {
it('should build a caveat with valid thresholds', () => {
const blockAfterThreshold = 5n;
const blockBeforeThreshold = 10n;
const caveat = buildWithThresholds(
blockAfterThreshold,
blockBeforeThreshold,
);
const terms = concat([
toHex(blockAfterThreshold, {
size: 16,
}),
toHex(blockBeforeThreshold, {
size: 16,
}),
]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.BlockNumberEnforcer,
terms,
args: '0x',
});
});
it('should build a caveat with only blockAfterThreshold', () => {
const blockAfterThreshold = 5n;
const blockBeforeThreshold = 0n;
const caveat = buildWithThresholds(
blockAfterThreshold,
blockBeforeThreshold,
);
const terms = concat([
toHex(blockAfterThreshold, {
size: 16,
}),
toHex(blockBeforeThreshold, {
size: 16,
}),
]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.BlockNumberEnforcer,
terms,
args: '0x',
});
});
it('should build a caveat with only blockBeforeThreshold', () => {
const blockAfterThreshold = 0n;
const blockBeforeThreshold = 10n;
const caveat = buildWithThresholds(
blockAfterThreshold,
blockBeforeThreshold,
);
const terms = concat([
toHex(blockAfterThreshold, {
size: 16,
}),
toHex(blockBeforeThreshold, {
size: 16,
}),
]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.BlockNumberEnforcer,
terms,
args: '0x',
});
});
});
it('should create a caveat with terms length matching number of targets', () => {
const blockAfterThreshold = 5n;
const blockBeforeThreshold = 10n;
const caveat = buildWithThresholds(
blockAfterThreshold,
blockBeforeThreshold,
);
expect(size(caveat.terms)).to.equal(EXPECTED_TERMS_LENGTH);
});
});