From 01f0a70d9c65518bdfe8a6e9b72a8e32b7e5f390 Mon Sep 17 00:00:00 2001 From: Coby Date: Wed, 23 Oct 2024 16:25:35 -0400 Subject: [PATCH] adding sort order and testing --- .../actions-service/actions-service.ts | 24 +++++- tests/resolvers.test.ts | 5 -- .../actions-service/actions-service.test.ts | 86 +++++++++++++++++++ 3 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 tests/services/actions-service/actions-service.test.ts diff --git a/src/services/actions-service/actions-service.ts b/src/services/actions-service/actions-service.ts index a672190..e12a937 100644 --- a/src/services/actions-service/actions-service.ts +++ b/src/services/actions-service/actions-service.ts @@ -143,7 +143,7 @@ class ActionsService implements IActionsService { } actions.push({ blockInfo, - actionData: actionsData.flat(), + actionData: this.sortActions(actionsData.flat()), actionState: { /* eslint-disable */ actionStateOne: action_state_value1!, @@ -157,4 +157,26 @@ class ActionsService implements IActionsService { } return actions; } + + sortActions(actions: Action[]): Action[] { + return actions.sort((a, b) => { + // Sort by sequence number + if ( + a.transactionInfo.sequenceNumber !== b.transactionInfo.sequenceNumber + ) { + return ( + a.transactionInfo.sequenceNumber - b.transactionInfo.sequenceNumber + ); + } + + // Sort by account update index if sequence number is the same + const aIndex = a.transactionInfo.zkappAccountUpdateIds.indexOf( + Number(a.accountUpdateId) + ); + const bIndex = b.transactionInfo.zkappAccountUpdateIds.indexOf( + Number(b.accountUpdateId) + ); + return aIndex - bIndex; + }); + } } diff --git a/tests/resolvers.test.ts b/tests/resolvers.test.ts index cb32748..1766b47 100644 --- a/tests/resolvers.test.ts +++ b/tests/resolvers.test.ts @@ -79,8 +79,6 @@ query getActions($input: ActionFilterOptionsInput!) { } `; -console.log('Running tests'); - // This is the default connection string provided by the lightnet postgres container const PG_CONN = 'postgresql://postgres:postgres@localhost:5432/archive '; @@ -90,10 +88,7 @@ describe('Query Resolvers', async () => { let zkAppKeypair: Keypair; let zkApp: HelloWorld; - console.log('Describe ', 'Query Resolvers'); - before(async () => { - console.log('Before'); try { setNetworkConfig(); diff --git a/tests/services/actions-service/actions-service.test.ts b/tests/services/actions-service/actions-service.test.ts new file mode 100644 index 0000000..3220991 --- /dev/null +++ b/tests/services/actions-service/actions-service.test.ts @@ -0,0 +1,86 @@ +import { test, before, describe, after } from 'node:test'; +import assert from 'node:assert'; +import { ActionsService } from '../../../src/services/actions-service/actions-service.js'; +import { Sql } from 'postgres'; +import { Action } from '../../../src/blockchain/types.js'; + +describe('ActionsService', () => { + let actionsService: ActionsService; + + before(() => { + const client = { + query: () => {}, + CLOSE: () => {}, + END: () => {}, + PostgresError: class {}, + options: {}, + } as unknown as Sql<{}>; + actionsService = new ActionsService(client); + }); + + describe('sortActions', () => { + let actions: Action[]; + describe('with actions with different sequence numbers', () => { + before(() => { + actions = [ + dummyAction({ sequenceNumber: 2 }), + dummyAction({ sequenceNumber: 1 }), + ]; + }); + test('it sorts actions by their sequence number', () => { + const sortedActions = actionsService.sortActions(actions); + assert.strictEqual(sortedActions[0].transactionInfo.sequenceNumber, 1); + assert.strictEqual(sortedActions[1].transactionInfo.sequenceNumber, 2); + }); + }); + describe('with actions with the same sequence number', () => { + const sequenceNumber = 1; + describe('with actions with different account update ids', () => { + const zkappAccountUpdateIds = [1, 2]; + before(() => { + actions = [ + dummyAction({ + sequenceNumber, + zkappAccountUpdateIds, + accountUpdateId: '2', + }), + dummyAction({ + sequenceNumber, + zkappAccountUpdateIds, + accountUpdateId: '1', + }), + ]; + }); + test('it sorts actions by their account update index', () => { + const sortedActions = actionsService.sortActions(actions); + assert.strictEqual(sortedActions[0].accountUpdateId, '1'); + assert.strictEqual(sortedActions[1].accountUpdateId, '2'); + }); + }); + }); + }); +}); + +function dummyAction({ + sequenceNumber = 1, + accountUpdateId = '1', + zkappAccountUpdateIds = [1], +}: { + sequenceNumber?: number; + accountUpdateId?: string; + zkappAccountUpdateIds?: number[]; +}): Action { + return { + accountUpdateId: accountUpdateId, + data: ['dummy'], + transactionInfo: { + sequenceNumber, + zkappAccountUpdateIds, + zkappEventElementIds: [], + authorizationKind: 'dummy', + hash: 'dummy', + memo: 'dummy', + status: 'dummy', + }, + }; +}