Skip to content

Commit

Permalink
test: etching api tests (#36)
Browse files Browse the repository at this point in the history
* add cenotaph column to runes table

* add cenotaph to existing mig

* chore: add api code to run test migrations

* fix migration setup and attempt some inserts

* fix some types and setup functions

* fix: testing setup and response asserts

* fix the spaced name regex and add test

* better expectation and review changes

---------

Co-authored-by: brady.ouren <[email protected]>
Co-authored-by: Rafael Cardenas <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 3d8d21a commit ada15c7
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 6 deletions.
23 changes: 22 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@
"TS_NODE_SKIP_IGNORE": "true"
},
"killBehavior": "polite"
}
},
{
"type": "node",
"request": "launch",
"name": "Jest",
"program": "${workspaceFolder}/api/node_modules/jest/bin/jest",
"cwd": "${workspaceFolder}/api/",
"args": [
"--testTimeout=3600000",
"--runInBand",
"--no-cache",
],
"outputCapture": "std",
"console": "integratedTerminal",
"preLaunchTask": "npm: testenv:run",
"postDebugTask": "npm: testenv:stop",
"env": {
"PGHOST": "localhost",
"PGUSER": "postgres",
"PGPASSWORD": "postgres",
},
},
]
}
43 changes: 43 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "npm: testenv:run",
"type": "shell",
"command": "npm run testenv:run -- -d",
"isBackground": true,
"options": {
"cwd": "${workspaceFolder}/api/",
},
"problemMatcher": {
"pattern": {
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
},
"background": {
"activeOnStart": true,
"beginsPattern": ".",
"endsPattern": "."
}
}
},
{
"label": "npm: testenv:stop",
"type": "shell",
"command": "npm run testenv:stop",
"options": {
"cwd": "${workspaceFolder}/api/",
},
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
]
}
5 changes: 4 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"generate:vercel": "npm run generate:git-info && npm run generate:openapi && npm run generate:docs",
"lint:eslint": "eslint . --ext .ts,.tsx -f unix",
"lint:prettier": "prettier --check src/**/*.ts tests/**/*.ts",
"lint:unused-exports": "ts-unused-exports tsconfig.json --showLineNumber --excludePathsFromReport=util/*"
"lint:unused-exports": "ts-unused-exports tsconfig.json --showLineNumber --excludePathsFromReport=util/*",
"testenv:run": "docker-compose -f ../docker/docker-compose.dev.postgres.yml up",
"testenv:stop": "docker-compose -f ../docker/docker-compose.dev.postgres.yml down -v -t 0",
"testenv:logs": "docker-compose -f ../docker/docker-compose.dev.postgres.yml logs -t -f"
},
"author": "Hiro Systems PBC <[email protected]> (https://hiro.so)",
"license": "Apache 2.0",
Expand Down
4 changes: 3 additions & 1 deletion api/src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const RuneNumberSchema = Type.RegEx(/^[0-9]+$/, { title: 'Rune number' });
export const RuneNumberSchemaCType = TypeCompiler.Compile(RuneNumberSchema);
const RuneNameSchema = Type.RegEx(/^[A-Z]+$/, { title: 'Rune name' });
export const RuneNameSchemaCType = TypeCompiler.Compile(RuneNameSchema);
const RuneSpacedNameSchema = Type.RegEx(/^[A-Z](•[A-Z]+)+$/, { title: 'Rune name with spacers' });
const RuneSpacedNameSchema = Type.RegEx(/^[A-Za-z]+(•[A-Za-z]+)+$/, {
title: 'Rune name with spacers',
});
export const RuneSpacedNameSchemaCType = TypeCompiler.Compile(RuneSpacedNameSchema);

export const RuneSchema = Type.Union([
Expand Down
2 changes: 1 addition & 1 deletion api/src/pg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type DbPaginatedResult<T> = {

export type DbCountedQueryResult<T> = T & { total: number };

type DbRune = {
export type DbRune = {
id: string;
number: number;
name: string;
Expand Down
111 changes: 109 additions & 2 deletions api/tests/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,110 @@
test('sample', () => {
expect(true);
import { ENV } from '../../src/env';
import { PgStore } from '../../src/pg/pg-store';
import {
dropDatabase,
insertDbLedgerEntry,
insertRune,
sampleRune,
runMigrations,
startTestApiServer,
TestFastifyServer,
insertSupplyChange,
sampleLedgerEntry,
} from '../helpers';

describe('Etchings', () => {
let db: PgStore;
let fastify: TestFastifyServer;

const rune = sampleRune('1:1', 'Sample Rune');
const ledgerEntry = sampleLedgerEntry(rune.id);

beforeEach(async () => {
ENV.PGDATABASE = 'postgres';
db = await PgStore.connect();
fastify = await startTestApiServer(db);
await runMigrations(db);
await insertRune(db, rune);
const event_index = 0;
await insertDbLedgerEntry(db, ledgerEntry, event_index);
await insertSupplyChange(db, rune.id, 1);
});

afterEach(async () => {
if (fastify) {
await fastify.close();
}

await dropDatabase(db);
await db.close();
});

test('lists runes', async () => {
const expected = {
divisibility: 0,
id: '1:1',
location: {
block_hash: '0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5',
block_height: 840000,
timestamp: 0,
tx_id: '2bb85f4b004be6da54f766c17c1e855187327112c231ef2ff35ebad0ea67c69e',
tx_index: 1,
},
mint_terms: {
amount: '100',
cap: '5000000',
height_end: null,
height_start: null,
offset_end: null,
offset_start: null,
},
name: 'Sample Rune',
number: 1,
spaced_name: 'Sample•Rune',
supply: {
burned: '0',
current: '0',
mint_percentage: '0.0000',
mintable: false,
minted: '0',
premine: '0',
total_burns: '0',
total_mints: '0',
},
symbol: 'ᚠ',
turbo: false,
};
const runesResponse = await fastify.inject({
method: 'GET',
url: '/runes/v1/etchings',
});
expect(runesResponse.statusCode).toBe(200);
expect(runesResponse.json().results).not.toHaveLength(0);

const response = await fastify.inject({
method: 'GET',
url: '/runes/v1/etchings/' + ledgerEntry.rune_id,
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual(expected);
});

test('can fetch by spaced name', async () => {
const url = '/runes/v1/etchings/' + rune.spaced_name;
const response = await fastify.inject({
method: 'GET',
url: url,
});
expect(response.statusCode).toBe(200);
expect(response.json().spaced_name).toEqual(rune.spaced_name);
});

test('can not fetch by spaced name if lacking bullets', async () => {
const url = '/runes/v1/etchings/' + rune.spaced_name.replaceAll('•', '-');
const response = await fastify.inject({
method: 'GET',
url: url,
});
expect(response.statusCode).toBe(400);
});
});
Loading

0 comments on commit ada15c7

Please sign in to comment.