mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 07:00:43 +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 { createRuntimeChannel } from "../plugins/runtime/runtime-channel.js";
|
||||
import { startChannelApprovalHandlerBootstrap } from "./approval-handler-bootstrap.js";
|
||||
import { createApprovalNativeRuntimeAdapterStubs } from "./approval-handler.test-helpers.js";
|
||||
|
||||
const { createChannelApprovalHandlerFromCapability } = vi.hoisted(() => ({
|
||||
createChannelApprovalHandlerFromCapability: vi.fn(),
|
||||
@@ -33,21 +34,7 @@ describe("startChannelApprovalHandlerBootstrap", () => {
|
||||
id: "slack",
|
||||
meta: { label: "Slack" },
|
||||
approvalCapability: {
|
||||
nativeRuntime: {
|
||||
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(),
|
||||
},
|
||||
},
|
||||
nativeRuntime: createApprovalNativeRuntimeAdapterStubs(),
|
||||
},
|
||||
}) as never;
|
||||
|
||||
|
||||
@@ -2,8 +2,11 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createChannelApprovalHandlerFromCapability,
|
||||
createLazyChannelApprovalNativeRuntimeAdapter,
|
||||
type ChannelApprovalNativeRuntimeAdapter,
|
||||
} from "./approval-handler-runtime.js";
|
||||
import {
|
||||
createApprovalNativeRuntimeAdapterStubs,
|
||||
type ApprovalNativeRuntimeAdapterStubParams,
|
||||
} from "./approval-handler.test-helpers.js";
|
||||
import type { ExecApprovalRequest } from "./exec-approvals.js";
|
||||
|
||||
type ApprovalCapability = NonNullable<
|
||||
@@ -53,15 +56,7 @@ function makeNativeApprovalCapability(
|
||||
>["preferredSurface"];
|
||||
supportsApproverDmSurface?: boolean;
|
||||
resolveApproverDmTargets?: ApprovalNativeAdapter["resolveApproverDmTargets"];
|
||||
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"];
|
||||
} = {},
|
||||
} & ApprovalNativeRuntimeAdapterStubParams = {},
|
||||
): ApprovalCapability {
|
||||
const preferredSurface = params.preferredSurface ?? "origin";
|
||||
return {
|
||||
@@ -78,31 +73,7 @@ function makeNativeApprovalCapability(
|
||||
? { resolveApproverDmTargets: params.resolveApproverDmTargets }
|
||||
: {}),
|
||||
},
|
||||
nativeRuntime: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
nativeRuntime: createApprovalNativeRuntimeAdapterStubs(params),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
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