Skip to content

Commit 0432498

Browse files
committed
✅ add test for the cli script
This commit adds a test for the cli script. Also I take the opportunity to: - Reorganize the tests folder by creating a dedicated file for the execute function and to import the fixtures - Add a pre npm script to build the project before running the tests as it is required to run the cli script
1 parent 0bd39c8 commit 0432498

File tree

8 files changed

+79
-40
lines changed

8 files changed

+79
-40
lines changed

.github/workflows/release-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ jobs:
3333
node-version: 18
3434
registry-url: https://npm.pkg.github.com/
3535
- run: npm ci
36+
# the test command starts by building the package
3637
- run: npm run test
37-
- run: npm run build
3838
- run: npm publish
3939
env:
4040
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"build": "parcel build",
2424
"build:verbose": "npm run build -- --detailed-report",
2525
"watch": "parcel watch",
26+
"pretest": "npm run build -- --log-level=none",
2627
"test": "jest --testPathIgnorePatterns=tests/differential",
2728
"test:coverage": "npm run test -- --coverage",
2829
"test:verbose": "npm run test -- --verbose",

tests/cli.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import execute from "./utils/execute";
2+
import fixtures from "./fixtures";
3+
4+
describe("cli", () => {
5+
it("test the cli script works as expected", async () => {
6+
// get a random fixture data from the list
7+
const { pubkey, points: referencePoints } =
8+
fixtures.data[Math.floor(Math.random() * fixtures.data.length)];
9+
10+
// precompute the points using the cli script by providing the public key
11+
const precomputedPoints = (await execute(
12+
`node ./cli.mjs ${pubkey.x} ${pubkey.y}`
13+
)) as string;
14+
15+
// check the precomputed points are the same than the reference points
16+
expect(precomputedPoints.trim()).toEqual(referencePoints);
17+
});
18+
});

tests/differential/index.spec.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
1-
import childProcess from "child_process";
21
import { secp256r1 } from "@noble/curves/p256";
32
import precomputePoints from "../../src";
4-
5-
/**
6-
* Executes a shell command and returns its output as a string.
7-
*
8-
* @param {string} command A shell command to execute
9-
* @return {Promise<string>} A promise that resolve to the output of the shell command, or an error
10-
*
11-
* @example
12-
* const output = await execute("ls -alh");
13-
*/
14-
function execute(command) {
15-
return new Promise(function (resolve, reject) {
16-
/**
17-
* @param {Error} error An error triggered during the execution of the childProcess.exec command
18-
* @param {string|Buffer} standardOutput The result of the shell command execution
19-
* @param {string|Buffer} standardError The error resulting of the shell command execution
20-
* @see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
21-
*/
22-
childProcess.exec(command, function (error, standardOutput, standardError) {
23-
if (error) {
24-
reject();
25-
return;
26-
}
27-
28-
if (standardError) {
29-
reject(standardError);
30-
return;
31-
}
32-
33-
resolve(standardOutput);
34-
});
35-
});
36-
}
3+
import execute from "../utils/execute";
374

385
describe("differential testing", () => {
396
const NUMBER_OF_TESTS = 15;

tests/fixtures.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import data from "./fixtures.json";
2+
3+
// cast the JSON to the expected types
4+
type Fixture = { data: { pubkey: { x: string; y: string }; points: string }[] };
5+
const fixtures = data as Fixture;
6+
7+
export default fixtures;

tests/index.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import precomputePoints from "../src";
22
import { secp256r1 } from "@noble/curves/p256";
33
import isPointOnCurve from "./utils/isPointOnCurve";
4-
import data from "./fixtures.json";
5-
6-
// cast the JSON to the expected types
7-
type Fixture = { data: { pubkey: { x: string; y: string }; points: string }[] };
8-
const fixtures = data as Fixture;
4+
import fixtures from "./fixtures";
95

106
/**
117
* Splits a concatenated string of x and y coordinates into an array of {x, y} points.

tests/utils/execute.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import execute from "./execute";
2+
3+
describe("execute", () => {
4+
it("Trigger an error during the execution of the childProcess.exec command", async () => {
5+
await expect(execute("addawafafafafawdf")).rejects.toBe(undefined);
6+
});
7+
// @DEV: This test is not working, it doesn't pass into the expected branch
8+
xit("Trigger an error in the shell command", async () => {
9+
await expect(execute("echo hello | grep \"asdf\"")).rejects.toBe(undefined);
10+
});
11+
it("ok", async () => {
12+
await expect(execute("echo yo")).resolves.toMatch("yo");
13+
});
14+
});

tests/utils/execute.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import childProcess from "child_process";
2+
3+
/**
4+
* Executes a shell command and returns its output as a string.
5+
*
6+
* @param {string} command A shell command to execute
7+
* @return {Promise<string>} A promise that resolve to the output of the shell command, or an error
8+
*
9+
* @example
10+
* const output = await execute("ls -alh");
11+
*/
12+
function execute(command) {
13+
return new Promise(function (resolve, reject) {
14+
/**
15+
* @param {Error} error An error triggered during the execution of the childProcess.exec command
16+
* @param {string|Buffer} standardOutput The result of the shell command execution
17+
* @param {string|Buffer} standardError The error resulting of the shell command execution
18+
* @see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
19+
*/
20+
childProcess.exec(command, function (error, standardOutput, standardError) {
21+
if (error) {
22+
reject();
23+
return;
24+
}
25+
26+
if (standardError) {
27+
reject(standardError);
28+
return;
29+
}
30+
31+
resolve(standardOutput);
32+
});
33+
});
34+
}
35+
36+
export default execute;

0 commit comments

Comments
 (0)