Skip to content

Commit

Permalink
Add test for fun
Browse files Browse the repository at this point in the history
Add tests and update monitor script to support block monitoring parameter.

* Add `test/sample.test.ts` with a sample test case using Jest.
* Add `test/monitor.test.ts` with a test case for the monitor script using Jest.
* Modify `.github/workflows/check.yml` and `.github/workflows/verify-compilation.yml` to include jobs to run tests using Jest.
* Modify `.dockerignore` and `.npmignore` to ensure the `test` directory is not excluded.
* Modify `src/tools/monitor.ts` to add a new feature to the monitor script that uses a parameter to specify the number of blocks to monitor until it stops.
* Modify `src/utils/monitoring.ts` to update the `listenBlocks` function to support the new parameter as optional.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Moonsong-Labs/moonbeam-tools?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
crystalin committed Oct 17, 2024
1 parent 721904e commit 7903828
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/test
/bins
/build
/dist
Expand All @@ -7,4 +6,4 @@
.github

*.log
*.bak
*.bak
15 changes: 15 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions .github/workflows/verify-compilation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/scripts
/test
/bins
/build
/docker
Expand All @@ -10,4 +9,4 @@
.github

*.log
*.bak
*.bak
7 changes: 6 additions & 1 deletion src/tools/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }));

Check failure on line 44 in src/tools/monitor.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type '{ network: string; finalized: boolean; blocks: number; }' is not assignable to parameter of type 'Argv'.

Check failure on line 44 in src/tools/monitor.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type '{ network: string; finalized: boolean; blocks: number; }' is not assignable to parameter of type 'Argv'.
} else {
getMonitoredApiFor(argv);
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,10 @@ export const listenBlocks = async (
api: ApiPromise,
finalized: boolean,
callBack: (blockDetails: RealtimeBlockDetails) => Promise<void>,
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)
Expand All @@ -398,6 +400,10 @@ export const listenBlocks = async (
elapsedMilliSecs: blockDetails.blockTime - latestBlockTime,
});
latestBlockTime = blockDetails.blockTime;
blockCount++;
if (blockCount >= blocksToMonitor) {
unsubHeads();
}
});
return unsubHeads;
};
Expand Down
12 changes: 12 additions & 0 deletions test/monitor.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
7 changes: 7 additions & 0 deletions test/sample.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 7903828

Please sign in to comment.