import { join } from "path" ; import { readFileSync , readdirSync } from "fs" ; import { fromHexString , Json , Type } from "@chainsafe/ssz" ; import { uncompress } from "snappyjs" ; import { loadYamlFile } from "./util" ; export interface IValidTestcase < T > { path : string ; root : Uint8Array ; serialized : Uint8Array ; value : T ; } export interface IInvalidTestcase { path : string ; serialized : Uint8Array ; } export function parseValidTestcase < T > ( path , type ) { const meta = loadYamlFile ( join ( path , "meta.yaml" ) ) ; const root = fromHexString ( meta . root as string ) ; const serialized = uncompress < Uint8Array > ( readFileSync ( join ( path , "serialized.ssz_snappy" ) ) ) ; const value = type . fromJson ( loadYamlFile ( join ( path , "value.yaml" ) ) as Json ) as T ; return { path , root , serialized , value , } ; } export function parseInvalidTestcase ( path ) { const serialized = uncompress ( readFileSync ( join ( path , "serialized.ssz_snappy" ) ) ) ; return { path , serialized , } ; } export function getValidTestcases < T > ( path , prefix , type ) { const subdirs = readdirSync ( path ) ; return subdirs . filter ( ( dir ) => dir . includes ( prefix ) ) . map ( ( d ) => join ( path , d ) ) . map ( ( p ) => parseValidTestcase ( p , type ) ) as IValidTestcase < T > [ ] ; } export function getInvalidTestcases ( path , prefix ) { const subdirs = readdirSync ( path ) ; return subdirs . filter ( ( dir ) => dir . includes ( prefix ) ) . map ( ( d ) => join ( path , d ) ) . map ( parseInvalidTestcase ) ; }