-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage.ts
113 lines (100 loc) · 3.51 KB
/
coverage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {
OpenrpcDocument,
ExamplePairingObject,
ExampleObject,
JSONSchema,
ContentDescriptorObject,
Servers,
MethodObjectParams
} from "@open-rpc/meta-schema";
const jsf = require("json-schema-faker"); // tslint:disable-line
import validate from "@json-schema-tools/validator";
import { isEqual } from "lodash";
const getFakeParams = (params: any[]): any[] => {
return params.map((p) => jsf.generate(p.schema));
};
interface IOptions {
openrpcDocument: OpenrpcDocument;
skip: string[];
only: string[];
transport(url: string, method: string, params: any[]): PromiseLike<any>;
reporter(value: any[], schema: OpenrpcDocument): any;
}
export interface ExampleCall {
methodName: string;
params: any[];
url: string;
result?: any;
valid?: boolean;
reason?: string;
resultSchema: JSONSchema;
expectedResult?: any;
requestError?: any;
}
const paramsToObj = (params: any[], methodParams: ContentDescriptorObject[]): any => {
return params.reduce((acc, val, i) => {
acc[methodParams[i].name] = val;
return acc;
}, {});
}
export default async (options: IOptions) => {
const filteredMethods = options.openrpcDocument.methods
.filter(({name}) => !options.skip.includes(name))
.filter(({name}) => options.only.length === 0 || options.only.includes(name));
if (filteredMethods.length === 0) {
throw new Error("No methods to test");
}
const exampleCalls: ExampleCall[] = [];
const servers: Servers = (options.openrpcDocument.servers || [{url: "http://localhost:3333"}]);
servers.forEach(({url}) => {
filteredMethods.forEach((method) => {
if (method.examples === undefined || method.examples.length === 0) {
for (let i = 0; i < 10; i++) {
const p = getFakeParams(method.params);
// handle object or array case
const params = method.paramStructure === "by-name" ? paramsToObj(p, method.params as ContentDescriptorObject[]) : p;
exampleCalls.push({
methodName: method.name,
params,
url,
resultSchema: (method.result as ContentDescriptorObject).schema
});
}
return;
}
(method.examples as ExamplePairingObject[]).forEach((ex) => {
const p = (ex.params as ExampleObject[]).map((e) => e.value);
const params = method.paramStructure === "by-name" ? paramsToObj(p, method.params as ContentDescriptorObject[]) : p;
exampleCalls.push({
methodName: method.name,
params,
url,
resultSchema: (method.result as ContentDescriptorObject).schema,
expectedResult: (ex.result as ExampleObject).value,
});
});
});
});
for (const exampleCall of exampleCalls) {
try {
const callResult = await options.transport(exampleCall.url, exampleCall.methodName, exampleCall.params);
exampleCall.result = callResult.result;
if (exampleCall.expectedResult) {
exampleCall.valid = isEqual(exampleCall.expectedResult, exampleCall.result);
} else {
const errors = validate(exampleCall.resultSchema, exampleCall.result);
console.log('errors', exampleCall, errors);
if (Array.isArray(errors) && errors.length > 0) {
exampleCall.valid = false;
exampleCall.reason = JSON.stringify(errors);
} else if (errors === true) {
exampleCall.valid = true;
}
}
} catch (e) {
exampleCall.valid = false;
exampleCall.requestError = e;
}
}
options.reporter(exampleCalls, options.openrpcDocument);
};