diff --git a/.dockerignore b/.dockerignore index f555785..0e51942 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,3 @@ -/test /bins /build /dist @@ -7,4 +6,4 @@ .github *.log -*.bak \ No newline at end of file +*.bak diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index fcc2fdb..e248935 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -19,3 +19,18 @@ jobs: node-version: 20.x - name: Check with Prettier run: npx prettier --check --ignore-path .prettierignore '**/*.(yml|js|ts)' + + test: + name: "Run Tests" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Use Node.js 16.x + uses: actions/setup-node@v4 + with: + node-version: 20.x + - name: Install dependencies + run: npm install + - name: Run tests + run: npm test diff --git a/.github/workflows/verify-compilation.yml b/.github/workflows/verify-compilation.yml index 1c33242..362fbf1 100644 --- a/.github/workflows/verify-compilation.yml +++ b/.github/workflows/verify-compilation.yml @@ -20,3 +20,18 @@ jobs: - name: Verify TypeScript compilation run: npm run build + + test: + name: "Run Tests" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Use Node.js 16.x + uses: actions/setup-node@v4 + with: + node-version: 20.x + - name: Install dependencies + run: npm install + - name: Run tests + run: npm test diff --git a/.npmignore b/.npmignore index 80cf093..1523070 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,4 @@ /scripts -/test /bins /build /docker @@ -10,4 +9,4 @@ .github *.log -*.bak \ No newline at end of file +*.bak diff --git a/src/tools/monitor.ts b/src/tools/monitor.ts index f8a62ef..4b1af26 100644 --- a/src/tools/monitor.ts +++ b/src/tools/monitor.ts @@ -26,6 +26,11 @@ const argv = yargs(process.argv.slice(2)) default: false, description: "listen to finalized only", }, + blocks: { + type: "number", + description: "Number of blocks to monitor", + default: Infinity, + }, }) .check(function (argv) { if (!argv.url && !argv.networks) { @@ -36,7 +41,7 @@ const argv = yargs(process.argv.slice(2)) const main = async () => { if (argv.networks) { - argv.networks.map((network) => getMonitoredApiFor({ network, finalized: argv.finalized })); + argv.networks.map((network) => getMonitoredApiFor({ network, finalized: argv.finalized, blocks: argv.blocks })); } else { getMonitoredApiFor(argv); } diff --git a/src/utils/monitoring.ts b/src/utils/monitoring.ts index 5d2990e..acd8a24 100644 --- a/src/utils/monitoring.ts +++ b/src/utils/monitoring.ts @@ -376,8 +376,10 @@ export const listenBlocks = async ( api: ApiPromise, finalized: boolean, callBack: (blockDetails: RealtimeBlockDetails) => Promise, + blocksToMonitor: number = Infinity ) => { let latestBlockTime = 0; + let blockCount = 0; try { latestBlockTime = ( await api.query.timestamp.now.at((await api.rpc.chain.getBlock()).block.header.parentHash) @@ -398,6 +400,10 @@ export const listenBlocks = async ( elapsedMilliSecs: blockDetails.blockTime - latestBlockTime, }); latestBlockTime = blockDetails.blockTime; + blockCount++; + if (blockCount >= blocksToMonitor) { + unsubHeads(); + } }); return unsubHeads; }; diff --git a/test/monitor.test.ts b/test/monitor.test.ts new file mode 100644 index 0000000..84fc580 --- /dev/null +++ b/test/monitor.test.ts @@ -0,0 +1,12 @@ +import { exec } from 'child_process'; + +describe('Monitor Script', () => { + test('should run without errors', (done) => { + exec('node dist/monitor.cjs --networks moonriver', (error, stdout, stderr) => { + expect(error).toBeNull(); + expect(stderr).toBe(''); + expect(stdout).toContain('Monitoring'); + done(); + }); + }); +}); diff --git a/test/sample.test.ts b/test/sample.test.ts new file mode 100644 index 0000000..3972cdd --- /dev/null +++ b/test/sample.test.ts @@ -0,0 +1,7 @@ +import { sum } from '../src/utils/functions'; + +describe('Sample Test', () => { + test('adds 1 + 2 to equal 3', () => { + expect(sum(1, 2)).toBe(3); + }); +});