Skip to content

Commit

Permalink
feat: make data2 works with ScriptConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Nov 8, 2023
1 parent 1c33153 commit cbab8d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
18 changes: 12 additions & 6 deletions packages/config-manager/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ function assertInteger(debugPath: string, i: string) {
}
}

export function assertHashType(debugPath: string, hashType: string): void {
if (
hashType !== "type" &&
hashType !== "data" &&
hashType !== "data1" &&
hashType !== "data2"
) {
throw new Error(`${debugPath} must one of type, data, data1, data2!`);
}

Check warning on line 36 in packages/config-manager/src/manager.ts

View check run for this annotation

Codecov / codecov/patch

packages/config-manager/src/manager.ts#L35-L36

Added lines #L35 - L36 were not covered by tests
}

function assert(condition: unknown, debugPath = "variable"): asserts condition {
if (!condition) throw new Error(`${debugPath} is not valid`);
}
Expand All @@ -41,12 +52,7 @@ export function validateConfig(config: Config): void {
assert(scriptConfig?.CODE_HASH);

assertHash(`SCRIPTS.${scriptName}.CODE_HASH`, scriptConfig.CODE_HASH);
const hashType = scriptConfig.HASH_TYPE;
if (hashType !== "type" && hashType !== "data" && hashType !== "data1") {
throw new Error(
`SCRIPTS.${scriptName}.HASH_TYPE must be type, data or data1!`
);
}
assertHashType(`SCRIPTS.${scriptName}.HASH_TYPE`, scriptConfig.HASH_TYPE);
assertHash(`SCRIPTS.${scriptName}.TX_HASH`, scriptConfig.TX_HASH);
assertInteger(`SCRIPTS.${scriptName}.INDEX`, scriptConfig.INDEX);
const depType = scriptConfig.DEP_TYPE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const test = require("ava");

const { validateConfig, predefined } = require("../lib/index");
import test from "ava";
import { validateConfig, predefined } from "../src";
import { assertHashType } from "../src/manager";

test.before(() => {
BigInt = () => {
BigInt = (() => {
throw new Error("can not find bigint");
};
}) as unknown as BigIntConstructor;
});

test("validate all predefined config", (t) => {
for (const name of Object.keys(predefined)) {
const keys = Object.keys(predefined) as [keyof typeof predefined];
for (const name of keys) {
t.notThrows(() => {
validateConfig(predefined[name]);
}, `Predefined config ${name} fails verification!`);
Expand All @@ -30,12 +31,23 @@ test("invalidate config", (t) => {
validateConfig({
PREFIX: "ckb",
SCRIPTS: {
//@ts-expect-error
ABC: {},
},
});
});

t.throws(() => {
//@ts-expect-error
validateConfig(null);
});
});

test("data2 works with ScriptConfig", (t) => {
t.notThrows(() => {
assertHashType("debugPath", "type");
assertHashType("debugPath", "data");
assertHashType("debugPath", "data1");
assertHashType("debugPath", "data2");
});
});

1 comment on commit cbab8d9

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-ckb2023-cbab8d9-20231108055648

npm install @ckb-lumos/[email protected]

Please sign in to comment.