Skip to content

Commit

Permalink
Add unittest package
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Feb 20, 2025
1 parent 89d89b3 commit ead5629
Show file tree
Hide file tree
Showing 25 changed files with 1,695 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/unittest/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
node_modules/
misc/

*test.js
*test.ts
*test.d.ts
*test.d.ts.map
*spec.js
*spec.ts
*spec.d.ts
*spec.d.ts.map

tsconfig.json
tsconfig.*.json
eslint.config.mjs
.prettierrc
.prettierignore

tsconfig.tsbuildinfo
tsconfig.*.tsbuildinfo
.github/
13 changes: 13 additions & 0 deletions packages/unittest/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/

dist/
dist.commonjs/

.npmignore
.prettierrc
tsconfig.json
eslint.config.mjs
.prettierrc

tsconfig.tsbuildinfo
.github/
5 changes: 5 additions & 0 deletions packages/unittest/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"trailingComma": "all",
"plugins": ["prettier-plugin-organize-imports"]
}
396 changes: 396 additions & 0 deletions packages/unittest/CHANGELOG.md

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions packages/unittest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<p align="center">
<a href="https://app.ckbccc.com/">
<img alt="Logo" src="https://raw.githubusercontent.com/ckb-devrel/ccc/master/assets/logoAndText.svg" style="height: 8rem; max-width: 90%; padding: 0.5rem 0;" />
</a>
</p>

<h1 align="center" style="font-size: 48px;">
CCC's unittest framework
</h1>

<p align="center">
<a href="https://www.npmjs.com/package/@ckb-ccc/okx"><img
alt="NPM Version" src="https://img.shields.io/npm/v/%40ckb-ccc%2Fokx"
/></a>
<img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/ckb-devrel/ccc" />
<img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/ckb-devrel/ccc/master" />
<img alt="GitHub branch check runs" src="https://img.shields.io/github/check-runs/ckb-devrel/ccc/master" />
<a href="https://live.ckbccc.com/"><img
alt="Playground" src="https://img.shields.io/website?url=https%3A%2F%2Flive.ckbccc.com%2F&label=Playground"
/></a>
<a href="https://app.ckbccc.com/"><img
alt="App" src="https://img.shields.io/website?url=https%3A%2F%2Fapp.ckbccc.com%2F&label=App"
/></a>
<a href="https://docs.ckbccc.com/"><img
alt="Docs" src="https://img.shields.io/website?url=https%3A%2F%2Fdocs.ckbccc.com%2F&label=Docs"
/></a>
</p>

<p align="center">
CCC - CKBers' Codebase is a one-stop solution for your CKB JS/TS ecosystem development.
<br />
Empower yourself with CCC to discover the unlimited potential of CKB.
<br />
Interoperate with wallets from different chain ecosystems.
<br />
Fully enabling CKB's Turing completeness and cryptographic freedom power.
</p>

<h3 align="center">
Read more about CCC on <a href="https://docs.ckbccc.com">our website</a> or <a href="https://github.com/ckb-devrel/ccc">GitHub Repo</a>.
</h3>
45 changes: 45 additions & 0 deletions packages/unittest/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check

import eslint from "@eslint/js";
import eslintPluginPrettierRecommended from "eslint-plugin-prettier";
import tseslint from "typescript-eslint";

import { dirname } from "path";
import { fileURLToPath } from "url";

export default tseslint.config({
files: ["**/*.ts"],
extends: [
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
],
rules: {
"@typescript-eslint/no-unused-vars": [
"error",
{
args: "all",
argsIgnorePattern: "^_",
caughtErrors: "all",
caughtErrorsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
varsIgnorePattern: "^_",
ignoreRestSiblings: true,
},
],
"@typescript-eslint/unbound-method": ["error", { ignoreStatic: true }],
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/require-await": "off",
"no-empty": "off",
"prefer-const": [
"error",
{ ignoreReadBeforeAssign: true, destructuring: "all" },
],
},
plugins: { prettier: eslintPluginPrettierRecommended },
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: dirname(fileURLToPath(import.meta.url)),
},
},
});
6 changes: 6 additions & 0 deletions packages/unittest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "/dist/", "/dist.commonjs/"],
};
3 changes: 3 additions & 0 deletions packages/unittest/misc/basedirs/dist.commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
3 changes: 3 additions & 0 deletions packages/unittest/misc/basedirs/dist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
54 changes: 54 additions & 0 deletions packages/unittest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "mohanson-unittest",
"version": "0.1.8",
"description": "CCC - CKBer's Codebase. Unittest framework.",
"author": "Mohanson <[email protected]>",
"license": "MIT",
"private": false,
"homepage": "https://github.com/ckb-devrel/ccc",
"repository": {
"type": "git",
"url": "git://github.com/ckb-devrel/ccc.git"
},
"sideEffects": false,
"main": "dist.commonjs/index.js",
"module": "dist/index.js",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist.commonjs/index.js",
"default": "./dist.commonjs/index.js"
},
"./advanced": {
"import": "./dist/advanced.js",
"require": "./dist.commonjs/advanced.js",
"default": "./dist.commonjs/advanced.js"
}
},
"scripts": {
"build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json && copyfiles -u 2 misc/basedirs/**/* .",
"format": "prettier --write . && eslint --fix ./src",
"lint": "eslint ./src",
"test": "jest"
},
"devDependencies": {
"@eslint/js": "^9.1.1",
"copyfiles": "^2.4.1",
"eslint": "^9.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"rimraf": "^5.0.5",
"typescript": "^5.4.5",
"typescript-eslint": "^7.7.0"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@ckb-ccc/core": "workspace:*",
"@types/node": "^22.13.4",
"jest": "^29.7.0"
}
}
2 changes: 2 additions & 0 deletions packages/unittest/src/advanced.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./advancedBarrel.js";
export * as cccA from "./advancedBarrel.js";
1 change: 1 addition & 0 deletions packages/unittest/src/advancedBarrel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./unittest/index.js";
1 change: 1 addition & 0 deletions packages/unittest/src/barrel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./unittest/index.js";
2 changes: 2 additions & 0 deletions packages/unittest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./barrel.js";
export * as ccc from "./barrel.js";
119 changes: 119 additions & 0 deletions packages/unittest/src/unittest/core.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { hexFrom, Transaction } from "@ckb-ccc/core";
import assert from "assert";
import { readFileSync } from "fs";
import {
DEFAULT_SCRIPT_ALWAYS_FAILURE,
DEFAULT_SCRIPT_ALWAYS_SUCCESS,
parseAllCycles,
parseRunResult,
Resource,
UnitTestClient,
Verifier,
} from "./index";

describe("example", () => {
test("alwaysSuccess", () => {
const resource = Resource.default();
const tx = Transaction.default();

// deploy a cell with risc-v binary, return a script.
const lockScript = resource.deployCell(
hexFrom(readFileSync(DEFAULT_SCRIPT_ALWAYS_SUCCESS)),
tx,
false,
);
// update args
lockScript.args = "0xEEFF";

// mock a input cell with the created script as lock script
const inputCell = resource.mockCell(lockScript);

// add input cell to the transaction
tx.inputs.push(Resource.createCellInput(inputCell));
// add output cell to the transaction
tx.outputs.push(Resource.createCellOutput(lockScript));
// add output data to the transaction
tx.outputsData.push(hexFrom("0x"));

// verify the transaction
const verifier = Verifier.from(resource, tx);
verifier.verifySuccess();
// turn off console.log
jest.spyOn(console, "log").mockImplementation(() => {});
expect(() => verifier.verifyFailure()).toThrow(
"Transaction verification should fail. No verification failure occurred.",
);
jest.spyOn(console, "log").mockRestore();
});

test("alwaysFailure", () => {
const resource = Resource.default();
const tx = Transaction.default();

const lockScript = resource.deployCell(
hexFrom(readFileSync(DEFAULT_SCRIPT_ALWAYS_FAILURE)),
tx,
false,
);
const inputCell = resource.mockCell(lockScript);
tx.inputs.push(Resource.createCellInput(inputCell));

const verifier = Verifier.from(resource, tx);
verifier.verifyFailure();
verifier.verifyFailure(-1);
// turn off console.log
jest.spyOn(console, "log").mockImplementation(() => {});
expect(() => verifier.verifySuccess()).toThrow(
"Transaction verification failed. See details above.",
);
expect(() => verifier.verifyFailure(2)).toThrow(
"Transaction verification failed with unexpected error code: expected 2, got -1. See details above.",
);
jest.spyOn(console, "log").mockRestore();
});

test("parse", () => {
const resource = Resource.default();
const tx = Transaction.default();

const lockScript = resource.deployCell(
hexFrom(readFileSync(DEFAULT_SCRIPT_ALWAYS_FAILURE)),
tx,
true,
);
const inputCell = resource.mockCell(lockScript);
tx.inputs.push(Resource.createCellInput(inputCell));

const verifier = Verifier.from(resource, tx);
const result = verifier.verify()[0];
assert(parseRunResult(result.stdout.toString()) == -1);
assert(parseAllCycles(result.stdout.toString()) == 543);
});
test("signHashInfo", async () => {
const resource = Resource.default();
const tx = Transaction.default();
const client = new UnitTestClient(resource);

const lockScript = resource.deployCell(
hexFrom(readFileSync(DEFAULT_SCRIPT_ALWAYS_SUCCESS)),
tx,
false,
);
const inputCell = resource.mockCell(lockScript);
tx.inputs.push(Resource.createCellInput(inputCell));
// the format of witness should follow WitnessArgs
tx.witnesses.push(
hexFrom(
"0x5500000010000000550000005500000041000000725e20eeee617616f881e65773fdb8d0f2d91619a71cfe18121f3fef67f9cfcb0c019c66ebf67ef2f41123443a786c554a5287ff2a3e92725fa14634c4f1550f01",
),
);
// extra witness, anything is OK
tx.witnesses.push(hexFrom("0x00112233445566778899aabbccddeeff"));

await tx.prepareSighashAllWitness(lockScript, 0, client);
const sigHashAll = await tx.getSignHashInfo(lockScript, client);
assert(sigHashAll?.message.length == 66);
const verifier = Verifier.from(resource, tx);
verifier.verifySuccess();
});
});
Loading

0 comments on commit ead5629

Please sign in to comment.