Skip to content

Commit 98eab59

Browse files
committed
Fix tests
1 parent 5b2a354 commit 98eab59

4 files changed

Lines changed: 307 additions & 273 deletions

File tree

packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import * as ERC20StreamingEnforcer from '../DelegationFramework/ERC20StreamingEn
55
import * as MultiTokenPeriodEnforcer from '../DelegationFramework/MultiTokenPeriodEnforcer';
66
import * as NativeTokenPeriodTransferEnforcer from '../DelegationFramework/NativeTokenPeriodTransferEnforcer';
77
import * as NativeTokenStreamingEnforcer from '../DelegationFramework/NativeTokenStreamingEnforcer';
8-
import { hashDelegation } from '../delegation';
98
import type { DeleGatorEnvironment, Delegation } from '../types';
109

10+
import { hashDelegation } from '@metamask/delegation-core';
11+
1112
/**
1213
* Parameters for all caveat enforcer actions.
1314
*/
@@ -48,11 +49,15 @@ function findMatchingCaveat(
4849
);
4950

5051
if (matchingCaveats.length === 0) {
51-
throw new Error(`No caveat found with enforcer matching ${enforcerAddress}`);
52+
throw new Error(
53+
`No caveat found with enforcer matching ${enforcerAddress}`,
54+
);
5255
}
5356

5457
if (matchingCaveats.length > 1) {
55-
throw new Error(`Multiple caveats found with enforcer matching ${enforcerAddress}`);
58+
throw new Error(
59+
`Multiple caveats found with enforcer matching ${enforcerAddress}`,
60+
);
5661
}
5762

5863
const [{ terms, args }] = matchingCaveats;
@@ -177,7 +182,10 @@ export async function getMultiTokenPeriodEnforcerAvailableAmount(
177182
);
178183

179184
const delegationHash = hashDelegation(params.delegation);
180-
const { terms, args } = findMatchingCaveat(params.delegation, enforcerAddress);
185+
const { terms, args } = findMatchingCaveat(
186+
params.delegation,
187+
enforcerAddress,
188+
);
181189

182190
return MultiTokenPeriodEnforcer.read.getAvailableAmount({
183191
client,

packages/delegation-toolkit/test/actions/caveatEnforcerClient.test.ts

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import { createPublicClient, http } from 'viem';
33
import { mainnet } from 'viem/chains';
4-
import { hashDelegation } from '@metamask/delegation-core';
4+
import { getDelegationHashOffchain } from '../../src/delegation';
5+
import type { DeleGatorEnvironment, Delegation } from '../../src/types';
56
import {
67
createCaveatEnforcerClient,
78
type CaveatEnforcerClient,
8-
type DeleGatorEnvironment,
9-
type Delegation,
10-
} from '../../src';
9+
} from '../../src/actions/getCaveatAvailableAmount';
1110

1211
// Mock the contract read functions
13-
vi.mock('../../src/DelegationFramework/MultiTokenPeriodEnforcer/methods/getAvailableAmount', () => ({
14-
read: vi.fn(),
15-
}));
16-
17-
vi.mock('../../src/DelegationFramework/ERC20PeriodTransferEnforcer/methods/getAvailableAmount', () => ({
18-
read: vi.fn(),
19-
}));
12+
vi.mock(
13+
'../../src/DelegationFramework/MultiTokenPeriodEnforcer/methods/getAvailableAmount',
14+
() => ({
15+
read: vi.fn(),
16+
}),
17+
);
18+
19+
vi.mock(
20+
'../../src/DelegationFramework/ERC20PeriodTransferEnforcer/methods/getAvailableAmount',
21+
() => ({
22+
read: vi.fn(),
23+
}),
24+
);
2025

2126
describe('CaveatEnforcerClient', () => {
2227
let client: CaveatEnforcerClient;
@@ -36,10 +41,13 @@ describe('CaveatEnforcerClient', () => {
3641
implementations: {},
3742
caveatEnforcers: {
3843
MultiTokenPeriodEnforcer: '0x4567890123456789012345678901234567890123',
39-
ERC20PeriodTransferEnforcer: '0x5678901234567890123456789012345678901234',
44+
ERC20PeriodTransferEnforcer:
45+
'0x5678901234567890123456789012345678901234',
4046
ERC20StreamingEnforcer: '0x6789012345678901234567890123456789012345',
41-
NativeTokenPeriodTransferEnforcer: '0x7890123456789012345678901234567890123456',
42-
NativeTokenStreamingEnforcer: '0x8901234567890123456789012345678901234567',
47+
NativeTokenPeriodTransferEnforcer:
48+
'0x7890123456789012345678901234567890123456',
49+
NativeTokenStreamingEnforcer:
50+
'0x8901234567890123456789012345678901234567',
4351
},
4452
};
4553

@@ -51,7 +59,8 @@ describe('CaveatEnforcerClient', () => {
5159
delegation = {
5260
delegate: '0x1111111111111111111111111111111111111111',
5361
delegator: '0x2222222222222222222222222222222222222222',
54-
authority: '0x0000000000000000000000000000000000000000000000000000000000000000',
62+
authority:
63+
'0x0000000000000000000000000000000000000000000000000000000000000000',
5564
caveats: [
5665
{
5766
enforcer: '0x4567890123456789012345678901234567890123',
@@ -72,30 +81,32 @@ describe('CaveatEnforcerClient', () => {
7281
currentPeriod: 1n,
7382
};
7483

75-
const { read } = await import('../../src/DelegationFramework/MultiTokenPeriodEnforcer/methods/getAvailableAmount');
84+
const { read } = await import(
85+
'../../src/DelegationFramework/MultiTokenPeriodEnforcer/methods/getAvailableAmount'
86+
);
7687
vi.mocked(read).mockResolvedValue(mockResult);
7788

7889
const result = await client.getMultiTokenPeriodEnforcerAvailableAmount({
7990
delegation,
8091
});
8192

93+
const delegationHash = getDelegationHashOffchain(delegation);
94+
8295
expect(result).toEqual(mockResult);
8396
expect(read).toHaveBeenCalledWith({
8497
client: expect.any(Object),
8598
contractAddress: environment.caveatEnforcers.MultiTokenPeriodEnforcer,
86-
delegationHash: hashDelegation(delegation),
99+
delegationHash,
87100
delegationManager: environment.DelegationManager,
88-
terms: delegation.caveats[0].terms,
89-
args: delegation.caveats[0].args,
101+
terms: delegation.caveats[0]?.terms,
102+
args: delegation.caveats[0]?.args,
90103
});
91104
});
92-
93-
94105
});
95106

96107
describe('getErc20PeriodTransferEnforcerAvailableAmount with delegation', () => {
97108
it('should successfully get available amount using client', async () => {
98-
const delegationWithErc20Caveat = {
109+
const delegationWithErc20Caveat: Delegation = {
99110
...delegation,
100111
caveats: [
101112
{
@@ -112,33 +123,48 @@ describe('CaveatEnforcerClient', () => {
112123
currentPeriod: 1n,
113124
};
114125

115-
const { read } = await import('../../src/DelegationFramework/ERC20PeriodTransferEnforcer/methods/getAvailableAmount');
126+
const { read } = await import(
127+
'../../src/DelegationFramework/ERC20PeriodTransferEnforcer/methods/getAvailableAmount'
128+
);
116129
vi.mocked(read).mockResolvedValue(mockResult);
117130

118-
const result = await client.getErc20PeriodTransferEnforcerAvailableAmount({
119-
delegation: delegationWithErc20Caveat,
120-
});
131+
const result = await client.getErc20PeriodTransferEnforcerAvailableAmount(
132+
{
133+
delegation: delegationWithErc20Caveat,
134+
},
135+
);
136+
137+
const delegationHash = getDelegationHashOffchain(
138+
delegationWithErc20Caveat,
139+
);
121140

122141
expect(result).toEqual(mockResult);
123142
expect(read).toHaveBeenCalledWith({
124143
client: expect.any(Object),
125-
contractAddress: environment.caveatEnforcers.ERC20PeriodTransferEnforcer,
126-
delegationHash: hashDelegation(delegationWithErc20Caveat),
144+
contractAddress:
145+
environment.caveatEnforcers.ERC20PeriodTransferEnforcer,
146+
delegationHash,
127147
delegationManager: environment.DelegationManager,
128-
terms: delegationWithErc20Caveat.caveats[0].terms,
148+
terms: delegationWithErc20Caveat.caveats[0]?.terms,
129149
});
130150
});
131151
});
132152

133-
134-
135153
describe('Client type', () => {
136154
it('should have all expected methods', () => {
137-
expect(client).toHaveProperty('getMultiTokenPeriodEnforcerAvailableAmount');
138-
expect(client).toHaveProperty('getErc20PeriodTransferEnforcerAvailableAmount');
155+
expect(client).toHaveProperty(
156+
'getMultiTokenPeriodEnforcerAvailableAmount',
157+
);
158+
expect(client).toHaveProperty(
159+
'getErc20PeriodTransferEnforcerAvailableAmount',
160+
);
139161
expect(client).toHaveProperty('getErc20StreamingEnforcerAvailableAmount');
140-
expect(client).toHaveProperty('getNativeTokenPeriodTransferEnforcerAvailableAmount');
141-
expect(client).toHaveProperty('getNativeTokenStreamingEnforcerAvailableAmount');
162+
expect(client).toHaveProperty(
163+
'getNativeTokenPeriodTransferEnforcerAvailableAmount',
164+
);
165+
expect(client).toHaveProperty(
166+
'getNativeTokenStreamingEnforcerAvailableAmount',
167+
);
142168
});
143169

144170
it('should still have base client methods', () => {
@@ -147,4 +173,4 @@ describe('CaveatEnforcerClient', () => {
147173
expect(client).toHaveProperty('readContract');
148174
});
149175
});
150-
});
176+
});

0 commit comments

Comments
 (0)