mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 09:01:22 +00:00
test(core): increase coverage for sessions, auth choice, and model listing
This commit is contained in:
@@ -1,6 +1,46 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { createBoundDeliveryRouter } from "./bound-delivery-router.js";
|
||||
import { __testing, registerSessionBindingAdapter } from "./session-binding-service.js";
|
||||
import {
|
||||
__testing,
|
||||
registerSessionBindingAdapter,
|
||||
type SessionBindingRecord,
|
||||
} from "./session-binding-service.js";
|
||||
|
||||
const TARGET_SESSION_KEY = "agent:main:subagent:child";
|
||||
|
||||
function createDiscordBinding(
|
||||
targetSessionKey: string,
|
||||
conversationId: string,
|
||||
boundAt: number,
|
||||
parentConversationId?: string,
|
||||
): SessionBindingRecord {
|
||||
return {
|
||||
bindingId: `runtime:${conversationId}`,
|
||||
targetSessionKey,
|
||||
targetKind: "subagent",
|
||||
conversation: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
conversationId,
|
||||
parentConversationId,
|
||||
},
|
||||
status: "active",
|
||||
boundAt,
|
||||
};
|
||||
}
|
||||
|
||||
function registerDiscordSessionBindings(
|
||||
targetSessionKey: string,
|
||||
bindings: SessionBindingRecord[],
|
||||
): void {
|
||||
registerSessionBindingAdapter({
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
listBySession: (requestedSessionKey) =>
|
||||
requestedSessionKey === targetSessionKey ? bindings : [],
|
||||
resolveByConversation: () => null,
|
||||
});
|
||||
}
|
||||
|
||||
describe("bound delivery router", () => {
|
||||
beforeEach(() => {
|
||||
@@ -8,33 +48,13 @@ describe("bound delivery router", () => {
|
||||
});
|
||||
|
||||
it("resolves to a bound destination when a single active binding exists", () => {
|
||||
registerSessionBindingAdapter({
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
listBySession: (targetSessionKey) =>
|
||||
targetSessionKey === "agent:main:subagent:child"
|
||||
? [
|
||||
{
|
||||
bindingId: "runtime:thread-1",
|
||||
targetSessionKey,
|
||||
targetKind: "subagent",
|
||||
conversation: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
conversationId: "thread-1",
|
||||
parentConversationId: "parent-1",
|
||||
},
|
||||
status: "active",
|
||||
boundAt: 1,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
resolveByConversation: () => null,
|
||||
});
|
||||
registerDiscordSessionBindings(TARGET_SESSION_KEY, [
|
||||
createDiscordBinding(TARGET_SESSION_KEY, "thread-1", 1, "parent-1"),
|
||||
]);
|
||||
|
||||
const route = createBoundDeliveryRouter().resolveDestination({
|
||||
eventKind: "task_completion",
|
||||
targetSessionKey: "agent:main:subagent:child",
|
||||
targetSessionKey: TARGET_SESSION_KEY,
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
@@ -67,44 +87,14 @@ describe("bound delivery router", () => {
|
||||
});
|
||||
|
||||
it("fails closed when multiple bindings exist without requester signal", () => {
|
||||
registerSessionBindingAdapter({
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
listBySession: (targetSessionKey) =>
|
||||
targetSessionKey === "agent:main:subagent:child"
|
||||
? [
|
||||
{
|
||||
bindingId: "runtime:thread-1",
|
||||
targetSessionKey,
|
||||
targetKind: "subagent",
|
||||
conversation: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
conversationId: "thread-1",
|
||||
},
|
||||
status: "active",
|
||||
boundAt: 1,
|
||||
},
|
||||
{
|
||||
bindingId: "runtime:thread-2",
|
||||
targetSessionKey,
|
||||
targetKind: "subagent",
|
||||
conversation: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
conversationId: "thread-2",
|
||||
},
|
||||
status: "active",
|
||||
boundAt: 2,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
resolveByConversation: () => null,
|
||||
});
|
||||
registerDiscordSessionBindings(TARGET_SESSION_KEY, [
|
||||
createDiscordBinding(TARGET_SESSION_KEY, "thread-1", 1),
|
||||
createDiscordBinding(TARGET_SESSION_KEY, "thread-2", 2),
|
||||
]);
|
||||
|
||||
const route = createBoundDeliveryRouter().resolveDestination({
|
||||
eventKind: "task_completion",
|
||||
targetSessionKey: "agent:main:subagent:child",
|
||||
targetSessionKey: TARGET_SESSION_KEY,
|
||||
failClosed: true,
|
||||
});
|
||||
|
||||
@@ -114,4 +104,49 @@ describe("bound delivery router", () => {
|
||||
reason: "ambiguous-without-requester",
|
||||
});
|
||||
});
|
||||
|
||||
it("selects requester-matching conversation when multiple bindings exist", () => {
|
||||
registerDiscordSessionBindings(TARGET_SESSION_KEY, [
|
||||
createDiscordBinding(TARGET_SESSION_KEY, "thread-1", 1),
|
||||
createDiscordBinding(TARGET_SESSION_KEY, "thread-2", 2),
|
||||
]);
|
||||
|
||||
const route = createBoundDeliveryRouter().resolveDestination({
|
||||
eventKind: "task_completion",
|
||||
targetSessionKey: TARGET_SESSION_KEY,
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
conversationId: "thread-2",
|
||||
},
|
||||
failClosed: true,
|
||||
});
|
||||
|
||||
expect(route.mode).toBe("bound");
|
||||
expect(route.reason).toBe("requester-match");
|
||||
expect(route.binding?.conversation.conversationId).toBe("thread-2");
|
||||
});
|
||||
|
||||
it("falls back for invalid requester conversation values", () => {
|
||||
registerDiscordSessionBindings(TARGET_SESSION_KEY, [
|
||||
createDiscordBinding(TARGET_SESSION_KEY, "thread-1", 1),
|
||||
]);
|
||||
|
||||
const route = createBoundDeliveryRouter().resolveDestination({
|
||||
eventKind: "task_completion",
|
||||
targetSessionKey: TARGET_SESSION_KEY,
|
||||
requester: {
|
||||
channel: "discord",
|
||||
accountId: "runtime",
|
||||
conversationId: " ",
|
||||
},
|
||||
failClosed: true,
|
||||
});
|
||||
|
||||
expect(route).toEqual({
|
||||
binding: null,
|
||||
mode: "fallback",
|
||||
reason: "invalid-requester",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user