File size: 7,820 Bytes
5cb63c1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | // Binding routing tests cover channel binding selection and message routing behavior.
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
testing,
registerSessionBindingAdapter,
type SessionBindingAdapter,
type SessionBindingRecord,
} from "../../infra/outbound/session-binding-service.js";
import type { ResolvedAgentRoute } from "../../routing/resolve-route.js";
import {
ensureConfiguredBindingRouteReady,
resolveRuntimeConversationBindingRoute,
type RuntimeConversationBindingRouteResult,
} from "./binding-routing.js";
import { registerStatefulBindingTargetDriver } from "./stateful-target-drivers.js";
function createRoute(): ResolvedAgentRoute {
return {
agentId: "main",
channel: "demo",
accountId: "default",
sessionKey: "agent:main:main",
mainSessionKey: "agent:main:main",
lastRoutePolicy: "main",
matchedBy: "default",
};
}
function createBinding(overrides?: Partial<SessionBindingRecord>): SessionBindingRecord {
return {
bindingId: "binding-1",
targetSessionKey: "agent:review:acp:session-1",
targetKind: "session",
conversation: {
channel: "demo",
accountId: "default",
conversationId: "room-1",
},
status: "active",
boundAt: 1,
...overrides,
};
}
function registerAdapter(record: SessionBindingRecord | null): {
resolveByConversation: ReturnType<typeof vi.fn>;
touch: ReturnType<typeof vi.fn>;
} {
const resolveByConversation = vi.fn<SessionBindingAdapter["resolveByConversation"]>(() => record);
const touch = vi.fn<NonNullable<SessionBindingAdapter["touch"]>>();
registerSessionBindingAdapter({
channel: record?.conversation.channel ?? "demo",
accountId: record?.conversation.accountId ?? "default",
listBySession: () => [],
resolveByConversation,
touch,
});
return { resolveByConversation, touch };
}
describe("runtime conversation binding route", () => {
beforeEach(() => {
testing.resetSessionBindingAdaptersForTests();
});
it("keeps the stable runtime-route result structurally assignable", () => {
const result: RuntimeConversationBindingRouteResult = {
bindingRecord: null,
route: createRoute(),
};
expect(result.bindingOwnerAvailable).toBeUndefined();
});
it("rewrites the route and touches only the owning channel account's binding", () => {
const binding = createBinding();
const { resolveByConversation, touch } = registerAdapter(binding);
const siblingTouches = [
{ channel: "other", accountId: "default" },
{ channel: "demo", accountId: "other" },
].map(
(scope) =>
registerAdapter(createBinding({ conversation: { ...binding.conversation, ...scope } }))
.touch,
);
const result = resolveRuntimeConversationBindingRoute({
route: createRoute(),
conversation: {
channel: "demo",
accountId: "default",
conversationId: "room-1",
},
});
expect(resolveByConversation).toHaveBeenCalledWith({
channel: "demo",
accountId: "default",
conversationId: "room-1",
});
expect(touch).toHaveBeenCalledWith("binding-1", undefined);
for (const siblingTouch of siblingTouches) {
expect(siblingTouch).not.toHaveBeenCalled();
}
expect(result.boundSessionKey).toBe("agent:review:acp:session-1");
expect(result.boundAgentId).toBe("review");
expect(result.route).toEqual({
agentId: "review",
accountId: "default",
channel: "demo",
sessionKey: "agent:review:acp:session-1",
mainSessionKey: "agent:main:main",
lastRoutePolicy: "session",
matchedBy: "binding.channel",
});
});
it("touches plugin-owned bindings without rewriting the channel route", () => {
const route = createRoute();
const binding = createBinding({
metadata: {
pluginBindingOwner: "plugin",
pluginId: "demo-plugin",
pluginRoot: "/tmp/demo-plugin",
},
});
const { touch } = registerAdapter(binding);
const result = resolveRuntimeConversationBindingRoute({
route,
conversation: {
channel: "demo",
accountId: "default",
conversationId: "room-1",
},
});
expect(touch).toHaveBeenCalledWith("binding-1", undefined);
expect(result.bindingRecord).toBe(binding);
expect(result.boundSessionKey).toBeUndefined();
expect(result.route).toBe(route);
});
it.each([
{ targetSessionKey: "global", metadata: { agentId: "review" }, agentId: "review" },
{ targetSessionKey: "global", metadata: undefined, agentId: "main" },
{
targetSessionKey: "agent:review:session-1",
metadata: { agentId: "other" },
agentId: "review",
},
])("resolves $targetSessionKey to owner $agentId", ({ targetSessionKey, metadata, agentId }) => {
const binding = createBinding({ targetSessionKey, metadata });
registerAdapter(binding);
const result = resolveRuntimeConversationBindingRoute({
route: createRoute(),
conversation: binding.conversation,
});
expect(result.route).toMatchObject({ sessionKey: targetSessionKey, agentId });
expect(result.boundAgentId).toBe(agentId);
});
it("rejects an opaque target when its plugin ownership metadata is missing", () => {
const binding = createBinding({
targetSessionKey: "plugin-thread-1",
metadata: { agentId: "review" },
});
registerAdapter(binding);
expect(() =>
resolveRuntimeConversationBindingRoute({
route: createRoute(),
conversation: binding.conversation,
}),
).toThrow();
});
it("inspects a runtime-bound route without touching the binding", () => {
const { touch } = registerAdapter(createBinding());
const result = resolveRuntimeConversationBindingRoute({
route: createRoute(),
touchBinding: false,
conversation: {
channel: "demo",
accountId: "default",
conversationId: "room-1",
},
});
expect(touch).not.toHaveBeenCalled();
expect(result.bindingOwnerAvailable).toBe(true);
expect(result.boundSessionKey).toBe("agent:review:acp:session-1");
});
it("ignores runtime bindings that target isolated cron run sessions", () => {
const route = createRoute();
const binding = createBinding({
targetSessionKey: "agent:youtube:cron:monthly-report:run:closed-run-1",
});
const { touch } = registerAdapter(binding);
const result = resolveRuntimeConversationBindingRoute({
route,
conversation: {
channel: "demo",
accountId: "default",
conversationId: "room-1",
},
});
expect(touch).not.toHaveBeenCalled();
expect(result.bindingRecord).toBeNull();
expect(result.boundSessionKey).toBeUndefined();
expect(result.route).toBe(route);
});
});
describe("ensureConfiguredBindingRouteReady", () => {
let unregisterDriver: (() => void) | undefined;
afterEach(() => {
vi.useRealTimers();
unregisterDriver?.();
});
it("returns a bounded failure when target readiness never settles", async () => {
vi.useFakeTimers();
unregisterDriver = registerStatefulBindingTargetDriver({
id: "slow",
ensureReady: async () => await new Promise<never>(() => {}),
ensureSession: async () => ({
ok: false,
sessionKey: "agent:slow:binding",
error: "not used",
}),
});
const resultPromise = ensureConfiguredBindingRouteReady({
cfg: {} as never,
bindingResolution: { statefulTarget: { driverId: "slow" } } as never,
});
await vi.advanceTimersByTimeAsync(30_000);
await expect(resultPromise).resolves.toEqual({
ok: false,
error: "Configured binding route ready check timed out",
});
});
});
|