UNPKG

testeranto

Version:

the AI powered BDD test framework for typescript projects

188 lines (165 loc) 4.96 kB
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-empty-object-type */ /* eslint-disable no-async-promise-executor */ /* eslint-disable @typescript-eslint/no-explicit-any */ // Do not add logging to this file as it is used by the pure runtime. import { PassThrough } from "stream"; import type { Ibdd_in_any, Ibdd_out_any, ITestSpecification, } from "../CoreTypes"; import { ITestJob, ITLog, IFinalResults, ITTestResourceRequest } from "."; import { ISuiteKlasser, IGivenKlasser, IWhenKlasser, IThenKlasser, IPM, } from "./types.js"; import { BaseWhen, BaseThen, BaseGiven } from "./abstractBase.js"; import { BaseSuite } from "./BaseSuite"; export abstract class BaseBuilder< I extends Ibdd_in_any, O extends Ibdd_out_any, SuiteExtensions, GivenExtensions, WhenExtensions, ThenExtensions > { specs: any; assertThis: (t: I["then"]) => any; testResourceRequirement: ITTestResourceRequest; artifacts: Promise<unknown>[] = []; testJobs: ITestJob[]; testSpecification: ITestSpecification<I, O>; suitesOverrides: Record<keyof SuiteExtensions, ISuiteKlasser<I, O>>; givenOverides: Record<keyof GivenExtensions, IGivenKlasser<I>>; whenOverides: Record<keyof WhenExtensions, IWhenKlasser<I>>; thenOverides: Record<keyof ThenExtensions, IThenKlasser<I>>; puppetMaster: IPM; constructor( input: I["iinput"], suitesOverrides: Record<keyof SuiteExtensions, ISuiteKlasser<I, O>>, givenOverides: Record<keyof GivenExtensions, IGivenKlasser<I>>, whenOverides: Record<keyof WhenExtensions, IWhenKlasser<I>>, thenOverides: Record<keyof ThenExtensions, IThenKlasser<I>>, testResourceRequirement: ITTestResourceRequest, testSpecification: any ) { this.artifacts = []; this.testResourceRequirement = testResourceRequirement; this.suitesOverrides = suitesOverrides; this.givenOverides = givenOverides; this.whenOverides = whenOverides; this.thenOverides = thenOverides; this.testSpecification = testSpecification; this.specs = testSpecification( this.Suites(), this.Given(), this.When(), this.Then() ); this.testJobs = this.specs.map((suite: BaseSuite<I, O>) => { const suiteRunner = (suite: BaseSuite<I, O>) => async (puppetMaster: IPM, tLog: ITLog): Promise<BaseSuite<I, O>> => { try { const x = await suite.run( input, puppetMaster.testResourceConfiguration, (fPath: string, value: string | Buffer | PassThrough) => puppetMaster.testArtiFactoryfileWriter( tLog, (p: Promise<void>) => { this.artifacts.push(p); } )( puppetMaster.testResourceConfiguration.fs + "/" + fPath, value ), tLog, puppetMaster ); return x; } catch (e) { console.error(e.stack); } }; const runner = suiteRunner(suite); return { test: suite, toObj: () => { return suite.toObj(); }, runner, receiveTestResourceConfig: async function ( puppetMaster: IPM ): Promise<IFinalResults> { const tLog = async (...l: string[]) => { // }; try { const suiteDone: BaseSuite<I, O> = await runner(puppetMaster, tLog); const fails = suiteDone.fails; await puppetMaster.writeFileSync( `tests.json`, JSON.stringify(this.toObj(), null, 2), "test" ); return { failed: fails > 0, fails, artifacts: this.artifacts || [], features: suiteDone.features(), }; } catch (e) { console.error(e.stack); return { failed: true, fails: -1, artifacts: this.artifacts || [], features: [], }; } }, }; }); } // testsJson() { // puppetMaster.writeFileSync( // `tests.json`, // JSON.stringify({ features: suiteDone.features() }, null, 2) // ); // } Specs() { return this.specs; } Suites() { return this.suitesOverrides; } Given(): Record< keyof GivenExtensions, ( name: string, features: string[], whens: BaseWhen<I>[], thens: BaseThen<I>[], gcb ) => BaseGiven<I> > { return this.givenOverides; } When(): Record< keyof WhenExtensions, (arg0: I["istore"], ...arg1: any) => BaseWhen<I> > { return this.whenOverides; } Then(): Record< keyof ThenExtensions, (selection: I["iselection"], expectation: any) => BaseThen<I> > { return this.thenOverides; } }