From cbab8d9882e52e730dba8c3c362c9ffce9b81b78 Mon Sep 17 00:00:00 2001 From: homura Date: Wed, 8 Nov 2023 13:16:57 +0800 Subject: [PATCH] feat: make data2 works with ScriptConfig --- packages/config-manager/src/manager.ts | 18 +++++++++----- .../tests/{validator.js => validator.test.ts} | 24 ++++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) rename packages/config-manager/tests/{validator.js => validator.test.ts} (51%) diff --git a/packages/config-manager/src/manager.ts b/packages/config-manager/src/manager.ts index 80ef87e06..98869cc6a 100644 --- a/packages/config-manager/src/manager.ts +++ b/packages/config-manager/src/manager.ts @@ -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!`); + } +} + function assert(condition: unknown, debugPath = "variable"): asserts condition { if (!condition) throw new Error(`${debugPath} is not valid`); } @@ -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; diff --git a/packages/config-manager/tests/validator.js b/packages/config-manager/tests/validator.test.ts similarity index 51% rename from packages/config-manager/tests/validator.js rename to packages/config-manager/tests/validator.test.ts index f9f6c2edf..d5c0623c5 100644 --- a/packages/config-manager/tests/validator.js +++ b/packages/config-manager/tests/validator.test.ts @@ -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!`); @@ -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"); + }); +});