forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-url.test.ts
More file actions
127 lines (105 loc) · 4.34 KB
/
Copy pathbuild-url.test.ts
File metadata and controls
127 lines (105 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { describe, it, expect } from 'vitest';
// buildUrl is not exported, so we test it indirectly via createFetchHttpClient
// or we can extract and test it if it were exported. Since it's private,
// we test the URL construction behavior through the HTTP client.
// However, issue #33 specifically asks to "Test buildUrl query serialization".
// Let's check if buildUrl is exported or if we need to test via integration.
// Actually, looking at fetch-http-client.ts, buildUrl is a module-private function.
// The bounty likely expects us to either export it for testing or test its effects.
// Given the pattern of other bounties, let's test the observable behavior:
// that query parameters are correctly serialized into the URL passed to fetch.
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import type { ResolvedLilySdkConfig } from '../src/config/types';
import { vi } from 'vitest';
function createMockConfig(overrides: Partial<ResolvedLilySdkConfig> = {}): ResolvedLilySdkConfig {
return {
baseUrl: new URL('https://api.example.com/v1/'),
apiKey: 'test-key',
authToken: undefined,
userAgent: 'lily-sdk/test',
defaultHeaders: {},
timeoutMs: 5000,
retry: { retries: 0, retryDelayMs: 100, retryableStatusCodes: [] },
fetch: vi.fn().mockResolvedValue({
ok: true,
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
json: async () => ({}),
text: async () => '{}',
}),
...overrides,
} as unknown as ResolvedLilySdkConfig;
}
describe('buildUrl query serialization', () => {
it('serializes string query parameters', async () => {
const config = createMockConfig();
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/agents',
query: { status: 'active', network: 'stellar-testnet' },
});
const calledUrl = vi.mocked(config.fetch).mock.calls[0][0] as URL;
expect(calledUrl.searchParams.get('status')).toBe('active');
expect(calledUrl.searchParams.get('network')).toBe('stellar-testnet');
});
it('serializes numeric query parameters as strings', async () => {
const config = createMockConfig();
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/agents',
query: { limit: 10, offset: 20 },
});
const calledUrl = vi.mocked(config.fetch).mock.calls[0][0] as URL;
expect(calledUrl.searchParams.get('limit')).toBe('10');
expect(calledUrl.searchParams.get('offset')).toBe('20');
});
it('serializes boolean query parameters as strings', async () => {
const config = createMockConfig();
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/agents',
query: { active: true, deleted: false },
});
const calledUrl = vi.mocked(config.fetch).mock.calls[0][0] as URL;
expect(calledUrl.searchParams.get('active')).toBe('true');
expect(calledUrl.searchParams.get('deleted')).toBe('false');
});
it('omits undefined query parameters', async () => {
const config = createMockConfig();
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/agents',
query: { status: 'active', network: undefined },
});
const calledUrl = vi.mocked(config.fetch).mock.calls[0][0] as URL;
expect(calledUrl.searchParams.has('status')).toBe(true);
expect(calledUrl.searchParams.has('network')).toBe(false);
});
it('handles empty query object without adding ?', async () => {
const config = createMockConfig();
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/agents',
query: {},
});
const calledUrl = vi.mocked(config.fetch).mock.calls[0][0] as URL;
expect(calledUrl.search).toBe('');
});
it('combines base URL path with request path correctly', async () => {
const config = createMockConfig({ baseUrl: new URL('https://api.example.com/v1/') });
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/agents',
query: { limit: 5 },
});
const calledUrl = vi.mocked(config.fetch).mock.calls[0][0] as URL;
expect(calledUrl.pathname).toBe('/v1/agents');
expect(calledUrl.origin).toBe('https://api.example.com');
});
});