@lobu/gateway 3.0.9 → 3.0.12

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.
Files changed (210) hide show
  1. package/dist/api/platform.d.ts.map +1 -1
  2. package/dist/api/platform.js +7 -26
  3. package/dist/api/platform.js.map +1 -1
  4. package/dist/auth/mcp/proxy.d.ts +14 -0
  5. package/dist/auth/mcp/proxy.d.ts.map +1 -1
  6. package/dist/auth/mcp/proxy.js +149 -13
  7. package/dist/auth/mcp/proxy.js.map +1 -1
  8. package/dist/cli/gateway.d.ts.map +1 -1
  9. package/dist/cli/gateway.js +29 -0
  10. package/dist/cli/gateway.js.map +1 -1
  11. package/dist/connections/chat-instance-manager.d.ts.map +1 -1
  12. package/dist/connections/chat-instance-manager.js +2 -1
  13. package/dist/connections/chat-instance-manager.js.map +1 -1
  14. package/dist/connections/interaction-bridge.d.ts +9 -2
  15. package/dist/connections/interaction-bridge.d.ts.map +1 -1
  16. package/dist/connections/interaction-bridge.js +121 -261
  17. package/dist/connections/interaction-bridge.js.map +1 -1
  18. package/dist/interactions.d.ts +9 -43
  19. package/dist/interactions.d.ts.map +1 -1
  20. package/dist/interactions.js +10 -52
  21. package/dist/interactions.js.map +1 -1
  22. package/dist/routes/public/agent.d.ts +4 -0
  23. package/dist/routes/public/agent.d.ts.map +1 -1
  24. package/dist/routes/public/agent.js +21 -0
  25. package/dist/routes/public/agent.js.map +1 -1
  26. package/dist/services/core-services.d.ts.map +1 -1
  27. package/dist/services/core-services.js +4 -0
  28. package/dist/services/core-services.js.map +1 -1
  29. package/package.json +9 -9
  30. package/src/__tests__/agent-config-routes.test.ts +0 -254
  31. package/src/__tests__/agent-history-routes.test.ts +0 -72
  32. package/src/__tests__/agent-routes.test.ts +0 -68
  33. package/src/__tests__/agent-schedules-routes.test.ts +0 -59
  34. package/src/__tests__/agent-settings-store.test.ts +0 -323
  35. package/src/__tests__/bedrock-model-catalog.test.ts +0 -40
  36. package/src/__tests__/bedrock-openai-service.test.ts +0 -157
  37. package/src/__tests__/bedrock-provider-module.test.ts +0 -56
  38. package/src/__tests__/chat-instance-manager-slack.test.ts +0 -204
  39. package/src/__tests__/chat-response-bridge.test.ts +0 -131
  40. package/src/__tests__/config-memory-plugins.test.ts +0 -92
  41. package/src/__tests__/config-request-store.test.ts +0 -127
  42. package/src/__tests__/connection-routes.test.ts +0 -144
  43. package/src/__tests__/core-services-store-selection.test.ts +0 -92
  44. package/src/__tests__/docker-deployment.test.ts +0 -1211
  45. package/src/__tests__/embedded-deployment.test.ts +0 -342
  46. package/src/__tests__/grant-store.test.ts +0 -148
  47. package/src/__tests__/http-proxy.test.ts +0 -281
  48. package/src/__tests__/instruction-service.test.ts +0 -37
  49. package/src/__tests__/link-buttons.test.ts +0 -112
  50. package/src/__tests__/lobu.test.ts +0 -32
  51. package/src/__tests__/mcp-config-service.test.ts +0 -347
  52. package/src/__tests__/mcp-proxy.test.ts +0 -694
  53. package/src/__tests__/message-handler-bridge.test.ts +0 -17
  54. package/src/__tests__/model-selection.test.ts +0 -172
  55. package/src/__tests__/oauth-templates.test.ts +0 -39
  56. package/src/__tests__/platform-adapter-slack-send.test.ts +0 -114
  57. package/src/__tests__/platform-helpers-model-resolution.test.ts +0 -253
  58. package/src/__tests__/provider-inheritance.test.ts +0 -212
  59. package/src/__tests__/routes/cli-auth.test.ts +0 -337
  60. package/src/__tests__/routes/interactions.test.ts +0 -121
  61. package/src/__tests__/secret-proxy.test.ts +0 -85
  62. package/src/__tests__/session-manager.test.ts +0 -572
  63. package/src/__tests__/setup.ts +0 -133
  64. package/src/__tests__/skill-and-mcp-registry.test.ts +0 -203
  65. package/src/__tests__/slack-routes.test.ts +0 -161
  66. package/src/__tests__/system-config-resolver.test.ts +0 -75
  67. package/src/__tests__/system-message-limiter.test.ts +0 -89
  68. package/src/__tests__/system-skills-service.test.ts +0 -362
  69. package/src/__tests__/transcription-service.test.ts +0 -222
  70. package/src/__tests__/utils/rate-limiter.test.ts +0 -102
  71. package/src/__tests__/worker-connection-manager.test.ts +0 -497
  72. package/src/__tests__/worker-job-router.test.ts +0 -722
  73. package/src/api/index.ts +0 -1
  74. package/src/api/platform.ts +0 -292
  75. package/src/api/response-renderer.ts +0 -157
  76. package/src/auth/agent-metadata-store.ts +0 -168
  77. package/src/auth/api-auth-middleware.ts +0 -69
  78. package/src/auth/api-key-provider-module.ts +0 -213
  79. package/src/auth/base-provider-module.ts +0 -201
  80. package/src/auth/bedrock/provider-module.ts +0 -110
  81. package/src/auth/chatgpt/chatgpt-oauth-module.ts +0 -185
  82. package/src/auth/chatgpt/device-code-client.ts +0 -218
  83. package/src/auth/chatgpt/index.ts +0 -1
  84. package/src/auth/claude/oauth-module.ts +0 -280
  85. package/src/auth/cli/token-service.ts +0 -249
  86. package/src/auth/external/client.ts +0 -560
  87. package/src/auth/external/device-code-client.ts +0 -235
  88. package/src/auth/mcp/config-service.ts +0 -420
  89. package/src/auth/mcp/proxy.ts +0 -1086
  90. package/src/auth/mcp/string-substitution.ts +0 -17
  91. package/src/auth/mcp/tool-cache.ts +0 -90
  92. package/src/auth/oauth/base-client.ts +0 -267
  93. package/src/auth/oauth/client.ts +0 -153
  94. package/src/auth/oauth/credentials.ts +0 -7
  95. package/src/auth/oauth/providers.ts +0 -69
  96. package/src/auth/oauth/state-store.ts +0 -150
  97. package/src/auth/oauth-templates.ts +0 -179
  98. package/src/auth/provider-catalog.ts +0 -220
  99. package/src/auth/provider-model-options.ts +0 -41
  100. package/src/auth/settings/agent-settings-store.ts +0 -565
  101. package/src/auth/settings/auth-profiles-manager.ts +0 -216
  102. package/src/auth/settings/index.ts +0 -12
  103. package/src/auth/settings/model-preference-store.ts +0 -52
  104. package/src/auth/settings/model-selection.ts +0 -135
  105. package/src/auth/settings/resolved-settings-view.ts +0 -298
  106. package/src/auth/settings/template-utils.ts +0 -44
  107. package/src/auth/settings/token-service.ts +0 -88
  108. package/src/auth/system-env-store.ts +0 -98
  109. package/src/auth/user-agents-store.ts +0 -68
  110. package/src/channels/binding-service.ts +0 -214
  111. package/src/channels/index.ts +0 -4
  112. package/src/cli/gateway.ts +0 -1312
  113. package/src/cli/index.ts +0 -74
  114. package/src/commands/built-in-commands.ts +0 -80
  115. package/src/commands/command-dispatcher.ts +0 -94
  116. package/src/commands/command-reply-adapters.ts +0 -27
  117. package/src/config/file-loader.ts +0 -618
  118. package/src/config/index.ts +0 -588
  119. package/src/config/network-allowlist.ts +0 -71
  120. package/src/connections/chat-instance-manager.ts +0 -1284
  121. package/src/connections/chat-response-bridge.ts +0 -618
  122. package/src/connections/index.ts +0 -7
  123. package/src/connections/interaction-bridge.ts +0 -831
  124. package/src/connections/message-handler-bridge.ts +0 -440
  125. package/src/connections/platform-auth-methods.ts +0 -15
  126. package/src/connections/types.ts +0 -84
  127. package/src/gateway/connection-manager.ts +0 -291
  128. package/src/gateway/index.ts +0 -698
  129. package/src/gateway/job-router.ts +0 -201
  130. package/src/gateway-main.ts +0 -200
  131. package/src/index.ts +0 -41
  132. package/src/infrastructure/queue/index.ts +0 -12
  133. package/src/infrastructure/queue/queue-producer.ts +0 -148
  134. package/src/infrastructure/queue/redis-queue.ts +0 -361
  135. package/src/infrastructure/queue/types.ts +0 -133
  136. package/src/infrastructure/redis/system-message-limiter.ts +0 -94
  137. package/src/interactions/config-request-store.ts +0 -198
  138. package/src/interactions.ts +0 -363
  139. package/src/lobu.ts +0 -311
  140. package/src/metrics/prometheus.ts +0 -159
  141. package/src/modules/module-system.ts +0 -179
  142. package/src/orchestration/base-deployment-manager.ts +0 -900
  143. package/src/orchestration/deployment-utils.ts +0 -98
  144. package/src/orchestration/impl/docker-deployment.ts +0 -620
  145. package/src/orchestration/impl/embedded-deployment.ts +0 -268
  146. package/src/orchestration/impl/index.ts +0 -8
  147. package/src/orchestration/impl/k8s/deployment.ts +0 -1061
  148. package/src/orchestration/impl/k8s/helpers.ts +0 -610
  149. package/src/orchestration/impl/k8s/index.ts +0 -1
  150. package/src/orchestration/index.ts +0 -333
  151. package/src/orchestration/message-consumer.ts +0 -584
  152. package/src/orchestration/scheduled-wakeup.ts +0 -704
  153. package/src/permissions/approval-policy.ts +0 -36
  154. package/src/permissions/grant-store.ts +0 -219
  155. package/src/platform/file-handler.ts +0 -66
  156. package/src/platform/link-buttons.ts +0 -57
  157. package/src/platform/renderer-utils.ts +0 -44
  158. package/src/platform/response-renderer.ts +0 -84
  159. package/src/platform/unified-thread-consumer.ts +0 -194
  160. package/src/platform.ts +0 -318
  161. package/src/proxy/http-proxy.ts +0 -752
  162. package/src/proxy/proxy-manager.ts +0 -81
  163. package/src/proxy/secret-proxy.ts +0 -402
  164. package/src/proxy/token-refresh-job.ts +0 -143
  165. package/src/routes/internal/audio.ts +0 -141
  166. package/src/routes/internal/device-auth.ts +0 -652
  167. package/src/routes/internal/files.ts +0 -226
  168. package/src/routes/internal/history.ts +0 -69
  169. package/src/routes/internal/images.ts +0 -127
  170. package/src/routes/internal/interactions.ts +0 -84
  171. package/src/routes/internal/middleware.ts +0 -23
  172. package/src/routes/internal/schedule.ts +0 -226
  173. package/src/routes/internal/types.ts +0 -22
  174. package/src/routes/openapi-auto.ts +0 -239
  175. package/src/routes/public/agent-access.ts +0 -23
  176. package/src/routes/public/agent-config.ts +0 -675
  177. package/src/routes/public/agent-history.ts +0 -422
  178. package/src/routes/public/agent-schedules.ts +0 -296
  179. package/src/routes/public/agent.ts +0 -1086
  180. package/src/routes/public/agents.ts +0 -373
  181. package/src/routes/public/channels.ts +0 -191
  182. package/src/routes/public/cli-auth.ts +0 -896
  183. package/src/routes/public/connections.ts +0 -574
  184. package/src/routes/public/landing.ts +0 -16
  185. package/src/routes/public/oauth.ts +0 -147
  186. package/src/routes/public/settings-auth.ts +0 -104
  187. package/src/routes/public/slack.ts +0 -173
  188. package/src/routes/shared/agent-ownership.ts +0 -101
  189. package/src/routes/shared/token-verifier.ts +0 -34
  190. package/src/services/bedrock-model-catalog.ts +0 -217
  191. package/src/services/bedrock-openai-service.ts +0 -658
  192. package/src/services/core-services.ts +0 -1072
  193. package/src/services/image-generation-service.ts +0 -257
  194. package/src/services/instruction-service.ts +0 -318
  195. package/src/services/mcp-registry.ts +0 -94
  196. package/src/services/platform-helpers.ts +0 -287
  197. package/src/services/session-manager.ts +0 -262
  198. package/src/services/settings-resolver.ts +0 -74
  199. package/src/services/system-config-resolver.ts +0 -89
  200. package/src/services/system-skills-service.ts +0 -229
  201. package/src/services/transcription-service.ts +0 -684
  202. package/src/session.ts +0 -110
  203. package/src/spaces/index.ts +0 -1
  204. package/src/spaces/space-resolver.ts +0 -17
  205. package/src/stores/in-memory-agent-store.ts +0 -403
  206. package/src/stores/redis-agent-store.ts +0 -279
  207. package/src/utils/public-url.ts +0 -44
  208. package/src/utils/rate-limiter.ts +0 -94
  209. package/tsconfig.json +0 -33
  210. package/tsconfig.tsbuildinfo +0 -1
@@ -1,497 +0,0 @@
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
- });