-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add sol to idb.api (#3995)
# Motivation Add Solana address in the IDB functions. Use it in the auth.service. # Changes - Add solana - Add tests for all networks - Add clean up in auth.service for sol # Tests Unit testing added --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
335bb06
commit 90d529b
Showing
4 changed files
with
262 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { | ||
deleteIdbBtcAddressMainnet, | ||
deleteIdbEthAddress, | ||
deleteIdbSolAddressMainnet, | ||
getIdbBtcAddressMainnet, | ||
getIdbEthAddress, | ||
getIdbSolAddressMainnet, | ||
setIdbBtcAddressMainnet, | ||
setIdbEthAddress, | ||
setIdbSolAddressMainnet, | ||
updateIdbBtcAddressMainnetLastUsage, | ||
updateIdbEthAddressLastUsage, | ||
updateIdbSolAddressMainnetLastUsage | ||
} from '$lib/api/idb.api'; | ||
import { mockPrincipal } from '$tests/mocks/identity.mock'; | ||
import * as idbKeyval from 'idb-keyval'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
vi.mock('idb-keyval', () => ({ | ||
createStore: vi.fn(() => ({ | ||
/* mock store implementation */ | ||
})), | ||
set: vi.fn(), | ||
get: vi.fn(), | ||
del: vi.fn(), | ||
update: vi.fn() | ||
})); | ||
|
||
vi.mock('$app/environment', () => ({ | ||
browser: true | ||
})); | ||
|
||
describe('idb.api', () => { | ||
const mockAddress = { | ||
address: '0x123', | ||
lastUsedTimestamp: Date.now(), | ||
createdAtTimestamp: Date.now() | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('BTC operations', () => { | ||
it('should set BTC address', async () => { | ||
await setIdbBtcAddressMainnet({ | ||
principal: mockPrincipal, | ||
address: mockAddress | ||
}); | ||
|
||
expect(idbKeyval.set).toHaveBeenCalledWith( | ||
mockPrincipal.toText(), | ||
mockAddress, | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it('should get BTC address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress); | ||
|
||
const result = await getIdbBtcAddressMainnet(mockPrincipal); | ||
|
||
expect(result).toEqual(mockAddress); | ||
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should delete BTC address', async () => { | ||
await deleteIdbBtcAddressMainnet(mockPrincipal); | ||
|
||
expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should update BTC address last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const updated = updater(mockAddress) as typeof mockAddress; | ||
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbBtcAddressMainnetLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('ETH operations', () => { | ||
it('should set ETH address', async () => { | ||
await setIdbEthAddress({ | ||
principal: mockPrincipal, | ||
address: mockAddress | ||
}); | ||
|
||
expect(idbKeyval.set).toHaveBeenCalledWith( | ||
mockPrincipal.toText(), | ||
mockAddress, | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it('should get ETH address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress); | ||
|
||
const result = await getIdbEthAddress(mockPrincipal); | ||
|
||
expect(result).toEqual(mockAddress); | ||
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should delete ETH address', async () => { | ||
await deleteIdbEthAddress(mockPrincipal); | ||
|
||
expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should update ETH address last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const updated = updater(mockAddress) as typeof mockAddress; | ||
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbEthAddressLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('SOL operations', () => { | ||
it('should set SOL address', async () => { | ||
await setIdbSolAddressMainnet({ | ||
principal: mockPrincipal, | ||
address: mockAddress | ||
}); | ||
|
||
expect(idbKeyval.set).toHaveBeenCalledWith( | ||
mockPrincipal.toText(), | ||
mockAddress, | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it('should get SOL address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress); | ||
|
||
const result = await getIdbSolAddressMainnet(mockPrincipal); | ||
|
||
expect(result).toEqual(mockAddress); | ||
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should delete SOL address', async () => { | ||
await deleteIdbSolAddressMainnet(mockPrincipal); | ||
|
||
expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should update SOL address last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const updated = updater(mockAddress) as typeof mockAddress; | ||
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbSolAddressMainnetLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('Edge cases', () => { | ||
it('should handle undefined address when updating last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const result = updater(undefined); | ||
expect(result).toBeUndefined(); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbBtcAddressMainnetLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return undefined when getting non-existent address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(undefined); | ||
|
||
const result = await getIdbBtcAddressMainnet(mockPrincipal); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); |