doc-detective-common 3.6.1-dev.1 → 3.6.1-dev.2
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.
- package/.c8rc.json +2 -2
- package/README.md +31 -1
- package/dist/files.d.ts +16 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +123 -0
- package/dist/files.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/resolvePaths.d.ts +28 -0
- package/dist/resolvePaths.d.ts.map +1 -0
- package/dist/resolvePaths.js +236 -0
- package/dist/resolvePaths.js.map +1 -0
- package/dist/schemas/index.d.ts +5 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +9 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/schemas.json +121359 -0
- package/dist/types/generated/checkLink_v3.d.ts +27 -0
- package/dist/types/generated/checkLink_v3.d.ts.map +1 -0
- package/dist/types/generated/checkLink_v3.js +8 -0
- package/dist/types/generated/checkLink_v3.js.map +1 -0
- package/dist/types/generated/click_v3.d.ts +16 -0
- package/dist/types/generated/click_v3.d.ts.map +1 -0
- package/dist/types/generated/click_v3.js +8 -0
- package/dist/types/generated/click_v3.js.map +1 -0
- package/dist/types/generated/config_v3.d.ts +398 -0
- package/dist/types/generated/config_v3.d.ts.map +1 -0
- package/dist/types/generated/config_v3.js +8 -0
- package/dist/types/generated/config_v3.js.map +1 -0
- package/dist/types/generated/context_v3.d.ts +108 -0
- package/dist/types/generated/context_v3.d.ts.map +1 -0
- package/dist/types/generated/context_v3.js +8 -0
- package/dist/types/generated/context_v3.js.map +1 -0
- package/dist/types/generated/dragAndDrop_v3.d.ts +37 -0
- package/dist/types/generated/dragAndDrop_v3.d.ts.map +1 -0
- package/dist/types/generated/dragAndDrop_v3.js +8 -0
- package/dist/types/generated/dragAndDrop_v3.js.map +1 -0
- package/dist/types/generated/endRecord_v3.d.ts +9 -0
- package/dist/types/generated/endRecord_v3.d.ts.map +1 -0
- package/dist/types/generated/endRecord_v3.js +8 -0
- package/dist/types/generated/endRecord_v3.js.map +1 -0
- package/dist/types/generated/find_v3.d.ts +16 -0
- package/dist/types/generated/find_v3.d.ts.map +1 -0
- package/dist/types/generated/find_v3.js +8 -0
- package/dist/types/generated/find_v3.js.map +1 -0
- package/dist/types/generated/goTo_v3.d.ts +46 -0
- package/dist/types/generated/goTo_v3.d.ts.map +1 -0
- package/dist/types/generated/goTo_v3.js +8 -0
- package/dist/types/generated/goTo_v3.js.map +1 -0
- package/dist/types/generated/httpRequest_v3.d.ts +16 -0
- package/dist/types/generated/httpRequest_v3.d.ts.map +1 -0
- package/dist/types/generated/httpRequest_v3.js +8 -0
- package/dist/types/generated/httpRequest_v3.js.map +1 -0
- package/dist/types/generated/loadCookie_v3.d.ts +16 -0
- package/dist/types/generated/loadCookie_v3.d.ts.map +1 -0
- package/dist/types/generated/loadCookie_v3.js +8 -0
- package/dist/types/generated/loadCookie_v3.js.map +1 -0
- package/dist/types/generated/loadVariables_v3.d.ts +9 -0
- package/dist/types/generated/loadVariables_v3.d.ts.map +1 -0
- package/dist/types/generated/loadVariables_v3.js +8 -0
- package/dist/types/generated/loadVariables_v3.js.map +1 -0
- package/dist/types/generated/openApi_v3.d.ts +62 -0
- package/dist/types/generated/openApi_v3.d.ts.map +1 -0
- package/dist/types/generated/openApi_v3.js +8 -0
- package/dist/types/generated/openApi_v3.js.map +1 -0
- package/dist/types/generated/record_v3.d.ts +32 -0
- package/dist/types/generated/record_v3.d.ts.map +1 -0
- package/dist/types/generated/record_v3.js +8 -0
- package/dist/types/generated/record_v3.js.map +1 -0
- package/dist/types/generated/report_v3.d.ts +174 -0
- package/dist/types/generated/report_v3.d.ts.map +1 -0
- package/dist/types/generated/report_v3.js +8 -0
- package/dist/types/generated/report_v3.js.map +1 -0
- package/dist/types/generated/resolvedTests_v3.d.ts +571 -0
- package/dist/types/generated/resolvedTests_v3.d.ts.map +1 -0
- package/dist/types/generated/resolvedTests_v3.js +8 -0
- package/dist/types/generated/resolvedTests_v3.js.map +1 -0
- package/dist/types/generated/runCode_v3.d.ts +57 -0
- package/dist/types/generated/runCode_v3.d.ts.map +1 -0
- package/dist/types/generated/runCode_v3.js +8 -0
- package/dist/types/generated/runCode_v3.js.map +1 -0
- package/dist/types/generated/runShell_v3.d.ts +56 -0
- package/dist/types/generated/runShell_v3.d.ts.map +1 -0
- package/dist/types/generated/runShell_v3.js +8 -0
- package/dist/types/generated/runShell_v3.js.map +1 -0
- package/dist/types/generated/saveCookie_v3.d.ts +16 -0
- package/dist/types/generated/saveCookie_v3.d.ts.map +1 -0
- package/dist/types/generated/saveCookie_v3.js +8 -0
- package/dist/types/generated/saveCookie_v3.js.map +1 -0
- package/dist/types/generated/screenshot_v3.d.ts +74 -0
- package/dist/types/generated/screenshot_v3.d.ts.map +1 -0
- package/dist/types/generated/screenshot_v3.js +8 -0
- package/dist/types/generated/screenshot_v3.js.map +1 -0
- package/dist/types/generated/sourceIntegration_v3.d.ts +30 -0
- package/dist/types/generated/sourceIntegration_v3.d.ts.map +1 -0
- package/dist/types/generated/sourceIntegration_v3.js +8 -0
- package/dist/types/generated/sourceIntegration_v3.js.map +1 -0
- package/dist/types/generated/spec_v3.d.ts +159 -0
- package/dist/types/generated/spec_v3.d.ts.map +1 -0
- package/dist/types/generated/spec_v3.js +8 -0
- package/dist/types/generated/spec_v3.js.map +1 -0
- package/dist/types/generated/step_v3.d.ts +1270 -0
- package/dist/types/generated/step_v3.d.ts.map +1 -0
- package/dist/types/generated/step_v3.js +8 -0
- package/dist/types/generated/step_v3.js.map +1 -0
- package/dist/types/generated/stopRecord_v3.d.ts +9 -0
- package/dist/types/generated/stopRecord_v3.d.ts.map +1 -0
- package/dist/types/generated/stopRecord_v3.js +8 -0
- package/dist/types/generated/stopRecord_v3.js.map +1 -0
- package/dist/types/generated/test_v3.d.ts +2915 -0
- package/dist/types/generated/test_v3.d.ts.map +1 -0
- package/dist/types/generated/test_v3.js +8 -0
- package/dist/types/generated/test_v3.js.map +1 -0
- package/dist/types/generated/type_v3.d.ts +54 -0
- package/dist/types/generated/type_v3.d.ts.map +1 -0
- package/dist/types/generated/type_v3.js +8 -0
- package/dist/types/generated/type_v3.js.map +1 -0
- package/dist/types/generated/wait_v3.d.ts +12 -0
- package/dist/types/generated/wait_v3.d.ts.map +1 -0
- package/dist/types/generated/wait_v3.js +8 -0
- package/dist/types/generated/wait_v3.js.map +1 -0
- package/dist/validate.d.ts +41 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +549 -0
- package/dist/validate.js.map +1 -0
- package/package.json +17 -4
- package/plans/plan-typescriptMigration.prompt.md +25 -0
- package/scripts/createEsmWrapper.js +24 -0
- package/scripts/generateTypes.js +54 -0
- package/src/files.ts +86 -0
- package/src/index.ts +4 -0
- package/src/resolvePaths.ts +233 -0
- package/src/schemas/index.ts +6 -0
- package/src/types/generated/checkLink_v3.ts +29 -0
- package/src/types/generated/click_v3.ts +17 -0
- package/src/types/generated/config_v3.ts +405 -0
- package/src/types/generated/context_v3.ts +112 -0
- package/src/types/generated/dragAndDrop_v3.ts +39 -0
- package/src/types/generated/endRecord_v3.ts +10 -0
- package/src/types/generated/find_v3.ts +17 -0
- package/src/types/generated/goTo_v3.ts +48 -0
- package/src/types/generated/httpRequest_v3.ts +17 -0
- package/src/types/generated/loadCookie_v3.ts +17 -0
- package/src/types/generated/loadVariables_v3.ts +10 -0
- package/src/types/generated/openApi_v3.ts +64 -0
- package/src/types/generated/record_v3.ts +34 -0
- package/src/types/generated/report_v3.ts +183 -0
- package/src/types/generated/resolvedTests_v3.ts +585 -0
- package/src/types/generated/runCode_v3.ts +59 -0
- package/src/types/generated/runShell_v3.ts +58 -0
- package/src/types/generated/saveCookie_v3.ts +17 -0
- package/src/types/generated/screenshot_v3.ts +76 -0
- package/src/types/generated/sourceIntegration_v3.ts +31 -0
- package/src/types/generated/spec_v3.ts +166 -0
- package/src/types/generated/step_v3.ts +1288 -0
- package/src/types/generated/stopRecord_v3.ts +10 -0
- package/src/types/generated/test_v3.ts +3048 -0
- package/src/types/generated/type_v3.ts +56 -0
- package/src/types/generated/wait_v3.ts +13 -0
- package/src/validate.ts +611 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Auto-generated from type_v3.schema.json
|
|
4
|
+
* Do not edit manually
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Type keys. To type special keys, begin and end the string with `$` and use the special key's keyword. For example, to type the Escape key, enter `$ESCAPE$`.
|
|
9
|
+
*/
|
|
10
|
+
export type TypeKeys = TypeKeysSimple | TypeKeysDetailed;
|
|
11
|
+
/**
|
|
12
|
+
* Sequence of keys to enter.
|
|
13
|
+
*/
|
|
14
|
+
export type TypeKeysSimple = string | string[];
|
|
15
|
+
/**
|
|
16
|
+
* Sequence of keys to enter.
|
|
17
|
+
*/
|
|
18
|
+
export type TypeKeysSimple1 = string | string[];
|
|
19
|
+
|
|
20
|
+
export interface TypeKeysDetailed {
|
|
21
|
+
keys: TypeKeysSimple1;
|
|
22
|
+
/**
|
|
23
|
+
* Delay in milliseconds between each key press during a recording
|
|
24
|
+
*/
|
|
25
|
+
inputDelay?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Selector for the element to type into. If not specified, the typing occurs in the active element.
|
|
28
|
+
*/
|
|
29
|
+
selector?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Display text of the element to type into. If combined with other element finding fields, the element must match all specified criteria.
|
|
32
|
+
*/
|
|
33
|
+
elementText?: string;
|
|
34
|
+
/**
|
|
35
|
+
* ID attribute of the element to find. Supports exact match or regex pattern using /pattern/ syntax.
|
|
36
|
+
*/
|
|
37
|
+
elementId?: string;
|
|
38
|
+
/**
|
|
39
|
+
* data-testid attribute of the element to find. Supports exact match or regex pattern using /pattern/ syntax.
|
|
40
|
+
*/
|
|
41
|
+
elementTestId?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Class or array of classes that the element must have. Each class supports exact match or regex pattern using /pattern/ syntax. Element must have all specified classes.
|
|
44
|
+
*/
|
|
45
|
+
elementClass?: string | string[];
|
|
46
|
+
/**
|
|
47
|
+
* Object of attribute key-value pairs that the element must have. Values can be strings (supporting /pattern/ regex), numbers, or booleans. Boolean true matches attribute presence, false matches absence.
|
|
48
|
+
*/
|
|
49
|
+
elementAttribute?: {
|
|
50
|
+
[k: string]: string | number | boolean;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Computed accessible name of the element per ARIA specification. Supports exact match or regex pattern using /pattern/ syntax.
|
|
54
|
+
*/
|
|
55
|
+
elementAria?: string;
|
|
56
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Auto-generated from wait_v3.schema.json
|
|
4
|
+
* Do not edit manually
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Pause (in milliseconds) before performing the next action.
|
|
9
|
+
*/
|
|
10
|
+
export type Wait = WaitSimple | WaitEnvironmentVariable | WaitBoolean;
|
|
11
|
+
export type WaitSimple = number;
|
|
12
|
+
export type WaitEnvironmentVariable = string;
|
|
13
|
+
export type WaitBoolean = boolean;
|
package/src/validate.ts
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { schemas, SchemaKey } from "./schemas";
|
|
2
|
+
import Ajv, { ValidateFunction } from "ajv";
|
|
3
|
+
// Ajv extra formats: https://ajv.js.org/packages/ajv-formats.html
|
|
4
|
+
import addFormats from "ajv-formats";
|
|
5
|
+
// Ajv extra keywords: https://ajv.js.org/packages/ajv-keywords.html
|
|
6
|
+
import addKeywords from "ajv-keywords";
|
|
7
|
+
// Ajv custom errors: https://ajv.js.org/packages/ajv-errors.html
|
|
8
|
+
import addErrors from "ajv-errors";
|
|
9
|
+
import { randomUUID } from "crypto";
|
|
10
|
+
|
|
11
|
+
// Configure base Ajv
|
|
12
|
+
const ajv = new Ajv({
|
|
13
|
+
strictSchema: false,
|
|
14
|
+
useDefaults: true,
|
|
15
|
+
allErrors: true,
|
|
16
|
+
allowUnionTypes: true,
|
|
17
|
+
coerceTypes: true,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Enable `uuid` dynamic default
|
|
21
|
+
// @ts-ignore - ajv-keywords has incomplete types for dynamicDefaults
|
|
22
|
+
const def = require("ajv-keywords/dist/definitions/dynamicDefaults");
|
|
23
|
+
def.DEFAULTS.uuid = () => randomUUID;
|
|
24
|
+
|
|
25
|
+
// Enhance Ajv
|
|
26
|
+
addFormats(ajv);
|
|
27
|
+
addKeywords(ajv);
|
|
28
|
+
addErrors(ajv);
|
|
29
|
+
|
|
30
|
+
// Add all schemas from `schema` object.
|
|
31
|
+
for (const [key, value] of Object.entries(schemas)) {
|
|
32
|
+
ajv.addSchema(value, key);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Define the specific schemas that have compatibility mappings
|
|
36
|
+
const compatibleSchemas = {
|
|
37
|
+
config_v3: ["config_v2"],
|
|
38
|
+
context_v3: ["context_v2"],
|
|
39
|
+
openApi_v3: ["openApi_v2"],
|
|
40
|
+
spec_v3: ["spec_v2"],
|
|
41
|
+
step_v3: [
|
|
42
|
+
"checkLink_v2",
|
|
43
|
+
"find_v2",
|
|
44
|
+
"goTo_v2",
|
|
45
|
+
"httpRequest_v2",
|
|
46
|
+
"runShell_v2",
|
|
47
|
+
"runCode_v2",
|
|
48
|
+
"saveScreenshot_v2",
|
|
49
|
+
"setVariables_v2",
|
|
50
|
+
"startRecording_v2",
|
|
51
|
+
"stopRecording_v2",
|
|
52
|
+
"typeKeys_v2",
|
|
53
|
+
"wait_v2",
|
|
54
|
+
],
|
|
55
|
+
test_v3: ["test_v2"],
|
|
56
|
+
} as const;
|
|
57
|
+
|
|
58
|
+
type CompatibleSchemaKey = keyof typeof compatibleSchemas;
|
|
59
|
+
|
|
60
|
+
export interface ValidateOptions {
|
|
61
|
+
schemaKey: string;
|
|
62
|
+
object: any;
|
|
63
|
+
addDefaults?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ValidateResult {
|
|
67
|
+
valid: boolean;
|
|
68
|
+
errors: string;
|
|
69
|
+
object: any;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface TransformOptions {
|
|
73
|
+
currentSchema: string;
|
|
74
|
+
targetSchema: string;
|
|
75
|
+
object: any;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Escapes special characters in a string for safe use in a regular expression pattern.
|
|
80
|
+
*
|
|
81
|
+
* @param string - The input string to escape.
|
|
82
|
+
* @returns The escaped string, safe for use in regular expressions.
|
|
83
|
+
*/
|
|
84
|
+
function escapeRegExp(string: string): string {
|
|
85
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Validates an object against a specified JSON schema, supporting backward compatibility and automatic transformation from older schema versions if needed.
|
|
90
|
+
*
|
|
91
|
+
* If validation against the target schema fails and compatible older schemas are defined, attempts validation against each compatible schema. On a match, transforms the object to the target schema and revalidates. Returns the validation result, any errors, and the (possibly transformed) object.
|
|
92
|
+
*
|
|
93
|
+
* @param options - Validation options
|
|
94
|
+
* @param options.schemaKey - The key identifying the target JSON schema.
|
|
95
|
+
* @param options.object - The object to validate.
|
|
96
|
+
* @param options.addDefaults - Whether to include default values in the returned object.
|
|
97
|
+
* @returns Validation result, error messages, and the validated (and possibly transformed) object.
|
|
98
|
+
*
|
|
99
|
+
* @throws {Error} If {@link schemaKey} or {@link object} is missing.
|
|
100
|
+
*/
|
|
101
|
+
export function validate({
|
|
102
|
+
schemaKey,
|
|
103
|
+
object,
|
|
104
|
+
addDefaults = true,
|
|
105
|
+
}: ValidateOptions): ValidateResult {
|
|
106
|
+
if (!schemaKey) {
|
|
107
|
+
throw new Error("Schema key is required.");
|
|
108
|
+
}
|
|
109
|
+
if (!object) {
|
|
110
|
+
throw new Error("Object is required.");
|
|
111
|
+
}
|
|
112
|
+
const result: ValidateResult = {
|
|
113
|
+
valid: false,
|
|
114
|
+
errors: "",
|
|
115
|
+
object: object,
|
|
116
|
+
};
|
|
117
|
+
let validationObject: any;
|
|
118
|
+
let check: ValidateFunction | undefined = ajv.getSchema(schemaKey);
|
|
119
|
+
if (!check) {
|
|
120
|
+
result.valid = false;
|
|
121
|
+
result.errors = `Schema not found: ${schemaKey}`;
|
|
122
|
+
result.object = object;
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Clone the object to avoid modifying the original object
|
|
127
|
+
validationObject = JSON.parse(JSON.stringify(object));
|
|
128
|
+
|
|
129
|
+
// Check if the object is compatible with the schema
|
|
130
|
+
result.valid = check(validationObject);
|
|
131
|
+
result.errors = "";
|
|
132
|
+
|
|
133
|
+
if (check.errors) {
|
|
134
|
+
// Check if the object is compatible with another schema
|
|
135
|
+
const compatibleSchemasList =
|
|
136
|
+
compatibleSchemas[schemaKey as keyof typeof compatibleSchemas];
|
|
137
|
+
if (!compatibleSchemasList) {
|
|
138
|
+
result.errors = check.errors
|
|
139
|
+
.map(
|
|
140
|
+
(error) =>
|
|
141
|
+
`${error.instancePath} ${error.message} (${JSON.stringify(
|
|
142
|
+
error.params,
|
|
143
|
+
)})`,
|
|
144
|
+
)
|
|
145
|
+
.join(", ");
|
|
146
|
+
result.object = object;
|
|
147
|
+
result.valid = false;
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
const matchedSchemaKey = compatibleSchemasList.find((key) => {
|
|
151
|
+
validationObject = JSON.parse(JSON.stringify(object));
|
|
152
|
+
const check = ajv.getSchema(key);
|
|
153
|
+
if (check && check(validationObject)) return key;
|
|
154
|
+
});
|
|
155
|
+
if (!matchedSchemaKey) {
|
|
156
|
+
result.errors = check.errors
|
|
157
|
+
.map(
|
|
158
|
+
(error) =>
|
|
159
|
+
`${error.instancePath} ${error.message} (${JSON.stringify(
|
|
160
|
+
error.params,
|
|
161
|
+
)})`,
|
|
162
|
+
)
|
|
163
|
+
.join(", ");
|
|
164
|
+
result.object = object;
|
|
165
|
+
result.valid = false;
|
|
166
|
+
return result;
|
|
167
|
+
} else {
|
|
168
|
+
const transformedObject = transformToSchemaKey({
|
|
169
|
+
currentSchema: matchedSchemaKey,
|
|
170
|
+
targetSchema: schemaKey,
|
|
171
|
+
object: validationObject,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
result.valid = check(transformedObject);
|
|
175
|
+
if (result.valid) {
|
|
176
|
+
validationObject = transformedObject;
|
|
177
|
+
object = transformedObject;
|
|
178
|
+
/* c8 ignore start - Defensive: transformToSchemaKey validates internally, so this is unreachable */
|
|
179
|
+
} else if (check.errors) {
|
|
180
|
+
const errors = check.errors.map(
|
|
181
|
+
(error) =>
|
|
182
|
+
`${error.instancePath} ${error.message} (${JSON.stringify(
|
|
183
|
+
error.params,
|
|
184
|
+
)})`,
|
|
185
|
+
);
|
|
186
|
+
result.errors = errors.join(", ");
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
/* c8 ignore stop */
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (addDefaults) {
|
|
193
|
+
result.object = validationObject;
|
|
194
|
+
} else {
|
|
195
|
+
result.object = object;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Transform an object from one schema key to another and return a validated instance of the target schema.
|
|
203
|
+
*
|
|
204
|
+
* @param params - Function parameters.
|
|
205
|
+
* @param params.currentSchema - Schema key representing the object's current version.
|
|
206
|
+
* @param params.targetSchema - Schema key to transform the object into.
|
|
207
|
+
* @param params.object - The source object to transform.
|
|
208
|
+
* @returns The transformed object conforming to the target schema.
|
|
209
|
+
* @throws {Error} If transformation between the specified schemas is not supported or if the transformed object fails validation.
|
|
210
|
+
*/
|
|
211
|
+
export function transformToSchemaKey({
|
|
212
|
+
currentSchema = "",
|
|
213
|
+
targetSchema = "",
|
|
214
|
+
object = {},
|
|
215
|
+
}: TransformOptions): any {
|
|
216
|
+
// Check if the current schema is the same as the target schema
|
|
217
|
+
if (currentSchema === targetSchema) {
|
|
218
|
+
return object;
|
|
219
|
+
}
|
|
220
|
+
// Check if the current schema is compatible with the target schema
|
|
221
|
+
const compatibleList = compatibleSchemas[targetSchema as CompatibleSchemaKey];
|
|
222
|
+
if (
|
|
223
|
+
!compatibleList ||
|
|
224
|
+
!(compatibleList as readonly string[]).includes(currentSchema)
|
|
225
|
+
) {
|
|
226
|
+
throw new Error(
|
|
227
|
+
`Can't transform from ${currentSchema} to ${targetSchema}.`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
// Transform the object
|
|
231
|
+
if (targetSchema === "step_v3") {
|
|
232
|
+
const transformedObject: any = {
|
|
233
|
+
stepId: object.id,
|
|
234
|
+
description: object.description,
|
|
235
|
+
};
|
|
236
|
+
if (currentSchema === "goTo_v2") {
|
|
237
|
+
transformedObject.goTo = {
|
|
238
|
+
url: object.url,
|
|
239
|
+
origin: object.origin,
|
|
240
|
+
};
|
|
241
|
+
} else if (currentSchema === "checkLink_v2") {
|
|
242
|
+
transformedObject.checkLink = {
|
|
243
|
+
url: object.url,
|
|
244
|
+
origin: object.origin,
|
|
245
|
+
statusCodes: object.statusCodes,
|
|
246
|
+
};
|
|
247
|
+
} else if (currentSchema === "find_v2") {
|
|
248
|
+
transformedObject.find = {
|
|
249
|
+
selector: object.selector,
|
|
250
|
+
elementText: object.matchText,
|
|
251
|
+
timeout: object.timeout,
|
|
252
|
+
moveTo: object.moveTo,
|
|
253
|
+
click: object.click,
|
|
254
|
+
type: object.typeKeys,
|
|
255
|
+
};
|
|
256
|
+
// Handle typeKeys.delay key change
|
|
257
|
+
if (typeof object.typeKeys === "object" && object.typeKeys.keys) {
|
|
258
|
+
transformedObject.find.type.inputDelay = object.typeKeys.delay;
|
|
259
|
+
delete transformedObject.find.type.delay;
|
|
260
|
+
}
|
|
261
|
+
transformedObject.variables = {};
|
|
262
|
+
object.setVariables?.forEach((variable: any) => {
|
|
263
|
+
transformedObject.variables[variable.name] =
|
|
264
|
+
`extract($$element.text, "${variable.regex}")`;
|
|
265
|
+
});
|
|
266
|
+
} else if (currentSchema === "httpRequest_v2") {
|
|
267
|
+
transformedObject.httpRequest = {
|
|
268
|
+
method: object.method,
|
|
269
|
+
url: object.url,
|
|
270
|
+
openApi: object.openApi,
|
|
271
|
+
request: {
|
|
272
|
+
body: object.requestData,
|
|
273
|
+
headers: object.requestHeaders,
|
|
274
|
+
parameters: object.requestParams,
|
|
275
|
+
},
|
|
276
|
+
response: {
|
|
277
|
+
body: object.responseData,
|
|
278
|
+
headers: object.responseHeaders,
|
|
279
|
+
},
|
|
280
|
+
statusCodes: object.statusCodes,
|
|
281
|
+
allowAdditionalFields: object.allowAdditionalFields,
|
|
282
|
+
timeout: object.timeout,
|
|
283
|
+
path: object.savePath,
|
|
284
|
+
directory: object.saveDirectory,
|
|
285
|
+
maxVariation: object.maxVariation / 100,
|
|
286
|
+
overwrite:
|
|
287
|
+
object.overwrite === "byVariation"
|
|
288
|
+
? "aboveVariation"
|
|
289
|
+
: object.overwrite,
|
|
290
|
+
};
|
|
291
|
+
// Handle openApi.requestHeaders key change
|
|
292
|
+
if (object.openApi) {
|
|
293
|
+
transformedObject.httpRequest.openApi = transformToSchemaKey({
|
|
294
|
+
currentSchema: "openApi_v2",
|
|
295
|
+
targetSchema: "openApi_v3",
|
|
296
|
+
object: object.openApi,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
transformedObject.variables = {};
|
|
300
|
+
object.envsFromResponseData?.forEach((variable: any) => {
|
|
301
|
+
transformedObject.variables[variable.name] =
|
|
302
|
+
`jq($$response.body, "${variable.jqFilter}")`;
|
|
303
|
+
});
|
|
304
|
+
} else if (currentSchema === "runShell_v2") {
|
|
305
|
+
transformedObject.runShell = {
|
|
306
|
+
command: object.command,
|
|
307
|
+
args: object.args,
|
|
308
|
+
workingDirectory: object.workingDirectory,
|
|
309
|
+
exitCodes: object.exitCodes,
|
|
310
|
+
stdio: object.output,
|
|
311
|
+
path: object.savePath,
|
|
312
|
+
directory: object.saveDirectory,
|
|
313
|
+
maxVariation: object.maxVariation / 100,
|
|
314
|
+
overwrite:
|
|
315
|
+
object.overwrite === "byVariation"
|
|
316
|
+
? "aboveVariation"
|
|
317
|
+
: object.overwrite,
|
|
318
|
+
timeout: object.timeout,
|
|
319
|
+
};
|
|
320
|
+
transformedObject.variables = {};
|
|
321
|
+
object.setVariables?.forEach((variable: any) => {
|
|
322
|
+
transformedObject.variables[variable.name] =
|
|
323
|
+
`extract($$stdio.stdout, "${variable.regex}")`;
|
|
324
|
+
});
|
|
325
|
+
} else if (currentSchema === "runCode_v2") {
|
|
326
|
+
transformedObject.runCode = {
|
|
327
|
+
language: object.language,
|
|
328
|
+
code: object.code,
|
|
329
|
+
args: object.args,
|
|
330
|
+
workingDirectory: object.workingDirectory,
|
|
331
|
+
exitCodes: object.exitCodes,
|
|
332
|
+
stdio: object.output,
|
|
333
|
+
path: object.savePath,
|
|
334
|
+
directory: object.saveDirectory,
|
|
335
|
+
maxVariation: object.maxVariation / 100,
|
|
336
|
+
overwrite:
|
|
337
|
+
object.overwrite === "byVariation"
|
|
338
|
+
? "aboveVariation"
|
|
339
|
+
: object.overwrite,
|
|
340
|
+
timeout: object.timeout,
|
|
341
|
+
};
|
|
342
|
+
transformedObject.variables = {};
|
|
343
|
+
object?.setVariables?.forEach((variable: any) => {
|
|
344
|
+
transformedObject.variables[variable.name] =
|
|
345
|
+
`extract($$stdio.stdout, "${variable.regex}")`;
|
|
346
|
+
});
|
|
347
|
+
} else if (currentSchema === "setVariables_v2") {
|
|
348
|
+
transformedObject.loadVariables = object.path;
|
|
349
|
+
} else if (currentSchema === "typeKeys_v2") {
|
|
350
|
+
transformedObject.type = {
|
|
351
|
+
keys: object.keys,
|
|
352
|
+
inputDelay: object.delay,
|
|
353
|
+
};
|
|
354
|
+
} else if (currentSchema === "saveScreenshot_v2") {
|
|
355
|
+
transformedObject.screenshot = {
|
|
356
|
+
path: object.path,
|
|
357
|
+
directory: object.directory,
|
|
358
|
+
maxVariation: object.maxVariation / 100,
|
|
359
|
+
overwrite:
|
|
360
|
+
object.overwrite === "byVariation"
|
|
361
|
+
? "aboveVariation"
|
|
362
|
+
: object.overwrite,
|
|
363
|
+
crop: object.crop,
|
|
364
|
+
};
|
|
365
|
+
} else if (currentSchema === "startRecording_v2") {
|
|
366
|
+
transformedObject.record = {
|
|
367
|
+
path: object.path,
|
|
368
|
+
directory: object.directory,
|
|
369
|
+
overwrite: object.overwrite,
|
|
370
|
+
};
|
|
371
|
+
} else if (currentSchema === "stopRecording_v2") {
|
|
372
|
+
transformedObject.stopRecord = true;
|
|
373
|
+
} else if (currentSchema === "wait_v2") {
|
|
374
|
+
transformedObject.wait = object;
|
|
375
|
+
}
|
|
376
|
+
const result = validate({
|
|
377
|
+
schemaKey: "step_v3",
|
|
378
|
+
object: transformedObject,
|
|
379
|
+
});
|
|
380
|
+
if (!result.valid) {
|
|
381
|
+
throw new Error(`Invalid object: ${result.errors}`);
|
|
382
|
+
}
|
|
383
|
+
return result.object;
|
|
384
|
+
} else if (targetSchema === "config_v3") {
|
|
385
|
+
// Handle config_v2 to config_v3 transformation
|
|
386
|
+
const transformedObject: any = {
|
|
387
|
+
loadVariables: object.envVariables,
|
|
388
|
+
input: object?.runTests?.input || object.input,
|
|
389
|
+
output: object?.runTests?.output || object.output,
|
|
390
|
+
recursive: object?.runTests?.recursive || object.recursive,
|
|
391
|
+
relativePathBase: object.relativePathBase,
|
|
392
|
+
detectSteps: object?.runTests?.detectSteps,
|
|
393
|
+
beforeAny: object?.runTests?.setup,
|
|
394
|
+
afterAll: object?.runTests?.cleanup,
|
|
395
|
+
logLevel: object.logLevel,
|
|
396
|
+
telemetry: object.telemetry,
|
|
397
|
+
};
|
|
398
|
+
// Handle context transformation
|
|
399
|
+
if (object?.runTests?.contexts)
|
|
400
|
+
transformedObject.runOn = object.runTests.contexts.map((context: any) =>
|
|
401
|
+
transformToSchemaKey({
|
|
402
|
+
currentSchema: "context_v2",
|
|
403
|
+
targetSchema: "context_v3",
|
|
404
|
+
object: context,
|
|
405
|
+
}),
|
|
406
|
+
);
|
|
407
|
+
// Handle openApi transformation
|
|
408
|
+
if (object?.integrations?.openApi) {
|
|
409
|
+
transformedObject.integrations = {};
|
|
410
|
+
transformedObject.integrations.openApi = object.integrations.openApi.map(
|
|
411
|
+
(description: any) =>
|
|
412
|
+
transformToSchemaKey({
|
|
413
|
+
currentSchema: "openApi_v2",
|
|
414
|
+
targetSchema: "openApi_v3",
|
|
415
|
+
object: description,
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
// Handle fileTypes transformation
|
|
420
|
+
if (object?.fileTypes)
|
|
421
|
+
transformedObject.fileTypes = object.fileTypes.map((fileType: any) => {
|
|
422
|
+
const transformedFileType: any = {
|
|
423
|
+
name: fileType.name,
|
|
424
|
+
extensions: fileType.extensions.map((extension: string) =>
|
|
425
|
+
// Trim leading `.` from extension
|
|
426
|
+
extension.replace(/^\./, ""),
|
|
427
|
+
),
|
|
428
|
+
inlineStatements: {
|
|
429
|
+
// Convert strings to regex, escaping special characters
|
|
430
|
+
testStart: `${escapeRegExp(
|
|
431
|
+
fileType.testStartStatementOpen,
|
|
432
|
+
)}(.*?)${escapeRegExp(fileType.testStartStatementClose)}`,
|
|
433
|
+
testEnd: escapeRegExp(fileType.testEndStatement),
|
|
434
|
+
ignoreStart: escapeRegExp(fileType.testIgnoreStatement),
|
|
435
|
+
step: `${escapeRegExp(
|
|
436
|
+
fileType.stepStatementOpen,
|
|
437
|
+
)}(.*?)${escapeRegExp(fileType.stepStatementClose)}`,
|
|
438
|
+
},
|
|
439
|
+
};
|
|
440
|
+
if (fileType.markup)
|
|
441
|
+
transformedFileType.markup = fileType.markup.map((markup: any) => {
|
|
442
|
+
const transformedMarkup: any = {
|
|
443
|
+
name: markup.name,
|
|
444
|
+
regex: markup.regex,
|
|
445
|
+
};
|
|
446
|
+
if (markup.actions)
|
|
447
|
+
transformedMarkup.actions = markup.actions.map((action: any) => {
|
|
448
|
+
if (typeof action === "string") return action;
|
|
449
|
+
if (typeof action === "object") {
|
|
450
|
+
if (action.params) {
|
|
451
|
+
action = {
|
|
452
|
+
action: action.name,
|
|
453
|
+
...action.params,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
const transformedAction = transformToSchemaKey({
|
|
457
|
+
currentSchema: `${action.action}_v2`,
|
|
458
|
+
targetSchema: "step_v3",
|
|
459
|
+
object: action,
|
|
460
|
+
});
|
|
461
|
+
return transformedAction;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
return transformedMarkup;
|
|
466
|
+
});
|
|
467
|
+
return transformedFileType;
|
|
468
|
+
});
|
|
469
|
+
const result = validate({
|
|
470
|
+
schemaKey: "config_v3",
|
|
471
|
+
object: transformedObject,
|
|
472
|
+
});
|
|
473
|
+
// Defensive: transformation always produces valid config_v3, unreachable
|
|
474
|
+
/* c8 ignore next 3 */
|
|
475
|
+
if (!result.valid) {
|
|
476
|
+
throw new Error(`Invalid object: ${result.errors}`);
|
|
477
|
+
}
|
|
478
|
+
return result.object;
|
|
479
|
+
} else if (targetSchema === "context_v3") {
|
|
480
|
+
const transformedObject: any = {};
|
|
481
|
+
// Handle context_v2 to context_v3 transformation
|
|
482
|
+
transformedObject.platforms = object.platforms;
|
|
483
|
+
if (object.app?.name) {
|
|
484
|
+
const name = object.app.name === "edge" ? "chrome" : object.app?.name;
|
|
485
|
+
transformedObject.browsers = [];
|
|
486
|
+
transformedObject.browsers.push({
|
|
487
|
+
name,
|
|
488
|
+
headless: object.app?.options?.headless,
|
|
489
|
+
window: {
|
|
490
|
+
width: object.app?.options?.width,
|
|
491
|
+
height: object.app?.options?.height,
|
|
492
|
+
},
|
|
493
|
+
viewport: {
|
|
494
|
+
width: object.app?.options?.viewport_width,
|
|
495
|
+
height: object.app?.options?.viewport_height,
|
|
496
|
+
},
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
const result = validate({
|
|
500
|
+
schemaKey: "context_v3",
|
|
501
|
+
object: transformedObject,
|
|
502
|
+
});
|
|
503
|
+
if (!result.valid) {
|
|
504
|
+
throw new Error(`Invalid object: ${result.errors}`);
|
|
505
|
+
}
|
|
506
|
+
return result.object;
|
|
507
|
+
} else if (targetSchema === "openApi_v3") {
|
|
508
|
+
let transformedObject: any;
|
|
509
|
+
// Handle openApi_v2 to openApi_v3 transformation
|
|
510
|
+
const { name, requestHeaders, ...intermediaryObject } = object;
|
|
511
|
+
intermediaryObject.name = object.name;
|
|
512
|
+
intermediaryObject.headers = object.requestHeaders;
|
|
513
|
+
transformedObject = { ...intermediaryObject };
|
|
514
|
+
|
|
515
|
+
const result = validate({
|
|
516
|
+
schemaKey: "openApi_v3",
|
|
517
|
+
object: transformedObject,
|
|
518
|
+
});
|
|
519
|
+
if (!result.valid) {
|
|
520
|
+
throw new Error(`Invalid object: ${result.errors}`);
|
|
521
|
+
}
|
|
522
|
+
return result.object;
|
|
523
|
+
} else if (targetSchema === "spec_v3") {
|
|
524
|
+
// Handle spec_v2 to spec_v3 transformation
|
|
525
|
+
const transformedObject: any = {
|
|
526
|
+
specId: object.id,
|
|
527
|
+
description: object.description,
|
|
528
|
+
contentPath: object.file,
|
|
529
|
+
};
|
|
530
|
+
if (object.contexts)
|
|
531
|
+
transformedObject.runOn = object.contexts.map((context: any) =>
|
|
532
|
+
transformToSchemaKey({
|
|
533
|
+
currentSchema: "context_v2",
|
|
534
|
+
targetSchema: "context_v3",
|
|
535
|
+
object: context,
|
|
536
|
+
}),
|
|
537
|
+
);
|
|
538
|
+
if (object.openApi)
|
|
539
|
+
transformedObject.openApi = object.openApi.map((description: any) =>
|
|
540
|
+
transformToSchemaKey({
|
|
541
|
+
currentSchema: "openApi_v2",
|
|
542
|
+
targetSchema: "openApi_v3",
|
|
543
|
+
object: description,
|
|
544
|
+
}),
|
|
545
|
+
);
|
|
546
|
+
transformedObject.tests = object.tests.map((test: any) =>
|
|
547
|
+
transformToSchemaKey({
|
|
548
|
+
currentSchema: "test_v2",
|
|
549
|
+
targetSchema: "test_v3",
|
|
550
|
+
object: test,
|
|
551
|
+
}),
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
const result = validate({
|
|
555
|
+
schemaKey: "spec_v3",
|
|
556
|
+
object: transformedObject,
|
|
557
|
+
});
|
|
558
|
+
// Defensive: nested transforms validate; this is unreachable
|
|
559
|
+
/* c8 ignore next 3 */
|
|
560
|
+
if (!result.valid) {
|
|
561
|
+
throw new Error(`Invalid object: ${result.errors}`);
|
|
562
|
+
}
|
|
563
|
+
return result.object;
|
|
564
|
+
} else if (targetSchema === "test_v3") {
|
|
565
|
+
// Handle test_v2 to test_v3 transformation
|
|
566
|
+
const transformedObject: any = {
|
|
567
|
+
testId: object.id,
|
|
568
|
+
description: object.description,
|
|
569
|
+
contentPath: object.file,
|
|
570
|
+
detectSteps: object.detectSteps,
|
|
571
|
+
before: object.setup,
|
|
572
|
+
after: object.cleanup,
|
|
573
|
+
};
|
|
574
|
+
if (object.contexts)
|
|
575
|
+
transformedObject.runOn = object.contexts.map((context: any) =>
|
|
576
|
+
transformToSchemaKey({
|
|
577
|
+
currentSchema: "context_v2",
|
|
578
|
+
targetSchema: "context_v3",
|
|
579
|
+
object: context,
|
|
580
|
+
}),
|
|
581
|
+
);
|
|
582
|
+
if (object.openApi)
|
|
583
|
+
transformedObject.openApi = object.openApi.map((description: any) =>
|
|
584
|
+
transformToSchemaKey({
|
|
585
|
+
currentSchema: "openApi_v2",
|
|
586
|
+
targetSchema: "openApi_v3",
|
|
587
|
+
object: description,
|
|
588
|
+
}),
|
|
589
|
+
);
|
|
590
|
+
transformedObject.steps = object.steps.map((step: any) =>
|
|
591
|
+
transformToSchemaKey({
|
|
592
|
+
currentSchema: `${step.action}_v2`,
|
|
593
|
+
targetSchema: "step_v3",
|
|
594
|
+
object: step,
|
|
595
|
+
}),
|
|
596
|
+
);
|
|
597
|
+
|
|
598
|
+
const result = validate({
|
|
599
|
+
schemaKey: "test_v3",
|
|
600
|
+
object: transformedObject,
|
|
601
|
+
});
|
|
602
|
+
// Defensive: nested transforms validate; this is unreachable
|
|
603
|
+
/* c8 ignore next 3 */
|
|
604
|
+
if (!result.valid) {
|
|
605
|
+
throw new Error(`Invalid object: ${result.errors}`);
|
|
606
|
+
}
|
|
607
|
+
return result.object;
|
|
608
|
+
}
|
|
609
|
+
/* c8 ignore next 2 - Dead code: incompatible schemas throw at line 226-228 */
|
|
610
|
+
return null;
|
|
611
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "Node16",
|
|
5
|
+
"moduleResolution": "Node16",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"outDir": "./dist",
|
|
11
|
+
"rootDir": "./src",
|
|
12
|
+
"strict": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"allowSyntheticDefaultImports": true,
|
|
18
|
+
"types": ["node"]
|
|
19
|
+
},
|
|
20
|
+
"include": ["src/**/*"],
|
|
21
|
+
"exclude": ["node_modules", "dist", "test", "src/schemas/dereferenceSchemas.js"]
|
|
22
|
+
}
|