mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 15:20:44 +00:00
test: share approval native runtime stubs
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { createRuntimeChannel } from "../plugins/runtime/runtime-channel.js";
|
import { createRuntimeChannel } from "../plugins/runtime/runtime-channel.js";
|
||||||
import { startChannelApprovalHandlerBootstrap } from "./approval-handler-bootstrap.js";
|
import { startChannelApprovalHandlerBootstrap } from "./approval-handler-bootstrap.js";
|
||||||
|
import { createApprovalNativeRuntimeAdapterStubs } from "./approval-handler.test-helpers.js";
|
||||||
|
|
||||||
const { createChannelApprovalHandlerFromCapability } = vi.hoisted(() => ({
|
const { createChannelApprovalHandlerFromCapability } = vi.hoisted(() => ({
|
||||||
createChannelApprovalHandlerFromCapability: vi.fn(),
|
createChannelApprovalHandlerFromCapability: vi.fn(),
|
||||||
@@ -33,21 +34,7 @@ describe("startChannelApprovalHandlerBootstrap", () => {
|
|||||||
id: "slack",
|
id: "slack",
|
||||||
meta: { label: "Slack" },
|
meta: { label: "Slack" },
|
||||||
approvalCapability: {
|
approvalCapability: {
|
||||||
nativeRuntime: {
|
nativeRuntime: createApprovalNativeRuntimeAdapterStubs(),
|
||||||
availability: {
|
|
||||||
isConfigured: vi.fn().mockReturnValue(true),
|
|
||||||
shouldHandle: vi.fn().mockReturnValue(true),
|
|
||||||
},
|
|
||||||
presentation: {
|
|
||||||
buildPendingPayload: vi.fn(),
|
|
||||||
buildResolvedResult: vi.fn(),
|
|
||||||
buildExpiredResult: vi.fn(),
|
|
||||||
},
|
|
||||||
transport: {
|
|
||||||
prepareTarget: vi.fn(),
|
|
||||||
deliverPending: vi.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}) as never;
|
}) as never;
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ import { describe, expect, it, vi } from "vitest";
|
|||||||
import {
|
import {
|
||||||
createChannelApprovalHandlerFromCapability,
|
createChannelApprovalHandlerFromCapability,
|
||||||
createLazyChannelApprovalNativeRuntimeAdapter,
|
createLazyChannelApprovalNativeRuntimeAdapter,
|
||||||
type ChannelApprovalNativeRuntimeAdapter,
|
|
||||||
} from "./approval-handler-runtime.js";
|
} from "./approval-handler-runtime.js";
|
||||||
|
import {
|
||||||
|
createApprovalNativeRuntimeAdapterStubs,
|
||||||
|
type ApprovalNativeRuntimeAdapterStubParams,
|
||||||
|
} from "./approval-handler.test-helpers.js";
|
||||||
import type { ExecApprovalRequest } from "./exec-approvals.js";
|
import type { ExecApprovalRequest } from "./exec-approvals.js";
|
||||||
|
|
||||||
type ApprovalCapability = NonNullable<
|
type ApprovalCapability = NonNullable<
|
||||||
@@ -53,15 +56,7 @@ function makeNativeApprovalCapability(
|
|||||||
>["preferredSurface"];
|
>["preferredSurface"];
|
||||||
supportsApproverDmSurface?: boolean;
|
supportsApproverDmSurface?: boolean;
|
||||||
resolveApproverDmTargets?: ApprovalNativeAdapter["resolveApproverDmTargets"];
|
resolveApproverDmTargets?: ApprovalNativeAdapter["resolveApproverDmTargets"];
|
||||||
resolveApprovalKind?: ChannelApprovalNativeRuntimeAdapter["resolveApprovalKind"];
|
} & ApprovalNativeRuntimeAdapterStubParams = {},
|
||||||
buildResolvedResult?: ChannelApprovalNativeRuntimeAdapter["presentation"]["buildResolvedResult"];
|
|
||||||
unbindPending?: NonNullable<
|
|
||||||
ChannelApprovalNativeRuntimeAdapter["interactions"]
|
|
||||||
>["unbindPending"];
|
|
||||||
prepareTarget?: ChannelApprovalNativeRuntimeAdapter["transport"]["prepareTarget"];
|
|
||||||
deliverPending?: ChannelApprovalNativeRuntimeAdapter["transport"]["deliverPending"];
|
|
||||||
bindPending?: NonNullable<ChannelApprovalNativeRuntimeAdapter["interactions"]>["bindPending"];
|
|
||||||
} = {},
|
|
||||||
): ApprovalCapability {
|
): ApprovalCapability {
|
||||||
const preferredSurface = params.preferredSurface ?? "origin";
|
const preferredSurface = params.preferredSurface ?? "origin";
|
||||||
return {
|
return {
|
||||||
@@ -78,31 +73,7 @@ function makeNativeApprovalCapability(
|
|||||||
? { resolveApproverDmTargets: params.resolveApproverDmTargets }
|
? { resolveApproverDmTargets: params.resolveApproverDmTargets }
|
||||||
: {}),
|
: {}),
|
||||||
},
|
},
|
||||||
nativeRuntime: {
|
nativeRuntime: createApprovalNativeRuntimeAdapterStubs(params),
|
||||||
resolveApprovalKind: params.resolveApprovalKind,
|
|
||||||
availability: {
|
|
||||||
isConfigured: vi.fn().mockReturnValue(true),
|
|
||||||
shouldHandle: vi.fn().mockReturnValue(true),
|
|
||||||
},
|
|
||||||
presentation: {
|
|
||||||
buildPendingPayload: vi.fn().mockResolvedValue({ text: "pending" }),
|
|
||||||
buildResolvedResult: params.buildResolvedResult ?? vi.fn(),
|
|
||||||
buildExpiredResult: vi.fn(),
|
|
||||||
},
|
|
||||||
transport: {
|
|
||||||
prepareTarget:
|
|
||||||
params.prepareTarget ??
|
|
||||||
vi.fn().mockResolvedValue({
|
|
||||||
dedupeKey: "origin-chat",
|
|
||||||
target: { to: "origin-chat" },
|
|
||||||
}),
|
|
||||||
deliverPending: params.deliverPending ?? vi.fn().mockResolvedValue({ messageId: "1" }),
|
|
||||||
},
|
|
||||||
interactions: {
|
|
||||||
bindPending: params.bindPending ?? vi.fn().mockResolvedValue({ bindingId: "bound" }),
|
|
||||||
unbindPending: params.unbindPending,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
41
src/infra/approval-handler.test-helpers.ts
Normal file
41
src/infra/approval-handler.test-helpers.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { vi } from "vitest";
|
||||||
|
import type { ChannelApprovalNativeRuntimeAdapter } from "./approval-handler-runtime.js";
|
||||||
|
|
||||||
|
export type ApprovalNativeRuntimeAdapterStubParams = {
|
||||||
|
resolveApprovalKind?: ChannelApprovalNativeRuntimeAdapter["resolveApprovalKind"];
|
||||||
|
buildResolvedResult?: ChannelApprovalNativeRuntimeAdapter["presentation"]["buildResolvedResult"];
|
||||||
|
unbindPending?: NonNullable<ChannelApprovalNativeRuntimeAdapter["interactions"]>["unbindPending"];
|
||||||
|
prepareTarget?: ChannelApprovalNativeRuntimeAdapter["transport"]["prepareTarget"];
|
||||||
|
deliverPending?: ChannelApprovalNativeRuntimeAdapter["transport"]["deliverPending"];
|
||||||
|
bindPending?: NonNullable<ChannelApprovalNativeRuntimeAdapter["interactions"]>["bindPending"];
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createApprovalNativeRuntimeAdapterStubs(
|
||||||
|
params: ApprovalNativeRuntimeAdapterStubParams = {},
|
||||||
|
): ChannelApprovalNativeRuntimeAdapter {
|
||||||
|
return {
|
||||||
|
resolveApprovalKind: params.resolveApprovalKind,
|
||||||
|
availability: {
|
||||||
|
isConfigured: vi.fn().mockReturnValue(true),
|
||||||
|
shouldHandle: vi.fn().mockReturnValue(true),
|
||||||
|
},
|
||||||
|
presentation: {
|
||||||
|
buildPendingPayload: vi.fn().mockResolvedValue({ text: "pending" }),
|
||||||
|
buildResolvedResult: params.buildResolvedResult ?? vi.fn(),
|
||||||
|
buildExpiredResult: vi.fn(),
|
||||||
|
},
|
||||||
|
transport: {
|
||||||
|
prepareTarget:
|
||||||
|
params.prepareTarget ??
|
||||||
|
vi.fn().mockResolvedValue({
|
||||||
|
dedupeKey: "origin-chat",
|
||||||
|
target: { to: "origin-chat" },
|
||||||
|
}),
|
||||||
|
deliverPending: params.deliverPending ?? vi.fn().mockResolvedValue({ messageId: "1" }),
|
||||||
|
},
|
||||||
|
interactions: {
|
||||||
|
bindPending: params.bindPending ?? vi.fn().mockResolvedValue({ bindingId: "bound" }),
|
||||||
|
unbindPending: params.unbindPending,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user