@lobu/gateway 3.0.8 → 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 (219) hide show
  1. package/dist/api/platform.d.ts.map +1 -1
  2. package/dist/api/platform.js +8 -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/cli/index.js +2 -2
  12. package/dist/cli/index.js.map +1 -1
  13. package/dist/connections/chat-instance-manager.d.ts.map +1 -1
  14. package/dist/connections/chat-instance-manager.js +2 -1
  15. package/dist/connections/chat-instance-manager.js.map +1 -1
  16. package/dist/connections/interaction-bridge.d.ts +9 -2
  17. package/dist/connections/interaction-bridge.d.ts.map +1 -1
  18. package/dist/connections/interaction-bridge.js +132 -230
  19. package/dist/connections/interaction-bridge.js.map +1 -1
  20. package/dist/connections/message-handler-bridge.d.ts.map +1 -1
  21. package/dist/connections/message-handler-bridge.js +44 -26
  22. package/dist/connections/message-handler-bridge.js.map +1 -1
  23. package/dist/interactions.d.ts +9 -43
  24. package/dist/interactions.d.ts.map +1 -1
  25. package/dist/interactions.js +10 -52
  26. package/dist/interactions.js.map +1 -1
  27. package/dist/orchestration/base-deployment-manager.js +7 -7
  28. package/dist/orchestration/base-deployment-manager.js.map +1 -1
  29. package/dist/platform/unified-thread-consumer.d.ts.map +1 -1
  30. package/dist/platform/unified-thread-consumer.js +38 -34
  31. package/dist/platform/unified-thread-consumer.js.map +1 -1
  32. package/dist/routes/public/agent.d.ts +4 -0
  33. package/dist/routes/public/agent.d.ts.map +1 -1
  34. package/dist/routes/public/agent.js +21 -0
  35. package/dist/routes/public/agent.js.map +1 -1
  36. package/dist/services/core-services.d.ts.map +1 -1
  37. package/dist/services/core-services.js +4 -0
  38. package/dist/services/core-services.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/__tests__/agent-config-routes.test.ts +0 -254
  41. package/src/__tests__/agent-history-routes.test.ts +0 -72
  42. package/src/__tests__/agent-routes.test.ts +0 -68
  43. package/src/__tests__/agent-schedules-routes.test.ts +0 -59
  44. package/src/__tests__/agent-settings-store.test.ts +0 -323
  45. package/src/__tests__/bedrock-model-catalog.test.ts +0 -40
  46. package/src/__tests__/bedrock-openai-service.test.ts +0 -157
  47. package/src/__tests__/bedrock-provider-module.test.ts +0 -56
  48. package/src/__tests__/chat-instance-manager-slack.test.ts +0 -204
  49. package/src/__tests__/chat-response-bridge.test.ts +0 -131
  50. package/src/__tests__/config-memory-plugins.test.ts +0 -92
  51. package/src/__tests__/config-request-store.test.ts +0 -127
  52. package/src/__tests__/connection-routes.test.ts +0 -144
  53. package/src/__tests__/core-services-store-selection.test.ts +0 -92
  54. package/src/__tests__/docker-deployment.test.ts +0 -1211
  55. package/src/__tests__/embedded-deployment.test.ts +0 -342
  56. package/src/__tests__/grant-store.test.ts +0 -148
  57. package/src/__tests__/http-proxy.test.ts +0 -281
  58. package/src/__tests__/instruction-service.test.ts +0 -37
  59. package/src/__tests__/link-buttons.test.ts +0 -112
  60. package/src/__tests__/lobu.test.ts +0 -32
  61. package/src/__tests__/mcp-config-service.test.ts +0 -347
  62. package/src/__tests__/mcp-proxy.test.ts +0 -694
  63. package/src/__tests__/message-handler-bridge.test.ts +0 -17
  64. package/src/__tests__/model-selection.test.ts +0 -172
  65. package/src/__tests__/oauth-templates.test.ts +0 -39
  66. package/src/__tests__/platform-adapter-slack-send.test.ts +0 -114
  67. package/src/__tests__/platform-helpers-model-resolution.test.ts +0 -253
  68. package/src/__tests__/provider-inheritance.test.ts +0 -212
  69. package/src/__tests__/routes/cli-auth.test.ts +0 -337
  70. package/src/__tests__/routes/interactions.test.ts +0 -121
  71. package/src/__tests__/secret-proxy.test.ts +0 -85
  72. package/src/__tests__/session-manager.test.ts +0 -572
  73. package/src/__tests__/setup.ts +0 -133
  74. package/src/__tests__/skill-and-mcp-registry.test.ts +0 -203
  75. package/src/__tests__/slack-routes.test.ts +0 -161
  76. package/src/__tests__/system-config-resolver.test.ts +0 -75
  77. package/src/__tests__/system-message-limiter.test.ts +0 -89
  78. package/src/__tests__/system-skills-service.test.ts +0 -362
  79. package/src/__tests__/transcription-service.test.ts +0 -222
  80. package/src/__tests__/utils/rate-limiter.test.ts +0 -102
  81. package/src/__tests__/worker-connection-manager.test.ts +0 -497
  82. package/src/__tests__/worker-job-router.test.ts +0 -722
  83. package/src/api/index.ts +0 -1
  84. package/src/api/platform.ts +0 -292
  85. package/src/api/response-renderer.ts +0 -157
  86. package/src/auth/agent-metadata-store.ts +0 -168
  87. package/src/auth/api-auth-middleware.ts +0 -69
  88. package/src/auth/api-key-provider-module.ts +0 -213
  89. package/src/auth/base-provider-module.ts +0 -201
  90. package/src/auth/bedrock/provider-module.ts +0 -110
  91. package/src/auth/chatgpt/chatgpt-oauth-module.ts +0 -185
  92. package/src/auth/chatgpt/device-code-client.ts +0 -218
  93. package/src/auth/chatgpt/index.ts +0 -1
  94. package/src/auth/claude/oauth-module.ts +0 -280
  95. package/src/auth/cli/token-service.ts +0 -249
  96. package/src/auth/external/client.ts +0 -560
  97. package/src/auth/external/device-code-client.ts +0 -235
  98. package/src/auth/mcp/config-service.ts +0 -420
  99. package/src/auth/mcp/proxy.ts +0 -1086
  100. package/src/auth/mcp/string-substitution.ts +0 -17
  101. package/src/auth/mcp/tool-cache.ts +0 -90
  102. package/src/auth/oauth/base-client.ts +0 -267
  103. package/src/auth/oauth/client.ts +0 -153
  104. package/src/auth/oauth/credentials.ts +0 -7
  105. package/src/auth/oauth/providers.ts +0 -69
  106. package/src/auth/oauth/state-store.ts +0 -150
  107. package/src/auth/oauth-templates.ts +0 -179
  108. package/src/auth/provider-catalog.ts +0 -220
  109. package/src/auth/provider-model-options.ts +0 -41
  110. package/src/auth/settings/agent-settings-store.ts +0 -565
  111. package/src/auth/settings/auth-profiles-manager.ts +0 -216
  112. package/src/auth/settings/index.ts +0 -12
  113. package/src/auth/settings/model-preference-store.ts +0 -52
  114. package/src/auth/settings/model-selection.ts +0 -135
  115. package/src/auth/settings/resolved-settings-view.ts +0 -298
  116. package/src/auth/settings/template-utils.ts +0 -44
  117. package/src/auth/settings/token-service.ts +0 -88
  118. package/src/auth/system-env-store.ts +0 -98
  119. package/src/auth/user-agents-store.ts +0 -68
  120. package/src/channels/binding-service.ts +0 -214
  121. package/src/channels/index.ts +0 -4
  122. package/src/cli/gateway.ts +0 -1312
  123. package/src/cli/index.ts +0 -74
  124. package/src/commands/built-in-commands.ts +0 -80
  125. package/src/commands/command-dispatcher.ts +0 -94
  126. package/src/commands/command-reply-adapters.ts +0 -27
  127. package/src/config/file-loader.ts +0 -618
  128. package/src/config/index.ts +0 -588
  129. package/src/config/network-allowlist.ts +0 -71
  130. package/src/connections/chat-instance-manager.ts +0 -1284
  131. package/src/connections/chat-response-bridge.ts +0 -618
  132. package/src/connections/index.ts +0 -7
  133. package/src/connections/interaction-bridge.ts +0 -831
  134. package/src/connections/message-handler-bridge.ts +0 -415
  135. package/src/connections/platform-auth-methods.ts +0 -15
  136. package/src/connections/types.ts +0 -84
  137. package/src/gateway/connection-manager.ts +0 -291
  138. package/src/gateway/index.ts +0 -698
  139. package/src/gateway/job-router.ts +0 -201
  140. package/src/gateway-main.ts +0 -200
  141. package/src/index.ts +0 -41
  142. package/src/infrastructure/queue/index.ts +0 -12
  143. package/src/infrastructure/queue/queue-producer.ts +0 -148
  144. package/src/infrastructure/queue/redis-queue.ts +0 -361
  145. package/src/infrastructure/queue/types.ts +0 -133
  146. package/src/infrastructure/redis/system-message-limiter.ts +0 -94
  147. package/src/interactions/config-request-store.ts +0 -198
  148. package/src/interactions.ts +0 -363
  149. package/src/lobu.ts +0 -311
  150. package/src/metrics/prometheus.ts +0 -159
  151. package/src/modules/module-system.ts +0 -179
  152. package/src/orchestration/base-deployment-manager.ts +0 -900
  153. package/src/orchestration/deployment-utils.ts +0 -98
  154. package/src/orchestration/impl/docker-deployment.ts +0 -620
  155. package/src/orchestration/impl/embedded-deployment.ts +0 -268
  156. package/src/orchestration/impl/index.ts +0 -8
  157. package/src/orchestration/impl/k8s/deployment.ts +0 -1061
  158. package/src/orchestration/impl/k8s/helpers.ts +0 -610
  159. package/src/orchestration/impl/k8s/index.ts +0 -1
  160. package/src/orchestration/index.ts +0 -333
  161. package/src/orchestration/message-consumer.ts +0 -584
  162. package/src/orchestration/scheduled-wakeup.ts +0 -704
  163. package/src/permissions/approval-policy.ts +0 -36
  164. package/src/permissions/grant-store.ts +0 -219
  165. package/src/platform/file-handler.ts +0 -66
  166. package/src/platform/link-buttons.ts +0 -57
  167. package/src/platform/renderer-utils.ts +0 -44
  168. package/src/platform/response-renderer.ts +0 -84
  169. package/src/platform/unified-thread-consumer.ts +0 -187
  170. package/src/platform.ts +0 -318
  171. package/src/proxy/http-proxy.ts +0 -752
  172. package/src/proxy/proxy-manager.ts +0 -81
  173. package/src/proxy/secret-proxy.ts +0 -402
  174. package/src/proxy/token-refresh-job.ts +0 -143
  175. package/src/routes/internal/audio.ts +0 -141
  176. package/src/routes/internal/device-auth.ts +0 -652
  177. package/src/routes/internal/files.ts +0 -226
  178. package/src/routes/internal/history.ts +0 -69
  179. package/src/routes/internal/images.ts +0 -127
  180. package/src/routes/internal/interactions.ts +0 -84
  181. package/src/routes/internal/middleware.ts +0 -23
  182. package/src/routes/internal/schedule.ts +0 -226
  183. package/src/routes/internal/types.ts +0 -22
  184. package/src/routes/openapi-auto.ts +0 -239
  185. package/src/routes/public/agent-access.ts +0 -23
  186. package/src/routes/public/agent-config.ts +0 -675
  187. package/src/routes/public/agent-history.ts +0 -422
  188. package/src/routes/public/agent-schedules.ts +0 -296
  189. package/src/routes/public/agent.ts +0 -1086
  190. package/src/routes/public/agents.ts +0 -373
  191. package/src/routes/public/channels.ts +0 -191
  192. package/src/routes/public/cli-auth.ts +0 -896
  193. package/src/routes/public/connections.ts +0 -574
  194. package/src/routes/public/landing.ts +0 -16
  195. package/src/routes/public/oauth.ts +0 -147
  196. package/src/routes/public/settings-auth.ts +0 -104
  197. package/src/routes/public/slack.ts +0 -173
  198. package/src/routes/shared/agent-ownership.ts +0 -101
  199. package/src/routes/shared/token-verifier.ts +0 -34
  200. package/src/services/bedrock-model-catalog.ts +0 -217
  201. package/src/services/bedrock-openai-service.ts +0 -658
  202. package/src/services/core-services.ts +0 -1072
  203. package/src/services/image-generation-service.ts +0 -257
  204. package/src/services/instruction-service.ts +0 -318
  205. package/src/services/mcp-registry.ts +0 -94
  206. package/src/services/platform-helpers.ts +0 -287
  207. package/src/services/session-manager.ts +0 -262
  208. package/src/services/settings-resolver.ts +0 -74
  209. package/src/services/system-config-resolver.ts +0 -89
  210. package/src/services/system-skills-service.ts +0 -229
  211. package/src/services/transcription-service.ts +0 -684
  212. package/src/session.ts +0 -110
  213. package/src/spaces/index.ts +0 -1
  214. package/src/spaces/space-resolver.ts +0 -17
  215. package/src/stores/in-memory-agent-store.ts +0 -403
  216. package/src/stores/redis-agent-store.ts +0 -279
  217. package/src/utils/public-url.ts +0 -44
  218. package/src/utils/rate-limiter.ts +0 -94
  219. package/tsconfig.json +0 -33
@@ -1,212 +0,0 @@
1
- import { beforeEach, describe, expect, test } from "bun:test";
2
- import { MockRedisClient } from "@lobu/core/testing";
3
- import {
4
- ProviderCatalogService,
5
- resolveInstalledProviders,
6
- } from "../auth/provider-catalog";
7
- import { AgentSettingsStore } from "../auth/settings/agent-settings-store";
8
- import { AuthProfilesManager } from "../auth/settings/auth-profiles-manager";
9
- import {
10
- canEditSettingsSection,
11
- canViewSettingsSection,
12
- resolveSettingsView,
13
- } from "../auth/settings/resolved-settings-view";
14
- import { buildDefaultSettingsFromSource } from "../auth/settings/template-utils";
15
- import { hasConfiguredProvider } from "../services/platform-helpers";
16
-
17
- describe("sandbox provider inheritance", () => {
18
- let redis: MockRedisClient;
19
- let store: AgentSettingsStore;
20
- let authProfilesManager: AuthProfilesManager;
21
-
22
- beforeEach(() => {
23
- redis = new MockRedisClient();
24
- store = new AgentSettingsStore(redis as any);
25
- authProfilesManager = new AuthProfilesManager(store);
26
- });
27
-
28
- test("inherits installed providers through metadata and connection template fallback", async () => {
29
- await store.saveSettings("template-agent", {
30
- installedProviders: [{ providerId: "z-ai", installedAt: 1 }],
31
- });
32
- await redis.set(
33
- "agent_metadata:telegram-6570514069",
34
- JSON.stringify({ parentConnectionId: "conn-1" })
35
- );
36
- await redis.set(
37
- "connection:conn-1",
38
- JSON.stringify({ templateAgentId: "template-agent" })
39
- );
40
-
41
- const providers = await resolveInstalledProviders(
42
- store,
43
- "telegram-6570514069"
44
- );
45
-
46
- expect(providers).toEqual([{ providerId: "z-ai", installedAt: 1 }]);
47
- });
48
-
49
- test("inherits auth profiles through metadata and connection template fallback", async () => {
50
- await store.saveSettings("template-agent", {
51
- authProfiles: [
52
- {
53
- id: "profile-1",
54
- provider: "z-ai",
55
- credential: "secret",
56
- authType: "api-key",
57
- label: "z.ai",
58
- model: "*",
59
- createdAt: 1,
60
- },
61
- ],
62
- installedProviders: [{ providerId: "z-ai", installedAt: 1 }],
63
- });
64
- await redis.set(
65
- "agent_metadata:telegram-6570514069",
66
- JSON.stringify({ parentConnectionId: "conn-1" })
67
- );
68
- await redis.set(
69
- "connection:conn-1",
70
- JSON.stringify({ templateAgentId: "template-agent" })
71
- );
72
-
73
- const profiles = await authProfilesManager.listProfiles(
74
- "telegram-6570514069"
75
- );
76
-
77
- expect(profiles).toHaveLength(1);
78
- expect(profiles[0]?.provider).toBe("z-ai");
79
- expect(profiles[0]?.credential).toBe("secret");
80
- });
81
-
82
- test("inherits auth profiles for cloned sandbox settings that copied providers", async () => {
83
- await store.saveSettings("template-agent", {
84
- authProfiles: [
85
- {
86
- id: "profile-1",
87
- provider: "z-ai",
88
- credential: "secret",
89
- authType: "api-key",
90
- label: "z.ai",
91
- model: "*",
92
- createdAt: 1,
93
- },
94
- ],
95
- installedProviders: [{ providerId: "z-ai", installedAt: 1 }],
96
- });
97
-
98
- const templateSettings = await store.getSettings("template-agent");
99
- const cloned = buildDefaultSettingsFromSource(templateSettings);
100
- cloned.templateAgentId = "template-agent";
101
- await store.saveSettings("telegram-6570514069", cloned);
102
-
103
- const effective = await store.getEffectiveSettings("telegram-6570514069");
104
- const profiles = await authProfilesManager.listProfiles(
105
- "telegram-6570514069"
106
- );
107
-
108
- expect(cloned.authProfiles).toBeUndefined();
109
- expect(effective?.authProfiles).toHaveLength(1);
110
- expect(profiles).toHaveLength(1);
111
- });
112
-
113
- test("treats cloned sandbox settings as configured when template provides credentials", async () => {
114
- await store.saveSettings("template-agent", {
115
- authProfiles: [
116
- {
117
- id: "profile-1",
118
- provider: "z-ai",
119
- credential: "secret",
120
- authType: "api-key",
121
- label: "z.ai",
122
- model: "*",
123
- createdAt: 1,
124
- },
125
- ],
126
- installedProviders: [{ providerId: "z-ai", installedAt: 1 }],
127
- });
128
-
129
- const templateSettings = await store.getSettings("template-agent");
130
- const cloned = buildDefaultSettingsFromSource(templateSettings);
131
- cloned.templateAgentId = "template-agent";
132
- await store.saveSettings("telegram-6570514069", cloned);
133
-
134
- await expect(
135
- hasConfiguredProvider("telegram-6570514069", store)
136
- ).resolves.toBe(true);
137
- });
138
-
139
- test("exposes inherited provider state with read-only model visibility", async () => {
140
- await store.saveSettings("template-agent", {
141
- installedProviders: [{ providerId: "z-ai", installedAt: 1 }],
142
- });
143
- await redis.set(
144
- "agent_metadata:telegram-6570514069",
145
- JSON.stringify({ parentConnectionId: "conn-1" })
146
- );
147
- await redis.set(
148
- "connection:conn-1",
149
- JSON.stringify({ templateAgentId: "template-agent" })
150
- );
151
-
152
- const settingsView = await resolveSettingsView({
153
- agentId: "telegram-6570514069",
154
- agentSettingsStore: store,
155
- viewer: {
156
- settingsMode: "user",
157
- allowedScopes: ["view-model"],
158
- isAdmin: false,
159
- },
160
- });
161
-
162
- expect(
163
- canViewSettingsSection("model", {
164
- settingsMode: "user",
165
- allowedScopes: ["view-model"],
166
- isAdmin: false,
167
- })
168
- ).toBe(true);
169
- expect(
170
- canEditSettingsSection("model", {
171
- settingsMode: "user",
172
- allowedScopes: ["view-model"],
173
- isAdmin: false,
174
- })
175
- ).toBe(false);
176
- expect(settingsView.scope).toBe("sandbox");
177
- expect(settingsView.sections.model.source).toBe("inherited");
178
- expect(settingsView.sections.model.editable).toBe(false);
179
- expect(settingsView.providerSources["z-ai"]?.source).toBe("inherited");
180
- expect(settingsView.providerSources["z-ai"]?.canEdit).toBe(false);
181
- });
182
-
183
- test("uninstalling an inherited sandbox provider writes a local override list", async () => {
184
- await store.saveSettings("template-agent", {
185
- installedProviders: [
186
- { providerId: "z-ai", installedAt: 1 },
187
- { providerId: "openai", installedAt: 2 },
188
- ],
189
- });
190
- await redis.set(
191
- "agent_metadata:telegram-6570514069",
192
- JSON.stringify({ parentConnectionId: "conn-1" })
193
- );
194
- await redis.set(
195
- "connection:conn-1",
196
- JSON.stringify({ templateAgentId: "template-agent" })
197
- );
198
-
199
- const catalog = new ProviderCatalogService(store, authProfilesManager);
200
- await catalog.uninstallProvider("telegram-6570514069", "z-ai");
201
-
202
- const local = await store.getSettings("telegram-6570514069");
203
- const effective = await store.getEffectiveSettings("telegram-6570514069");
204
-
205
- expect(local?.installedProviders).toEqual([
206
- { providerId: "openai", installedAt: 2 },
207
- ]);
208
- expect(effective?.installedProviders).toEqual([
209
- { providerId: "openai", installedAt: 2 },
210
- ]);
211
- });
212
- });
@@ -1,337 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
2
- import { decrypt } from "@lobu/core";
3
- import { MockRedisClient } from "../../../../core/src/__tests__/fixtures/mock-redis";
4
- import {
5
- createCliAuthRoutes,
6
- createConnectAuthRoutes,
7
- } from "../../routes/public/cli-auth";
8
-
9
- describe("cli auth routes", () => {
10
- let originalKey: string | undefined;
11
- let redis: MockRedisClient;
12
- let queue: { getRedisClient(): MockRedisClient };
13
-
14
- beforeEach(() => {
15
- mock.restore();
16
- originalKey = process.env.ENCRYPTION_KEY;
17
- process.env.ENCRYPTION_KEY =
18
- "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
19
- redis = new MockRedisClient();
20
- queue = {
21
- getRedisClient: () => redis,
22
- };
23
- });
24
-
25
- afterEach(() => {
26
- if (originalKey !== undefined) {
27
- process.env.ENCRYPTION_KEY = originalKey;
28
- } else {
29
- delete process.env.ENCRYPTION_KEY;
30
- }
31
- });
32
-
33
- test("POST /cli/start returns device mode when the external provider supports device auth", async () => {
34
- const router = createCliAuthRoutes({
35
- queue: queue as any,
36
- externalAuthClient: {
37
- getCapabilities: mock(async () => ({ browser: true, device: true })),
38
- startDeviceAuthorization: mock(async () => ({
39
- deviceAuthId: "device-123",
40
- userCode: "ABCD-EFGH",
41
- verificationUri: "https://issuer.example.com/device",
42
- verificationUriComplete:
43
- "https://issuer.example.com/device?user_code=ABCD-EFGH",
44
- interval: 5,
45
- expiresIn: 600,
46
- })),
47
- } as any,
48
- });
49
-
50
- const res = await router.request("/cli/start", {
51
- method: "POST",
52
- headers: { "Content-Type": "application/json" },
53
- });
54
-
55
- expect(res.status).toBe(200);
56
- const body = await res.json();
57
- expect(body.mode).toBe("device");
58
- expect(body.deviceAuthId).toBe("device-123");
59
- expect(body.userCode).toBe("ABCD-EFGH");
60
- });
61
-
62
- test("POST /cli/start falls back to browser mode when device auth is unavailable", async () => {
63
- const router = createCliAuthRoutes({
64
- queue: queue as any,
65
- externalAuthClient: {
66
- getCapabilities: mock(async () => ({ browser: true, device: false })),
67
- } as any,
68
- });
69
-
70
- const res = await router.request("https://gateway.example.com/cli/start", {
71
- method: "POST",
72
- headers: { "Content-Type": "application/json" },
73
- });
74
-
75
- expect(res.status).toBe(200);
76
- const body = await res.json();
77
- expect(body.mode).toBe("browser");
78
- expect(typeof body.requestId).toBe("string");
79
- expect(body.loginUrl).toContain("/api/v1/auth/cli/session/login?request=");
80
- });
81
-
82
- test("GET /connect/oauth/login redirects into external browser auth", async () => {
83
- const router = createConnectAuthRoutes({
84
- queue: queue as any,
85
- externalAuthClient: {
86
- generateCodeVerifier: () => "code-verifier",
87
- buildAuthUrl: mock(async (state: string, codeVerifier: string) => {
88
- expect(state).toBeTruthy();
89
- expect(codeVerifier).toBe("code-verifier");
90
- return "https://issuer.example.com/oauth/authorize";
91
- }),
92
- } as any,
93
- });
94
-
95
- const res = await router.request(
96
- "https://gateway.example.com/connect/oauth/login?returnUrl=%2Fdone"
97
- );
98
-
99
- expect(res.status).toBe(302);
100
- expect(res.headers.get("location")).toBe(
101
- "https://issuer.example.com/oauth/authorize"
102
- );
103
- });
104
-
105
- test("GET /connect/oauth/callback sets a settings session and redirects back", async () => {
106
- await redis.setex(
107
- "cli:auth:connect:state-123",
108
- 600,
109
- JSON.stringify({
110
- returnUrl: "/done",
111
- codeVerifier: "code-verifier",
112
- })
113
- );
114
-
115
- const router = createConnectAuthRoutes({
116
- queue: queue as any,
117
- externalAuthClient: {
118
- exchangeCodeForToken: mock(async () => ({
119
- accessToken: "provider-access-token",
120
- refreshToken: "provider-refresh-token",
121
- tokenType: "Bearer",
122
- expiresAt: Date.now() + 3600_000,
123
- scopes: ["profile:read"],
124
- })),
125
- fetchUserInfo: mock(async () => ({
126
- sub: "user-123",
127
- email: "user@example.com",
128
- name: "Example User",
129
- })),
130
- } as any,
131
- });
132
-
133
- const res = await router.request(
134
- "https://gateway.example.com/connect/oauth/callback?code=auth-code&state=state-123"
135
- );
136
-
137
- expect(res.status).toBe(302);
138
- expect(res.headers.get("location")).toBe("/done");
139
- expect(res.headers.get("set-cookie")).toContain("lobu_settings_session=");
140
-
141
- const setCookie = res.headers.get("set-cookie");
142
- const token = setCookie?.match(/lobu_settings_session=([^;]+)/)?.[1];
143
- expect(token).toBeTruthy();
144
-
145
- const payload = JSON.parse(decrypt(decodeURIComponent(token!))) as Record<
146
- string,
147
- unknown
148
- >;
149
- expect(payload.userId).toBe("user-123");
150
- expect(payload.platform).toBe("external");
151
- expect(payload.isAdmin).toBeUndefined();
152
- expect(payload.settingsMode).toBeUndefined();
153
- });
154
-
155
- test("POST /cli/poll mints Lobu tokens after device auth completes", async () => {
156
- await redis.setex(
157
- "cli:auth:device:device-123",
158
- 600,
159
- JSON.stringify({
160
- status: "pending",
161
- createdAt: Date.now(),
162
- expiresAt: Date.now() + 600_000,
163
- interval: 5,
164
- userCode: "ABCD-EFGH",
165
- verificationUri: "https://issuer.example.com/device",
166
- })
167
- );
168
-
169
- const router = createCliAuthRoutes({
170
- queue: queue as any,
171
- externalAuthClient: {
172
- pollDeviceAuthorization: mock(async () => ({
173
- status: "complete",
174
- credentials: {
175
- accessToken: "provider-access-token",
176
- refreshToken: "provider-refresh-token",
177
- tokenType: "Bearer",
178
- expiresAt: Date.now() + 3600_000,
179
- scopes: ["profile:read"],
180
- },
181
- user: {
182
- sub: "user-123",
183
- email: "user@example.com",
184
- name: "Example User",
185
- },
186
- })),
187
- } as any,
188
- });
189
-
190
- const res = await router.request("/cli/poll", {
191
- method: "POST",
192
- headers: { "Content-Type": "application/json" },
193
- body: JSON.stringify({ deviceAuthId: "device-123" }),
194
- });
195
-
196
- expect(res.status).toBe(200);
197
- const body = await res.json();
198
- expect(body.status).toBe("complete");
199
- expect(typeof body.accessToken).toBe("string");
200
- expect(typeof body.refreshToken).toBe("string");
201
- expect(body.user.userId).toBe("user-123");
202
- expect(body.user.email).toBe("user@example.com");
203
- });
204
-
205
- test("POST /cli/poll returns a completed browser result from stored request state", async () => {
206
- await redis.setex(
207
- "cli:auth:request:req-123",
208
- 600,
209
- JSON.stringify({
210
- status: "complete",
211
- createdAt: Date.now(),
212
- result: {
213
- accessToken: "lobu-access-token",
214
- refreshToken: "lobu-refresh-token",
215
- expiresAt: Date.now() + 3600_000,
216
- user: {
217
- userId: "user-123",
218
- email: "user@example.com",
219
- name: "Example User",
220
- },
221
- },
222
- })
223
- );
224
-
225
- const router = createCliAuthRoutes({
226
- queue: queue as any,
227
- externalAuthClient: {} as any,
228
- });
229
-
230
- const res = await router.request("/cli/poll", {
231
- method: "POST",
232
- headers: { "Content-Type": "application/json" },
233
- body: JSON.stringify({ requestId: "req-123" }),
234
- });
235
-
236
- expect(res.status).toBe(200);
237
- const body = await res.json();
238
- expect(body.status).toBe("complete");
239
- expect(body.user.userId).toBe("user-123");
240
- });
241
-
242
- test("POST /cli/admin-login mints tokens when development fallback is enabled", async () => {
243
- const router = createCliAuthRoutes({
244
- queue: queue as any,
245
- allowAdminPasswordLogin: true,
246
- adminPassword: "dev-secret",
247
- });
248
-
249
- const res = await router.request("/cli/admin-login", {
250
- method: "POST",
251
- headers: {
252
- "Content-Type": "application/json",
253
- "X-Forwarded-For": "10.0.0.1",
254
- },
255
- body: JSON.stringify({ password: "dev-secret" }),
256
- });
257
-
258
- expect(res.status).toBe(200);
259
- const body = await res.json();
260
- expect(body.status).toBe("complete");
261
- expect(typeof body.accessToken).toBe("string");
262
- expect(body.user.userId).toBe("admin");
263
- });
264
-
265
- test("POST /cli/admin-login is rejected when disabled or password is wrong", async () => {
266
- const disabledRouter = createCliAuthRoutes({
267
- queue: queue as any,
268
- allowAdminPasswordLogin: false,
269
- adminPassword: "dev-secret",
270
- });
271
-
272
- const disabled = await disabledRouter.request("/cli/admin-login", {
273
- method: "POST",
274
- headers: { "Content-Type": "application/json" },
275
- body: JSON.stringify({ password: "dev-secret" }),
276
- });
277
- expect(disabled.status).toBe(403);
278
-
279
- const enabledRouter = createCliAuthRoutes({
280
- queue: queue as any,
281
- allowAdminPasswordLogin: true,
282
- adminPassword: "dev-secret",
283
- });
284
-
285
- const wrong = await enabledRouter.request("/cli/admin-login", {
286
- method: "POST",
287
- headers: {
288
- "Content-Type": "application/json",
289
- "X-Forwarded-For": "10.0.0.1",
290
- },
291
- body: JSON.stringify({ password: "wrong-secret" }),
292
- });
293
- expect(wrong.status).toBe(401);
294
- });
295
-
296
- test("POST /cli/admin-login is rate limited per client IP", async () => {
297
- const router = createCliAuthRoutes({
298
- queue: queue as any,
299
- allowAdminPasswordLogin: true,
300
- adminPassword: "dev-secret",
301
- });
302
-
303
- for (let attempt = 0; attempt < 5; attempt += 1) {
304
- const res = await router.request("/cli/admin-login", {
305
- method: "POST",
306
- headers: {
307
- "Content-Type": "application/json",
308
- "X-Forwarded-For": "10.0.0.9",
309
- },
310
- body: JSON.stringify({ password: "wrong-secret" }),
311
- });
312
- expect(res.status).toBe(401);
313
- }
314
-
315
- const limited = await router.request("/cli/admin-login", {
316
- method: "POST",
317
- headers: {
318
- "Content-Type": "application/json",
319
- "X-Forwarded-For": "10.0.0.9",
320
- },
321
- body: JSON.stringify({ password: "wrong-secret" }),
322
- });
323
-
324
- expect(limited.status).toBe(429);
325
- expect(limited.headers.get("retry-after")).toBeTruthy();
326
-
327
- const differentIp = await router.request("/cli/admin-login", {
328
- method: "POST",
329
- headers: {
330
- "Content-Type": "application/json",
331
- "X-Forwarded-For": "10.0.0.10",
332
- },
333
- body: JSON.stringify({ password: "dev-secret" }),
334
- });
335
- expect(differentIp.status).toBe(200);
336
- });
337
- });
@@ -1,121 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
2
- import { generateWorkerToken } from "@lobu/core";
3
- import { createInteractionRoutes } from "../../routes/internal/interactions";
4
-
5
- describe("interaction routes", () => {
6
- let originalKey: string | undefined;
7
- let workerToken: string;
8
- let mockInteractionService: any;
9
- let router: ReturnType<typeof createInteractionRoutes>;
10
-
11
- beforeEach(() => {
12
- originalKey = process.env.ENCRYPTION_KEY;
13
- process.env.ENCRYPTION_KEY =
14
- "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
15
-
16
- workerToken = generateWorkerToken("user-1", "conv-1", "deploy-1", {
17
- channelId: "chan-1",
18
- teamId: "team-1",
19
- });
20
-
21
- mockInteractionService = {
22
- postQuestion: mock(() => Promise.resolve({ id: "interaction-123" })),
23
- createSuggestion: mock(() => Promise.resolve()),
24
- };
25
-
26
- router = createInteractionRoutes(mockInteractionService);
27
- });
28
-
29
- afterEach(() => {
30
- if (originalKey !== undefined) {
31
- process.env.ENCRYPTION_KEY = originalKey;
32
- } else {
33
- delete process.env.ENCRYPTION_KEY;
34
- }
35
- });
36
-
37
- describe("POST /internal/interactions/create", () => {
38
- test("returns 401 without auth header", async () => {
39
- const res = await router.request("/internal/interactions/create", {
40
- method: "POST",
41
- headers: { "Content-Type": "application/json" },
42
- body: JSON.stringify({ question: "test?", options: [] }),
43
- });
44
- expect(res.status).toBe(401);
45
- });
46
-
47
- test("returns 401 with invalid token", async () => {
48
- const res = await router.request("/internal/interactions/create", {
49
- method: "POST",
50
- headers: {
51
- "Content-Type": "application/json",
52
- Authorization: "Bearer invalid-token",
53
- },
54
- body: JSON.stringify({ question: "test?", options: [] }),
55
- });
56
- expect(res.status).toBe(401);
57
- });
58
-
59
- test("posts question and returns id", async () => {
60
- const res = await router.request("/internal/interactions/create", {
61
- method: "POST",
62
- headers: {
63
- "Content-Type": "application/json",
64
- Authorization: `Bearer ${workerToken}`,
65
- },
66
- body: JSON.stringify({
67
- question: "Which option?",
68
- options: ["A", "B"],
69
- }),
70
- });
71
- expect(res.status).toBe(200);
72
- const body = await res.json();
73
- expect(body.id).toBe("interaction-123");
74
- expect(body.status).toBe("posted");
75
- expect(mockInteractionService.postQuestion).toHaveBeenCalledTimes(1);
76
- });
77
-
78
- test("returns 500 on service error", async () => {
79
- mockInteractionService.postQuestion = mock(() =>
80
- Promise.reject(new Error("service down"))
81
- );
82
- const res = await router.request("/internal/interactions/create", {
83
- method: "POST",
84
- headers: {
85
- "Content-Type": "application/json",
86
- Authorization: `Bearer ${workerToken}`,
87
- },
88
- body: JSON.stringify({ question: "test?", options: [] }),
89
- });
90
- expect(res.status).toBe(500);
91
- });
92
- });
93
-
94
- describe("POST /internal/suggestions/create", () => {
95
- test("creates suggestions", async () => {
96
- const res = await router.request("/internal/suggestions/create", {
97
- method: "POST",
98
- headers: {
99
- "Content-Type": "application/json",
100
- Authorization: `Bearer ${workerToken}`,
101
- },
102
- body: JSON.stringify({
103
- prompts: ["Try this", "Or this"],
104
- }),
105
- });
106
- expect(res.status).toBe(200);
107
- const body = await res.json();
108
- expect(body.success).toBe(true);
109
- expect(mockInteractionService.createSuggestion).toHaveBeenCalledTimes(1);
110
- });
111
-
112
- test("returns 401 without auth", async () => {
113
- const res = await router.request("/internal/suggestions/create", {
114
- method: "POST",
115
- headers: { "Content-Type": "application/json" },
116
- body: JSON.stringify({ prompts: ["test"] }),
117
- });
118
- expect(res.status).toBe(401);
119
- });
120
- });
121
- });