@lobu/gateway 3.0.5 → 3.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/__tests__/agent-config-routes.test.ts +254 -0
- package/src/__tests__/agent-history-routes.test.ts +72 -0
- package/src/__tests__/agent-routes.test.ts +68 -0
- package/src/__tests__/agent-schedules-routes.test.ts +59 -0
- package/src/__tests__/agent-settings-store.test.ts +323 -0
- package/src/__tests__/chat-instance-manager-slack.test.ts +204 -0
- package/src/__tests__/chat-response-bridge.test.ts +131 -0
- package/src/__tests__/config-memory-plugins.test.ts +92 -0
- package/src/__tests__/config-request-store.test.ts +127 -0
- package/src/__tests__/connection-routes.test.ts +144 -0
- package/src/__tests__/core-services-store-selection.test.ts +92 -0
- package/src/__tests__/docker-deployment.test.ts +1211 -0
- package/src/__tests__/embedded-deployment.test.ts +342 -0
- package/src/__tests__/grant-store.test.ts +148 -0
- package/src/__tests__/http-proxy.test.ts +281 -0
- package/src/__tests__/instruction-service.test.ts +37 -0
- package/src/__tests__/link-buttons.test.ts +112 -0
- package/src/__tests__/lobu.test.ts +32 -0
- package/src/__tests__/mcp-config-service.test.ts +347 -0
- package/src/__tests__/mcp-proxy.test.ts +696 -0
- package/src/__tests__/message-handler-bridge.test.ts +17 -0
- package/src/__tests__/model-selection.test.ts +172 -0
- package/src/__tests__/oauth-templates.test.ts +39 -0
- package/src/__tests__/platform-adapter-slack-send.test.ts +114 -0
- package/src/__tests__/platform-helpers-model-resolution.test.ts +253 -0
- package/src/__tests__/provider-inheritance.test.ts +212 -0
- package/src/__tests__/routes/cli-auth.test.ts +337 -0
- package/src/__tests__/routes/interactions.test.ts +121 -0
- package/src/__tests__/secret-proxy.test.ts +85 -0
- package/src/__tests__/session-manager.test.ts +572 -0
- package/src/__tests__/setup.ts +133 -0
- package/src/__tests__/skill-and-mcp-registry.test.ts +203 -0
- package/src/__tests__/slack-routes.test.ts +161 -0
- package/src/__tests__/system-config-resolver.test.ts +75 -0
- package/src/__tests__/system-message-limiter.test.ts +89 -0
- package/src/__tests__/system-skills-service.test.ts +362 -0
- package/src/__tests__/transcription-service.test.ts +222 -0
- package/src/__tests__/utils/rate-limiter.test.ts +102 -0
- package/src/__tests__/worker-connection-manager.test.ts +497 -0
- package/src/__tests__/worker-job-router.test.ts +722 -0
- package/src/api/index.ts +1 -0
- package/src/api/platform.ts +292 -0
- package/src/api/response-renderer.ts +157 -0
- package/src/auth/agent-metadata-store.ts +168 -0
- package/src/auth/api-auth-middleware.ts +69 -0
- package/src/auth/api-key-provider-module.ts +213 -0
- package/src/auth/base-provider-module.ts +201 -0
- package/src/auth/chatgpt/chatgpt-oauth-module.ts +185 -0
- package/src/auth/chatgpt/device-code-client.ts +218 -0
- package/src/auth/chatgpt/index.ts +1 -0
- package/src/auth/claude/oauth-module.ts +280 -0
- package/src/auth/cli/token-service.ts +249 -0
- package/src/auth/external/client.ts +560 -0
- package/src/auth/external/device-code-client.ts +225 -0
- package/src/auth/mcp/config-service.ts +392 -0
- package/src/auth/mcp/proxy.ts +1088 -0
- package/src/auth/mcp/string-substitution.ts +17 -0
- package/src/auth/mcp/tool-cache.ts +90 -0
- package/src/auth/oauth/base-client.ts +267 -0
- package/src/auth/oauth/client.ts +153 -0
- package/src/auth/oauth/credentials.ts +7 -0
- package/src/auth/oauth/providers.ts +69 -0
- package/src/auth/oauth/state-store.ts +150 -0
- package/src/auth/oauth-templates.ts +179 -0
- package/src/auth/provider-catalog.ts +220 -0
- package/src/auth/provider-model-options.ts +41 -0
- package/src/auth/settings/agent-settings-store.ts +565 -0
- package/src/auth/settings/auth-profiles-manager.ts +216 -0
- package/src/auth/settings/index.ts +12 -0
- package/src/auth/settings/model-preference-store.ts +52 -0
- package/src/auth/settings/model-selection.ts +135 -0
- package/src/auth/settings/resolved-settings-view.ts +298 -0
- package/src/auth/settings/template-utils.ts +44 -0
- package/src/auth/settings/token-service.ts +88 -0
- package/src/auth/system-env-store.ts +98 -0
- package/src/auth/user-agents-store.ts +68 -0
- package/src/channels/binding-service.ts +214 -0
- package/src/channels/index.ts +4 -0
- package/src/cli/gateway.ts +1304 -0
- package/src/cli/index.ts +74 -0
- package/src/commands/built-in-commands.ts +80 -0
- package/src/commands/command-dispatcher.ts +94 -0
- package/src/commands/command-reply-adapters.ts +27 -0
- package/src/config/file-loader.ts +618 -0
- package/src/config/index.ts +588 -0
- package/src/config/network-allowlist.ts +71 -0
- package/src/connections/chat-instance-manager.ts +1284 -0
- package/src/connections/chat-response-bridge.ts +618 -0
- package/src/connections/index.ts +7 -0
- package/src/connections/interaction-bridge.ts +831 -0
- package/src/connections/message-handler-bridge.ts +415 -0
- package/src/connections/platform-auth-methods.ts +15 -0
- package/src/connections/types.ts +84 -0
- package/src/gateway/connection-manager.ts +291 -0
- package/src/gateway/index.ts +700 -0
- package/src/gateway/job-router.ts +201 -0
- package/src/gateway-main.ts +200 -0
- package/src/index.ts +41 -0
- package/src/infrastructure/queue/index.ts +12 -0
- package/src/infrastructure/queue/queue-producer.ts +148 -0
- package/src/infrastructure/queue/redis-queue.ts +361 -0
- package/src/infrastructure/queue/types.ts +133 -0
- package/src/infrastructure/redis/system-message-limiter.ts +94 -0
- package/src/interactions/config-request-store.ts +198 -0
- package/src/interactions.ts +363 -0
- package/src/lobu.ts +311 -0
- package/src/metrics/prometheus.ts +159 -0
- package/src/modules/module-system.ts +179 -0
- package/src/orchestration/base-deployment-manager.ts +900 -0
- package/src/orchestration/deployment-utils.ts +98 -0
- package/src/orchestration/impl/docker-deployment.ts +620 -0
- package/src/orchestration/impl/embedded-deployment.ts +268 -0
- package/src/orchestration/impl/index.ts +8 -0
- package/src/orchestration/impl/k8s/deployment.ts +1061 -0
- package/src/orchestration/impl/k8s/helpers.ts +610 -0
- package/src/orchestration/impl/k8s/index.ts +1 -0
- package/src/orchestration/index.ts +333 -0
- package/src/orchestration/message-consumer.ts +584 -0
- package/src/orchestration/scheduled-wakeup.ts +704 -0
- package/src/permissions/approval-policy.ts +36 -0
- package/src/permissions/grant-store.ts +219 -0
- package/src/platform/file-handler.ts +66 -0
- package/src/platform/link-buttons.ts +57 -0
- package/src/platform/renderer-utils.ts +44 -0
- package/src/platform/response-renderer.ts +84 -0
- package/src/platform/unified-thread-consumer.ts +187 -0
- package/src/platform.ts +318 -0
- package/src/proxy/http-proxy.ts +752 -0
- package/src/proxy/proxy-manager.ts +81 -0
- package/src/proxy/secret-proxy.ts +402 -0
- package/src/proxy/token-refresh-job.ts +143 -0
- package/src/routes/internal/audio.ts +141 -0
- package/src/routes/internal/device-auth.ts +566 -0
- package/src/routes/internal/files.ts +226 -0
- package/src/routes/internal/history.ts +69 -0
- package/src/routes/internal/images.ts +127 -0
- package/src/routes/internal/interactions.ts +84 -0
- package/src/routes/internal/middleware.ts +23 -0
- package/src/routes/internal/schedule.ts +226 -0
- package/src/routes/internal/types.ts +22 -0
- package/src/routes/openapi-auto.ts +239 -0
- package/src/routes/public/agent-access.ts +23 -0
- package/src/routes/public/agent-config.ts +675 -0
- package/src/routes/public/agent-history.ts +422 -0
- package/src/routes/public/agent-schedules.ts +296 -0
- package/src/routes/public/agent.ts +1086 -0
- package/src/routes/public/agents.ts +373 -0
- package/src/routes/public/channels.ts +191 -0
- package/src/routes/public/cli-auth.ts +883 -0
- package/src/routes/public/connections.ts +574 -0
- package/src/routes/public/landing.ts +16 -0
- package/src/routes/public/oauth.ts +147 -0
- package/src/routes/public/settings-auth.ts +104 -0
- package/src/routes/public/slack.ts +173 -0
- package/src/routes/shared/agent-ownership.ts +101 -0
- package/src/routes/shared/token-verifier.ts +34 -0
- package/src/services/core-services.ts +1053 -0
- package/src/services/image-generation-service.ts +257 -0
- package/src/services/instruction-service.ts +318 -0
- package/src/services/mcp-registry.ts +94 -0
- package/src/services/platform-helpers.ts +287 -0
- package/src/services/session-manager.ts +262 -0
- package/src/services/settings-resolver.ts +74 -0
- package/src/services/system-config-resolver.ts +90 -0
- package/src/services/system-skills-service.ts +229 -0
- package/src/services/transcription-service.ts +684 -0
- package/src/session.ts +110 -0
- package/src/spaces/index.ts +1 -0
- package/src/spaces/space-resolver.ts +17 -0
- package/src/stores/in-memory-agent-store.ts +403 -0
- package/src/stores/redis-agent-store.ts +279 -0
- package/src/utils/public-url.ts +44 -0
- package/src/utils/rate-limiter.ts +94 -0
- package/tsconfig.json +33 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for WorkerConnectionManager
|
|
3
|
+
* Tests SSE connection lifecycle, heartbeats, and cleanup
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
7
|
+
import { WorkerConnectionManager } from "../gateway/connection-manager";
|
|
8
|
+
import {
|
|
9
|
+
cleanupTestEnv,
|
|
10
|
+
MockResponse,
|
|
11
|
+
setupTestEnv,
|
|
12
|
+
TestHelpers,
|
|
13
|
+
} from "./setup";
|
|
14
|
+
|
|
15
|
+
describe("WorkerConnectionManager", () => {
|
|
16
|
+
let manager: WorkerConnectionManager;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
setupTestEnv();
|
|
20
|
+
manager = new WorkerConnectionManager();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
manager.shutdown();
|
|
25
|
+
cleanupTestEnv();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("Connection Lifecycle", () => {
|
|
29
|
+
test("adds new connection and sends initial event", () => {
|
|
30
|
+
const res = new MockResponse() as any;
|
|
31
|
+
|
|
32
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
33
|
+
|
|
34
|
+
expect(manager.isConnected("worker-1")).toBe(true);
|
|
35
|
+
expect(res.getWritten().length).toBeGreaterThan(0);
|
|
36
|
+
|
|
37
|
+
// Parse SSE output
|
|
38
|
+
const events = TestHelpers.parseSSE(res.getAllWrites());
|
|
39
|
+
expect(events).toHaveLength(1);
|
|
40
|
+
expect(events[0].event).toBe("connected");
|
|
41
|
+
expect(events[0].data).toEqual({
|
|
42
|
+
deploymentName: "worker-1",
|
|
43
|
+
userId: "U123",
|
|
44
|
+
conversationId: "thread-1",
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("removes connection and ends response", () => {
|
|
49
|
+
const res = new MockResponse() as any;
|
|
50
|
+
|
|
51
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
52
|
+
expect(manager.isConnected("worker-1")).toBe(true);
|
|
53
|
+
|
|
54
|
+
manager.removeConnection("worker-1");
|
|
55
|
+
|
|
56
|
+
expect(manager.isConnected("worker-1")).toBe(false);
|
|
57
|
+
expect(res.isEnded()).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("ignores stale disconnect callbacks from replaced SSE writers", () => {
|
|
61
|
+
const res1 = new MockResponse() as any;
|
|
62
|
+
const res2 = new MockResponse() as any;
|
|
63
|
+
|
|
64
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res1);
|
|
65
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res2);
|
|
66
|
+
|
|
67
|
+
manager.removeConnection("worker-1", res1);
|
|
68
|
+
|
|
69
|
+
expect(manager.isConnected("worker-1")).toBe(true);
|
|
70
|
+
expect(manager.getConnection("worker-1")?.writer).toBe(res2);
|
|
71
|
+
|
|
72
|
+
manager.removeConnection("worker-1", res2);
|
|
73
|
+
|
|
74
|
+
expect(manager.isConnected("worker-1")).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("handles removing non-existent connection gracefully", () => {
|
|
78
|
+
expect(() => manager.removeConnection("non-existent")).not.toThrow();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("gets connection by deployment name", () => {
|
|
82
|
+
const res = new MockResponse() as any;
|
|
83
|
+
|
|
84
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
85
|
+
|
|
86
|
+
const connection = manager.getConnection("worker-1");
|
|
87
|
+
expect(connection).toBeDefined();
|
|
88
|
+
expect(connection?.deploymentName).toBe("worker-1");
|
|
89
|
+
expect(connection?.userId).toBe("U123");
|
|
90
|
+
expect(connection?.conversationId).toBe("thread-1");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("returns undefined for non-existent connection", () => {
|
|
94
|
+
const connection = manager.getConnection("non-existent");
|
|
95
|
+
expect(connection).toBeUndefined();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("tracks multiple connections simultaneously", () => {
|
|
99
|
+
const res1 = new MockResponse() as any;
|
|
100
|
+
const res2 = new MockResponse() as any;
|
|
101
|
+
const res3 = new MockResponse() as any;
|
|
102
|
+
|
|
103
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res1);
|
|
104
|
+
manager.addConnection("worker-2", "U456", "thread-2", "agent-2", res2);
|
|
105
|
+
manager.addConnection("worker-3", "U789", "thread-3", "agent-3", res3);
|
|
106
|
+
|
|
107
|
+
expect(manager.isConnected("worker-1")).toBe(true);
|
|
108
|
+
expect(manager.isConnected("worker-2")).toBe(true);
|
|
109
|
+
expect(manager.isConnected("worker-3")).toBe(true);
|
|
110
|
+
|
|
111
|
+
const activeConnections = manager.getActiveConnections();
|
|
112
|
+
expect(activeConnections).toHaveLength(3);
|
|
113
|
+
expect(activeConnections).toContain("worker-1");
|
|
114
|
+
expect(activeConnections).toContain("worker-2");
|
|
115
|
+
expect(activeConnections).toContain("worker-3");
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("Connection Activity", () => {
|
|
120
|
+
test("updates connection activity timestamp", async () => {
|
|
121
|
+
const res = new MockResponse() as any;
|
|
122
|
+
|
|
123
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
124
|
+
|
|
125
|
+
const conn1 = manager.getConnection("worker-1");
|
|
126
|
+
const initialActivity = conn1?.lastActivity;
|
|
127
|
+
|
|
128
|
+
// Wait a bit and touch
|
|
129
|
+
await TestHelpers.delay(10);
|
|
130
|
+
manager.touchConnection("worker-1");
|
|
131
|
+
|
|
132
|
+
const conn2 = manager.getConnection("worker-1");
|
|
133
|
+
expect(conn2?.lastActivity).toBeGreaterThan(initialActivity!);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("handles touching non-existent connection gracefully", () => {
|
|
137
|
+
expect(() => manager.touchConnection("non-existent")).not.toThrow();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("SSE Event Sending", () => {
|
|
142
|
+
test("sends SSE events with correct format", () => {
|
|
143
|
+
const res = new MockResponse() as any;
|
|
144
|
+
|
|
145
|
+
manager.sendSSE(res, "test-event", { key: "value" });
|
|
146
|
+
|
|
147
|
+
const writes = res.getAllWrites();
|
|
148
|
+
expect(writes).toContain("event: test-event\n");
|
|
149
|
+
expect(writes).toContain('data: {"key":"value"}\n\n');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("handles SSE send errors gracefully", () => {
|
|
153
|
+
const badRes = {
|
|
154
|
+
write: () => {
|
|
155
|
+
throw new Error("Connection closed");
|
|
156
|
+
},
|
|
157
|
+
} as any;
|
|
158
|
+
|
|
159
|
+
// Should not throw
|
|
160
|
+
expect(() =>
|
|
161
|
+
manager.sendSSE(badRes, "test", { data: "test" })
|
|
162
|
+
).not.toThrow();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("sends complex data objects correctly", () => {
|
|
166
|
+
const res = new MockResponse() as any;
|
|
167
|
+
|
|
168
|
+
const complexData = {
|
|
169
|
+
job: {
|
|
170
|
+
id: "job-123",
|
|
171
|
+
config: {
|
|
172
|
+
model: "claude-3-5-sonnet",
|
|
173
|
+
nested: { deep: true },
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
timestamp: Date.now(),
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
manager.sendSSE(res, "job", complexData);
|
|
180
|
+
|
|
181
|
+
const events = TestHelpers.parseSSE(res.getAllWrites());
|
|
182
|
+
expect(events).toHaveLength(1);
|
|
183
|
+
expect(events[0].data).toEqual(complexData);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("Heartbeat Mechanism", () => {
|
|
188
|
+
test("sends heartbeat pings to all connected workers", async () => {
|
|
189
|
+
const res1 = new MockResponse() as any;
|
|
190
|
+
const res2 = new MockResponse() as any;
|
|
191
|
+
|
|
192
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res1);
|
|
193
|
+
manager.addConnection("worker-2", "U456", "thread-2", "agent-2", res2);
|
|
194
|
+
|
|
195
|
+
// Clear initial connection events
|
|
196
|
+
res1.clearWrites();
|
|
197
|
+
res2.clearWrites();
|
|
198
|
+
|
|
199
|
+
// Wait for heartbeat (30s interval, but we can manually trigger for testing)
|
|
200
|
+
// Instead, we'll test that heartbeats are sent by accessing the private method
|
|
201
|
+
// This is a workaround since we can't wait 30s in tests
|
|
202
|
+
(manager as any).sendHeartbeats();
|
|
203
|
+
|
|
204
|
+
// Both workers should receive ping events
|
|
205
|
+
const events1 = TestHelpers.parseSSE(res1.getAllWrites());
|
|
206
|
+
const events2 = TestHelpers.parseSSE(res2.getAllWrites());
|
|
207
|
+
|
|
208
|
+
expect(events1.length).toBeGreaterThan(0);
|
|
209
|
+
expect(events2.length).toBeGreaterThan(0);
|
|
210
|
+
|
|
211
|
+
expect(events1[0].event).toBe("ping");
|
|
212
|
+
expect(events2[0].event).toBe("ping");
|
|
213
|
+
|
|
214
|
+
expect(events1[0].data).toHaveProperty("timestamp");
|
|
215
|
+
expect(events2[0].data).toHaveProperty("timestamp");
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("updates lastPing timestamp after sending heartbeat", () => {
|
|
219
|
+
const res = new MockResponse() as any;
|
|
220
|
+
|
|
221
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
222
|
+
|
|
223
|
+
const conn1 = manager.getConnection("worker-1");
|
|
224
|
+
const initialPing = conn1?.lastPing;
|
|
225
|
+
|
|
226
|
+
// Send heartbeat
|
|
227
|
+
(manager as any).sendHeartbeats();
|
|
228
|
+
|
|
229
|
+
const conn2 = manager.getConnection("worker-1");
|
|
230
|
+
expect(conn2?.lastPing).toBeGreaterThanOrEqual(initialPing!);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("continues heartbeat even if individual send fails", () => {
|
|
234
|
+
const goodRes = new MockResponse() as any;
|
|
235
|
+
const badRes = {
|
|
236
|
+
write: () => {
|
|
237
|
+
throw new Error("Connection failed");
|
|
238
|
+
},
|
|
239
|
+
end: () => {
|
|
240
|
+
// noop
|
|
241
|
+
},
|
|
242
|
+
onClose: () => {
|
|
243
|
+
// noop
|
|
244
|
+
},
|
|
245
|
+
} as any;
|
|
246
|
+
|
|
247
|
+
manager.addConnection(
|
|
248
|
+
"worker-good",
|
|
249
|
+
"U123",
|
|
250
|
+
"thread-1",
|
|
251
|
+
"agent-1",
|
|
252
|
+
goodRes
|
|
253
|
+
);
|
|
254
|
+
manager.addConnection(
|
|
255
|
+
"worker-bad",
|
|
256
|
+
"U456",
|
|
257
|
+
"thread-2",
|
|
258
|
+
"agent-2",
|
|
259
|
+
badRes
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
goodRes.clearWrites();
|
|
263
|
+
|
|
264
|
+
// Should not throw even if one connection fails
|
|
265
|
+
expect(() => (manager as any).sendHeartbeats()).not.toThrow();
|
|
266
|
+
|
|
267
|
+
// Good connection should still receive ping
|
|
268
|
+
const events = TestHelpers.parseSSE(goodRes.getAllWrites());
|
|
269
|
+
expect(events.length).toBeGreaterThan(0);
|
|
270
|
+
expect(events[0].event).toBe("ping");
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
describe("Stale Connection Cleanup", () => {
|
|
275
|
+
test("removes connections exceeding timeout threshold", () => {
|
|
276
|
+
const res = new MockResponse() as any;
|
|
277
|
+
|
|
278
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
279
|
+
|
|
280
|
+
const connection = manager.getConnection("worker-1");
|
|
281
|
+
if (connection) {
|
|
282
|
+
// Simulate stale connection (11 minutes old, default timeout is 10 minutes)
|
|
283
|
+
connection.lastActivity = Date.now() - 11 * 60 * 1000;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Trigger cleanup
|
|
287
|
+
(manager as any).cleanupStaleConnections();
|
|
288
|
+
|
|
289
|
+
expect(manager.isConnected("worker-1")).toBe(false);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("keeps connections within timeout threshold", () => {
|
|
293
|
+
const res = new MockResponse() as any;
|
|
294
|
+
|
|
295
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
296
|
+
|
|
297
|
+
const connection = manager.getConnection("worker-1");
|
|
298
|
+
if (connection) {
|
|
299
|
+
// Simulate recent activity (9 minutes old, within 10 minute timeout)
|
|
300
|
+
connection.lastActivity = Date.now() - 9 * 60 * 1000;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Trigger cleanup
|
|
304
|
+
(manager as any).cleanupStaleConnections();
|
|
305
|
+
|
|
306
|
+
expect(manager.isConnected("worker-1")).toBe(true);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("keeps connection alive after recent verified activity", async () => {
|
|
310
|
+
const res = new MockResponse() as any;
|
|
311
|
+
|
|
312
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
313
|
+
|
|
314
|
+
const connection = manager.getConnection("worker-1");
|
|
315
|
+
if (connection) {
|
|
316
|
+
connection.lastActivity = Date.now() - 11 * 60 * 1000;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
await TestHelpers.delay(10);
|
|
320
|
+
manager.touchConnection("worker-1");
|
|
321
|
+
|
|
322
|
+
(manager as any).cleanupStaleConnections();
|
|
323
|
+
|
|
324
|
+
expect(manager.isConnected("worker-1")).toBe(true);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("respects custom WORKER_STALE_TIMEOUT_MINUTES from env", () => {
|
|
328
|
+
// Set custom timeout to 5 minutes
|
|
329
|
+
process.env.WORKER_STALE_TIMEOUT_MINUTES = "5";
|
|
330
|
+
|
|
331
|
+
const customManager = new WorkerConnectionManager();
|
|
332
|
+
const res = new MockResponse() as any;
|
|
333
|
+
|
|
334
|
+
customManager.addConnection(
|
|
335
|
+
"worker-1",
|
|
336
|
+
"U123",
|
|
337
|
+
"thread-1",
|
|
338
|
+
"agent-1",
|
|
339
|
+
res
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
const connection = customManager.getConnection("worker-1");
|
|
343
|
+
if (connection) {
|
|
344
|
+
// 6 minutes old (exceeds 5 minute custom timeout)
|
|
345
|
+
connection.lastActivity = Date.now() - 6 * 60 * 1000;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Trigger cleanup
|
|
349
|
+
(customManager as any).cleanupStaleConnections();
|
|
350
|
+
|
|
351
|
+
expect(customManager.isConnected("worker-1")).toBe(false);
|
|
352
|
+
|
|
353
|
+
customManager.shutdown();
|
|
354
|
+
delete process.env.WORKER_STALE_TIMEOUT_MINUTES;
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("cleans up only stale connections, keeps active ones", () => {
|
|
358
|
+
const staleRes = new MockResponse() as any;
|
|
359
|
+
const activeRes = new MockResponse() as any;
|
|
360
|
+
|
|
361
|
+
manager.addConnection(
|
|
362
|
+
"worker-stale",
|
|
363
|
+
"U123",
|
|
364
|
+
"thread-1",
|
|
365
|
+
"agent-1",
|
|
366
|
+
staleRes
|
|
367
|
+
);
|
|
368
|
+
manager.addConnection(
|
|
369
|
+
"worker-active",
|
|
370
|
+
"U456",
|
|
371
|
+
"thread-2",
|
|
372
|
+
"agent-2",
|
|
373
|
+
activeRes
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
const staleConn = manager.getConnection("worker-stale");
|
|
377
|
+
if (staleConn) {
|
|
378
|
+
staleConn.lastActivity = Date.now() - 11 * 60 * 1000;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Trigger cleanup
|
|
382
|
+
(manager as any).cleanupStaleConnections();
|
|
383
|
+
|
|
384
|
+
expect(manager.isConnected("worker-stale")).toBe(false);
|
|
385
|
+
expect(manager.isConnected("worker-active")).toBe(true);
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
describe("Shutdown", () => {
|
|
390
|
+
test("clears heartbeat and cleanup intervals", () => {
|
|
391
|
+
// Heartbeat and cleanup intervals are set in constructor
|
|
392
|
+
// After shutdown, they should be cleared
|
|
393
|
+
manager.shutdown();
|
|
394
|
+
|
|
395
|
+
// Create new connection after shutdown shouldn't have active intervals
|
|
396
|
+
// This is hard to test directly, but we can verify no errors occur
|
|
397
|
+
expect(() => manager.shutdown()).not.toThrow();
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
test("closes all active connections", () => {
|
|
401
|
+
const res1 = new MockResponse() as any;
|
|
402
|
+
const res2 = new MockResponse() as any;
|
|
403
|
+
const res3 = new MockResponse() as any;
|
|
404
|
+
|
|
405
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res1);
|
|
406
|
+
manager.addConnection("worker-2", "U456", "thread-2", "agent-2", res2);
|
|
407
|
+
manager.addConnection("worker-3", "U789", "thread-3", "agent-3", res3);
|
|
408
|
+
|
|
409
|
+
manager.shutdown();
|
|
410
|
+
|
|
411
|
+
expect(manager.isConnected("worker-1")).toBe(false);
|
|
412
|
+
expect(manager.isConnected("worker-2")).toBe(false);
|
|
413
|
+
expect(manager.isConnected("worker-3")).toBe(false);
|
|
414
|
+
|
|
415
|
+
expect(res1.isEnded()).toBe(true);
|
|
416
|
+
expect(res2.isEnded()).toBe(true);
|
|
417
|
+
expect(res3.isEnded()).toBe(true);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("shutdown is idempotent", () => {
|
|
421
|
+
const res = new MockResponse() as any;
|
|
422
|
+
|
|
423
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
424
|
+
|
|
425
|
+
manager.shutdown();
|
|
426
|
+
manager.shutdown(); // Call twice
|
|
427
|
+
|
|
428
|
+
expect(manager.isConnected("worker-1")).toBe(false);
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
describe("Error Handling", () => {
|
|
433
|
+
test("handles connection removal when response already ended", () => {
|
|
434
|
+
const res = new MockResponse() as any;
|
|
435
|
+
|
|
436
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res);
|
|
437
|
+
res.end(); // End response manually
|
|
438
|
+
|
|
439
|
+
// Should not throw when removing
|
|
440
|
+
expect(() => manager.removeConnection("worker-1")).not.toThrow();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
test("handles malformed data in SSE gracefully", () => {
|
|
444
|
+
const res = new MockResponse() as any;
|
|
445
|
+
|
|
446
|
+
// Circular reference should be handled
|
|
447
|
+
const circular: any = { a: 1 };
|
|
448
|
+
circular.self = circular;
|
|
449
|
+
|
|
450
|
+
// Should handle gracefully (JSON.stringify will throw, but sendSSE catches it)
|
|
451
|
+
expect(() => manager.sendSSE(res, "test", circular)).not.toThrow();
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
describe("Active Connections Tracking", () => {
|
|
456
|
+
test("returns empty array when no connections", () => {
|
|
457
|
+
const activeConnections = manager.getActiveConnections();
|
|
458
|
+
expect(activeConnections).toEqual([]);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
test("returns all active connection names", () => {
|
|
462
|
+
const res1 = new MockResponse() as any;
|
|
463
|
+
const res2 = new MockResponse() as any;
|
|
464
|
+
|
|
465
|
+
manager.addConnection(
|
|
466
|
+
"worker-alpha",
|
|
467
|
+
"U123",
|
|
468
|
+
"thread-1",
|
|
469
|
+
"agent-1",
|
|
470
|
+
res1
|
|
471
|
+
);
|
|
472
|
+
manager.addConnection("worker-beta", "U456", "thread-2", "agent-2", res2);
|
|
473
|
+
|
|
474
|
+
const activeConnections = manager.getActiveConnections();
|
|
475
|
+
expect(activeConnections).toHaveLength(2);
|
|
476
|
+
expect(activeConnections).toContain("worker-alpha");
|
|
477
|
+
expect(activeConnections).toContain("worker-beta");
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
test("updates active connections list after removal", () => {
|
|
481
|
+
const res1 = new MockResponse() as any;
|
|
482
|
+
const res2 = new MockResponse() as any;
|
|
483
|
+
|
|
484
|
+
manager.addConnection("worker-1", "U123", "thread-1", "agent-1", res1);
|
|
485
|
+
manager.addConnection("worker-2", "U456", "thread-2", "agent-2", res2);
|
|
486
|
+
|
|
487
|
+
expect(manager.getActiveConnections()).toHaveLength(2);
|
|
488
|
+
|
|
489
|
+
manager.removeConnection("worker-1");
|
|
490
|
+
|
|
491
|
+
const activeConnections = manager.getActiveConnections();
|
|
492
|
+
expect(activeConnections).toHaveLength(1);
|
|
493
|
+
expect(activeConnections).toContain("worker-2");
|
|
494
|
+
expect(activeConnections).not.toContain("worker-1");
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
});
|