|
| 1 | +import { CanisterApi } from '$lib/api/canister.api'; |
| 2 | +import type { Canister } from '@dfinity/utils'; |
| 3 | +import { Ed25519KeyIdentity } from '@icp-sdk/core/identity'; |
| 4 | +import { Principal } from '@icp-sdk/core/principal'; |
| 5 | + |
| 6 | +describe('canister.api', () => { |
| 7 | + const identityA = Ed25519KeyIdentity.generate(); |
| 8 | + const identityB = Ed25519KeyIdentity.generate(); |
| 9 | + const mockCanisterId = 'aaaaa-aa'; |
| 10 | + const mockCanisterPrincipal = Principal.fromText(mockCanisterId); |
| 11 | + |
| 12 | + interface CanisterType { |
| 13 | + id: string; |
| 14 | + } |
| 15 | + |
| 16 | + let api: CanisterApi<Canister<CanisterType>, CanisterType>; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + api = new CanisterApi(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should create a new canister instance on first call', async () => { |
| 23 | + const create = vi.fn().mockResolvedValue({ id: 'canister-a' }); |
| 24 | + |
| 25 | + const result = await api.getCanister({ |
| 26 | + identity: identityA, |
| 27 | + canisterId: mockCanisterId, |
| 28 | + create |
| 29 | + }); |
| 30 | + |
| 31 | + expect(result).toEqual({ id: 'canister-a' }); |
| 32 | + expect(create).toHaveBeenCalledExactlyOnceWith({ |
| 33 | + identity: identityA, |
| 34 | + canisterId: mockCanisterPrincipal |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return cached instance for the same principal', async () => { |
| 39 | + const create = vi.fn().mockResolvedValue({ id: 'canister-a' }); |
| 40 | + |
| 41 | + const first = await api.getCanister({ |
| 42 | + identity: identityA, |
| 43 | + canisterId: mockCanisterId, |
| 44 | + create |
| 45 | + }); |
| 46 | + const second = await api.getCanister({ |
| 47 | + identity: identityA, |
| 48 | + canisterId: mockCanisterId, |
| 49 | + create |
| 50 | + }); |
| 51 | + |
| 52 | + expect(first).toBe(second); |
| 53 | + expect(create).toHaveBeenCalledOnce(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should create separate instances for different principals', async () => { |
| 57 | + const createA = vi.fn().mockResolvedValue({ id: 'canister-a' }); |
| 58 | + const createB = vi.fn().mockResolvedValue({ id: 'canister-b' }); |
| 59 | + |
| 60 | + const resultA = await api.getCanister({ |
| 61 | + identity: identityA, |
| 62 | + canisterId: mockCanisterId, |
| 63 | + create: createA |
| 64 | + }); |
| 65 | + const resultB = await api.getCanister({ |
| 66 | + identity: identityB, |
| 67 | + canisterId: mockCanisterId, |
| 68 | + create: createB |
| 69 | + }); |
| 70 | + |
| 71 | + expect(resultA).toEqual({ id: 'canister-a' }); |
| 72 | + expect(resultB).toEqual({ id: 'canister-b' }); |
| 73 | + expect(createA).toHaveBeenCalledOnce(); |
| 74 | + expect(createB).toHaveBeenCalledOnce(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should not call create again after caching for a given principal', async () => { |
| 78 | + const create = vi.fn().mockResolvedValue({ id: 'canister-a' }); |
| 79 | + |
| 80 | + await api.getCanister({ identity: identityA, canisterId: mockCanisterId, create }); |
| 81 | + await api.getCanister({ identity: identityA, canisterId: mockCanisterId, create }); |
| 82 | + await api.getCanister({ identity: identityA, canisterId: mockCanisterId, create }); |
| 83 | + |
| 84 | + expect(create).toHaveBeenCalledOnce(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should call create only once for concurrent calls with the same principal', async () => { |
| 88 | + const create = vi.fn().mockResolvedValue({ id: 'canister-a' }); |
| 89 | + |
| 90 | + const results = await Promise.all([ |
| 91 | + api.getCanister({ identity: identityA, canisterId: mockCanisterId, create }), |
| 92 | + api.getCanister({ identity: identityA, canisterId: mockCanisterId, create }), |
| 93 | + api.getCanister({ identity: identityA, canisterId: mockCanisterId, create }) |
| 94 | + ]); |
| 95 | + |
| 96 | + expect(create).toHaveBeenCalledOnce(); |
| 97 | + expect(results[1]).toBe(results[0]); |
| 98 | + expect(results[2]).toBe(results[0]); |
| 99 | + }); |
| 100 | +}); |
0 commit comments