Skip to content

Commit

Permalink
Update imports and fix type errors in actor and visitor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Feb 17, 2024
1 parent 1753a78 commit 3386b7c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
13 changes: 9 additions & 4 deletions packages/store/test/actor.test.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -20,16 +24,17 @@ describe("createReActorStore", () => {
})

test("Uninitialized", () => {
const value = visitFunction.get_app(
new VisitRandomResponse<Example, "get_app">()
)
const value = visitFunction.get_app(new VisitRandomResponse<Example>())
const data = visitFunction.get_app(new VisitTransform<Example>(), {
value,
label: "app",
})

console.log(data.values?.[0].value)

const args = visitFunction.get_app(new VisitRandomArgs<Example>())
console.log(args)

const { methodState, initialized, initializing, error } =
actorStore.getState()

Expand Down
2 changes: 1 addition & 1 deletion packages/visitor/src/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
23 changes: 10 additions & 13 deletions packages/visitor/src/random/args.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
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.
* It returns the extracted service fields.
*
* @category Main
*/
export class VisitRandomArgs<
A = BaseActor,
M extends FunctionName<A> = FunctionName<A>
> extends IDL.Visitor<unknown, ExtractActorMethodArgs<A[M]> | unknown> {
public visitFunc<Method extends M>(
export class VisitRandomArgs<A = BaseActor> extends IDL.Visitor<
unknown,
unknown
> {
public visitFunc<Method extends FunctionName<A>>(
t: IDL.FuncClass,
functionName: Method
): ExtractActorMethodArgs<A[Method]> {
): ServiceDefaultValues<Method> {
const defaultValue = t.argTypes.reduce((acc, type, index) => {
acc[`arg${index}`] = type.accept(this, false)

return acc
}, {} as Record<string, unknown>)

const defaultValues = {
const result = {
[functionName]: defaultValue,
}

return defaultValues as unknown as ExtractActorMethodArgs<A[Method]>
return result as ServiceDefaultValues<Method>
}

public visitRecord(
Expand Down
28 changes: 14 additions & 14 deletions packages/visitor/src/random/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
*
* @category Main
*/
export class VisitRandomResponse<
A = BaseActor,
M extends FunctionName<A> = FunctionName<A>
> extends IDL.Visitor<unknown, ExtractActorMethodReturnType<A[M]> | unknown> {
public visitFunc<Method extends M>(
export class VisitRandomResponse<A = BaseActor> extends IDL.Visitor<
unknown,
unknown
> {
public visitFunc<Method extends FunctionName<A>>(
t: IDL.FuncClass
): ExtractActorMethodReturnType<A[Method]> {
return t.retTypes.map((type) =>
Expand All @@ -26,7 +26,7 @@ export class VisitRandomResponse<

public visitRecord(
_t: IDL.RecordClass,
fields: [string, IDL.Type<unknown>][],
fields: [string, IDL.Type][],
isRecursive: boolean
): unknown {
return fields.reduce((acc, [key, type]) => {
Expand All @@ -37,7 +37,7 @@ export class VisitRandomResponse<

public visitVariant(
_t: IDL.VariantClass,
fields: [string, IDL.Type<unknown>][],
fields: [string, IDL.Type][],
isRecursive: boolean
): unknown {
const [key, type] = fields[Math.floor(Math.random() * fields.length)]
Expand All @@ -48,7 +48,7 @@ export class VisitRandomResponse<

public visitVec<T>(
_t: IDL.VecClass<T>,
type: IDL.Type<unknown>,
type: IDL.Type,
isRecursive: boolean
): unknown {
if (isRecursive) {
Expand All @@ -63,7 +63,7 @@ export class VisitRandomResponse<

public visitOpt<T>(
_t: IDL.OptClass<T>,
type: IDL.Type<unknown>,
type: IDL.Type,
isRecursive: boolean
): unknown {
if (Math.random() < 0.5) {
Expand All @@ -73,29 +73,29 @@ export class VisitRandomResponse<
}
}

public visitTuple<T extends unknown[]>(
public visitTuple<T extends IDL.Type[]>(
_t: IDL.TupleClass<T>,
components: IDL.Type<unknown>[],
components: IDL.Type[],
isRecursive: boolean
): unknown {
return components.map((type) => type.accept(this, isRecursive))
}

private savedRec: Record<string, unknown> = {}

public visitRec<T>(_t: IDL.RecClass<T>, ty: IDL.ConstructType<T>): unknown {
public visitRec<T>(_t: IDL.RecClass<T>, ty: IDL.ConstructType<T>) {
if (!this.savedRec[ty.name]) {
this.savedRec[ty.name] = ty.accept(this, true)
}

return this.savedRec[ty.name]
}

public visitType<T>(_t: IDL.Type<T>): unknown {
public visitType<T>(_t: IDL.Type<T>) {
return Math.random().toString(36).substring(6)
}

public visitPrincipal(_t: IDL.PrincipalClass): unknown {
public visitPrincipal(_t: IDL.PrincipalClass) {
return Principal.fromUint8Array(this.generateRandomBytes(29))
}

Expand Down
9 changes: 5 additions & 4 deletions packages/visitor/test/visitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
VisitTransformTable,
VisitFields,
VisitDetails,
VisitRandomArgs,
} from "../src"
import { b3system, idlFactory } from "./candid/b3system"

Expand All @@ -23,13 +24,13 @@ describe("createReActorStore", () => {

test("Uninitialized", () => {
const field = visitFunction.get_app(new VisitFields<B3System>())
console.log(field.defaultValues)
console.log(field)
const args = visitFunction.get_app(new VisitRandomArgs<B3System>())
console.log(args)
const details = visitFunction.get_app(new VisitDetails())
console.log(details)

const value = visitFunction.get_app(
new VisitRandomResponse<B3System, "get_app">()
)
const value = visitFunction.get_app(new VisitRandomResponse<B3System>())
console.log(value)

const transform = visitFunction.get_app(
Expand Down

0 comments on commit 3386b7c

Please sign in to comment.