From 3386b7cd3042c647de4495202d2ef20f05daf694 Mon Sep 17 00:00:00 2001 From: b3hr4d Date: Sat, 17 Feb 2024 19:38:56 +0300 Subject: [PATCH] Update imports and fix type errors in actor and visitor tests --- packages/store/test/actor.test.ts | 13 ++++++++---- packages/visitor/src/fields/index.ts | 2 +- packages/visitor/src/random/args.ts | 23 +++++++++----------- packages/visitor/src/random/response.ts | 28 ++++++++++++------------- packages/visitor/test/visitor.test.ts | 9 ++++---- 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/packages/store/test/actor.test.ts b/packages/store/test/actor.test.ts index 56c36bd416..54f625e338 100644 --- a/packages/store/test/actor.test.ts +++ b/packages/store/test/actor.test.ts @@ -1,4 +1,8 @@ -import { VisitRandomResponse, VisitTransform } from "../../visitor/src" +import { + VisitRandomArgs, + VisitRandomResponse, + VisitTransform, +} from "../../visitor/src" import { createReActorStore } from "../src" import { example, idlFactory } from "./candid/example" @@ -20,9 +24,7 @@ describe("createReActorStore", () => { }) test("Uninitialized", () => { - const value = visitFunction.get_app( - new VisitRandomResponse() - ) + const value = visitFunction.get_app(new VisitRandomResponse()) const data = visitFunction.get_app(new VisitTransform(), { value, label: "app", @@ -30,6 +32,9 @@ describe("createReActorStore", () => { console.log(data.values?.[0].value) + const args = visitFunction.get_app(new VisitRandomArgs()) + console.log(args) + const { methodState, initialized, initializing, error } = actorStore.getState() diff --git a/packages/visitor/src/fields/index.ts b/packages/visitor/src/fields/index.ts index d6acdc419a..cbc248b232 100644 --- a/packages/visitor/src/fields/index.ts +++ b/packages/visitor/src/fields/index.ts @@ -53,7 +53,7 @@ export class VisitFields< acc.fields.push(field) acc.defaultValue[`arg${index}`] = - field.defaultValue || field.defaultValues + field.defaultValue ?? field.defaultValues ?? {} return acc }, diff --git a/packages/visitor/src/random/args.ts b/packages/visitor/src/random/args.ts index bb2b3030f9..04e509210a 100644 --- a/packages/visitor/src/random/args.ts +++ b/packages/visitor/src/random/args.ts @@ -1,10 +1,7 @@ import { Principal } from "@dfinity/principal" import { IDL } from "@dfinity/candid" -import { - BaseActor, - ExtractActorMethodArgs, - FunctionName, -} from "@ic-reactor/store" +import { BaseActor, FunctionName } from "@ic-reactor/store" +import { ServiceDefaultValues } from "../fields" /** * Visit the candid file and extract the fields. @@ -12,25 +9,25 @@ import { * * @category Main */ -export class VisitRandomArgs< - A = BaseActor, - M extends FunctionName = FunctionName -> extends IDL.Visitor | unknown> { - public visitFunc( +export class VisitRandomArgs extends IDL.Visitor< + unknown, + unknown +> { + public visitFunc>( t: IDL.FuncClass, functionName: Method - ): ExtractActorMethodArgs { + ): ServiceDefaultValues { const defaultValue = t.argTypes.reduce((acc, type, index) => { acc[`arg${index}`] = type.accept(this, false) return acc }, {} as Record) - const defaultValues = { + const result = { [functionName]: defaultValue, } - return defaultValues as unknown as ExtractActorMethodArgs + return result as ServiceDefaultValues } public visitRecord( diff --git a/packages/visitor/src/random/response.ts b/packages/visitor/src/random/response.ts index 760e0615d6..f8dab6d444 100644 --- a/packages/visitor/src/random/response.ts +++ b/packages/visitor/src/random/response.ts @@ -12,11 +12,11 @@ import { * * @category Main */ -export class VisitRandomResponse< - A = BaseActor, - M extends FunctionName = FunctionName -> extends IDL.Visitor | unknown> { - public visitFunc( +export class VisitRandomResponse extends IDL.Visitor< + unknown, + unknown +> { + public visitFunc>( t: IDL.FuncClass ): ExtractActorMethodReturnType { return t.retTypes.map((type) => @@ -26,7 +26,7 @@ export class VisitRandomResponse< public visitRecord( _t: IDL.RecordClass, - fields: [string, IDL.Type][], + fields: [string, IDL.Type][], isRecursive: boolean ): unknown { return fields.reduce((acc, [key, type]) => { @@ -37,7 +37,7 @@ export class VisitRandomResponse< public visitVariant( _t: IDL.VariantClass, - fields: [string, IDL.Type][], + fields: [string, IDL.Type][], isRecursive: boolean ): unknown { const [key, type] = fields[Math.floor(Math.random() * fields.length)] @@ -48,7 +48,7 @@ export class VisitRandomResponse< public visitVec( _t: IDL.VecClass, - type: IDL.Type, + type: IDL.Type, isRecursive: boolean ): unknown { if (isRecursive) { @@ -63,7 +63,7 @@ export class VisitRandomResponse< public visitOpt( _t: IDL.OptClass, - type: IDL.Type, + type: IDL.Type, isRecursive: boolean ): unknown { if (Math.random() < 0.5) { @@ -73,9 +73,9 @@ export class VisitRandomResponse< } } - public visitTuple( + public visitTuple( _t: IDL.TupleClass, - components: IDL.Type[], + components: IDL.Type[], isRecursive: boolean ): unknown { return components.map((type) => type.accept(this, isRecursive)) @@ -83,7 +83,7 @@ export class VisitRandomResponse< private savedRec: Record = {} - public visitRec(_t: IDL.RecClass, ty: IDL.ConstructType): unknown { + public visitRec(_t: IDL.RecClass, ty: IDL.ConstructType) { if (!this.savedRec[ty.name]) { this.savedRec[ty.name] = ty.accept(this, true) } @@ -91,11 +91,11 @@ export class VisitRandomResponse< return this.savedRec[ty.name] } - public visitType(_t: IDL.Type): unknown { + public visitType(_t: IDL.Type) { return Math.random().toString(36).substring(6) } - public visitPrincipal(_t: IDL.PrincipalClass): unknown { + public visitPrincipal(_t: IDL.PrincipalClass) { return Principal.fromUint8Array(this.generateRandomBytes(29)) } diff --git a/packages/visitor/test/visitor.test.ts b/packages/visitor/test/visitor.test.ts index 9001fb76e2..2ddeb7edc3 100644 --- a/packages/visitor/test/visitor.test.ts +++ b/packages/visitor/test/visitor.test.ts @@ -4,6 +4,7 @@ import { VisitTransformTable, VisitFields, VisitDetails, + VisitRandomArgs, } from "../src" import { b3system, idlFactory } from "./candid/b3system" @@ -23,13 +24,13 @@ describe("createReActorStore", () => { test("Uninitialized", () => { const field = visitFunction.get_app(new VisitFields()) - console.log(field.defaultValues) + console.log(field) + const args = visitFunction.get_app(new VisitRandomArgs()) + console.log(args) const details = visitFunction.get_app(new VisitDetails()) console.log(details) - const value = visitFunction.get_app( - new VisitRandomResponse() - ) + const value = visitFunction.get_app(new VisitRandomResponse()) console.log(value) const transform = visitFunction.get_app(