|
@@ -1,10 +1,17 @@
|
|
|
import { Observable } from "rxjs"
|
|
|
import { filter } from "rxjs/operators"
|
|
|
import { chain, right, TaskEither } from "fp-ts/lib/TaskEither"
|
|
|
-import { pipe } from "fp-ts/function"
|
|
|
+import { flow, pipe } from "fp-ts/function"
|
|
|
import * as O from "fp-ts/Option"
|
|
|
-import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
|
|
|
+import * as A from "fp-ts/Array"
|
|
|
+import { Environment } from "@hoppscotch/data"
|
|
|
+import {
|
|
|
+ SandboxTestResult,
|
|
|
+ runTestScript,
|
|
|
+ TestDescriptor,
|
|
|
+} from "@hoppscotch/js-sandbox"
|
|
|
import { isRight } from "fp-ts/Either"
|
|
|
+import cloneDeep from "lodash/cloneDeep"
|
|
|
import {
|
|
|
getCombinedEnvVariables,
|
|
|
getFinalEnvsFromPreRequest,
|
|
@@ -15,6 +22,14 @@ import { createRESTNetworkRequestStream } from "./network"
|
|
|
import { HoppTestData, HoppTestResult } from "./types/HoppTestResult"
|
|
|
import { isJSONContentType } from "./utils/contenttypes"
|
|
|
import { getRESTRequest, setRESTTestResults } from "~/newstore/RESTSession"
|
|
|
+import {
|
|
|
+ environmentsStore,
|
|
|
+ getCurrentEnvironment,
|
|
|
+ getEnviroment,
|
|
|
+ getGlobalVariables,
|
|
|
+ setGlobalEnvVariables,
|
|
|
+ updateEnvironment,
|
|
|
+} from "~/newstore/environments"
|
|
|
|
|
|
const getTestableBody = (
|
|
|
res: HoppRESTResponse & { type: "success" | "fail" }
|
|
@@ -43,6 +58,11 @@ const getTestableBody = (
|
|
|
return x
|
|
|
}
|
|
|
|
|
|
+const combineEnvVariables = (env: {
|
|
|
+ global: Environment["variables"]
|
|
|
+ selected: Environment["variables"]
|
|
|
+}) => [...env.selected, ...env.global]
|
|
|
+
|
|
|
export const runRESTRequest$ = (): TaskEither<
|
|
|
string | Error,
|
|
|
Observable<HoppRESTResponse>
|
|
@@ -55,7 +75,7 @@ export const runRESTRequest$ = (): TaskEither<
|
|
|
chain((envs) => {
|
|
|
const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
|
|
|
name: "Env",
|
|
|
- variables: envs,
|
|
|
+ variables: combineEnvVariables(envs),
|
|
|
})
|
|
|
|
|
|
const stream = createRESTNetworkRequestStream(effectiveRequest)
|
|
@@ -65,7 +85,7 @@ export const runRESTRequest$ = (): TaskEither<
|
|
|
.pipe(filter((res) => res.type === "success" || res.type === "fail"))
|
|
|
.subscribe(async (res) => {
|
|
|
if (res.type === "success" || res.type === "fail") {
|
|
|
- const runResult = await runTestScript(res.req.testScript, {
|
|
|
+ const runResult = await runTestScript(res.req.testScript, envs, {
|
|
|
status: res.statusCode,
|
|
|
body: getTestableBody(res),
|
|
|
headers: res.headers,
|
|
@@ -73,11 +93,38 @@ export const runRESTRequest$ = (): TaskEither<
|
|
|
|
|
|
if (isRight(runResult)) {
|
|
|
setRESTTestResults(translateToSandboxTestResults(runResult.right))
|
|
|
+
|
|
|
+ setGlobalEnvVariables(runResult.right.envs.global)
|
|
|
+
|
|
|
+ if (environmentsStore.value.currentEnvironmentIndex !== -1) {
|
|
|
+ const env = getEnviroment(
|
|
|
+ environmentsStore.value.currentEnvironmentIndex
|
|
|
+ )
|
|
|
+ updateEnvironment(
|
|
|
+ environmentsStore.value.currentEnvironmentIndex,
|
|
|
+ {
|
|
|
+ name: env.name,
|
|
|
+ variables: runResult.right.envs.selected,
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
} else {
|
|
|
setRESTTestResults({
|
|
|
description: "",
|
|
|
expectResults: [],
|
|
|
tests: [],
|
|
|
+ envDiff: {
|
|
|
+ global: {
|
|
|
+ additions: [],
|
|
|
+ deletions: [],
|
|
|
+ updations: [],
|
|
|
+ },
|
|
|
+ selected: {
|
|
|
+ additions: [],
|
|
|
+ deletions: [],
|
|
|
+ updations: [],
|
|
|
+ },
|
|
|
+ },
|
|
|
scriptError: true,
|
|
|
})
|
|
|
}
|
|
@@ -90,8 +137,47 @@ export const runRESTRequest$ = (): TaskEither<
|
|
|
})
|
|
|
)
|
|
|
|
|
|
+const getAddedEnvVariables = (
|
|
|
+ current: Environment["variables"],
|
|
|
+ updated: Environment["variables"]
|
|
|
+) => updated.filter((x) => current.findIndex((y) => y.key === x.key) === -1)
|
|
|
+
|
|
|
+const getRemovedEnvVariables = (
|
|
|
+ current: Environment["variables"],
|
|
|
+ updated: Environment["variables"]
|
|
|
+) => current.filter((x) => updated.findIndex((y) => y.key === x.key) === -1)
|
|
|
+
|
|
|
+const getUpdatedEnvVariables = (
|
|
|
+ current: Environment["variables"],
|
|
|
+ updated: Environment["variables"]
|
|
|
+) =>
|
|
|
+ pipe(
|
|
|
+ updated,
|
|
|
+ A.filterMap(
|
|
|
+ flow(
|
|
|
+ O.of,
|
|
|
+ O.bindTo("env"),
|
|
|
+ O.bind("index", ({ env }) =>
|
|
|
+ pipe(
|
|
|
+ current.findIndex((x) => x.key === env.key),
|
|
|
+ O.fromPredicate((x) => x !== -1)
|
|
|
+ )
|
|
|
+ ),
|
|
|
+ O.chain(
|
|
|
+ O.fromPredicate(
|
|
|
+ ({ env, index }) => env.value !== current[index].value
|
|
|
+ )
|
|
|
+ ),
|
|
|
+ O.map(({ env, index }) => ({
|
|
|
+ ...env,
|
|
|
+ previousValue: current[index].value,
|
|
|
+ }))
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
function translateToSandboxTestResults(
|
|
|
- testDesc: TestDescriptor
|
|
|
+ testDesc: SandboxTestResult
|
|
|
): HoppTestResult {
|
|
|
const translateChildTests = (child: TestDescriptor): HoppTestData => {
|
|
|
return {
|
|
@@ -100,10 +186,32 @@ function translateToSandboxTestResults(
|
|
|
tests: child.children.map(translateChildTests),
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ const globals = cloneDeep(getGlobalVariables())
|
|
|
+ const env = cloneDeep(getCurrentEnvironment())
|
|
|
+
|
|
|
return {
|
|
|
description: "",
|
|
|
- expectResults: testDesc.expectResults,
|
|
|
- tests: testDesc.children.map(translateChildTests),
|
|
|
+ expectResults: testDesc.tests.expectResults,
|
|
|
+ tests: testDesc.tests.children.map(translateChildTests),
|
|
|
scriptError: false,
|
|
|
+ envDiff: {
|
|
|
+ global: {
|
|
|
+ additions: getAddedEnvVariables(globals, testDesc.envs.global),
|
|
|
+ deletions: getRemovedEnvVariables(globals, testDesc.envs.global),
|
|
|
+ updations: getUpdatedEnvVariables(globals, testDesc.envs.global),
|
|
|
+ },
|
|
|
+ selected: {
|
|
|
+ additions: getAddedEnvVariables(env.variables, testDesc.envs.selected),
|
|
|
+ deletions: getRemovedEnvVariables(
|
|
|
+ env.variables,
|
|
|
+ testDesc.envs.selected
|
|
|
+ ),
|
|
|
+ updations: getUpdatedEnvVariables(
|
|
|
+ env.variables,
|
|
|
+ testDesc.envs.selected
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ },
|
|
|
}
|
|
|
}
|