Tests: align approval runtime helpers

This commit is contained in:
Gustavo Madeira Santana
2026-04-07 15:36:22 -04:00
parent 0995ee0134
commit ad6bfc44d5
8 changed files with 183 additions and 1174 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -173,7 +173,7 @@ describe("matrix native approval adapter", () => {
createdAtMs: 0,
expiresAtMs: 1000,
},
}),
} as never),
).toBe(true);
});

View File

@@ -3,6 +3,7 @@ import {
createChannelApprovalHandlerFromCapability,
createLazyChannelApprovalNativeRuntimeAdapter,
} from "./approval-handler-runtime.js";
import type { ExecApprovalRequest } from "./exec-approvals.js";
describe("createChannelApprovalHandlerFromCapability", () => {
it("returns null when the capability does not expose a native runtime", async () => {
@@ -113,7 +114,7 @@ describe("createChannelApprovalHandlerFromCapability", () => {
);
});
it("unbinds and finalizes every prior pending delivery when the same approval id is requested again", async () => {
it("ignores duplicate pending request ids before finalization", async () => {
const unbindPending = vi.fn();
const buildResolvedResult = vi.fn().mockResolvedValue({ kind: "leave" });
const runtime = await createChannelApprovalHandlerFromCapability({
@@ -183,27 +184,18 @@ describe("createChannelApprovalHandlerFromCapability", () => {
resolvedBy: "operator",
} as never);
expect(unbindPending).toHaveBeenCalledTimes(2);
expect(unbindPending).toHaveBeenNthCalledWith(
1,
expect(unbindPending).toHaveBeenCalledTimes(1);
expect(unbindPending).toHaveBeenCalledWith(
expect.objectContaining({
entry: { messageId: "1" },
binding: { bindingId: "bound-1" },
request,
}),
);
expect(unbindPending).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
entry: { messageId: "2" },
binding: { bindingId: "bound-2" },
request,
}),
);
expect(buildResolvedResult).toHaveBeenCalledTimes(2);
expect(buildResolvedResult).toHaveBeenCalledTimes(1);
});
it("continues finalizing later entries when one resolved entry cleanup throws", async () => {
it("continues finalization cleanup after one resolved entry unbind failure", async () => {
const unbindPending = vi
.fn()
.mockRejectedValueOnce(new Error("unbind failed"))
@@ -214,12 +206,13 @@ describe("createChannelApprovalHandlerFromCapability", () => {
native: {
describeDeliveryCapabilities: vi.fn().mockReturnValue({
enabled: true,
preferredSurface: "origin",
preferredSurface: "both",
supportsOriginSurface: true,
supportsApproverDmSurface: false,
supportsApproverDmSurface: true,
notifyOriginWhenDmOnly: false,
}),
resolveOriginTarget: vi.fn().mockReturnValue({ to: "origin-chat" }),
resolveApproverDmTargets: vi.fn().mockResolvedValue([{ to: "approver-dm" }]),
},
nativeRuntime: {
availability: {
@@ -232,10 +225,10 @@ describe("createChannelApprovalHandlerFromCapability", () => {
buildExpiredResult: vi.fn(),
},
transport: {
prepareTarget: vi.fn().mockResolvedValue({
dedupeKey: "origin-chat",
target: { to: "origin-chat" },
}),
prepareTarget: vi.fn().mockImplementation(async ({ plannedTarget }) => ({
dedupeKey: String(plannedTarget.target.to),
target: { to: plannedTarget.target.to },
})),
deliverPending: vi
.fn()
.mockResolvedValueOnce({ messageId: "1" })
@@ -267,7 +260,6 @@ describe("createChannelApprovalHandlerFromCapability", () => {
},
} as never;
await runtime?.handleRequested(request);
await runtime?.handleRequested(request);
await expect(
runtime?.handleResolved({
@@ -339,7 +331,7 @@ describe("createChannelApprovalHandlerFromCapability", () => {
cfg: { channels: {} } as never,
});
const request = {
const request: ExecApprovalRequest = {
id: "exec:stop-1",
expiresAtMs: Date.now() + 60_000,
request: {
@@ -347,7 +339,8 @@ describe("createChannelApprovalHandlerFromCapability", () => {
turnSourceChannel: "test",
turnSourceTo: "origin-chat",
},
} as never;
createdAtMs: Date.now(),
};
await runtime?.handleRequested(request);
await runtime?.handleRequested({

View File

@@ -9,7 +9,10 @@ afterEach(() => {
});
function createGatewayRequestMock() {
return vi.fn(async <T = unknown>() => ({ ok: true }) as T);
return vi.fn(async (_method: string, _params: Record<string, unknown>) => ({
ok: true,
})) as unknown as (<T = unknown>(method: string, params: Record<string, unknown>) => Promise<T>) &
ReturnType<typeof vi.fn>;
}
describe("createApprovalNativeRouteReporter", () => {

View File

@@ -4,7 +4,11 @@ import type { PluginApprovalRequest, PluginApprovalResolved } from "./plugin-app
const mockGatewayClientStarts = vi.hoisted(() => vi.fn());
const mockGatewayClientStops = vi.hoisted(() => vi.fn());
const mockGatewayClientRequests = vi.hoisted(() => vi.fn(async () => ({ ok: true })));
const mockGatewayClientRequests = vi.hoisted(() =>
vi.fn<(method: string, params?: Record<string, unknown>) => Promise<unknown>>(async () => ({
ok: true,
})),
);
const mockCreateOperatorApprovalsGatewayClient = vi.hoisted(() => vi.fn());
const loggerMocks = vi.hoisted(() => ({
debug: vi.fn(),
@@ -401,7 +405,7 @@ describe("createExecApprovalChannelRuntime", () => {
});
it("replays pending approvals after the gateway connection is ready", async () => {
mockGatewayClientRequests.mockImplementation(async (method) => {
mockGatewayClientRequests.mockImplementation(async (method: string) => {
if (method === "exec.approval.list") {
return [
{
@@ -438,7 +442,7 @@ describe("createExecApprovalChannelRuntime", () => {
});
it("ignores live duplicate approval events after replay", async () => {
mockGatewayClientRequests.mockImplementation(async (method) => {
mockGatewayClientRequests.mockImplementation(async (method: string) => {
if (method === "plugin.approval.list") {
return [
{
@@ -500,7 +504,7 @@ describe("createExecApprovalChannelRuntime", () => {
expiresAtMs: number;
}>
>();
mockGatewayClientRequests.mockImplementation(async (method) => {
mockGatewayClientRequests.mockImplementation(async (method: string) => {
if (method === "exec.approval.list") {
return replayDeferred.promise;
}

View File

@@ -134,17 +134,22 @@ function buildTelegramExecApprovalPendingPayloadForTest(params: {
const telegramApprovalPlugin: Pick<
ChannelPlugin,
"id" | "meta" | "capabilities" | "config" | "approvals"
"id" | "meta" | "capabilities" | "config" | "approvalCapability"
> = {
...createChannelTestPluginBase({ id: "telegram" }),
approvals: {
approvalCapability: {
delivery: {
shouldSuppressForwardingFallback: (params) =>
shouldSuppressTelegramExecApprovalForwardingFallbackForTest(params),
shouldSuppressForwardingFallback: (params: {
cfg: OpenClawConfig;
target: { channel: string; accountId?: string | null };
request: {
request: { turnSourceChannel?: string | null; turnSourceAccountId?: string | null };
};
}) => shouldSuppressTelegramExecApprovalForwardingFallbackForTest(params),
},
render: {
exec: {
buildPendingPayload: ({ request }) =>
buildPendingPayload: ({ request }: { request: { id: string } }) =>
buildTelegramExecApprovalPendingPayloadForTest({ request }),
},
},
@@ -152,12 +157,18 @@ const telegramApprovalPlugin: Pick<
};
const discordApprovalPlugin: Pick<
ChannelPlugin,
"id" | "meta" | "capabilities" | "config" | "approvals"
"id" | "meta" | "capabilities" | "config" | "approvalCapability"
> = {
...createChannelTestPluginBase({ id: "discord" }),
approvals: {
approvalCapability: {
delivery: {
shouldSuppressForwardingFallback: ({ cfg, target }) =>
shouldSuppressForwardingFallback: ({
cfg,
target,
}: {
cfg: OpenClawConfig;
target: { channel: string; accountId?: string | null };
}) =>
target.channel === "discord" &&
isDiscordExecApprovalClientEnabledForTest({ cfg, accountId: target.accountId }),
},

View File

@@ -178,10 +178,10 @@ describe("plugin approval forwarding", () => {
const mockPayload = { text: "custom adapter payload" };
const adapterPlugin: Pick<
ChannelPlugin,
"id" | "meta" | "capabilities" | "config" | "approvals"
"id" | "meta" | "capabilities" | "config" | "approvalCapability"
> = {
...createChannelTestPluginBase({ id: "slack" as ChannelPlugin["id"] }),
approvals: {
approvalCapability: {
render: {
plugin: {
buildPendingPayload: vi.fn().mockReturnValue(mockPayload),
@@ -234,10 +234,10 @@ describe("plugin approval forwarding", () => {
const mockPayload = { text: "custom resolved payload" };
const adapterPlugin: Pick<
ChannelPlugin,
"id" | "meta" | "capabilities" | "config" | "approvals"
"id" | "meta" | "capabilities" | "config" | "approvalCapability"
> = {
...createChannelTestPluginBase({ id: "slack" as ChannelPlugin["id"] }),
approvals: {
approvalCapability: {
render: {
plugin: {
buildResolvedPayload: vi.fn().mockReturnValue(mockPayload),

View File

@@ -422,7 +422,11 @@ describe("createChannelApprovalCapability", () => {
deliverPending: vi.fn(),
},
};
const render = { buildPendingReplyPayload: vi.fn() };
const render = {
exec: {
buildPendingPayload: vi.fn(),
},
};
const native = { describeDeliveryCapabilities: vi.fn() };
expect(