diff --git a/.coderabbit.yaml b/.coderabbit.yaml index 24d8af3..2b782d2 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -24,7 +24,7 @@ reviews: ignore_title_keywords: [] labels: [] drafts: false - base_branches: ["rc"] + base_branches: ["main","rc"] tools: shellcheck: enabled: true diff --git a/dev/index.js b/dev/index.js index bba9a47..ea8a80f 100644 --- a/dev/index.js +++ b/dev/index.js @@ -5,17 +5,61 @@ const path = require("path"); main(); +/** + * Detects and resolves test cases in a specified markdown file using configured patterns and actions, then outputs the results to a JSON file. + * + * The function analyzes the input markdown file for test-related statements and code blocks according to the provided configuration, processes detected tests, and writes the structured results to "output.json" in the current directory. + */ async function main() { const json = { - input: "/home/hawkeyexl/Workspaces/resolver/dev/dev.spec.json", - integrations:{ - openApi: [ - { - name: "reqres", - descriptionPath: "/home/hawkeyexl/Workspaces/resolver/dev/reqres.openapi.json", - } - ] - }, + input: "/home/hawkeyexl/Workspaces/resolver/dev/doc-content copy.md", + fileTypes: [ + { + name: "markdown", + extensions: ["md", "markdown", "mdx"], + inlineStatements: { + testStart: [ + "{\\/\\*\\s*test\\s+?([\\s\\S]*?)\\s*\\*\\/}", + "", + "\\[comment\\]:\\s+#\\s+\\(test\\s*(.*?)\\s*\\)", + "\\[comment\\]:\\s+#\\s+\\(test start\\s*(.*?)\\s*\\)", + ], + testEnd: [ + "{\\/\\*\\s*test end\\s*\\*\\/}", + "", + "\\[comment\\]:\\s+#\\s+\\(test end\\)", + ], + ignoreStart: [ + "{\\/\\*\\s*test ignore start\\s*\\*\\/}", + "", + ], + ignoreEnd: [ + "{\\/\\*\\s*test ignore end\\s*\\*\\/}", + "", + ], + step: [ + "{\\/\\*\\s*step\\s+?([\\s\\S]*?)\\s*\\*\\/}", + "", + "\\[comment\\]:\\s+#\\s+\\(step\\s*(.*?)\\s*\\)", + ], + }, + markup: [ + { + name: "runPython", + regex: ["```(?:python)\\b\\s*\\n(?.*?)(?=\\n```)"], + batchMatches: true, + actions: [ + { + runCode: { + language: "python", + code: "$1", + }, + }, + ], + }, + ], + }, + ], logLevel: "debug", }; result = await detectAndResolveTests({ config: json }); @@ -25,4 +69,4 @@ async function main() { const fs = require("fs"); fs.writeFileSync(outputPath, JSON.stringify(result, null, 2)); console.log(`Output written to ${outputPath}`); -} \ No newline at end of file +} diff --git a/dev/output.json b/dev/output.json index 84177a6..16bc329 100644 --- a/dev/output.json +++ b/dev/output.json @@ -1,302 +1,8 @@ { "config": { "input": [ - "/home/hawkeyexl/Workspaces/resolver/dev/dev.spec.json" + "/home/hawkeyexl/Workspaces/resolver/dev/doc-content copy.md" ], - "integrations": { - "openApi": [ - { - "name": "reqres", - "descriptionPath": "/home/hawkeyexl/Workspaces/resolver/dev/reqres.openapi.json", - "validateAgainstSchema": "both", - "useExample": "none", - "exampleKey": "", - "definition": { - "openapi": "3.0.3", - "info": { - "title": "Reqres API", - "description": "Sample API for testing and prototyping", - "version": "0.0.1" - }, - "servers": [ - { - "url": "https://reqres.in/api" - } - ], - "tags": [ - { - "name": "Test", - "description": "Test operations" - } - ], - "security": [ - {} - ], - "paths": { - "/users": { - "post": { - "tags": [ - "Test" - ], - "summary": "Add a new user", - "description": "Add a new user", - "operationId": "addUser", - "requestBody": { - "description": "Create a new user", - "content": { - "application/json": { - "schema": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - }, - "required": true - }, - "responses": { - "201": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "description": "response payload" - }, - "examples": { - "test": { - "value": { - "name": "morpheus", - "job": "leader" - } - }, - "foobar": { - "value": { - "name": "neo", - "job": "the-one" - } - } - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - }, - "message": { - "type": "string" - } - } - } - } - } - } - } - }, - "get": { - "tags": [ - "Test" - ], - "summary": "Return a list of users", - "description": "Return a list of users", - "operationId": "getUsers", - "parameters": [ - { - "name": "page", - "in": "query", - "description": "Select the portition of record you want back", - "required": false, - "schema": { - "type": "integer", - "example": 1 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "description": "response payload" - } - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - } - }, - "/users/{id}": { - "put": { - "tags": [ - "Test" - ], - "summary": "Update an existing user", - "description": "Update an existing user by Id", - "operationId": "updateUser", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "id of user to delete", - "required": true, - "example": 1, - "schema": { - "type": "integer", - "format": "int64" - } - } - ], - "requestBody": { - "description": "Update an existent pet in the store", - "content": { - "application/json": { - "schema": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "description": "response payload" - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - }, - "delete": { - "tags": [ - "Test" - ], - "summary": "Deletes a user", - "description": "delete a user", - "operationId": "deleteUser", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "id of user to delete", - "required": true, - "example": 1, - "schema": { - "type": "integer", - "format": "int64" - } - } - ], - "responses": { - "204": { - "description": "No content" - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - } - } - }, - "components": { - "schemas": { - "userResponse": { - "description": "response payload" - }, - "userRequest": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - } - } - } - ] - }, - "logLevel": "debug", - "output": ".", - "recursive": true, - "relativePathBase": "file", - "detectSteps": true, "fileTypes": [ { "name": "markdown", @@ -333,117 +39,32 @@ }, "markup": [ { - "name": "checkHyperlink", - "regex": [ - "(?.*?)(?=\\n```)" ], + "batchMatches": true, "actions": [ - "click" - ] - }, - { - "name": "findOnscreenText", - "regex": [ - "\\*\\*((?:(?!\\*\\*).)+)\\*\\*" - ], - "actions": [ - "find" - ] - }, - { - "name": "goToUrl", - "regex": [ - "\\b(?:[Gg]o\\s+to|[Oo]pen|[Nn]avigate\\s+to|[Vv]isit|[Aa]ccess|[Pp]roceed\\s+to|[Ll]aunch)\\b\\s+\\[[^\\]]+\\]\\(\\s*(https?:\\/\\/[^\\s)]+)(?:\\s+\"[^\"]*\")?\\s*\\)" - ], - "actions": [ - "goTo" - ] - }, - { - "name": "screenshotImage", - "regex": [ - "!\\[[^\\]]*\\]\\(\\s*([^\\s)]+)(?:\\s+\"[^\"]*\")?\\s*\\)\\s*\\{(?=[^}]*\\.screenshot)[^}]*\\}" - ], - "actions": [ - "screenshot" - ] - }, - { - "name": "typeText", - "regex": [ - "\\b(?:press|enter|type)\\b\\s+\"([^\"]+)\"" - ], - "actions": [ - "type" + { + "runCode": { + "language": "python", + "code": "print(\"Hello, world!\")\nprint(\"Hello to you too!\")" + } + } ] } ] - }, - { - "name": "asciidoc", - "extensions": [ - "adoc", - "asciidoc", - "asc" - ], - "inlineStatements": { - "testStart": [ - "\\/\\/\\s+\\(\\s*test\\s+([\\s\\S]*?)\\s*\\)" - ], - "testEnd": [ - "\\/\\/\\s+\\(\\s*test end\\s*\\)" - ], - "ignoreStart": [ - "\\/\\/\\s+\\(\\s*test ignore start\\s*\\)" - ], - "ignoreEnd": [ - "\\/\\/\\s+\\(\\s*test ignore end\\s*\\)" - ], - "step": [ - "\\/\\/\\s+\\(\\s*step\\s+([\\s\\S]*?)\\s*\\)" - ] - }, - "markup": [] - }, - { - "name": "html", - "extensions": [ - "html", - "htm" - ], - "inlineStatements": { - "testStart": [ - "" - ], - "testEnd": [ - "" - ], - "ignoreStart": [ - "" - ], - "ignoreEnd": [ - "" - ], - "step": [ - "" - ] - }, - "markup": [] } ], + "logLevel": "debug", + "output": ".", + "recursive": true, + "relativePathBase": "file", + "detectSteps": true, "telemetry": { "send": true }, - "configId": "343eeb23-6a45-496f-84ef-6210be7e10c7", + "configId": "6ffa39d0-4353-42f0-a891-09aae0980197", "environment": { "arch": "x64", "platform": "linux", @@ -452,606 +73,31 @@ }, "specs": [ { + "specId": "b0d003dd-0fe2-4e46-8844-05f7cd0be602", + "contentPath": "/home/hawkeyexl/Workspaces/resolver/dev/doc-content copy.md", "tests": [ { - "after": "/home/hawkeyexl/Workspaces/resolver/dev/cleanup.spec.json", - "testId": "17e04f88-1c54-447f-ab6b-1d3a38ef1033", + "testId": "2374e8d5-8c4b-444a-a321-034455045740", "runOn": [], - "openApi": [ - { - "name": "reqres", - "descriptionPath": "/home/hawkeyexl/Workspaces/resolver/dev/reqres.openapi.json", - "validateAgainstSchema": "both", - "useExample": "none", - "exampleKey": "", - "definition": { - "openapi": "3.0.3", - "info": { - "title": "Reqres API", - "description": "Sample API for testing and prototyping", - "version": "0.0.1" - }, - "servers": [ - { - "url": "https://reqres.in/api" - } - ], - "tags": [ - { - "name": "Test", - "description": "Test operations" - } - ], - "security": [ - {} - ], - "paths": { - "/users": { - "post": { - "tags": [ - "Test" - ], - "summary": "Add a new user", - "description": "Add a new user", - "operationId": "addUser", - "requestBody": { - "description": "Create a new user", - "content": { - "application/json": { - "schema": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - }, - "required": true - }, - "responses": { - "201": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "description": "response payload" - }, - "examples": { - "test": { - "value": { - "name": "morpheus", - "job": "leader" - } - }, - "foobar": { - "value": { - "name": "neo", - "job": "the-one" - } - } - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - }, - "message": { - "type": "string" - } - } - } - } - } - } - } - }, - "get": { - "tags": [ - "Test" - ], - "summary": "Return a list of users", - "description": "Return a list of users", - "operationId": "getUsers", - "parameters": [ - { - "name": "page", - "in": "query", - "description": "Select the portition of record you want back", - "required": false, - "schema": { - "type": "integer", - "example": 1 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "description": "response payload" - } - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - } - }, - "/users/{id}": { - "put": { - "tags": [ - "Test" - ], - "summary": "Update an existing user", - "description": "Update an existing user by Id", - "operationId": "updateUser", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "id of user to delete", - "required": true, - "example": 1, - "schema": { - "type": "integer", - "format": "int64" - } - } - ], - "requestBody": { - "description": "Update an existent pet in the store", - "content": { - "application/json": { - "schema": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "description": "response payload" - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - }, - "delete": { - "tags": [ - "Test" - ], - "summary": "Deletes a user", - "description": "delete a user", - "operationId": "deleteUser", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "id of user to delete", - "required": true, - "example": 1, - "schema": { - "type": "integer", - "format": "int64" - } - } - ], - "responses": { - "204": { - "description": "No content" - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - } - } - }, - "components": { - "schemas": { - "userResponse": { - "description": "response payload" - }, - "userRequest": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - } - } - } - ], + "openApi": [], "contexts": [ { "openApi": [], "steps": [ { - "goTo": "https://duckduckgo.com" - }, - { - "find": "/Set As.+/" - }, - { - "runShell": "echo cleanup" + "runCode": { + "language": "python", + "code": "print(\"Hello, world!\")\nprint(\"Hello to you too!\")" + } } ], - "contextId": "e7c3c74c-315a-453e-86c8-537171c1ff72" + "contextId": "4eb87d33-034d-49ee-8252-be3981ff92ed" } ] } ], - "specId": "722226ef-bc40-4e98-8ddf-c7c0da8b377f", "runOn": [], - "openApi": [ - { - "name": "reqres", - "descriptionPath": "/home/hawkeyexl/Workspaces/resolver/dev/reqres.openapi.json", - "validateAgainstSchema": "both", - "useExample": "none", - "exampleKey": "", - "definition": { - "openapi": "3.0.3", - "info": { - "title": "Reqres API", - "description": "Sample API for testing and prototyping", - "version": "0.0.1" - }, - "servers": [ - { - "url": "https://reqres.in/api" - } - ], - "tags": [ - { - "name": "Test", - "description": "Test operations" - } - ], - "security": [ - {} - ], - "paths": { - "/users": { - "post": { - "tags": [ - "Test" - ], - "summary": "Add a new user", - "description": "Add a new user", - "operationId": "addUser", - "requestBody": { - "description": "Create a new user", - "content": { - "application/json": { - "schema": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - }, - "required": true - }, - "responses": { - "201": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "description": "response payload" - }, - "examples": { - "test": { - "value": { - "name": "morpheus", - "job": "leader" - } - }, - "foobar": { - "value": { - "name": "neo", - "job": "the-one" - } - } - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - }, - "message": { - "type": "string" - } - } - } - } - } - } - } - }, - "get": { - "tags": [ - "Test" - ], - "summary": "Return a list of users", - "description": "Return a list of users", - "operationId": "getUsers", - "parameters": [ - { - "name": "page", - "in": "query", - "description": "Select the portition of record you want back", - "required": false, - "schema": { - "type": "integer", - "example": 1 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "description": "response payload" - } - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - } - }, - "/users/{id}": { - "put": { - "tags": [ - "Test" - ], - "summary": "Update an existing user", - "description": "Update an existing user by Id", - "operationId": "updateUser", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "id of user to delete", - "required": true, - "example": 1, - "schema": { - "type": "integer", - "format": "int64" - } - } - ], - "requestBody": { - "description": "Update an existent pet in the store", - "content": { - "application/json": { - "schema": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "description": "response payload" - } - } - } - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - }, - "delete": { - "tags": [ - "Test" - ], - "summary": "Deletes a user", - "description": "delete a user", - "operationId": "deleteUser", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "id of user to delete", - "required": true, - "example": 1, - "schema": { - "type": "integer", - "format": "int64" - } - } - ], - "responses": { - "204": { - "description": "No content" - }, - "400": { - "description": "Invalid input", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "error": { - "type": "string" - } - } - } - } - } - } - } - } - } - }, - "components": { - "schemas": { - "userResponse": { - "description": "response payload" - }, - "userRequest": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "job": { - "type": "string" - } - } - } - } - } - } - } - ] + "openApi": [] } ] } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9502d76..8d2873b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "doc-detective-resolver", - "version": "3.0.0", + "version": "3.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "doc-detective-resolver", - "version": "3.0.0", + "version": "3.0.1", "license": "AGPL-3.0-only", "dependencies": { "@apidevtools/json-schema-ref-parser": "^12.0.2", @@ -15,7 +15,7 @@ "doc-detective-common": "^3.0.4", "dotenv": "^16.5.0", "json-schema-faker": "^0.5.9", - "posthog-node": "^4.17.1", + "posthog-node": "^4.17.2", "uuid": "^11.1.0" }, "devDependencies": { @@ -1819,9 +1819,9 @@ "license": "ISC" }, "node_modules/posthog-node": { - "version": "4.17.1", - "resolved": "https://registry.npmjs.org/posthog-node/-/posthog-node-4.17.1.tgz", - "integrity": "sha512-cVlQPOwOPjakUnrueKRCQe1m2Ku+XzKaOos7Tn/zDZkkZFeBT/byP7tbNf7LiwhaBRWFBRowZZb/MsTtSRaorg==", + "version": "4.17.2", + "resolved": "https://registry.npmjs.org/posthog-node/-/posthog-node-4.17.2.tgz", + "integrity": "sha512-bFmwOTk4QdYavopeHVXtyFGQ9vyLMVaNWkWocwjix+0n6sQgv7Zq5nYjYulz7ThmK18zsvNJ337ahuMLv3ulow==", "license": "MIT", "dependencies": { "axios": "^1.8.2" diff --git a/package.json b/package.json index 0a90e3b..c28ce6b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "doc-detective-resolver", - "version": "3.0.0", + "version": "3.0.1", "description": "Detect and resolve docs into Doc Detective tests.", "main": "src/index.js", "scripts": { @@ -30,7 +30,7 @@ "doc-detective-common": "^3.0.4", "dotenv": "^16.5.0", "json-schema-faker": "^0.5.9", - "posthog-node": "^4.17.1", + "posthog-node": "^4.17.2", "uuid": "^11.1.0" }, "devDependencies": { diff --git a/src/utils.js b/src/utils.js index 35a9902..d73f67a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -251,6 +251,18 @@ async function isValidSourceFile({ config, files, source }) { return true; } +/** + * Parses raw test content into an array of structured test objects. + * + * Processes input content using inline statement and markup regex patterns defined by {@link fileType}, extracting test and step definitions. Supports detection of test boundaries, ignored sections, and step definitions, including batch markup matches. Converts and validates extracted objects against the test and step schemas, handling both v2 and v3 formats. Returns an array of validated test objects with their associated steps. + * + * @param {Object} options - Options for parsing. + * @param {Object} options.config - Test configuration object. + * @param {string|Object} options.content - Raw file content as a string or object. + * @param {string} options.filePath - Path to the file being parsed. + * @param {Object} options.fileType - File type definition containing parsing rules. + * @returns {Array} Array of parsed and validated test objects. + */ async function parseContent({ config, content, filePath, fileType }) { const statements = []; const statementTypes = [ @@ -343,7 +355,7 @@ async function parseContent({ config, content, filePath, fileType }) { if (matches.length > 0 && markup.batchMatches) { // Combine all matches into a single match const combinedMatch = { - 1: matches.map((match) => match[1] || match[0]).join(""), + 1: matches.map((match) => match[1] || match[0]).join(os.EOL), type: "detectedStep", markup: markup, sortIndex: Math.min(...matches.map((match) => match.index)),