-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathallowedCalldataBuilder.test.ts
More file actions
70 lines (56 loc) · 2.17 KB
/
Copy pathallowedCalldataBuilder.test.ts
File metadata and controls
70 lines (56 loc) · 2.17 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
import { concat, size, toHex, type Hex } from 'viem';
import { expect, describe, it } from 'vitest';
import { allowedCalldataBuilder } from '../../src/caveatBuilder/allowedCalldataBuilder';
import type { DeleGatorEnvironment } from '../../src/types';
import { randomAddress } from '../utils';
describe('allowedCalldataBuilder()', () => {
const EXPECTED_TERMS_LENGTH_EXCLUDING_CALLDATA = 32; // 32 bytes for startIndex
const environment = {
caveatEnforcers: { AllowedCalldataEnforcer: randomAddress() },
} as any as DeleGatorEnvironment;
const buildWithParams = (dataStart: number, value: Hex) => {
return allowedCalldataBuilder(environment, dataStart, value);
};
describe('validation', () => {
it('should fail with empty value', () => {
expect(() => buildWithParams(0, '' as Hex)).to.throw(
'Invalid value: must be a valid hex string',
);
});
it('should fail with negative dataStart', () => {
expect(() => buildWithParams(-1, '0x1234')).to.throw(
'Invalid startIndex: must be zero or positive',
);
});
it('should fail with non-integer dataStart', () => {
expect(() => buildWithParams(1.1, '0x1234')).to.throw(
'Invalid startIndex: must be a whole number',
);
});
it('should allow valid parameters', () => {
expect(() => buildWithParams(0, '0x1234')).to.not.throw();
});
});
describe('builds a caveat', () => {
it('should build a caveat with valid parameters', () => {
const startIndex = 32;
const value = '0x1234';
const caveat = buildWithParams(startIndex, value);
const dataStartHex = toHex(startIndex, { size: 32 });
const expectedTerms = concat([dataStartHex, value]);
expect(caveat).to.deep.equal({
enforcer: environment.caveatEnforcers.AllowedCalldataEnforcer,
terms: expectedTerms,
args: '0x',
});
});
});
it('should create a caveat with terms of the correct length', () => {
const startIndex = 32;
const value = '0x1234';
const caveat = buildWithParams(startIndex, value);
expect(size(caveat.terms)).to.equal(
EXPECTED_TERMS_LENGTH_EXCLUDING_CALLDATA + size(value),
);
});
});