From 49703d806d34901523f95120f17fc5c737511bf3 Mon Sep 17 00:00:00 2001 From: janniks Date: Tue, 16 May 2023 15:28:40 +0200 Subject: [PATCH] test: add simple stats unit test --- tests/stats.test.ts | 128 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 tests/stats.test.ts diff --git a/tests/stats.test.ts b/tests/stats.test.ts new file mode 100644 index 00000000..b319151e --- /dev/null +++ b/tests/stats.test.ts @@ -0,0 +1,128 @@ +import { buildApiServer } from '../src/api/init'; +import { cycleMigrations } from '../src/pg/migrations'; +import { PgStore } from '../src/pg/pg-store'; +import { DbInscriptionCountPerBlock } from '../src/pg/types'; +import { TestChainhookPayloadBuilder, TestFastifyServer } from './helpers'; + +describe('/stats', () => { + let db: PgStore; + let fastify: TestFastifyServer; + + beforeEach(async () => { + db = await PgStore.connect({ skipMigrations: true }); + fastify = await buildApiServer({ db }); + await cycleMigrations(); + }); + + afterEach(async () => { + await fastify.close(); + await db.close(); + }); + + describe('/stats/inscriptions', () => { + test('returns stats', async () => { + await db.updateInscriptions( + new TestChainhookPayloadBuilder() + .apply() + .block({ + height: 778000, + hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', + timestamp: 1676913207, + }) + .transaction({ + hash: '0x9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201', + }) + .inscriptionRevealed({ + content_bytes: '0x48656C6C6F', + content_type: 'text/plain;charset=utf-8', + content_length: 5, + inscription_number: 188, + inscription_fee: 705, + inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0', + inscription_output_value: 10000, + inscriber_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj', + ordinal_number: 257418248345364, + ordinal_block_height: 650000, + ordinal_offset: 0, + satpoint_post_inscription: + '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0', + }) + .build() + ); + await db.updateInscriptions( + new TestChainhookPayloadBuilder() + .apply() + .block({ + height: 778000, + hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', + timestamp: 1676913207, + }) + .transaction({ + hash: '0x9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201', + }) + .inscriptionTransferred({ + inscription_number: 188, + inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0', + ordinal_number: 257418248345364, + post_transfer_output_value: 10000, + satpoint_post_transfer: + '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0', + satpoint_pre_transfer: + '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0', + updated_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj', + }) + .build() + ); + await db.updateInscriptions( + new TestChainhookPayloadBuilder() + .apply() + .block({ + height: 778001, + hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', + timestamp: 1676913207, + }) + .transaction({ + hash: '0x38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', + }) + .inscriptionRevealed({ + content_bytes: '0x48656C6C6F', + content_type: 'image/png', + content_length: 5, + inscription_number: 7, + inscription_fee: 2805, + inscription_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0', + inscription_output_value: 10000, + inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td', + ordinal_number: 257418248345364, + ordinal_block_height: 51483, + ordinal_offset: 0, + satpoint_post_inscription: + '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0', + }) + .build() + ); + + const response = await fastify.inject({ + method: 'GET', + url: '/ordinals/v1/stats/inscriptions', + }); + expect(response.statusCode).toBe(200); + + const json: { results: DbInscriptionCountPerBlock[] } = response.json(); + const totals = json.results.map(i => i.inscriptions_total); + expect(isNonDecreasing(totals)).toBeTruthy(); + + const expected = { + results: [ + { block_height: 778_000, inscriptions: 1, inscriptions_total: 1 }, + { block_height: 778_001, inscriptions: 1, inscriptions_total: 2 }, + ], + }; + expect(json).toStrictEqual(expected); + }); + }); +}); + +function isNonDecreasing(arr: number[]) { + return arr.every((val, i) => i === 0 || val >= arr[i - 1]); +}