forked from ckb-devrel/ccc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,695 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"singleQuote": false, | ||
"trailingComma": "all", | ||
"plugins": ["prettier-plugin-organize-imports"] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./advancedBarrel.js"; | ||
export * as cccA from "./advancedBarrel.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./unittest/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./unittest/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./barrel.js"; | ||
export * as ccc from "./barrel.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.