-
Notifications
You must be signed in to change notification settings - Fork 38
test(core-acs-reader): add unit tests #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mateuszpiatkowski-da
merged 17 commits into
main
from
mateuszpiatkowski-da/core-acs-reader-unit-tests
Jun 17, 2026
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fb208cc
test(core-acs-reader): add cache test
mateuszpiatkowski-da 35dde8b
test(core-acs-reader): add collection tests
mateuszpiatkowski-da 30f4e4f
test(core-acs-reader): add reader tests
mateuszpiatkowski-da 2eb3fea
test(core-acs-reader): add test for service
mateuszpiatkowski-da f02745e
fix(core-acs-reader): undo vitest config modification
mateuszpiatkowski-da e9363ac
test(core-acs-reader): remove redundant test
mateuszpiatkowski-da 7531418
test(core-acs-reader): add tests for collection & cache items
mateuszpiatkowski-da 43b90e4
test(core-acs-reader): continue working on tests :wip:
mateuszpiatkowski-da 646b89d
test(core-acs-reader): finish working on tests
mateuszpiatkowski-da 00c7708
Merge branch 'main' into mateuszpiatkowski-da/core-acs-reader-unit-tests
mateuszpiatkowski-da cc6cfb6
refactor(core-acs-reader): remove redundant flags
mateuszpiatkowski-da cc8d5fd
refactor(wallet-sdk): remove redundant method
mateuszpiatkowski-da 9ebff8b
Merge branch 'main' into mateuszpiatkowski-da/core-acs-reader-unit-tests
mateuszpiatkowski-da e2f83ab
test(core-acs-reader): add additional test assertion
mateuszpiatkowski-da 9ed38de
Merge branch 'main' into mateuszpiatkowski-da/core-acs-reader-unit-tests
pawelstepien-da a59ab81
Merge branch 'main' into mateuszpiatkowski-da/core-acs-reader-unit-tests
pawelstepien-da 9961dee
test(core-acs-reader): fix test build
mateuszpiatkowski-da File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,226 @@ | ||
| // Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| ACSCacheCollection, | ||
| PaginatedACSCacheCollection, | ||
| } from '../../cache/collection' | ||
|
|
||
| const { mockCache, MockACSCache } = vi.hoisted(() => { | ||
| const update = vi.fn() | ||
| const calculateAt = vi.fn() | ||
|
|
||
| const mockCache = { | ||
| update, | ||
| calculateAt, | ||
| } | ||
|
|
||
| const MockACSCache = vi.fn( | ||
| class { | ||
| update = update | ||
| calculateAt = calculateAt | ||
| } | ||
| ) | ||
|
|
||
| return { mockCache, MockACSCache } | ||
| }) | ||
|
|
||
| vi.mock('../../cache/item', () => { | ||
| return { | ||
| ACSCache: MockACSCache, | ||
| PaginatedACSCache: MockACSCache, | ||
| } | ||
| }) | ||
|
|
||
| const ledgerProvider = vi.hoisted(() => ({ | ||
| request: vi.fn(), | ||
| })) | ||
|
|
||
| describe('cache collection', () => { | ||
| ;[ACSCacheCollection, PaginatedACSCacheCollection].forEach( | ||
| (cacheConstructor) => { | ||
| describe(`using ${cacheConstructor.name}`, () => { | ||
| let collection: ACSCacheCollection | PaginatedACSCacheCollection | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockCache.calculateAt.mockReturnValue([ | ||
| { | ||
| workflowId: 'test-workflow', | ||
| contractEntry: { | ||
| JsActiveContract: { | ||
| createdEvent: { | ||
| contractId: 'contract-1', | ||
| templateId: 'template1', | ||
| }, | ||
| synchronizerId: 'sync1', | ||
| reassignmentCounter: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| ]) | ||
|
|
||
| collection = new cacheConstructor(ledgerProvider) | ||
| }) | ||
|
|
||
| it('should create cache collection with custom options', () => { | ||
| expect(collection).toBeDefined() | ||
|
|
||
| const customCollection = new cacheConstructor( | ||
| ledgerProvider, | ||
| { | ||
| maxSize: 50, | ||
| entryExpirationTimeInMS: 5 * 60 * 1000, | ||
| } | ||
| ) | ||
| expect(customCollection).toBeDefined() | ||
| }) | ||
|
|
||
| describe('readFromCache', () => { | ||
| it('should read from cache with single party and template', async () => { | ||
| const options = { | ||
| offset: 100, | ||
| parties: ['party1'], | ||
| templateIds: ['template1'], | ||
| } | ||
|
|
||
| const result = await collection.readFromCache(options) | ||
|
|
||
| expect(MockACSCache).toHaveBeenCalledWith( | ||
| ledgerProvider | ||
| ) | ||
| expect(mockCache.update).toHaveBeenCalledWith(options) | ||
| expect(mockCache.calculateAt).toHaveBeenCalledWith(100) | ||
| expect(result).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('should read from cache with multiple parties and templates', async () => { | ||
| const options = { | ||
| offset: 100, | ||
| parties: ['party1', 'party2'], | ||
| templateIds: [ | ||
| 'template1', | ||
| 'template2', | ||
| 'template3', | ||
| ], | ||
| } | ||
|
|
||
| const result = await collection.readFromCache(options) | ||
|
|
||
| // Should create 6 cache instances (2 parties × 3 templates) | ||
| expect(MockACSCache).toHaveBeenCalledTimes(6) | ||
| expect(mockCache.update).toHaveBeenCalledTimes(6) | ||
| expect(mockCache.calculateAt).toHaveBeenCalledTimes(6) | ||
| for (let i = 0; i < 6; ++i) { | ||
| expect(mockCache.update).toHaveBeenNthCalledWith( | ||
| i + 1, | ||
| options | ||
| ) | ||
| expect( | ||
| mockCache.calculateAt | ||
| ).toHaveBeenNthCalledWith(i + 1, options.offset) | ||
| } | ||
| expect(result).toHaveLength(6) | ||
| }) | ||
|
|
||
| it('should read from cache with parties and interfaces', async () => { | ||
| const options = { | ||
| offset: 100, | ||
| parties: ['party1'], | ||
| interfaceIds: ['interface1', 'interface2'], | ||
| } | ||
|
|
||
| const result = await collection.readFromCache(options) | ||
|
|
||
| // Should create 2 cache instances (1 party × 2 interfaces) | ||
| expect(MockACSCache).toHaveBeenCalledTimes(2) | ||
| expect(mockCache.update).toHaveBeenCalledTimes(2) | ||
| expect(result).toHaveLength(2) | ||
| }) | ||
|
|
||
| it('should read from cache with parties, templates, and interfaces', async () => { | ||
| const options = { | ||
| offset: 150, | ||
| parties: ['party1'], | ||
| templateIds: ['template1'], | ||
| interfaceIds: ['interface1'], | ||
| } | ||
|
|
||
| const result = await collection.readFromCache(options) | ||
|
|
||
| // Should create 2 cache instances (1 for interface, 1 for template) | ||
| expect(MockACSCache).toHaveBeenCalledTimes(2) | ||
| expect(mockCache.update).toHaveBeenCalledTimes(2) | ||
| expect(mockCache.calculateAt).toHaveBeenCalledWith(150) | ||
| expect(result).toHaveLength(2) | ||
| }) | ||
|
|
||
| it('should reuse existing cache for same key', async () => { | ||
| const options = { | ||
| offset: 100, | ||
| parties: ['party1'], | ||
| templateIds: ['template1'], | ||
| } | ||
|
|
||
| await collection.readFromCache(options) | ||
| await collection.readFromCache(options) | ||
|
|
||
| // Should only create cache once | ||
| expect(MockACSCache).toHaveBeenCalledOnce() | ||
| // But should update and calculate twice | ||
| expect(mockCache.update).toHaveBeenCalledTimes(2) | ||
| expect(mockCache.calculateAt).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('should create different caches for different keys', async () => { | ||
| await collection.readFromCache({ | ||
| offset: 100, | ||
| parties: ['party1'], | ||
| templateIds: ['template1'], | ||
| }) | ||
|
|
||
| await collection.readFromCache({ | ||
| offset: 100, | ||
| parties: ['party2'], | ||
| templateIds: ['template1'], | ||
| }) | ||
|
|
||
| // Should create two different caches | ||
| expect(MockACSCache).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('should flatten results from multiple queries', async () => { | ||
| mockCache.calculateAt.mockReturnValue([ | ||
| { | ||
| contractEntry: { | ||
| JsActiveContract: { | ||
| createdEvent: { contractId: 'c1' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| contractEntry: { | ||
| JsActiveContract: { | ||
| createdEvent: { contractId: 'c2' }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]) | ||
|
|
||
| const options = { | ||
| offset: 100, | ||
| parties: ['party1'], | ||
| templateIds: ['template1', 'template2'], | ||
| } | ||
|
|
||
| const result = await collection.readFromCache(options) | ||
|
|
||
| // 2 templates × 2 contracts per template = 4 total contracts | ||
| expect(result).toHaveLength(4) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| ) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.