|
| 1 | +import { |
| 2 | + registerSessionBindingAdapter, |
| 3 | + type SessionBindingAdapter, |
| 4 | + testing as sessionBindingTesting, |
| 5 | + unregisterSessionBindingAdapter, |
| 6 | +} from "openclaw/plugin-sdk/conversation-runtime"; |
| 7 | +import { |
| 8 | + createTestRegistry, |
| 9 | + resetPluginRuntimeStateForTest, |
| 10 | + setActivePluginRegistry, |
| 11 | +} from "openclaw/plugin-sdk/plugin-test-runtime"; |
| 12 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 13 | +import { inspectDiscordConversationRouteOwner } from "./conversation-route-owner.js"; |
| 14 | + |
| 15 | +describe("inspectDiscordConversationRouteOwner", () => { |
| 16 | + let adapter: SessionBindingAdapter; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + resetPluginRuntimeStateForTest(); |
| 20 | + setActivePluginRegistry( |
| 21 | + createTestRegistry([ |
| 22 | + { |
| 23 | + pluginId: "discord", |
| 24 | + source: "test", |
| 25 | + plugin: { |
| 26 | + id: "discord", |
| 27 | + meta: { aliases: [] }, |
| 28 | + conversationBindings: { |
| 29 | + supportsCurrentConversationBinding: true, |
| 30 | + createManager: () => ({ stop: () => undefined }), |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + ]), |
| 35 | + ); |
| 36 | + sessionBindingTesting.resetSessionBindingAdaptersForTests(); |
| 37 | + adapter = { |
| 38 | + channel: "discord", |
| 39 | + accountId: "default", |
| 40 | + listBySession: () => [], |
| 41 | + resolveByConversation: () => null, |
| 42 | + }; |
| 43 | + registerSessionBindingAdapter(adapter); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + resetPluginRuntimeStateForTest(); |
| 48 | + sessionBindingTesting.resetSessionBindingAdaptersForTests(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("uses the direct-user runtime identity without touching liveness", () => { |
| 52 | + const touch = vi.fn(); |
| 53 | + const resolveByConversation = vi.fn((conversation) => ({ |
| 54 | + bindingId: "binding-direct", |
| 55 | + targetSessionKey: "agent:finance:bound", |
| 56 | + targetKind: "session" as const, |
| 57 | + conversation, |
| 58 | + status: "active" as const, |
| 59 | + boundAt: 1, |
| 60 | + })); |
| 61 | + registerSessionBindingAdapter({ |
| 62 | + channel: "discord", |
| 63 | + accountId: "default", |
| 64 | + listBySession: () => [], |
| 65 | + resolveByConversation, |
| 66 | + touch, |
| 67 | + }); |
| 68 | + |
| 69 | + expect( |
| 70 | + inspectDiscordConversationRouteOwner({ |
| 71 | + cfg: {}, |
| 72 | + accountId: "default", |
| 73 | + conversation: { kind: "direct", peerId: "user-1", nativeChannelId: "dm-1" }, |
| 74 | + }), |
| 75 | + ).toEqual({ kind: "agent", agentId: "finance" }); |
| 76 | + expect(resolveByConversation).toHaveBeenCalledWith( |
| 77 | + expect.objectContaining({ conversationId: "user:user-1" }), |
| 78 | + ); |
| 79 | + expect(touch).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it.each([ |
| 83 | + { kind: "group" as const, peerId: "group-dm-1" }, |
| 84 | + { kind: "channel" as const, peerId: "channel-1" }, |
| 85 | + ])("uses the native channel runtime identity for $kind conversations", ({ kind, peerId }) => { |
| 86 | + const resolveByConversation = vi.fn(() => null); |
| 87 | + registerSessionBindingAdapter({ |
| 88 | + channel: "discord", |
| 89 | + accountId: "default", |
| 90 | + listBySession: () => [], |
| 91 | + resolveByConversation, |
| 92 | + }); |
| 93 | + |
| 94 | + inspectDiscordConversationRouteOwner({ |
| 95 | + cfg: {}, |
| 96 | + accountId: "default", |
| 97 | + conversation: { kind, peerId, nativeChannelId: peerId }, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(resolveByConversation).toHaveBeenCalledWith( |
| 101 | + expect.objectContaining({ conversationId: peerId }), |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it("reports temporary adapter unavailability only while bindings are enabled", () => { |
| 106 | + unregisterSessionBindingAdapter({ channel: "discord", accountId: "default", adapter }); |
| 107 | + const conversation = { kind: "channel" as const, peerId: "channel-1" }; |
| 108 | + |
| 109 | + expect( |
| 110 | + inspectDiscordConversationRouteOwner({ cfg: {}, accountId: "default", conversation }), |
| 111 | + ).toEqual({ kind: "unavailable" }); |
| 112 | + expect( |
| 113 | + inspectDiscordConversationRouteOwner({ |
| 114 | + cfg: { channels: { discord: { threadBindings: { enabled: false } } } }, |
| 115 | + accountId: "default", |
| 116 | + conversation, |
| 117 | + }), |
| 118 | + ).toEqual({ kind: "agent", agentId: "main" }); |
| 119 | + }); |
| 120 | + |
| 121 | + it("preserves explicit plugin ownership independently of the target session key", () => { |
| 122 | + registerSessionBindingAdapter({ |
| 123 | + channel: "discord", |
| 124 | + accountId: "default", |
| 125 | + listBySession: () => [], |
| 126 | + resolveByConversation: (conversation) => ({ |
| 127 | + bindingId: "binding-plugin", |
| 128 | + targetSessionKey: "agent:review:looks-owned", |
| 129 | + targetKind: "session", |
| 130 | + conversation, |
| 131 | + status: "active", |
| 132 | + boundAt: 1, |
| 133 | + metadata: { |
| 134 | + pluginBindingOwner: "plugin", |
| 135 | + pluginId: "review-plugin", |
| 136 | + pluginRoot: "/plugins/review", |
| 137 | + }, |
| 138 | + }), |
| 139 | + }); |
| 140 | + |
| 141 | + expect( |
| 142 | + inspectDiscordConversationRouteOwner({ |
| 143 | + cfg: {}, |
| 144 | + accountId: "default", |
| 145 | + conversation: { kind: "channel", peerId: "channel-1" }, |
| 146 | + }), |
| 147 | + ).toEqual({ kind: "plugin", pluginId: "review-plugin", fallbackAgentId: "main" }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments