Remove testStep endpoint and fix types.

This commit is contained in:
Sam Rose 2025-01-21 17:06:09 +00:00
parent 31fc2e45c9
commit 0e6ad4db93
No known key found for this signature in database
12 changed files with 26 additions and 141 deletions

View File

@ -28,18 +28,11 @@ import {
TriggerAutomationResponse,
TestAutomationRequest,
TestAutomationResponse,
TestAutomationStepRequest,
TestAutomationStepResponse,
} from "@budibase/types"
import {
getActionDefinitions as actionDefs,
getAction,
} from "../../automations/actions"
import { getActionDefinitions as actionDefs } from "../../automations/actions"
import sdk from "../../sdk"
import { builderSocket } from "../../websockets"
import env from "../../environment"
import { NoopEmitter } from "../../events"
import { enrichBaseContext } from "../../threads/automation"
async function getActionDefinitions() {
return removeDeprecated(await actionDefs())
@ -260,46 +253,3 @@ export async function test(
await events.automation.tested(automation)
}
export async function testStep(
ctx: UserCtx<TestAutomationStepRequest, TestAutomationStepResponse>
) {
const { id, stepId } = ctx.params
const db = context.getAppDB()
const automation = await db.tryGet<Automation>(id)
if (!automation) {
ctx.throw(404, `Automation ${ctx.params.id} not found`)
}
const step = automation.definition.steps.find(s => s.stepId === stepId)
if (!step) {
ctx.throw(404, `Step ${stepId} not found on automation ${id}`)
}
if (step.stepId === AutomationActionStepId.BRANCH) {
ctx.throw(400, "Branch steps cannot be tested directly")
}
if (step.stepId === AutomationActionStepId.LOOP) {
ctx.throw(400, "Loop steps cannot be tested directly")
}
const { body } = ctx.request
const fn = await getAction(step.stepId)
if (!fn) {
ctx.throw(400, `Step ${stepId} is not a valid step`)
}
const output = await withTestFlag(
automation._id!,
async () =>
await fn({
inputs: body.inputs,
context: await enrichBaseContext(body.context),
appId: ctx.appId,
emitter: new NoopEmitter(),
})
)
ctx.body = output
}

View File

@ -1,6 +1,6 @@
import Router from "@koa/router"
import * as controller from "../controllers/automation"
import authorized, { authorizedResource } from "../../middleware/authorized"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import { bodyResource, paramResource } from "../../middleware/resourceId"
import {
@ -82,15 +82,5 @@ router
),
controller.test
)
.post(
"/api/automations/:id/step/:stepId/test",
appInfoMiddleware({ appType: AppType.DEV }),
authorizedResource(
permissions.PermissionType.AUTOMATION,
permissions.PermissionLevel.EXECUTE,
"id"
),
controller.testStep
)
export default router

View File

@ -5,8 +5,11 @@ import {
sendAutomationAttachmentsToStorage,
} from "../automationUtils"
import { buildCtx } from "./utils"
import { CreateRowStepInputs, CreateRowStepOutputs } from "@budibase/types"
import { EventEmitter } from "events"
import {
ContextEmitter,
CreateRowStepInputs,
CreateRowStepOutputs,
} from "@budibase/types"
export async function run({
inputs,
@ -15,7 +18,7 @@ export async function run({
}: {
inputs: CreateRowStepInputs
appId: string
emitter: EventEmitter
emitter: ContextEmitter
}): Promise<CreateRowStepOutputs> {
if (inputs.row == null || inputs.row.tableId == null) {
return {

View File

@ -1,8 +1,11 @@
import { EventEmitter } from "events"
import { destroy } from "../../api/controllers/row"
import { buildCtx } from "./utils"
import { getError } from "../automationUtils"
import { DeleteRowStepInputs, DeleteRowStepOutputs } from "@budibase/types"
import {
ContextEmitter,
DeleteRowStepInputs,
DeleteRowStepOutputs,
} from "@budibase/types"
export async function run({
inputs,
@ -11,7 +14,7 @@ export async function run({
}: {
inputs: DeleteRowStepInputs
appId: string
emitter: EventEmitter
emitter: ContextEmitter
}): Promise<DeleteRowStepOutputs> {
if (inputs.id == null) {
return {

View File

@ -1,8 +1,8 @@
import { EventEmitter } from "events"
import * as queryController from "../../api/controllers/query"
import { buildCtx } from "./utils"
import * as automationUtils from "../automationUtils"
import {
ContextEmitter,
ExecuteQueryStepInputs,
ExecuteQueryStepOutputs,
} from "@budibase/types"
@ -14,7 +14,7 @@ export async function run({
}: {
inputs: ExecuteQueryStepInputs
appId: string
emitter: EventEmitter
emitter: ContextEmitter
}): Promise<ExecuteQueryStepOutputs> {
if (inputs.query == null) {
return {

View File

@ -2,10 +2,10 @@ import * as scriptController from "../../api/controllers/script"
import { buildCtx } from "./utils"
import * as automationUtils from "../automationUtils"
import {
ContextEmitter,
ExecuteScriptStepInputs,
ExecuteScriptStepOutputs,
} from "@budibase/types"
import { EventEmitter } from "events"
export async function run({
inputs,
@ -16,7 +16,7 @@ export async function run({
inputs: ExecuteScriptStepInputs
appId: string
context: object
emitter: EventEmitter
emitter: ContextEmitter
}): Promise<ExecuteScriptStepOutputs> {
if (inputs.code == null) {
return {

View File

@ -1,8 +1,11 @@
import { EventEmitter } from "events"
import * as rowController from "../../api/controllers/row"
import * as automationUtils from "../automationUtils"
import { buildCtx } from "./utils"
import { UpdateRowStepInputs, UpdateRowStepOutputs } from "@budibase/types"
import {
ContextEmitter,
UpdateRowStepInputs,
UpdateRowStepOutputs,
} from "@budibase/types"
export async function run({
inputs,
@ -11,7 +14,7 @@ export async function run({
}: {
inputs: UpdateRowStepInputs
appId: string
emitter: EventEmitter
emitter: ContextEmitter
}): Promise<UpdateRowStepOutputs> {
if (inputs.rowId == null || inputs.row == null) {
return {

View File

@ -1,4 +1,4 @@
import { EventEmitter } from "events"
import { ContextEmitter } from "@budibase/types"
export async function getFetchResponse(fetched: any) {
let status = fetched.status,
@ -22,7 +22,7 @@ export async function getFetchResponse(fetched: any) {
// opts can contain, body, params and version
export function buildCtx(
appId: string,
emitter?: EventEmitter | null,
emitter?: ContextEmitter | null,
opts: any = {}
) {
const ctx: any = {

View File

@ -1,39 +0,0 @@
import { EventEmitter } from "events"
import {
Table,
Row,
ContextEmitter,
EventType,
UserBindings,
} from "@budibase/types"
export class NoopEmitter extends EventEmitter implements ContextEmitter {
emitRow(values: {
eventName: EventType.ROW_SAVE
appId: string
row: Row
table: Table
user: UserBindings
}): void
emitRow(values: {
eventName: EventType.ROW_UPDATE
appId: string
row: Row
table: Table
oldRow: Row
user: UserBindings
}): void
emitRow(values: {
eventName: EventType.ROW_DELETE
appId: string
row: Row
user: UserBindings
}): void
emitRow(_values: unknown): void {
return
}
emitTable(_eventName: string, _appId: string, _table?: Table) {
return
}
}

View File

@ -2,6 +2,5 @@ import BudibaseEmitter from "./BudibaseEmitter"
const emitter = new BudibaseEmitter()
export { NoopEmitter } from "./NoopEmitter"
export { init } from "./docUpdates"
export default emitter

View File

@ -3,8 +3,6 @@ import {
FetchAutomationResponse,
TestAutomationRequest,
TestAutomationResponse,
TestAutomationStepRequest,
TestAutomationStepResponse,
} from "@budibase/types"
import { Expectations, TestAPI } from "./base"
@ -54,19 +52,4 @@ export class AutomationAPI extends TestAPI {
}
)
}
testStep = async (
id: string,
stepId: string,
body: TestAutomationStepRequest,
expectations?: Expectations
): Promise<TestAutomationStepResponse> => {
return await this._post<TestAutomationStepResponse>(
`/api/automations/${id}/steps/${stepId}/test`,
{
body,
expectations,
}
)
}
}

View File

@ -83,10 +83,3 @@ export function isDidNotTriggerResponse(
): response is DidNotTriggerResponse {
return !!("message" in response && response.message)
}
export interface TestAutomationStepRequest {
inputs: Record<string, any>
context: Record<string, any>
}
export type TestAutomationStepResponse = any