|
| 1 | +import { buildApiServer } from '../src/api/init'; |
| 2 | +import { cycleMigrations } from '../src/pg/migrations'; |
| 3 | +import { PgStore } from '../src/pg/pg-store'; |
| 4 | +import { TestFastifyServer, randomHash, testRevealApply } from './helpers'; |
| 5 | + |
| 6 | +describe('recursion routes', () => { |
| 7 | + let db: PgStore; |
| 8 | + let fastify: TestFastifyServer; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + db = await PgStore.connect({ skipMigrations: true }); |
| 12 | + fastify = await buildApiServer({ db }); |
| 13 | + await cycleMigrations(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(async () => { |
| 17 | + await fastify.close(); |
| 18 | + await db.close(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('/blockheight', () => { |
| 22 | + test('returns default `blockheight` when no blocks found', async () => { |
| 23 | + const response = await fastify.inject({ |
| 24 | + method: 'GET', |
| 25 | + url: '/ordinals/v1/blockheight', |
| 26 | + }); |
| 27 | + expect(response.statusCode).toBe(200); |
| 28 | + expect(response.body).toBe('767430'); |
| 29 | + expect(response.headers).toEqual( |
| 30 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + test('returns latest block height', async () => { |
| 35 | + await db.updateInscriptions(testRevealApply(778_001)); |
| 36 | + |
| 37 | + let response = await fastify.inject({ |
| 38 | + method: 'GET', |
| 39 | + url: '/ordinals/v1/blockheight', |
| 40 | + }); |
| 41 | + expect(response.statusCode).toBe(200); |
| 42 | + expect(response.body).toBe('778001'); |
| 43 | + expect(response.headers).toEqual( |
| 44 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 45 | + ); |
| 46 | + |
| 47 | + await db.updateInscriptions(testRevealApply(778_002)); |
| 48 | + |
| 49 | + response = await fastify.inject({ |
| 50 | + method: 'GET', |
| 51 | + url: '/ordinals/v1/blockheight', |
| 52 | + }); |
| 53 | + expect(response.statusCode).toBe(200); |
| 54 | + expect(response.body).toBe('778002'); |
| 55 | + expect(response.headers).toEqual( |
| 56 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 57 | + ); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('/blockhash', () => { |
| 62 | + test('returns default `blockhash` when no blocks found', async () => { |
| 63 | + const response = await fastify.inject({ |
| 64 | + method: 'GET', |
| 65 | + url: '/ordinals/v1/blockhash', |
| 66 | + }); |
| 67 | + expect(response.statusCode).toBe(200); |
| 68 | + expect(response.body).toBe('blockhash'); |
| 69 | + expect(response.headers).toEqual( |
| 70 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + test('returns latest block hash', async () => { |
| 75 | + let blockHash = randomHash(); |
| 76 | + await db.updateInscriptions(testRevealApply(778_001, { blockHash })); |
| 77 | + |
| 78 | + let response = await fastify.inject({ |
| 79 | + method: 'GET', |
| 80 | + url: '/ordinals/v1/blockhash', |
| 81 | + }); |
| 82 | + expect(response.statusCode).toBe(200); |
| 83 | + expect(response.body).toBe(blockHash); |
| 84 | + expect(response.headers).toEqual( |
| 85 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 86 | + ); |
| 87 | + |
| 88 | + blockHash = randomHash(); |
| 89 | + await db.updateInscriptions(testRevealApply(778_002, { blockHash })); |
| 90 | + |
| 91 | + response = await fastify.inject({ |
| 92 | + method: 'GET', |
| 93 | + url: '/ordinals/v1/blockhash', |
| 94 | + }); |
| 95 | + expect(response.statusCode).toBe(200); |
| 96 | + expect(response.body).toBe(blockHash); |
| 97 | + expect(response.headers).toEqual( |
| 98 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + test('returns block hash by block height', async () => { |
| 103 | + const blockHash = randomHash(); |
| 104 | + await db.updateInscriptions(testRevealApply(778_001)); |
| 105 | + await db.updateInscriptions(testRevealApply(778_002, { blockHash })); |
| 106 | + await db.updateInscriptions(testRevealApply(778_003)); |
| 107 | + |
| 108 | + const response = await fastify.inject({ |
| 109 | + method: 'GET', |
| 110 | + url: `/ordinals/v1/blockhash/778002`, |
| 111 | + }); |
| 112 | + expect(response.statusCode).toBe(200); |
| 113 | + expect(response.body).toBe(blockHash); |
| 114 | + expect(response.headers).toEqual( |
| 115 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('/blocktime', () => { |
| 121 | + test('returns default `blocktime` when no blocks found', async () => { |
| 122 | + const response = await fastify.inject({ |
| 123 | + method: 'GET', |
| 124 | + url: '/ordinals/v1/blocktime', |
| 125 | + }); |
| 126 | + expect(response.statusCode).toBe(200); |
| 127 | + expect(response.body).toBe('blocktime'); |
| 128 | + expect(response.headers).toEqual( |
| 129 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + test('returns latest block timestamp', async () => { |
| 134 | + let timestamp = Date.now(); |
| 135 | + await db.updateInscriptions(testRevealApply(778_001, { timestamp })); |
| 136 | + |
| 137 | + let response = await fastify.inject({ |
| 138 | + method: 'GET', |
| 139 | + url: '/ordinals/v1/blocktime', |
| 140 | + }); |
| 141 | + expect(response.statusCode).toBe(200); |
| 142 | + expect(response.body).toBe(timestamp.toString()); |
| 143 | + expect(response.headers).toEqual( |
| 144 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 145 | + ); |
| 146 | + |
| 147 | + timestamp = Date.now(); |
| 148 | + await db.updateInscriptions(testRevealApply(778_002, { timestamp })); |
| 149 | + |
| 150 | + response = await fastify.inject({ |
| 151 | + method: 'GET', |
| 152 | + url: '/ordinals/v1/blocktime', |
| 153 | + }); |
| 154 | + expect(response.statusCode).toBe(200); |
| 155 | + expect(response.body).toBe(timestamp.toString()); |
| 156 | + expect(response.headers).toEqual( |
| 157 | + expect.objectContaining({ 'content-type': 'text/plain; charset=utf-8' }) |
| 158 | + ); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments