/**
 * Copyright 2018 Twitter, Inc.
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 */ import { SqrlInstance , FunctionCostData } from "../function/Instance" ; import { registerAllFunctions } from "../function/registerAllFunctions" ; import { AssertService } from "sqrl-common" ; import { Instance , FunctionServices } from "../api/execute" ; import { getDefaultConfig , Config } from "../api/config" ; export class CapturingAssertService implements AssertService { private firstError : Error ; private captureError ( err ) { this . firstError = this . firstError || err ; console . log ( err . message ) ; } compare ( left , operator , right , arrow ) { try { ( expect ( left ) as any ) . toSqrlCompare ( operator , right , arrow ) ; } catch ( err ) { this . captureError ( err ) ; } } ok ( value , arrow ) { try { ( expect ( value ) as any ) . toBeSqrlTruthy ( arrow ) ; } catch ( err ) { this . captureError ( err ) ; } } throwFirstError ( ) { if ( this . firstError ) { throw this . firstError ; } } } export async function buildTestInstance ( options = { } ) { const instance = new SqrlInstance ( { functionCost : options . functionCost , } ) ; let assert ; try { const { JestAssertService } = await require ( "sqrl-test-utils" ) ; assert = new JestAssertService ( ) ; } catch ( err ) { assert = new CapturingAssertService ( ) ; } registerAllFunctions ( instance , { assert , ... ( options . services || { } ) , } ) ; return new Instance ( { ... getDefaultConfig ( ) , "state.allow-in-memory" : true , ... ( options . config || { } ) , } , instance ) ; }