@lobehub/lobehub 2.0.0-next.32 → 2.0.0-next.34
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/.github/workflows/test.yml +1 -0
- package/CHANGELOG.md +58 -0
- package/apps/desktop/package.json +1 -1
- package/changelog/v1.json +21 -0
- package/docker-compose/local/.env.example +3 -0
- package/docs/self-hosting/server-database/docker-compose.mdx +29 -0
- package/docs/self-hosting/server-database/docker-compose.zh-CN.mdx +29 -0
- package/package.json +1 -1
- package/packages/const/src/hotkeys.ts +3 -3
- package/packages/const/src/models.ts +2 -2
- package/packages/const/src/utils/merge.ts +3 -3
- package/packages/conversation-flow/package.json +13 -0
- package/packages/conversation-flow/src/__tests__/fixtures/index.ts +48 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/assistant-chain-with-followup.json +56 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/assistant-with-tools.json +144 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/branch/active-index-1.json +131 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/branch/assistant-branch.json +96 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/branch/assistant-user-branch.json +123 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/branch/conversation.json +128 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/branch/index.ts +14 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/branch/nested.json +179 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/compare/index.ts +8 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/compare/simple.json +85 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/compare/with-tools.json +169 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/complex-scenario.json +107 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/index.ts +14 -0
- package/packages/conversation-flow/src/__tests__/fixtures/inputs/linear-conversation.json +59 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/assistant-chain-with-followup.json +135 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/assistant-with-tools.json +340 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/branch/active-index-1.json +242 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/branch/assistant-branch.json +208 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/branch/assistant-user-branch.json +254 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/branch/conversation.json +260 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/branch/index.ts +14 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/branch/nested.json +389 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/compare/index.ts +8 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/compare/simple.json +224 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/compare/with-tools.json +418 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/complex-scenario.json +239 -0
- package/packages/conversation-flow/src/__tests__/fixtures/outputs/linear-conversation.json +138 -0
- package/packages/conversation-flow/src/__tests__/parse.test.ts +97 -0
- package/packages/conversation-flow/src/index.ts +17 -0
- package/packages/conversation-flow/src/indexing.ts +58 -0
- package/packages/conversation-flow/src/parse.ts +53 -0
- package/packages/conversation-flow/src/structuring.ts +38 -0
- package/packages/conversation-flow/src/transformation/BranchResolver.ts +66 -0
- package/packages/conversation-flow/src/transformation/ContextTreeBuilder.ts +292 -0
- package/packages/conversation-flow/src/transformation/FlatListBuilder.ts +421 -0
- package/packages/conversation-flow/src/transformation/MessageCollector.ts +166 -0
- package/packages/conversation-flow/src/transformation/MessageTransformer.ts +177 -0
- package/packages/conversation-flow/src/transformation/__tests__/BranchResolver.test.ts +151 -0
- package/packages/conversation-flow/src/transformation/__tests__/ContextTreeBuilder.test.ts +385 -0
- package/packages/conversation-flow/src/transformation/__tests__/FlatListBuilder.test.ts +511 -0
- package/packages/conversation-flow/src/transformation/__tests__/MessageCollector.test.ts +220 -0
- package/packages/conversation-flow/src/transformation/__tests__/MessageTransformer.test.ts +287 -0
- package/packages/conversation-flow/src/transformation/index.ts +78 -0
- package/packages/conversation-flow/src/types/contextTree.ts +65 -0
- package/packages/conversation-flow/src/types/flatMessageList.ts +66 -0
- package/packages/conversation-flow/src/types/shared.ts +63 -0
- package/packages/conversation-flow/src/types.ts +36 -0
- package/packages/conversation-flow/vitest.config.mts +10 -0
- package/packages/model-bank/src/aiModels/google.ts +1 -1
- package/packages/types/src/message/common/metadata.ts +5 -1
- package/src/app/[variants]/(main)/chat/@session/features/SessionListContent/List/Item/index.tsx +3 -4
- package/src/app/[variants]/(main)/settings/provider/ProviderMenu/List.tsx +97 -7
- package/src/app/[variants]/(main)/settings/provider/features/ModelList/DisabledModels.tsx +144 -8
- package/src/envs/__tests__/app.test.ts +47 -13
- package/src/envs/app.ts +6 -0
- package/src/locales/default/modelProvider.ts +15 -1
- package/src/server/routers/async/__tests__/caller.test.ts +333 -0
- package/src/server/routers/async/caller.ts +2 -1
- package/src/server/routers/lambda/__tests__/integration/message.integration.test.ts +57 -57
- package/src/server/routers/lambda/message.ts +2 -2
- package/src/server/services/mcp/deps/checkers/ManualInstallationChecker.test.ts +162 -0
- package/src/server/services/mcp/deps/checkers/NpmInstallationChecker.test.ts +374 -0
- package/src/server/services/mcp/deps/checkers/PythonInstallationChecker.test.ts +368 -0
- package/src/server/services/message/__tests__/index.test.ts +4 -4
- package/src/server/services/message/index.ts +1 -1
- package/src/services/message/index.ts +2 -3
- package/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts +8 -8
- package/src/store/chat/slices/aiChat/actions/__tests__/generateAIChatV2.test.ts +8 -8
- package/src/store/chat/slices/aiChat/actions/__tests__/helpers.ts +1 -1
- package/src/store/chat/slices/aiChat/actions/generateAIChatV2.ts +1 -1
- package/src/store/chat/slices/message/action.test.ts +7 -7
- package/src/store/chat/slices/message/action.ts +2 -2
- package/src/store/chat/slices/plugin/action.test.ts +7 -7
- package/src/store/chat/slices/plugin/action.ts +1 -1
- package/src/store/global/initialState.ts +4 -0
- package/src/store/global/selectors/systemStatus.ts +6 -0
- package/packages/context-engine/ARCHITECTURE.md +0 -425
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
// Import the module under test after mocks are set up
|
|
3
|
+
import { createTRPCClient, httpLink } from '@trpc/client';
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
|
|
7
|
+
|
|
8
|
+
import { createAsyncServerClient } from '../caller';
|
|
9
|
+
|
|
10
|
+
// Create mockable appEnv - use object property to allow mutation
|
|
11
|
+
const mockAppEnv: { APP_URL?: string; INTERNAL_APP_URL?: string | null | undefined } = {
|
|
12
|
+
APP_URL: 'https://public.example.com',
|
|
13
|
+
INTERNAL_APP_URL: 'http://localhost:3210',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Mock dependencies before importing the module under test
|
|
17
|
+
vi.mock('@trpc/client', () => ({
|
|
18
|
+
createTRPCClient: vi.fn(),
|
|
19
|
+
httpLink: vi.fn((options) => options),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
vi.mock('@/envs/app', () => ({
|
|
23
|
+
get appEnv() {
|
|
24
|
+
return mockAppEnv;
|
|
25
|
+
},
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
vi.mock('@/server/modules/KeyVaultsEncrypt', () => ({
|
|
29
|
+
KeyVaultsGateKeeper: {
|
|
30
|
+
initWithEnvKey: vi.fn(),
|
|
31
|
+
},
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
vi.mock('@/config/db', () => ({
|
|
35
|
+
serverDBEnv: {
|
|
36
|
+
KEY_VAULTS_SECRET: 'test-secret-key',
|
|
37
|
+
},
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
vi.mock('@/const/version', () => ({
|
|
41
|
+
isDesktop: false,
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
describe('createAsyncServerClient - INTERNAL_APP_URL Tests', () => {
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.clearAllMocks();
|
|
47
|
+
|
|
48
|
+
const mockEncrypt = vi.fn().mockResolvedValue('encrypted-payload-data');
|
|
49
|
+
vi.mocked(KeyVaultsGateKeeper.initWithEnvKey).mockResolvedValue({
|
|
50
|
+
encrypt: mockEncrypt,
|
|
51
|
+
} as any);
|
|
52
|
+
vi.mocked(createTRPCClient).mockReturnValue({ _mockClient: true } as any);
|
|
53
|
+
|
|
54
|
+
// Reset to default values by mutating the object
|
|
55
|
+
mockAppEnv.APP_URL = 'https://public.example.com';
|
|
56
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://localhost:3210';
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('URL selection logic', () => {
|
|
60
|
+
it('should use INTERNAL_APP_URL when both APP_URL and INTERNAL_APP_URL are set', async () => {
|
|
61
|
+
mockAppEnv.APP_URL = 'https://public.example.com';
|
|
62
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://localhost:3210';
|
|
63
|
+
|
|
64
|
+
await createAsyncServerClient('user-123', { apiKey: 'test-key' });
|
|
65
|
+
|
|
66
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
67
|
+
const httpLinkOptions = config.links[0] as any;
|
|
68
|
+
|
|
69
|
+
expect(httpLinkOptions.url).toBe('http://localhost:3210/trpc/async');
|
|
70
|
+
expect(httpLinkOptions.url).not.toContain('public.example.com');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should fall back to APP_URL when INTERNAL_APP_URL is not set in env', async () => {
|
|
74
|
+
// Simulating the result of getInternalAppUrl() when INTERNAL_APP_URL is not in env
|
|
75
|
+
// In this case, appEnv.INTERNAL_APP_URL would equal appEnv.APP_URL
|
|
76
|
+
mockAppEnv.APP_URL = 'https://fallback.example.com';
|
|
77
|
+
mockAppEnv.INTERNAL_APP_URL = 'https://fallback.example.com'; // getInternalAppUrl() returns APP_URL
|
|
78
|
+
|
|
79
|
+
await createAsyncServerClient('user-456', {});
|
|
80
|
+
|
|
81
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
82
|
+
const httpLinkOptions = config.links[0] as any;
|
|
83
|
+
|
|
84
|
+
expect(httpLinkOptions.url).toBe('https://fallback.example.com/trpc/async');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should use localhost to bypass CDN proxy', async () => {
|
|
88
|
+
mockAppEnv.APP_URL = 'https://cdn-proxied.example.com';
|
|
89
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://127.0.0.1:3210';
|
|
90
|
+
|
|
91
|
+
await createAsyncServerClient('user-789', {});
|
|
92
|
+
|
|
93
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
94
|
+
const httpLinkOptions = config.links[0] as any;
|
|
95
|
+
|
|
96
|
+
expect(httpLinkOptions.url).toBe('http://127.0.0.1:3210/trpc/async');
|
|
97
|
+
expect(httpLinkOptions.url).not.toContain('cdn-proxied');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should use internal service name in Docker network', async () => {
|
|
101
|
+
mockAppEnv.APP_URL = 'https://public.example.com';
|
|
102
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://lobe-service:3210';
|
|
103
|
+
|
|
104
|
+
await createAsyncServerClient('user-docker', {});
|
|
105
|
+
|
|
106
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
107
|
+
const httpLinkOptions = config.links[0] as any;
|
|
108
|
+
|
|
109
|
+
expect(httpLinkOptions.url).toBe('http://lobe-service:3210/trpc/async');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should handle INTERNAL_APP_URL with trailing slash', async () => {
|
|
113
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://localhost:3210/';
|
|
114
|
+
|
|
115
|
+
await createAsyncServerClient('user-trailing', {});
|
|
116
|
+
|
|
117
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
118
|
+
const httpLinkOptions = config.links[0] as any;
|
|
119
|
+
|
|
120
|
+
// urlJoin should normalize the trailing slash
|
|
121
|
+
expect(httpLinkOptions.url).toBe('http://localhost:3210/trpc/async');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should handle INTERNAL_APP_URL without trailing slash', async () => {
|
|
125
|
+
mockAppEnv.INTERNAL_APP_URL = 'https://example.com';
|
|
126
|
+
|
|
127
|
+
await createAsyncServerClient('user-no-trailing', {});
|
|
128
|
+
|
|
129
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
130
|
+
const httpLinkOptions = config.links[0] as any;
|
|
131
|
+
|
|
132
|
+
expect(httpLinkOptions.url).toBe('https://example.com/trpc/async');
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('authentication and headers', () => {
|
|
137
|
+
it('should include Authorization header with KEY_VAULTS_SECRET', async () => {
|
|
138
|
+
await createAsyncServerClient('user-auth', {});
|
|
139
|
+
|
|
140
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
141
|
+
const httpLinkOptions = config.links[0] as any;
|
|
142
|
+
|
|
143
|
+
expect(httpLinkOptions.headers).toHaveProperty('Authorization');
|
|
144
|
+
expect(httpLinkOptions.headers.Authorization).toBe('Bearer test-secret-key');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should encrypt and include user payload in x-lobe-chat-auth header', async () => {
|
|
148
|
+
const testPayload = { apiKey: 'test-api-key-value', provider: 'openai' };
|
|
149
|
+
const mockEncrypt = vi.fn().mockResolvedValue('test-encrypted-auth-data');
|
|
150
|
+
vi.mocked(KeyVaultsGateKeeper.initWithEnvKey).mockResolvedValueOnce({
|
|
151
|
+
encrypt: mockEncrypt,
|
|
152
|
+
} as any);
|
|
153
|
+
|
|
154
|
+
await createAsyncServerClient('user-encrypt', testPayload);
|
|
155
|
+
|
|
156
|
+
expect(KeyVaultsGateKeeper.initWithEnvKey).toHaveBeenCalled();
|
|
157
|
+
expect(mockEncrypt).toHaveBeenCalledWith(
|
|
158
|
+
JSON.stringify({ payload: testPayload, userId: 'user-encrypt' }),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
162
|
+
const httpLinkOptions = config.links[0] as any;
|
|
163
|
+
|
|
164
|
+
// The header name is from LOBE_CHAT_AUTH_HEADER constant
|
|
165
|
+
expect(httpLinkOptions.headers).toHaveProperty('Authorization');
|
|
166
|
+
// The X-lobe-chat-auth header should be present
|
|
167
|
+
expect(Object.keys(httpLinkOptions.headers)).toContain('X-lobe-chat-auth');
|
|
168
|
+
expect(httpLinkOptions.headers['X-lobe-chat-auth']).toBe('test-encrypted-auth-data');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should include Vercel bypass secret when available', async () => {
|
|
172
|
+
const originalEnv = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;
|
|
173
|
+
process.env.VERCEL_AUTOMATION_BYPASS_SECRET = 'test-bypass-value';
|
|
174
|
+
|
|
175
|
+
await createAsyncServerClient('user-vercel', {});
|
|
176
|
+
|
|
177
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
178
|
+
const httpLinkOptions = config.links[0] as any;
|
|
179
|
+
|
|
180
|
+
expect(httpLinkOptions.headers).toHaveProperty('x-vercel-protection-bypass');
|
|
181
|
+
expect(httpLinkOptions.headers['x-vercel-protection-bypass']).toBe('test-bypass-value');
|
|
182
|
+
|
|
183
|
+
// Restore original env
|
|
184
|
+
if (originalEnv) {
|
|
185
|
+
process.env.VERCEL_AUTOMATION_BYPASS_SECRET = originalEnv;
|
|
186
|
+
} else {
|
|
187
|
+
delete process.env.VERCEL_AUTOMATION_BYPASS_SECRET;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should not include Vercel bypass secret when not available', async () => {
|
|
192
|
+
delete process.env.VERCEL_AUTOMATION_BYPASS_SECRET;
|
|
193
|
+
|
|
194
|
+
await createAsyncServerClient('user-no-vercel', {});
|
|
195
|
+
|
|
196
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
197
|
+
const httpLinkOptions = config.links[0] as any;
|
|
198
|
+
|
|
199
|
+
expect(httpLinkOptions.headers).not.toHaveProperty('x-vercel-protection-bypass');
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('error handling', () => {
|
|
204
|
+
it('should handle encryption failure gracefully', async () => {
|
|
205
|
+
const mockEncrypt = vi.fn().mockRejectedValueOnce(new Error('Encryption failed'));
|
|
206
|
+
vi.mocked(KeyVaultsGateKeeper.initWithEnvKey).mockResolvedValueOnce({
|
|
207
|
+
encrypt: mockEncrypt,
|
|
208
|
+
} as any);
|
|
209
|
+
|
|
210
|
+
await expect(createAsyncServerClient('user-enc-fail', {})).rejects.toThrow(
|
|
211
|
+
'Encryption failed',
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
expect(KeyVaultsGateKeeper.initWithEnvKey).toHaveBeenCalled();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should handle missing INTERNAL_APP_URL by using APP_URL', async () => {
|
|
218
|
+
// When INTERNAL_APP_URL is not set in env, getInternalAppUrl() returns APP_URL
|
|
219
|
+
mockAppEnv.APP_URL = 'https://only-app-url.com';
|
|
220
|
+
mockAppEnv.INTERNAL_APP_URL = 'https://only-app-url.com'; // Result of fallback
|
|
221
|
+
|
|
222
|
+
await createAsyncServerClient('user-null', {});
|
|
223
|
+
|
|
224
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
225
|
+
const httpLinkOptions = config.links[0] as any;
|
|
226
|
+
|
|
227
|
+
// Should use APP_URL when INTERNAL_APP_URL is not set in environment
|
|
228
|
+
expect(httpLinkOptions.url).toContain('only-app-url.com');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should handle empty string INTERNAL_APP_URL', async () => {
|
|
232
|
+
mockAppEnv.APP_URL = 'https://fallback-from-empty.com';
|
|
233
|
+
mockAppEnv.INTERNAL_APP_URL = '';
|
|
234
|
+
|
|
235
|
+
await createAsyncServerClient('user-empty', {});
|
|
236
|
+
|
|
237
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
238
|
+
const httpLinkOptions = config.links[0] as any;
|
|
239
|
+
|
|
240
|
+
// Empty string is falsy, so urlJoin will use it but result may vary
|
|
241
|
+
expect(httpLinkOptions.url).toBeDefined();
|
|
242
|
+
expect(httpLinkOptions.url).toContain('trpc/async');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should handle malformed URL gracefully', async () => {
|
|
246
|
+
mockAppEnv.INTERNAL_APP_URL = 'not-a-valid-url';
|
|
247
|
+
|
|
248
|
+
await createAsyncServerClient('user-malformed', {});
|
|
249
|
+
|
|
250
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
251
|
+
const httpLinkOptions = config.links[0] as any;
|
|
252
|
+
|
|
253
|
+
// urlJoin will still create a result, even if base is malformed
|
|
254
|
+
expect(httpLinkOptions.url).toBeDefined();
|
|
255
|
+
expect(httpLinkOptions.url).toContain('trpc/async');
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
describe('TRPC client configuration', () => {
|
|
260
|
+
it('should configure httpLink with proper options', async () => {
|
|
261
|
+
await createAsyncServerClient('user-config', {});
|
|
262
|
+
|
|
263
|
+
expect(httpLink).toHaveBeenCalled();
|
|
264
|
+
const httpLinkOptions = vi.mocked(httpLink).mock.calls[0][0];
|
|
265
|
+
|
|
266
|
+
expect(httpLinkOptions).toHaveProperty('url');
|
|
267
|
+
expect(httpLinkOptions).toHaveProperty('headers');
|
|
268
|
+
expect(httpLinkOptions).toHaveProperty('transformer');
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('should pass httpLink result to createTRPCClient', async () => {
|
|
272
|
+
await createAsyncServerClient('user-link', {});
|
|
273
|
+
|
|
274
|
+
expect(createTRPCClient).toHaveBeenCalledWith({
|
|
275
|
+
links: expect.arrayContaining([
|
|
276
|
+
expect.objectContaining({
|
|
277
|
+
url: expect.any(String),
|
|
278
|
+
headers: expect.any(Object),
|
|
279
|
+
}),
|
|
280
|
+
]),
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('should return the created TRPC client', async () => {
|
|
285
|
+
const client = await createAsyncServerClient('user-return', {});
|
|
286
|
+
|
|
287
|
+
expect(client).toBeDefined();
|
|
288
|
+
expect(client).toHaveProperty('_mockClient');
|
|
289
|
+
expect((client as any)._mockClient).toBe(true);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
describe('real-world scenarios', () => {
|
|
294
|
+
it('should handle production deployment behind Cloudflare', async () => {
|
|
295
|
+
mockAppEnv.APP_URL = 'https://lobechat.example.com';
|
|
296
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://localhost:3210';
|
|
297
|
+
|
|
298
|
+
await createAsyncServerClient('prod-user', { apiKey: 'test-key' });
|
|
299
|
+
|
|
300
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
301
|
+
const httpLinkOptions = config.links[0] as any;
|
|
302
|
+
|
|
303
|
+
// Should use localhost to avoid CDN timeout
|
|
304
|
+
expect(httpLinkOptions.url).toBe('http://localhost:3210/trpc/async');
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('should handle Docker Compose deployment with service names', async () => {
|
|
308
|
+
mockAppEnv.APP_URL = 'https://public.example.com';
|
|
309
|
+
mockAppEnv.INTERNAL_APP_URL = 'http://lobe-chat-database:3210';
|
|
310
|
+
|
|
311
|
+
await createAsyncServerClient('docker-user', {});
|
|
312
|
+
|
|
313
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
314
|
+
const httpLinkOptions = config.links[0] as any;
|
|
315
|
+
|
|
316
|
+
expect(httpLinkOptions.url).toBe('http://lobe-chat-database:3210/trpc/async');
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('should handle deployment without CDN (INTERNAL_APP_URL not set)', async () => {
|
|
320
|
+
// When not using CDN, INTERNAL_APP_URL is not set, so it falls back to APP_URL
|
|
321
|
+
mockAppEnv.APP_URL = 'https://direct-access.example.com';
|
|
322
|
+
mockAppEnv.INTERNAL_APP_URL = 'https://direct-access.example.com'; // Result of getInternalAppUrl() fallback
|
|
323
|
+
|
|
324
|
+
await createAsyncServerClient('direct-user', {});
|
|
325
|
+
|
|
326
|
+
const config = vi.mocked(createTRPCClient).mock.calls[0][0];
|
|
327
|
+
const httpLinkOptions = config.links[0] as any;
|
|
328
|
+
|
|
329
|
+
// Should fallback to APP_URL
|
|
330
|
+
expect(httpLinkOptions.url).toBe('https://direct-access.example.com/trpc/async');
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
});
|
|
@@ -30,7 +30,8 @@ export const createAsyncServerClient = async (userId: string, payload: ClientSec
|
|
|
30
30
|
httpLink({
|
|
31
31
|
headers,
|
|
32
32
|
transformer: superjson,
|
|
33
|
-
|
|
33
|
+
// Use INTERNAL_APP_URL for server-to-server calls to bypass CDN/proxy
|
|
34
|
+
url: urlJoin(appEnv.INTERNAL_APP_URL!, '/trpc/async'),
|
|
34
35
|
}),
|
|
35
36
|
],
|
|
36
37
|
});
|