testeranto 0.38.0 → 0.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,205 @@
1
+ import { BaseFeature } from './Features';
2
+ import { IBaseConfig } from "./IBaseConfig";
3
+ export type { IBaseConfig };
4
+ declare type ITTestResourceConfiguration = {
5
+ "fs": string;
6
+ "ports": number[];
7
+ };
8
+ export declare type ITTestResourceRequirement = {
9
+ "ports": number;
10
+ "fs": string;
11
+ };
12
+ declare type IRunner = (x: ITTestResourceConfiguration, t: ITLog) => Promise<boolean>;
13
+ export declare type IT = {
14
+ toObj(): object;
15
+ name: string;
16
+ givens: BaseGiven<unknown, unknown, unknown, unknown, Record<string, BaseFeature>>[];
17
+ checks: BaseCheck<unknown, unknown, unknown, unknown, ITTestShape, unknown>[];
18
+ testResourceConfiguration: ITTestResourceConfiguration;
19
+ };
20
+ export declare type ITestJob = {
21
+ toObj(): object;
22
+ test: IT;
23
+ runner: IRunner;
24
+ testResourceRequirement: ITTestResourceRequirement;
25
+ receiveTestResourceConfig: (testResource?: any) => boolean;
26
+ };
27
+ export declare type ITestResults = Promise<{
28
+ test: IT;
29
+ }>[];
30
+ export declare type ITTestShape = {
31
+ suites: any;
32
+ givens: any;
33
+ whens: any;
34
+ thens: any;
35
+ checks: any;
36
+ };
37
+ export declare type ITestSpecification<ITestShape extends ITTestShape, IFeatureShape> = (Suite: {
38
+ [K in keyof ITestShape["suites"]]: (name: string, givens: BaseGiven<unknown, unknown, unknown, unknown, Record<string, BaseFeature>>[], checks: BaseCheck<unknown, unknown, unknown, unknown, ITestShape, IFeatureShape>[]) => BaseSuite<unknown, unknown, unknown, unknown, unknown, ITestShape, IFeatureShape>;
39
+ }, Given: {
40
+ [K in keyof ITestShape["givens"]]: (features: (keyof IFeatureShape)[], whens: BaseWhen<unknown, unknown, unknown>[], thens: BaseThen<unknown, unknown, unknown>[], ...xtras: ITestShape["givens"][K]) => BaseGiven<unknown, unknown, unknown, unknown, unknown>;
41
+ }, When: {
42
+ [K in keyof ITestShape["whens"]]: (...xtras: ITestShape["whens"][K]) => BaseWhen<unknown, unknown, unknown>;
43
+ }, Then: {
44
+ [K in keyof ITestShape["thens"]]: (...xtras: ITestShape["thens"][K]) => BaseThen<unknown, unknown, unknown>;
45
+ }, Check: {
46
+ [K in keyof ITestShape["checks"]]: (name: string, features: (keyof IFeatureShape)[], callbackA: (whens: {
47
+ [K in keyof ITestShape["whens"]]: (...unknown: any[]) => BaseWhen<unknown, unknown, unknown>;
48
+ }, thens: {
49
+ [K in keyof ITestShape["thens"]]: (...unknown: any[]) => BaseThen<unknown, unknown, unknown>;
50
+ }) => unknown, ...xtras: ITestShape["checks"][K]) => BaseCheck<unknown, unknown, unknown, unknown, ITestShape, IFeatureShape>;
51
+ }) => any[];
52
+ export declare type ITestImplementation<IState, ISelection, IWhenShape, IThenShape, ITestShape extends ITTestShape> = {
53
+ Suites: {
54
+ [K in keyof ITestShape["suites"]]: string;
55
+ };
56
+ Givens: {
57
+ [K in keyof ITestShape["givens"]]: (...Ig: ITestShape["givens"][K]) => IState;
58
+ };
59
+ Whens: {
60
+ [K in keyof ITestShape["whens"]]: (...Iw: ITestShape["whens"][K]) => (zel: ISelection) => IWhenShape;
61
+ };
62
+ Thens: {
63
+ [K in keyof ITestShape["thens"]]: (...It: ITestShape["thens"][K]) => (ssel: ISelection) => IThenShape;
64
+ };
65
+ Checks: {
66
+ [K in keyof ITestShape["checks"]]: (...Ic: ITestShape["checks"][K]) => IState;
67
+ };
68
+ };
69
+ declare type ITestArtifactory = (key: string, value: string) => unknown;
70
+ declare type ITLog = (...string: any[]) => void;
71
+ export declare abstract class BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITestShape extends ITTestShape, IFeatureShape> {
72
+ name: string;
73
+ givens: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[];
74
+ checks: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>[];
75
+ store: IStore;
76
+ fails: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[];
77
+ testResourceConfiguration: ITTestResourceConfiguration;
78
+ constructor(name: string, givens?: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[], checks?: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>[]);
79
+ toObj(): {
80
+ name: string;
81
+ givens: {
82
+ name: string;
83
+ whens: {
84
+ name: string;
85
+ error: boolean;
86
+ }[];
87
+ thens: {
88
+ name: string;
89
+ error: boolean;
90
+ }[];
91
+ errors: Error;
92
+ features: (keyof IFeatureShape)[];
93
+ }[];
94
+ fails: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[];
95
+ };
96
+ setup(s: IInput, artifactory: ITestArtifactory): Promise<ISubject>;
97
+ test(t: IThenShape): unknown;
98
+ run(input: any, testResourceConfiguration: ITTestResourceConfiguration, artifactory: (gndex: string) => (a: string, b: string) => void, tLog: (...string: any[]) => void): Promise<BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>>;
99
+ }
100
+ export declare abstract class BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape> {
101
+ name: string;
102
+ features: (keyof IFeatureShape)[];
103
+ whens: BaseWhen<IStore, ISelection, IThenShape>[];
104
+ thens: BaseThen<ISelection, IStore, IThenShape>[];
105
+ error: Error;
106
+ store: IStore;
107
+ recommendedFsPath: string;
108
+ constructor(name: string, features: (keyof IFeatureShape)[], whens: BaseWhen<IStore, ISelection, IThenShape>[], thens: BaseThen<ISelection, IStore, IThenShape>[]);
109
+ afterAll(store: IStore, artifactory: ITestArtifactory): void;
110
+ toObj(): {
111
+ name: string;
112
+ whens: {
113
+ name: string;
114
+ error: boolean;
115
+ }[];
116
+ thens: {
117
+ name: string;
118
+ error: boolean;
119
+ }[];
120
+ errors: Error;
121
+ features: (keyof IFeatureShape)[];
122
+ };
123
+ abstract givenThat(subject: ISubject, testResourceConfiguration: any, artifactory: ITestArtifactory): Promise<IStore>;
124
+ afterEach(store: IStore, ndx: number, artifactory: ITestArtifactory): Promise<unknown>;
125
+ give(subject: ISubject, index: number, testResourceConfiguration: any, tester: any, artifactory: ITestArtifactory, tLog: ITLog): Promise<IStore>;
126
+ }
127
+ export declare abstract class BaseWhen<IStore, ISelection, IThenShape> {
128
+ name: string;
129
+ actioner: (x: ISelection) => IThenShape;
130
+ error: boolean;
131
+ constructor(name: string, actioner: (xyz: ISelection) => IThenShape);
132
+ abstract andWhen(store: IStore, actioner: (x: ISelection) => IThenShape, testResource: any): any;
133
+ toObj(): {
134
+ name: string;
135
+ error: boolean;
136
+ };
137
+ test(store: IStore, testResourceConfiguration: any, tLog: ITLog): Promise<any>;
138
+ }
139
+ export declare abstract class BaseThen<ISelection, IStore, IThenShape> {
140
+ name: string;
141
+ thenCB: (storeState: ISelection) => IThenShape;
142
+ error: boolean;
143
+ constructor(name: string, thenCB: (val: ISelection) => IThenShape);
144
+ toObj(): {
145
+ name: string;
146
+ error: boolean;
147
+ };
148
+ abstract butThen(store: any, testResourceConfiguration?: any): Promise<ISelection>;
149
+ test(store: IStore, testResourceConfiguration: any, tLog: ITLog): Promise<IThenShape | undefined>;
150
+ }
151
+ export declare abstract class BaseCheck<ISubject, IStore, ISelection, IThenShape, ITestShape extends ITTestShape, IFeatureShape> {
152
+ name: string;
153
+ features: (keyof IFeatureShape)[];
154
+ checkCB: (whens: any, thens: any) => any;
155
+ whens: {
156
+ [K in keyof ITestShape["whens"]]: (p: any, tc: any) => BaseWhen<IStore, ISelection, IThenShape>;
157
+ };
158
+ thens: {
159
+ [K in keyof ITestShape["thens"]]: (p: any, tc: any) => BaseThen<ISelection, IStore, IThenShape>;
160
+ };
161
+ constructor(name: string, features: (keyof IFeatureShape)[], checkCB: (whens: any, thens: any) => any, whens: any, thens: any);
162
+ abstract checkThat(subject: ISubject, testResourceConfiguration: any, artifactory: ITestArtifactory): Promise<IStore>;
163
+ afterEach(store: IStore, ndx: number, cb?: any): Promise<unknown>;
164
+ check(subject: ISubject, ndx: number, testResourceConfiguration: any, tester: any, artifactory: ITestArtifactory, tLog: ITLog): Promise<void>;
165
+ }
166
+ export declare abstract class TesterantoLevelZero<IInput, ISubject, IStore, ISelection, SuiteExtensions, GivenExtensions, WhenExtensions, ThenExtensions, CheckExtensions, IThenShape, IFeatureShape> {
167
+ readonly cc: IStore;
168
+ constructorator: IStore;
169
+ suitesOverrides: Record<keyof SuiteExtensions, (name: string, givens: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[], checks: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>[]) => BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>>;
170
+ givenOverides: Record<keyof GivenExtensions, (name: string, features: (keyof IFeatureShape)[], whens: BaseWhen<IStore, ISelection, IThenShape>[], thens: BaseThen<ISelection, IStore, IThenShape>[], ...xtraArgs: any[]) => BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>>;
171
+ whenOverides: Record<keyof WhenExtensions, (any: any) => BaseWhen<IStore, ISelection, IThenShape>>;
172
+ thenOverides: Record<keyof ThenExtensions, (selection: ISelection, expectation: any) => BaseThen<ISelection, IStore, IThenShape>>;
173
+ checkOverides: Record<keyof CheckExtensions, (feature: string, callback: (whens: any, thens: any) => any, ...xtraArgs: any[]) => BaseCheck<ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>>;
174
+ constructor(cc: IStore, suitesOverrides: Record<keyof SuiteExtensions, (name: string, givens: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[], checks: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>[]) => BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>>, givenOverides: Record<keyof GivenExtensions, (name: string, features: (keyof IFeatureShape)[], whens: BaseWhen<IStore, ISelection, IThenShape>[], thens: BaseThen<ISelection, IStore, IThenShape>[], ...xtraArgs: any[]) => BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>>, whenOverides: Record<keyof WhenExtensions, (c: any) => BaseWhen<IStore, ISelection, IThenShape>>, thenOverides: Record<keyof ThenExtensions, (selection: ISelection, expectation: any) => BaseThen<ISelection, IStore, IThenShape>>, checkOverides: Record<keyof CheckExtensions, (feature: string, callback: (whens: any, thens: any) => any, ...xtraArgs: any[]) => BaseCheck<ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>>);
175
+ Suites(): Record<keyof SuiteExtensions, (name: string, givens: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[], checks: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>[]) => BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>>;
176
+ Given(): Record<keyof GivenExtensions, (name: string, features: (keyof IFeatureShape)[], whens: BaseWhen<IStore, ISelection, IThenShape>[], thens: BaseThen<ISelection, IStore, IThenShape>[], ...xtraArgs: any[]) => BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>>;
177
+ When(): Record<keyof WhenExtensions, (arg0: IStore, ...arg1: any) => BaseWhen<IStore, ISelection, IThenShape>>;
178
+ Then(): Record<keyof ThenExtensions, (selection: ISelection, expectation: any) => BaseThen<ISelection, IStore, IThenShape>>;
179
+ Check(): Record<keyof CheckExtensions, (feature: string, callback: (whens: any, thens: any) => any, whens: any, thens: any) => BaseCheck<ISubject, IStore, ISelection, IThenShape, ITTestShape, IFeatureShape>>;
180
+ }
181
+ export declare abstract class TesterantoLevelOne<ITestShape extends ITTestShape, IInitialState, ISelection, IStore, ISubject, IWhenShape, IThenShape, IInput, IFeatureShape extends Record<string, BaseFeature>> {
182
+ constructor(testImplementation: ITestImplementation<IInitialState, ISelection, IWhenShape, IThenShape, ITestShape>, testSpecification: (Suite: {
183
+ [K in keyof ITestShape["suites"]]: (feature: string, givens: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[], checks: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>[]) => BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>;
184
+ }, Given: {
185
+ [K in keyof ITestShape["givens"]]: (name: string, features: (keyof IFeatureShape)[], whens: BaseWhen<IStore, ISelection, IThenShape>[], thens: BaseThen<ISelection, IStore, IThenShape>[], ...a: ITestShape["givens"][K]) => BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>;
186
+ }, When: {
187
+ [K in keyof ITestShape["whens"]]: (...a: ITestShape["whens"][K]) => BaseWhen<IStore, ISelection, IThenShape>;
188
+ }, Then: {
189
+ [K in keyof ITestShape["thens"]]: (...a: ITestShape["thens"][K]) => BaseThen<ISelection, IStore, IThenShape>;
190
+ }, Check: {
191
+ [K in keyof ITestShape["checks"]]: (name: string, features: (keyof IFeatureShape)[], cbz: (...any: any[]) => Promise<void>) => any;
192
+ }) => BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>[], input: IInput, suiteKlasser: (name: string, givens: BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>[], checks: BaseCheck<ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>[]) => BaseSuite<IInput, ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>, givenKlasser: (n: any, f: any, w: any, t: any, z?: any) => BaseGiven<ISubject, IStore, ISelection, IThenShape, IFeatureShape>, whenKlasser: (s: any, o: any) => BaseWhen<IStore, ISelection, IThenShape>, thenKlasser: (s: any, o: any) => BaseThen<IStore, ISelection, IThenShape>, checkKlasser: (n: any, f: any, cb: any, w: any, t: any) => BaseCheck<ISubject, IStore, ISelection, IThenShape, ITestShape, IFeatureShape>, testResourceRequirement: any, nameKey: string);
193
+ }
194
+ declare type ITestArtificer = (key: string, data: any) => void;
195
+ declare const _default: <TestShape extends ITTestShape, Input, Subject, Store, Selection_1, WhenShape, ThenShape, InitialStateShape, IFeatureShape extends Record<string, BaseFeature>>(input: Input, testSpecification: ITestSpecification<TestShape, IFeatureShape>, testImplementation: any, testInterface: {
196
+ actionHandler?: ((b: (...any: any[]) => any) => any) | undefined;
197
+ andWhen: (store: Store, actioner: any, testResource: ITTestResourceConfiguration) => Promise<Selection_1>;
198
+ butThen?: ((store: Store, callback: any, testResource: ITTestResourceConfiguration) => Promise<Selection_1>) | undefined;
199
+ assertioner?: ((t: ThenShape) => any) | undefined;
200
+ afterAll?: ((store: Store, artificer: ITestArtificer) => any) | undefined;
201
+ afterEach?: ((store: Store, ndx: number, artificer: ITestArtificer) => Promise<unknown>) | undefined;
202
+ beforeAll?: ((input: Input, artificer: ITestArtificer) => Promise<Subject>) | undefined;
203
+ beforeEach?: ((subject: Subject, initialValues: any, testResource: ITTestResourceConfiguration, artificer: ITestArtificer) => Promise<Store>) | undefined;
204
+ }, nameKey: string, testResourceRequirement?: ITTestResourceRequirement) => Promise<void>;
205
+ export default _default;
package/dist/index.mjs ADDED
@@ -0,0 +1,468 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ const defaultTestResource = { "fs": ".", ports: [] };
4
+ const defaultTestResourceRequirement = { "fs": ".", ports: 0 };
5
+ const fPaths = [];
6
+ const testArtiFactoryfileWriter = (tLog) => (fp) => (givenNdx) => (key, value) => {
7
+ tLog("testArtiFactory =>", key);
8
+ const fPath = `${fp}/${givenNdx}/${key}`;
9
+ const cleanPath = path.resolve(fPath);
10
+ fPaths.push(cleanPath.replace(process.cwd(), ``));
11
+ const targetDir = cleanPath.split('/').slice(0, -1).join('/');
12
+ fs.mkdir(targetDir, { recursive: true }, async (error) => {
13
+ if (error) {
14
+ console.error(`❗️testArtiFactory failed`, targetDir, error);
15
+ }
16
+ fs.writeFileSync(path.resolve(targetDir.split('/').slice(0, -1).join('/'), "manifest"), fPaths.join(`\n`), {
17
+ encoding: 'utf-8'
18
+ });
19
+ if (Buffer.isBuffer(value)) {
20
+ fs.writeFileSync(fPath, value, "binary");
21
+ }
22
+ else if (`string` === (typeof value)) {
23
+ fs.writeFileSync(fPath, value.toString(), {
24
+ encoding: 'utf-8'
25
+ });
26
+ }
27
+ else {
28
+ /* @ts-ignore:next-line */
29
+ const pipeStream = value;
30
+ var myFile = fs.createWriteStream(fPath);
31
+ pipeStream.pipe(myFile);
32
+ pipeStream.on("close", () => {
33
+ myFile.close();
34
+ });
35
+ }
36
+ });
37
+ };
38
+ export class BaseSuite {
39
+ constructor(name, givens = [], checks = []) {
40
+ this.name = name;
41
+ this.givens = givens;
42
+ this.checks = checks;
43
+ this.fails = [];
44
+ }
45
+ toObj() {
46
+ return {
47
+ name: this.name,
48
+ givens: this.givens.map((g) => g.toObj()),
49
+ fails: this.fails
50
+ };
51
+ }
52
+ setup(s, artifactory) {
53
+ return new Promise((res) => res(s));
54
+ }
55
+ test(t) {
56
+ return t;
57
+ }
58
+ async run(input, testResourceConfiguration, artifactory, tLog) {
59
+ this.testResourceConfiguration = testResourceConfiguration;
60
+ const subject = await this.setup(input, artifactory("-1"));
61
+ tLog("\nSuite:", this.name, testResourceConfiguration);
62
+ for (const [ndx, giver] of this.givens.entries()) {
63
+ try {
64
+ this.store = await giver.give(subject, ndx, testResourceConfiguration, this.test, artifactory(ndx.toString()), tLog);
65
+ }
66
+ catch (e) {
67
+ console.error(e);
68
+ this.fails.push(giver);
69
+ return this;
70
+ }
71
+ }
72
+ for (const [ndx, thater] of this.checks.entries()) {
73
+ await thater.check(subject, ndx, testResourceConfiguration, this.test, artifactory, tLog);
74
+ }
75
+ // @TODO fix me
76
+ for (const [ndx, giver] of this.givens.entries()) {
77
+ giver.afterAll(this.store, artifactory);
78
+ }
79
+ ////////////////
80
+ return this;
81
+ }
82
+ }
83
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////
84
+ export class BaseGiven {
85
+ constructor(name, features, whens, thens) {
86
+ this.name = name;
87
+ this.features = features;
88
+ this.whens = whens;
89
+ this.thens = thens;
90
+ }
91
+ afterAll(store, artifactory) {
92
+ return;
93
+ }
94
+ ;
95
+ toObj() {
96
+ return {
97
+ name: this.name,
98
+ whens: this.whens.map((w) => w.toObj()),
99
+ thens: this.thens.map((t) => t.toObj()),
100
+ errors: this.error,
101
+ features: this.features,
102
+ };
103
+ }
104
+ async afterEach(store, ndx, artifactory) {
105
+ return;
106
+ }
107
+ async give(subject, index, testResourceConfiguration, tester, artifactory, tLog) {
108
+ tLog(`\n Given: ${this.name}`);
109
+ try {
110
+ this.store = await this.givenThat(subject, testResourceConfiguration, artifactory);
111
+ for (const whenStep of this.whens) {
112
+ await whenStep.test(this.store, testResourceConfiguration, tLog);
113
+ }
114
+ for (const thenStep of this.thens) {
115
+ const t = await thenStep.test(this.store, testResourceConfiguration, tLog);
116
+ tester(t);
117
+ }
118
+ }
119
+ catch (e) {
120
+ this.error = e;
121
+ tLog('\u0007'); // bell
122
+ // throw e;
123
+ }
124
+ finally {
125
+ try {
126
+ await this.afterEach(this.store, index, artifactory);
127
+ }
128
+ catch (_a) {
129
+ console.error("afterEach failed! no error will be recorded!");
130
+ }
131
+ }
132
+ return this.store;
133
+ }
134
+ }
135
+ export class BaseWhen {
136
+ constructor(name, actioner) {
137
+ this.name = name;
138
+ this.actioner = actioner;
139
+ }
140
+ toObj() {
141
+ return {
142
+ name: this.name,
143
+ error: this.error,
144
+ };
145
+ }
146
+ async test(store, testResourceConfiguration, tLog) {
147
+ tLog(" When:", this.name);
148
+ try {
149
+ return await this.andWhen(store, this.actioner, testResourceConfiguration);
150
+ }
151
+ catch (e) {
152
+ this.error = true;
153
+ throw e;
154
+ }
155
+ }
156
+ }
157
+ export class BaseThen {
158
+ constructor(name, thenCB) {
159
+ this.name = name;
160
+ this.thenCB = thenCB;
161
+ }
162
+ toObj() {
163
+ return {
164
+ name: this.name,
165
+ error: this.error,
166
+ };
167
+ }
168
+ async test(store, testResourceConfiguration, tLog) {
169
+ tLog(" Then:", this.name);
170
+ try {
171
+ return this.thenCB(await this.butThen(store, testResourceConfiguration));
172
+ }
173
+ catch (e) {
174
+ console.log("wtf");
175
+ this.error = true;
176
+ throw e;
177
+ }
178
+ // try {
179
+ // return await (this.thenCB(
180
+ // await (async () => {
181
+ // try {
182
+ // return await (
183
+ // (() => {
184
+ // try {
185
+ // return this.butThen(store, testResourceConfiguration)
186
+ // } catch (e) {
187
+ // this.error = true;
188
+ // throw e
189
+ // }
190
+ // })()
191
+ // );
192
+ // } catch (e) {
193
+ // this.error = true;
194
+ // throw e
195
+ // }
196
+ // })()
197
+ // ));
198
+ // } catch (e) {
199
+ // this.error = true;
200
+ // throw e
201
+ // }
202
+ }
203
+ }
204
+ export class BaseCheck {
205
+ constructor(name, features, checkCB, whens, thens) {
206
+ this.name = name;
207
+ this.features = features;
208
+ this.checkCB = checkCB;
209
+ this.whens = whens;
210
+ this.thens = thens;
211
+ }
212
+ async afterEach(store, ndx, cb) {
213
+ return;
214
+ }
215
+ async check(subject, ndx, testResourceConfiguration, tester, artifactory, tLog) {
216
+ tLog(`\n Check: ${this.name}`);
217
+ const store = await this.checkThat(subject, testResourceConfiguration, artifactory);
218
+ await this.checkCB((Object.entries(this.whens)
219
+ .reduce((a, [key, when]) => {
220
+ a[key] = async (payload) => {
221
+ return await when(payload, testResourceConfiguration).test(store, testResourceConfiguration, tLog);
222
+ };
223
+ return a;
224
+ }, {})), (Object.entries(this.thens)
225
+ .reduce((a, [key, then]) => {
226
+ a[key] = async (payload) => {
227
+ const t = await then(payload, testResourceConfiguration).test(store, testResourceConfiguration, tLog);
228
+ tester(t);
229
+ };
230
+ return a;
231
+ }, {})));
232
+ await this.afterEach(store, ndx);
233
+ return;
234
+ }
235
+ }
236
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////
237
+ export class TesterantoLevelZero {
238
+ constructor(cc, suitesOverrides, givenOverides, whenOverides, thenOverides, checkOverides) {
239
+ this.cc = cc;
240
+ this.constructorator = cc;
241
+ this.suitesOverrides = suitesOverrides;
242
+ this.givenOverides = givenOverides;
243
+ this.whenOverides = whenOverides;
244
+ this.thenOverides = thenOverides;
245
+ this.checkOverides = checkOverides;
246
+ }
247
+ Suites() {
248
+ return this.suitesOverrides;
249
+ }
250
+ Given() {
251
+ return this.givenOverides;
252
+ }
253
+ When() {
254
+ return this.whenOverides;
255
+ }
256
+ Then() {
257
+ return this.thenOverides;
258
+ }
259
+ Check() {
260
+ return this.checkOverides;
261
+ }
262
+ }
263
+ export class TesterantoLevelOne {
264
+ constructor(testImplementation, testSpecification, input, suiteKlasser, givenKlasser, whenKlasser, thenKlasser, checkKlasser, testResourceRequirement, nameKey) {
265
+ const classySuites = Object.entries(testImplementation.Suites)
266
+ .reduce((a, [key]) => {
267
+ a[key] = (somestring, givens, checks) => {
268
+ return new suiteKlasser.prototype.constructor(somestring, givens, checks);
269
+ };
270
+ return a;
271
+ }, {});
272
+ const classyGivens = Object.entries(testImplementation.Givens)
273
+ .reduce((a, [key, z]) => {
274
+ a[key] = (features, whens, thens, ...xtrasW) => {
275
+ return new givenKlasser.prototype.constructor(z.name, features, whens, thens, z(...xtrasW));
276
+ };
277
+ return a;
278
+ }, {});
279
+ const classyWhens = Object.entries(testImplementation.Whens)
280
+ .reduce((a, [key, whEn]) => {
281
+ a[key] = (payload) => {
282
+ return new whenKlasser.prototype.constructor(`${whEn.name}: ${payload && payload.toString()}`, whEn(payload));
283
+ };
284
+ return a;
285
+ }, {});
286
+ const classyThens = Object.entries(testImplementation.Thens)
287
+ .reduce((a, [key, thEn]) => {
288
+ a[key] = (expected, x) => {
289
+ return new thenKlasser.prototype.constructor(`${thEn.name}: ${expected && expected.toString()}`, thEn(expected));
290
+ };
291
+ return a;
292
+ }, {});
293
+ const classyChecks = Object.entries(testImplementation.Checks)
294
+ .reduce((a, [key, z]) => {
295
+ a[key] = (somestring, features, callback) => {
296
+ return new checkKlasser.prototype.constructor(somestring, features, callback, classyWhens, classyThens);
297
+ };
298
+ return a;
299
+ }, {});
300
+ const classyTesteranto = new (class extends TesterantoLevelZero {
301
+ })(input, classySuites, classyGivens, classyWhens, classyThens, classyChecks);
302
+ const suites = testSpecification(
303
+ /* @ts-ignore:next-line */
304
+ classyTesteranto.Suites(), classyTesteranto.Given(), classyTesteranto.When(), classyTesteranto.Then(), classyTesteranto.Check());
305
+ const suiteRunner = (suite) => (testResourceConfiguration, tLog) => {
306
+ return suite.run(input, testResourceConfiguration, testArtiFactoryfileWriter(tLog)(testResourceConfiguration.fs + "/"), tLog);
307
+ };
308
+ /* @ts-ignore:next-line */
309
+ const toReturn = suites.map((suite) => {
310
+ const runner = suiteRunner(suite);
311
+ return {
312
+ test: suite,
313
+ testResourceRequirement,
314
+ toObj: () => {
315
+ return suite.toObj();
316
+ },
317
+ runner,
318
+ receiveTestResourceConfig: async function (testResourceConfiguration = defaultTestResource) {
319
+ console.log(`testResourceConfiguration ${JSON.stringify(testResourceConfiguration, null, 2)}`);
320
+ await fs.mkdirSync(testResourceConfiguration.fs, { recursive: true });
321
+ const logFilePath = path.resolve(`${testResourceConfiguration.fs}/log.txt`);
322
+ var access = fs.createWriteStream(logFilePath);
323
+ const tLog = (...l) => {
324
+ console.log(...l);
325
+ access.write(`${l.toString()}\n`);
326
+ };
327
+ const suiteDone = await runner(testResourceConfiguration, tLog);
328
+ const resultsFilePath = path.resolve(`${testResourceConfiguration.fs}/results.json`);
329
+ fs.writeFileSync(resultsFilePath, JSON.stringify(suiteDone.toObj(), null, 2));
330
+ access.close();
331
+ const numberOfFailures = suiteDone.givens.filter((g) => g.error).length;
332
+ console.log(`exiting gracefully with ${numberOfFailures} failures.`);
333
+ process.exitCode = numberOfFailures;
334
+ }
335
+ };
336
+ });
337
+ return toReturn;
338
+ }
339
+ }
340
+ export default async (input, testSpecification, testImplementation, testInterface, nameKey, testResourceRequirement = defaultTestResourceRequirement) => {
341
+ const butThen = testInterface.butThen || (async (a) => a);
342
+ const { andWhen } = testInterface;
343
+ const actionHandler = testInterface.actionHandler || function (b) {
344
+ return b;
345
+ };
346
+ const assertioner = testInterface.assertioner || (async (t) => t);
347
+ const beforeAll = testInterface.beforeAll || (async (input) => input);
348
+ const beforeEach = testInterface.beforeEach || async function (subject, initialValues, testResource) {
349
+ return subject;
350
+ };
351
+ const afterEach = testInterface.afterEach || (async (s) => s);
352
+ const afterAll = testInterface.afterAll || ((store) => undefined);
353
+ class MrT extends TesterantoLevelOne {
354
+ constructor() {
355
+ super(testImplementation,
356
+ /* @ts-ignore:next-line */
357
+ testSpecification, input, (class extends BaseSuite {
358
+ async setup(s, artifactory) {
359
+ return beforeAll(s, artifactory);
360
+ }
361
+ test(t) {
362
+ return assertioner(t);
363
+ }
364
+ }), class Given extends BaseGiven {
365
+ constructor(name, features, whens, thens, initialValues) {
366
+ super(name, features, whens, thens);
367
+ this.initialValues = initialValues;
368
+ }
369
+ async givenThat(subject, testResource, artifactory) {
370
+ return beforeEach(subject, this.initialValues, testResource, artifactory);
371
+ }
372
+ afterEach(store, ndx, artifactory) {
373
+ return new Promise((res) => res(afterEach(store, ndx, artifactory)));
374
+ }
375
+ afterAll(store, artifactory) {
376
+ return afterAll(store, artifactory);
377
+ }
378
+ }, class When extends BaseWhen {
379
+ constructor(name, actioner, payload) {
380
+ super(name, (store) => {
381
+ return actionHandler(actioner);
382
+ });
383
+ this.payload = payload;
384
+ }
385
+ async andWhen(store, actioner, testResource) {
386
+ return await andWhen(store, actioner, testResource);
387
+ }
388
+ }, class Then extends BaseThen {
389
+ constructor(name, callback) {
390
+ super(name, callback);
391
+ }
392
+ async butThen(store, testResourceConfiguration) {
393
+ return await butThen(store, this.thenCB, testResourceConfiguration);
394
+ }
395
+ }, class Check extends BaseCheck {
396
+ constructor(name, features, checkCallback, whens, thens, initialValues) {
397
+ super(name, features, checkCallback, whens, thens);
398
+ this.initialValues = initialValues;
399
+ }
400
+ async checkThat(subject, testResourceConfiguration, artifactory) {
401
+ return beforeEach(subject, this.initialValues, testResourceConfiguration, artifactory);
402
+ }
403
+ afterEach(store, ndx, artifactory) {
404
+ return new Promise((res) => res(afterEach(store, ndx, artifactory)));
405
+ }
406
+ }, testResourceRequirement, nameKey);
407
+ }
408
+ }
409
+ const mrt = new MrT();
410
+ const t = mrt[0];
411
+ const testResourceArg = process.argv[2] || `{}`;
412
+ try {
413
+ const partialTestResource = JSON.parse(testResourceArg);
414
+ if (partialTestResource.fs && partialTestResource.ports) {
415
+ await t.receiveTestResourceConfig(partialTestResource);
416
+ // process.exit(0); // :-)
417
+ }
418
+ else {
419
+ console.log("test configuration is incomplete");
420
+ if (process.send) {
421
+ console.log("requesting test resources from pm2 ...", testResourceRequirement);
422
+ /* @ts-ignore:next-line */
423
+ process.send({
424
+ type: 'testeranto:hola',
425
+ data: {
426
+ testResourceRequirement
427
+ }
428
+ });
429
+ console.log("awaiting test resources from pm2...");
430
+ process.on('message', async function (packet) {
431
+ const resourcesFromPm2 = packet.data.testResourceConfiguration;
432
+ const secondTestResource = (Object.assign(Object.assign({}, JSON.parse(JSON.stringify(resourcesFromPm2))), JSON.parse(JSON.stringify(partialTestResource))));
433
+ if (await t.receiveTestResourceConfig(secondTestResource)) {
434
+ /* @ts-ignore:next-line */
435
+ process.send({
436
+ type: 'testeranto:adios',
437
+ data: {
438
+ testResourceConfiguration: mrt[0].test.testResourceConfiguration,
439
+ results: mrt[0].toObj()
440
+ }
441
+ }, (err) => {
442
+ if (!err) {
443
+ console.log(`✅`);
444
+ }
445
+ else {
446
+ console.error(`❗️`, err);
447
+ }
448
+ // process.exit(0); // :-)
449
+ });
450
+ }
451
+ });
452
+ }
453
+ else {
454
+ console.log("Pass run-time test resources by STDIN");
455
+ process.stdin.on('data', async (data) => {
456
+ const resourcesFromStdin = JSON.parse(data.toString());
457
+ const secondTestResource = (Object.assign(Object.assign({}, JSON.parse(JSON.stringify(resourcesFromStdin))), JSON.parse(JSON.stringify(partialTestResource))));
458
+ await t.receiveTestResourceConfig(secondTestResource);
459
+ // process.exit(0); // :-)
460
+ });
461
+ }
462
+ }
463
+ }
464
+ catch (e) {
465
+ console.error(`the test resource passed by command-line arugument "${process.argv[2]}" was malformed.`);
466
+ process.exit(-1);
467
+ }
468
+ };