@orchagent/cli 0.3.33 → 0.3.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.
@@ -1,330 +0,0 @@
1
- "use strict";
2
- /**
3
- * Tests for the run command and LLM utilities.
4
- *
5
- * These tests cover downloading and running agents locally:
6
- * - parseAgentRef() parsing org/agent@version formats
7
- * - downloadAgent() fetching from API
8
- * - LLM key detection from environment
9
- * - Building prompts with variable substitution
10
- */
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const vitest_1 = require("vitest");
16
- const commander_1 = require("commander");
17
- // Mock modules before importing
18
- vitest_1.vi.mock('fs/promises');
19
- vitest_1.vi.mock('../lib/config');
20
- vitest_1.vi.mock('../lib/api');
21
- const promises_1 = __importDefault(require("fs/promises"));
22
- const run_1 = require("./run");
23
- const config_1 = require("../lib/config");
24
- const api_1 = require("../lib/api");
25
- const llm_1 = require("../lib/llm");
26
- const mockFs = vitest_1.vi.mocked(promises_1.default);
27
- const mockGetResolvedConfig = vitest_1.vi.mocked(config_1.getResolvedConfig);
28
- const mockPublicRequest = vitest_1.vi.mocked(api_1.publicRequest);
29
- const mockGetPublicAgent = vitest_1.vi.mocked(api_1.getPublicAgent);
30
- (0, vitest_1.describe)('run command - agent ref parsing', () => {
31
- let program;
32
- let stdoutSpy;
33
- let stderrSpy;
34
- (0, vitest_1.beforeEach)(() => {
35
- vitest_1.vi.clearAllMocks();
36
- program = new commander_1.Command();
37
- program.exitOverride();
38
- (0, run_1.registerRunCommand)(program);
39
- stdoutSpy = vitest_1.vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
40
- stderrSpy = vitest_1.vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
41
- mockGetResolvedConfig.mockResolvedValue({
42
- apiKey: 'sk_test_123',
43
- apiUrl: 'https://api.test.com',
44
- defaultOrg: 'default-org',
45
- });
46
- // Mock mkdir for agent saving
47
- mockFs.mkdir.mockResolvedValue(undefined);
48
- mockFs.writeFile.mockResolvedValue(undefined);
49
- });
50
- (0, vitest_1.afterEach)(() => {
51
- stdoutSpy.mockRestore();
52
- stderrSpy.mockRestore();
53
- vitest_1.vi.restoreAllMocks();
54
- });
55
- (0, vitest_1.it)('parses org/agent@version format', async () => {
56
- mockPublicRequest.mockResolvedValue({
57
- type: 'code',
58
- name: 'my-agent',
59
- version: 'v2',
60
- supported_providers: ['any'],
61
- });
62
- await program.parseAsync(['node', 'test', 'run', 'myorg/my-agent@v2']);
63
- (0, vitest_1.expect)(mockPublicRequest).toHaveBeenCalledWith(vitest_1.expect.any(Object), '/public/agents/myorg/my-agent/v2/download');
64
- });
65
- (0, vitest_1.it)('parses org/agent format with default version', async () => {
66
- mockPublicRequest.mockResolvedValue({
67
- type: 'code',
68
- name: 'my-agent',
69
- version: 'latest',
70
- supported_providers: ['any'],
71
- });
72
- await program.parseAsync(['node', 'test', 'run', 'myorg/my-agent']);
73
- (0, vitest_1.expect)(mockPublicRequest).toHaveBeenCalledWith(vitest_1.expect.any(Object), '/public/agents/myorg/my-agent/latest/download');
74
- });
75
- (0, vitest_1.it)('uses defaultOrg when no org specified', async () => {
76
- mockPublicRequest.mockResolvedValue({
77
- type: 'code',
78
- name: 'my-agent',
79
- version: 'latest',
80
- supported_providers: ['any'],
81
- });
82
- await program.parseAsync(['node', 'test', 'run', 'my-agent']);
83
- (0, vitest_1.expect)(mockPublicRequest).toHaveBeenCalledWith(vitest_1.expect.any(Object), '/public/agents/default-org/my-agent/latest/download');
84
- });
85
- (0, vitest_1.it)('parses agent@version format', async () => {
86
- mockPublicRequest.mockResolvedValue({
87
- type: 'code',
88
- name: 'my-agent',
89
- version: 'v3',
90
- supported_providers: ['any'],
91
- });
92
- await program.parseAsync(['node', 'test', 'run', 'my-agent@v3']);
93
- (0, vitest_1.expect)(mockPublicRequest).toHaveBeenCalledWith(vitest_1.expect.any(Object), '/public/agents/default-org/my-agent/v3/download');
94
- });
95
- (0, vitest_1.it)('throws error for invalid agent ref format', async () => {
96
- mockGetResolvedConfig.mockResolvedValue({
97
- apiUrl: 'https://api.test.com',
98
- // No defaultOrg
99
- });
100
- await (0, vitest_1.expect)(program.parseAsync(['node', 'test', 'run', 'just-agent'])).rejects.toThrow('Missing org');
101
- });
102
- (0, vitest_1.it)('throws error for too many segments', async () => {
103
- await (0, vitest_1.expect)(program.parseAsync(['node', 'test', 'run', 'a/b/c/d'])).rejects.toThrow('Invalid agent reference');
104
- });
105
- });
106
- (0, vitest_1.describe)('run command - download agent', () => {
107
- let program;
108
- let stdoutSpy;
109
- let stderrSpy;
110
- (0, vitest_1.beforeEach)(() => {
111
- vitest_1.vi.clearAllMocks();
112
- program = new commander_1.Command();
113
- program.exitOverride();
114
- (0, run_1.registerRunCommand)(program);
115
- stdoutSpy = vitest_1.vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
116
- stderrSpy = vitest_1.vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
117
- mockGetResolvedConfig.mockResolvedValue({
118
- apiKey: 'sk_test_123',
119
- apiUrl: 'https://api.test.com',
120
- defaultOrg: 'test-org',
121
- });
122
- mockFs.mkdir.mockResolvedValue(undefined);
123
- mockFs.writeFile.mockResolvedValue(undefined);
124
- });
125
- (0, vitest_1.afterEach)(() => {
126
- stdoutSpy.mockRestore();
127
- stderrSpy.mockRestore();
128
- vitest_1.vi.restoreAllMocks();
129
- });
130
- (0, vitest_1.it)('downloads agent via public API', async () => {
131
- mockPublicRequest.mockResolvedValue({
132
- type: 'prompt',
133
- name: 'test-agent',
134
- version: 'v1',
135
- description: 'A test agent',
136
- prompt: 'You are a test assistant.',
137
- supported_providers: ['openai'],
138
- });
139
- await program.parseAsync(['node', 'test', 'run', 'test-org/test-agent@v1']);
140
- (0, vitest_1.expect)(mockPublicRequest).toHaveBeenCalledWith({ apiKey: 'sk_test_123', apiUrl: 'https://api.test.com', defaultOrg: 'test-org' }, '/public/agents/test-org/test-agent/v1/download');
141
- });
142
- (0, vitest_1.it)('falls back to getPublicAgent if download fails', async () => {
143
- mockPublicRequest.mockRejectedValue(new Error('Not found'));
144
- mockGetPublicAgent.mockResolvedValue({
145
- id: 'agent-123',
146
- org_id: 'org-123',
147
- name: 'fallback-agent',
148
- version: 'v1',
149
- type: 'code',
150
- supported_providers: ['any'],
151
- is_public: true,
152
- });
153
- await program.parseAsync(['node', 'test', 'run', 'test-org/fallback-agent@v1']);
154
- (0, vitest_1.expect)(mockGetPublicAgent).toHaveBeenCalledWith(vitest_1.expect.any(Object), 'test-org', 'fallback-agent', 'v1');
155
- });
156
- (0, vitest_1.it)('saves agent metadata locally', async () => {
157
- mockPublicRequest.mockResolvedValue({
158
- type: 'prompt',
159
- name: 'saved-agent',
160
- version: 'v1',
161
- prompt: 'Test prompt',
162
- supported_providers: ['any'],
163
- });
164
- await program.parseAsync(['node', 'test', 'run', 'test-org/saved-agent@v1']);
165
- (0, vitest_1.expect)(mockFs.mkdir).toHaveBeenCalled();
166
- (0, vitest_1.expect)(mockFs.writeFile).toHaveBeenCalledWith(vitest_1.expect.stringContaining('agent.json'), vitest_1.expect.stringContaining('"name": "saved-agent"'));
167
- });
168
- (0, vitest_1.it)('saves prompt.md for prompt agents', async () => {
169
- mockPublicRequest.mockResolvedValue({
170
- type: 'prompt',
171
- name: 'prompt-agent',
172
- version: 'v1',
173
- prompt: 'You are a helpful assistant.',
174
- supported_providers: ['any'],
175
- });
176
- await program.parseAsync(['node', 'test', 'run', 'test-org/prompt-agent@v1']);
177
- (0, vitest_1.expect)(mockFs.writeFile).toHaveBeenCalledWith(vitest_1.expect.stringContaining('prompt.md'), 'You are a helpful assistant.');
178
- });
179
- (0, vitest_1.it)('handles --download-only flag', async () => {
180
- mockPublicRequest.mockResolvedValue({
181
- type: 'prompt',
182
- name: 'download-only-agent',
183
- version: 'v1',
184
- prompt: 'Test',
185
- supported_providers: ['any'],
186
- });
187
- await program.parseAsync([
188
- 'node',
189
- 'test',
190
- 'run',
191
- 'test-org/download-only-agent@v1',
192
- '--download-only',
193
- ]);
194
- (0, vitest_1.expect)(stdoutSpy).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Agent downloaded'));
195
- });
196
- });
197
- (0, vitest_1.describe)('LLM utilities - detectLlmKeyFromEnv', () => {
198
- const originalEnv = process.env;
199
- (0, vitest_1.beforeEach)(() => {
200
- process.env = { ...originalEnv };
201
- delete process.env.OPENAI_API_KEY;
202
- delete process.env.ANTHROPIC_API_KEY;
203
- delete process.env.GEMINI_API_KEY;
204
- });
205
- (0, vitest_1.afterEach)(() => {
206
- process.env = originalEnv;
207
- });
208
- (0, vitest_1.it)('returns null when no keys found', () => {
209
- const result = (0, llm_1.detectLlmKeyFromEnv)(['openai']);
210
- (0, vitest_1.expect)(result).toBeNull();
211
- });
212
- (0, vitest_1.it)('detects OpenAI key', () => {
213
- process.env.OPENAI_API_KEY = 'sk-openai-test';
214
- const result = (0, llm_1.detectLlmKeyFromEnv)(['openai']);
215
- (0, vitest_1.expect)(result).toEqual({ provider: 'openai', key: 'sk-openai-test' });
216
- });
217
- (0, vitest_1.it)('detects Anthropic key', () => {
218
- process.env.ANTHROPIC_API_KEY = 'sk-ant-test';
219
- const result = (0, llm_1.detectLlmKeyFromEnv)(['anthropic']);
220
- (0, vitest_1.expect)(result).toEqual({ provider: 'anthropic', key: 'sk-ant-test' });
221
- });
222
- (0, vitest_1.it)('detects Gemini key', () => {
223
- process.env.GEMINI_API_KEY = 'AIza-gemini-test';
224
- const result = (0, llm_1.detectLlmKeyFromEnv)(['gemini']);
225
- (0, vitest_1.expect)(result).toEqual({ provider: 'gemini', key: 'AIza-gemini-test' });
226
- });
227
- (0, vitest_1.it)('returns first matching provider in order', () => {
228
- process.env.ANTHROPIC_API_KEY = 'sk-ant-test';
229
- process.env.OPENAI_API_KEY = 'sk-openai-test';
230
- const result = (0, llm_1.detectLlmKeyFromEnv)(['openai', 'anthropic']);
231
- (0, vitest_1.expect)(result).toEqual({ provider: 'openai', key: 'sk-openai-test' });
232
- });
233
- (0, vitest_1.it)('handles "any" provider - checks all in order', () => {
234
- process.env.GEMINI_API_KEY = 'AIza-gemini-test';
235
- const result = (0, llm_1.detectLlmKeyFromEnv)(['any']);
236
- // 'any' should find the first available key
237
- (0, vitest_1.expect)(result?.key).toBe('AIza-gemini-test');
238
- });
239
- (0, vitest_1.it)('handles "any" provider - returns first found', () => {
240
- process.env.OPENAI_API_KEY = 'sk-openai-test';
241
- process.env.ANTHROPIC_API_KEY = 'sk-ant-test';
242
- const result = (0, llm_1.detectLlmKeyFromEnv)(['any']);
243
- // OpenAI is checked first in the provider order
244
- (0, vitest_1.expect)(result).toEqual({ provider: 'openai', key: 'sk-openai-test' });
245
- });
246
- (0, vitest_1.it)('skips unavailable providers', () => {
247
- process.env.ANTHROPIC_API_KEY = 'sk-ant-test';
248
- // No OpenAI key
249
- const result = (0, llm_1.detectLlmKeyFromEnv)(['openai', 'anthropic']);
250
- (0, vitest_1.expect)(result).toEqual({ provider: 'anthropic', key: 'sk-ant-test' });
251
- });
252
- });
253
- (0, vitest_1.describe)('LLM utilities - getDefaultModel', () => {
254
- (0, vitest_1.it)('returns default model for OpenAI', () => {
255
- (0, vitest_1.expect)((0, llm_1.getDefaultModel)('openai')).toBe('gpt-4o');
256
- });
257
- (0, vitest_1.it)('returns default model for Anthropic', () => {
258
- (0, vitest_1.expect)((0, llm_1.getDefaultModel)('anthropic')).toBe('claude-sonnet-4-20250514');
259
- });
260
- (0, vitest_1.it)('returns default model for Gemini', () => {
261
- (0, vitest_1.expect)((0, llm_1.getDefaultModel)('gemini')).toBe('gemini-1.5-pro');
262
- });
263
- (0, vitest_1.it)('returns gpt-4o for unknown provider', () => {
264
- (0, vitest_1.expect)((0, llm_1.getDefaultModel)('unknown')).toBe('gpt-4o');
265
- });
266
- });
267
- (0, vitest_1.describe)('LLM utilities - buildPrompt', () => {
268
- (0, vitest_1.it)('substitutes single variable', () => {
269
- const template = 'Analyze this: {{input}}';
270
- const result = (0, llm_1.buildPrompt)(template, { input: 'Hello world' });
271
- (0, vitest_1.expect)(result).toContain('Analyze this: Hello world');
272
- });
273
- (0, vitest_1.it)('substitutes multiple variables', () => {
274
- const template = 'Name: {{name}}, Age: {{age}}';
275
- const result = (0, llm_1.buildPrompt)(template, { name: 'Alice', age: 30 });
276
- (0, vitest_1.expect)(result).toContain('Name: Alice, Age: 30');
277
- });
278
- (0, vitest_1.it)('appends JSON input block', () => {
279
- const template = 'Process this';
280
- const result = (0, llm_1.buildPrompt)(template, { key: 'value' });
281
- (0, vitest_1.expect)(result).toContain('Input:');
282
- (0, vitest_1.expect)(result).toContain('```json');
283
- (0, vitest_1.expect)(result).toContain('"key": "value"');
284
- });
285
- (0, vitest_1.it)('handles empty input data', () => {
286
- const template = 'No inputs needed';
287
- const result = (0, llm_1.buildPrompt)(template, {});
288
- (0, vitest_1.expect)(result).toBe('No inputs needed');
289
- });
290
- (0, vitest_1.it)('handles multiple occurrences of same variable', () => {
291
- const template = '{{name}} said hello. {{name}} waved goodbye.';
292
- const result = (0, llm_1.buildPrompt)(template, { name: 'Bob' });
293
- (0, vitest_1.expect)(result).toContain('Bob said hello. Bob waved goodbye.');
294
- });
295
- (0, vitest_1.it)('preserves unused placeholders', () => {
296
- const template = 'Value: {{exists}}, Missing: {{missing}}';
297
- const result = (0, llm_1.buildPrompt)(template, { exists: 'here' });
298
- (0, vitest_1.expect)(result).toContain('Value: here');
299
- (0, vitest_1.expect)(result).toContain('{{missing}}');
300
- });
301
- (0, vitest_1.it)('handles complex nested objects in JSON', () => {
302
- const template = 'Process data';
303
- const inputData = {
304
- nested: { deep: { value: 42 } },
305
- array: [1, 2, 3],
306
- };
307
- const result = (0, llm_1.buildPrompt)(template, inputData);
308
- (0, vitest_1.expect)(result).toContain('"nested"');
309
- (0, vitest_1.expect)(result).toContain('"deep"');
310
- (0, vitest_1.expect)(result).toContain('"array"');
311
- });
312
- (0, vitest_1.it)('converts non-string values to strings', () => {
313
- const template = 'Count: {{count}}, Active: {{active}}';
314
- const result = (0, llm_1.buildPrompt)(template, { count: 42, active: true });
315
- (0, vitest_1.expect)(result).toContain('Count: 42');
316
- (0, vitest_1.expect)(result).toContain('Active: true');
317
- });
318
- });
319
- (0, vitest_1.describe)('LLM utilities - constants', () => {
320
- (0, vitest_1.it)('has correct provider env vars', () => {
321
- (0, vitest_1.expect)(llm_1.PROVIDER_ENV_VARS.openai).toBe('OPENAI_API_KEY');
322
- (0, vitest_1.expect)(llm_1.PROVIDER_ENV_VARS.anthropic).toBe('ANTHROPIC_API_KEY');
323
- (0, vitest_1.expect)(llm_1.PROVIDER_ENV_VARS.gemini).toBe('GEMINI_API_KEY');
324
- });
325
- (0, vitest_1.it)('has correct default models', () => {
326
- (0, vitest_1.expect)(llm_1.DEFAULT_MODELS.openai).toBe('gpt-4o');
327
- (0, vitest_1.expect)(llm_1.DEFAULT_MODELS.anthropic).toBe('claude-sonnet-4-20250514');
328
- (0, vitest_1.expect)(llm_1.DEFAULT_MODELS.gemini).toBe('gemini-1.5-pro');
329
- });
330
- });
@@ -1,230 +0,0 @@
1
- "use strict";
2
- /**
3
- * Tests for API client functions.
4
- *
5
- * These tests cover the core API client that all CLI commands depend on:
6
- * - Request building and authentication
7
- * - Error parsing
8
- * - Public vs authenticated requests
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- const vitest_1 = require("vitest");
12
- const api_1 = require("./api");
13
- // Mock fetch globally
14
- const mockFetch = vitest_1.vi.fn();
15
- global.fetch = mockFetch;
16
- (0, vitest_1.describe)('ApiError', () => {
17
- (0, vitest_1.it)('includes status code and message', () => {
18
- const error = new api_1.ApiError('Not found', 404);
19
- (0, vitest_1.expect)(error.message).toBe('Not found');
20
- (0, vitest_1.expect)(error.status).toBe(404);
21
- });
22
- (0, vitest_1.it)('includes optional payload', () => {
23
- const payload = { error: { code: 'NOT_FOUND' } };
24
- const error = new api_1.ApiError('Not found', 404, payload);
25
- (0, vitest_1.expect)(error.payload).toEqual(payload);
26
- });
27
- });
28
- (0, vitest_1.describe)('request', () => {
29
- const config = {
30
- apiKey: 'sk_test_123',
31
- apiUrl: 'https://api.test.com',
32
- };
33
- (0, vitest_1.beforeEach)(() => {
34
- mockFetch.mockReset();
35
- });
36
- (0, vitest_1.afterEach)(() => {
37
- vitest_1.vi.restoreAllMocks();
38
- });
39
- (0, vitest_1.it)('adds Authorization header with Bearer token', async () => {
40
- mockFetch.mockResolvedValueOnce({
41
- ok: true,
42
- json: () => Promise.resolve({ data: 'test' }),
43
- });
44
- await (0, api_1.request)(config, 'GET', '/test');
45
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('https://api.test.com/test', vitest_1.expect.objectContaining({
46
- method: 'GET',
47
- headers: vitest_1.expect.objectContaining({
48
- Authorization: 'Bearer sk_test_123',
49
- }),
50
- }));
51
- });
52
- (0, vitest_1.it)('returns parsed JSON response', async () => {
53
- const responseData = { org: 'test', slug: 'test-org' };
54
- mockFetch.mockResolvedValueOnce({
55
- ok: true,
56
- json: () => Promise.resolve(responseData),
57
- });
58
- const result = await (0, api_1.request)(config, 'GET', '/org');
59
- (0, vitest_1.expect)(result).toEqual(responseData);
60
- });
61
- (0, vitest_1.it)('throws ApiError when response not ok', async () => {
62
- mockFetch.mockResolvedValueOnce({
63
- ok: false,
64
- status: 404,
65
- statusText: 'Not Found',
66
- text: () => Promise.resolve(JSON.stringify({
67
- error: { message: 'Agent not found' }
68
- })),
69
- });
70
- await (0, vitest_1.expect)((0, api_1.request)(config, 'GET', '/agents/missing'))
71
- .rejects.toThrow(api_1.ApiError);
72
- });
73
- (0, vitest_1.it)('throws ApiError with 401 when no API key', async () => {
74
- const noKeyConfig = {
75
- apiKey: undefined,
76
- apiUrl: 'https://api.test.com',
77
- };
78
- await (0, vitest_1.expect)((0, api_1.request)(noKeyConfig, 'GET', '/test'))
79
- .rejects.toThrow('Missing API key');
80
- });
81
- (0, vitest_1.it)('parses error message from JSON response', async () => {
82
- mockFetch.mockResolvedValueOnce({
83
- ok: false,
84
- status: 403,
85
- statusText: 'Forbidden',
86
- text: () => Promise.resolve(JSON.stringify({
87
- error: { message: 'Access denied to private agent' }
88
- })),
89
- });
90
- try {
91
- await (0, api_1.request)(config, 'GET', '/agents/private');
92
- vitest_1.expect.fail('Should have thrown');
93
- }
94
- catch (error) {
95
- (0, vitest_1.expect)(error).toBeInstanceOf(api_1.ApiError);
96
- (0, vitest_1.expect)(error.message).toBe('Access denied to private agent');
97
- (0, vitest_1.expect)(error.status).toBe(403);
98
- }
99
- });
100
- (0, vitest_1.it)('uses statusText when no JSON message', async () => {
101
- mockFetch.mockResolvedValueOnce({
102
- ok: false,
103
- status: 500,
104
- statusText: 'Internal Server Error',
105
- text: () => Promise.resolve('not json'),
106
- });
107
- try {
108
- await (0, api_1.request)(config, 'GET', '/broken');
109
- vitest_1.expect.fail('Should have thrown');
110
- }
111
- catch (error) {
112
- (0, vitest_1.expect)(error).toBeInstanceOf(api_1.ApiError);
113
- (0, vitest_1.expect)(error.message).toBe('Internal Server Error');
114
- }
115
- });
116
- (0, vitest_1.it)('includes custom headers', async () => {
117
- mockFetch.mockResolvedValueOnce({
118
- ok: true,
119
- json: () => Promise.resolve({}),
120
- });
121
- await (0, api_1.request)(config, 'POST', '/agents', {
122
- body: JSON.stringify({ name: 'test' }),
123
- headers: { 'Content-Type': 'application/json' },
124
- });
125
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith(vitest_1.expect.any(String), vitest_1.expect.objectContaining({
126
- headers: vitest_1.expect.objectContaining({
127
- 'Content-Type': 'application/json',
128
- Authorization: 'Bearer sk_test_123',
129
- }),
130
- }));
131
- });
132
- (0, vitest_1.it)('strips trailing slash from API URL', async () => {
133
- const configWithSlash = {
134
- apiKey: 'sk_test_123',
135
- apiUrl: 'https://api.test.com/',
136
- };
137
- mockFetch.mockResolvedValueOnce({
138
- ok: true,
139
- json: () => Promise.resolve({}),
140
- });
141
- await (0, api_1.request)(configWithSlash, 'GET', '/test');
142
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('https://api.test.com/test', vitest_1.expect.any(Object));
143
- });
144
- });
145
- (0, vitest_1.describe)('publicRequest', () => {
146
- const config = {
147
- apiUrl: 'https://api.test.com',
148
- };
149
- (0, vitest_1.beforeEach)(() => {
150
- mockFetch.mockReset();
151
- });
152
- (0, vitest_1.it)('makes unauthenticated request', async () => {
153
- mockFetch.mockResolvedValueOnce({
154
- ok: true,
155
- json: () => Promise.resolve([]),
156
- });
157
- await (0, api_1.publicRequest)(config, '/public/agents');
158
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('https://api.test.com/public/agents');
159
- });
160
- (0, vitest_1.it)('returns parsed JSON', async () => {
161
- const agents = [{ name: 'agent1' }, { name: 'agent2' }];
162
- mockFetch.mockResolvedValueOnce({
163
- ok: true,
164
- json: () => Promise.resolve(agents),
165
- });
166
- const result = await (0, api_1.publicRequest)(config, '/public/agents');
167
- (0, vitest_1.expect)(result).toEqual(agents);
168
- });
169
- (0, vitest_1.it)('throws ApiError on failure', async () => {
170
- mockFetch.mockResolvedValueOnce({
171
- ok: false,
172
- status: 404,
173
- statusText: 'Not Found',
174
- text: () => Promise.resolve('{}'),
175
- });
176
- await (0, vitest_1.expect)((0, api_1.publicRequest)(config, '/public/agents/missing'))
177
- .rejects.toThrow(api_1.ApiError);
178
- });
179
- });
180
- (0, vitest_1.describe)('getOrg', () => {
181
- const config = {
182
- apiKey: 'sk_test_123',
183
- apiUrl: 'https://api.test.com',
184
- };
185
- (0, vitest_1.beforeEach)(() => {
186
- mockFetch.mockReset();
187
- });
188
- (0, vitest_1.it)('calls GET /org endpoint', async () => {
189
- const orgData = { id: '123', slug: 'test-org', name: 'Test Org' };
190
- mockFetch.mockResolvedValueOnce({
191
- ok: true,
192
- json: () => Promise.resolve(orgData),
193
- });
194
- const result = await (0, api_1.getOrg)(config);
195
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('https://api.test.com/org', vitest_1.expect.objectContaining({ method: 'GET' }));
196
- (0, vitest_1.expect)(result).toEqual(orgData);
197
- });
198
- });
199
- (0, vitest_1.describe)('searchAgents', () => {
200
- const config = {
201
- apiUrl: 'https://api.test.com',
202
- };
203
- (0, vitest_1.beforeEach)(() => {
204
- mockFetch.mockReset();
205
- });
206
- (0, vitest_1.it)('builds search query params', async () => {
207
- mockFetch.mockResolvedValueOnce({
208
- ok: true,
209
- json: () => Promise.resolve([]),
210
- });
211
- await (0, api_1.searchAgents)(config, 'pdf analyzer', { sort: 'stars' });
212
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('https://api.test.com/public/agents?search=pdf+analyzer&sort=stars');
213
- });
214
- (0, vitest_1.it)('handles empty query', async () => {
215
- mockFetch.mockResolvedValueOnce({
216
- ok: true,
217
- json: () => Promise.resolve([]),
218
- });
219
- await (0, api_1.searchAgents)(config, '');
220
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('https://api.test.com/public/agents');
221
- });
222
- (0, vitest_1.it)('includes tags in query', async () => {
223
- mockFetch.mockResolvedValueOnce({
224
- ok: true,
225
- json: () => Promise.resolve([]),
226
- });
227
- await (0, api_1.searchAgents)(config, 'test', { tags: ['ai', 'document'] });
228
- (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith(vitest_1.expect.stringContaining('tags=ai%2Cdocument'));
229
- });
230
- });