-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathchannel-plugin.test.ts
More file actions
176 lines (155 loc) · 5.37 KB
/
Copy pathchannel-plugin.test.ts
File metadata and controls
176 lines (155 loc) · 5.37 KB
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
/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Channel Plugin Integration Test — Real E2E with WebSocket
*
* Tests the actual MockPluginChannel (from @qwen-code/channel-plugin-example) connected
* to an in-process mock server via WebSocket. The full message flow is:
*
* server.sendMessage("What is 2+2?")
* → WebSocket push to MockPluginChannel
* → ChannelBase.handleInbound(envelope)
* → SenderGate (open policy)
* → SessionRouter (creates/reuses session)
* → AcpBridge.prompt(sessionId, text)
* → qwen-code --acp (REAL model request)
* → MockPluginChannel.sendMessage(chatId, response)
* → WebSocket response to mock server
* → server resolves promise with agent text
*
* This exercises the real WebSocket protocol, real message serialization,
* real ChannelPlugin interface, and real model backend — all in one test process.
*/
import { describe, it, expect, afterAll } from 'vitest';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { mkdirSync } from 'node:fs';
// Import from the monorepo channel packages
import { AcpBridge, SessionRouter } from '@qwen-code/channel-base';
import {
MockPluginChannel,
createMockServer,
} from '../packages/channels/plugin-example/src/index.js';
import type {
MockServerHandle,
MockPluginConfig,
} from '../packages/channels/plugin-example/src/index.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const CLI_PATH = join(__dirname, '..', 'dist', 'cli.js');
const RESPONSE_TIMEOUT_MS = 120_000;
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe('Channel Plugin (Mock WebSocket E2E)', () => {
let bridge: InstanceType<typeof AcpBridge>;
let channel: MockPluginChannel;
let server: MockServerHandle;
let testDir: string;
const setup = async () => {
const baseDir =
process.env['INTEGRATION_TEST_FILE_DIR'] ||
join(__dirname, '..', '.integration-tests', `channel-${Date.now()}`);
testDir = join(baseDir, 'channel-plugin-example-e2e');
mkdirSync(testDir, { recursive: true });
// 1. Start mock server on random ports (no port conflicts)
server = await createMockServer({ httpPort: 0, wsPort: 0 });
// 2. Start AcpBridge (spawns real qwen-code --acp)
bridge = new AcpBridge({
cliEntryPath: CLI_PATH,
cwd: testDir,
});
await bridge.start();
// 3. Create and connect MockPluginChannel via WebSocket
// MockPluginConfig, not ChannelConfig: the constructor below requires
// `serverWsUrl`, and typing the literal as the base interface erased it.
const config: MockPluginConfig & Record<string, unknown> = {
type: 'plugin-example',
token: '',
senderPolicy: 'open',
allowedUsers: [],
sessionScope: 'user',
cwd: testDir,
groupPolicy: 'disabled',
dmPolicy: 'open',
groups: {},
serverWsUrl: server.wsUrl,
};
const router = new SessionRouter(bridge, testDir, 'user');
channel = new MockPluginChannel('test-mock', config, bridge, { router });
await channel.connect();
// 4. Wait for the channel's WebSocket to be registered by the server
await server.waitForConnection(5_000);
};
afterAll(async () => {
try {
channel?.disconnect();
} catch {
// ignore
}
try {
bridge?.stop();
} catch {
// ignore
}
try {
await server?.close();
} catch {
// ignore
}
});
it(
'should send a message through WebSocket and receive a real agent response',
async () => {
await setup();
// This goes: server → WS → MockPluginChannel → ChannelBase → AcpBridge → agent → back
const response = await server.sendMessage(
'What is 2+2? Reply with ONLY the number, nothing else.',
);
expect(response).toBeTruthy();
expect(response).toContain('4');
console.log(`[mock-e2e] Single turn response: "${response}"`);
},
RESPONSE_TIMEOUT_MS,
);
it(
'should maintain session state across multiple WebSocket messages',
async () => {
const chatId = 'ws-session-test';
const opts = { chatId };
const r1 = await server.sendMessage(
'My favorite fruit is "pineapple". Remember it.',
opts,
);
expect(r1).toBeTruthy();
console.log(`[mock-e2e] Memory set response: "${r1}"`);
const r2 = await server.sendMessage(
'What is my favorite fruit? Reply with ONLY the fruit, nothing else.',
opts,
);
expect(r2).toBeTruthy();
expect(r2.toLowerCase()).toContain('pineapple');
console.log(`[mock-e2e] Memory recall response: "${r2}"`);
},
RESPONSE_TIMEOUT_MS * 2,
);
it(
'should handle a different sender through the same WebSocket pipeline',
async () => {
const response = await server.sendMessage(
'What is 10 * 5? Reply with ONLY the number, nothing else.',
{
senderId: 'another-user',
senderName: 'Another User',
chatId: 'dm-another-user',
},
);
expect(response).toBeTruthy();
expect(response).toContain('50');
console.log(`[mock-e2e] Different sender response: "${response}"`);
},
RESPONSE_TIMEOUT_MS,
);
});