-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.test.ts
More file actions
297 lines (268 loc) · 11.3 KB
/
Copy pathclient.test.ts
File metadata and controls
297 lines (268 loc) · 11.3 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import {
describe,
it,
expect,
beforeEach,
afterEach,
vi,
type Mock,
} from 'vitest';
import { EngineServicesClient } from './client';
const API = 'https://api.example.com';
const TOKEN = 'test-token';
function okResponse(data: unknown): Response {
return {
ok: true,
status: 200,
statusText: 'OK',
text: async () => JSON.stringify(data),
json: async () => data,
} as unknown as Response;
}
function errorResponse(status: number, message = 'Bad Request'): Response {
return {
ok: false,
status,
statusText: message,
text: async () => message,
json: async () => ({ message }),
} as unknown as Response;
}
function getCall(
fetchMock: Mock,
index = 0,
): { url: string; init: RequestInit } {
const call = fetchMock.mock.calls[index];
return { url: call[0] as string, init: call[1] as RequestInit };
}
function parseUrl(url: string): { pathname: string; params: URLSearchParams } {
const u = new URL(url);
return { pathname: u.pathname, params: u.searchParams };
}
describe('EngineServicesClient — HTTP contract', () => {
let fetchMock: Mock;
beforeEach(() => {
fetchMock = vi.fn();
globalThis.fetch = fetchMock as unknown as typeof fetch;
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('auth mode', () => {
it('access-token mode puts token in query string', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listFiles();
const { url, init } = getCall(fetchMock);
const { params } = parseUrl(url);
expect(params.get('accessToken')).toBe(TOKEN);
expect(
(init.headers as Record<string, string>).Authorization,
).toBeUndefined();
});
it('bearer mode sets Authorization header and omits accessToken query param', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API, { useBearer: true });
await client.listFiles();
const { url, init } = getCall(fetchMock);
const { params } = parseUrl(url);
expect(params.get('accessToken')).toBeNull();
expect((init.headers as Record<string, string>).Authorization).toBe(
`Bearer ${TOKEN}`,
);
});
});
describe('executeComponent', () => {
it('POSTs to /processor/:id/execute with JSON body including projectId when supplied', async () => {
fetchMock.mockResolvedValue(okResponse({ executionId: 'exec-1' }));
const client = new EngineServicesClient(TOKEN, API);
const result = await client.executeComponent(
'comp-42',
{ projectId: 'proj-99', foo: 'bar' },
'v1',
);
expect(result).toEqual({ executionId: 'exec-1' });
const { url, init } = getCall(fetchMock);
const { pathname, params } = parseUrl(url);
expect(pathname).toBe('/api/processor/comp-42/execute');
expect(init.method).toBe('POST');
expect(params.get('versionTag')).toBe('v1');
expect(init.body).toBe(
JSON.stringify({ projectId: 'proj-99', foo: 'bar' }),
);
});
it('omits versionTag from query when not supplied', async () => {
fetchMock.mockResolvedValue(okResponse({ executionId: 'exec-2' }));
const client = new EngineServicesClient(TOKEN, API);
await client.executeComponent('comp-42', {});
const { url } = getCall(fetchMock);
const { params } = parseUrl(url);
expect(params.get('versionTag')).toBeNull();
});
});
describe('listExecutions', () => {
it('passes projectId as a query parameter when provided', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listExecutions('comp-1', 'proj-1');
const { url } = getCall(fetchMock);
const { pathname, params } = parseUrl(url);
expect(pathname).toBe('/api/processor/comp-1/progress');
expect(params.get('projectId')).toBe('proj-1');
});
it('omits projectId when not supplied', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listExecutions('comp-1');
const { url } = getCall(fetchMock);
const { params } = parseUrl(url);
expect(params.get('projectId')).toBeNull();
});
});
// `checkPermission` and `checkPermissionBatch` live on `PlatformClient`
// (JWT-only routes) — their contract tests are in `platform-client.test.ts`.
describe('project-scoped list methods — via projectId query on /item and /item/folder', () => {
it('listFiles({ projectId }) forwards projectId on /item', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listFiles({ projectId: 'proj-1', archived: true });
const { url, init } = getCall(fetchMock);
const { pathname, params } = parseUrl(url);
expect(pathname).toBe('/api/item');
expect(init.method).toBe('GET');
expect(params.get('itemType')).toBe('FILE');
expect(params.get('projectId')).toBe('proj-1');
expect(params.get('archived')).toBe('true');
});
it('listFolders({ projectId }) forwards projectId on /item/folder', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listFolders({ projectId: 'proj-1' });
const { url, init } = getCall(fetchMock);
const { pathname, params } = parseUrl(url);
expect(pathname).toBe('/api/item/folder');
expect(init.method).toBe('GET');
expect(params.get('projectId')).toBe('proj-1');
});
it('listApps({ projectId }) forwards projectId on /item', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listApps({ projectId: 'proj-1' });
const { url, params } = {
...getCall(fetchMock),
...parseUrl(getCall(fetchMock).url),
};
expect(url).toMatch(/\/api\/item\b/);
expect(params.get('itemType')).toBe('APP');
expect(params.get('projectId')).toBe('proj-1');
});
it('listComponents({ projectId }) forwards projectId on /item', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listComponents({ projectId: 'proj-1' });
const { params } = parseUrl(getCall(fetchMock).url);
expect(params.get('itemType')).toBe('TOOL');
expect(params.get('projectId')).toBe('proj-1');
});
});
describe('createFile / createFolder / createComponent / createApp pass projectId', () => {
it('createFolder POSTs projectId in JSON body', async () => {
fetchMock.mockResolvedValue(okResponse({}));
const client = new EngineServicesClient(TOKEN, API);
await client.createFolder('My folder', undefined, 'proj-1');
const { url, init } = getCall(fetchMock);
const { pathname } = parseUrl(url);
expect(pathname).toBe('/api/item/folder');
expect(init.method).toBe('POST');
const body = JSON.parse(init.body as string);
expect(body).toMatchObject({ name: 'My folder', projectId: 'proj-1' });
});
it('createFile attaches projectId to the FormData body', async () => {
fetchMock.mockResolvedValue(okResponse({}));
const client = new EngineServicesClient(TOKEN, API);
const file = new Blob(['dummy']) as Blob;
await client.createFile({
file,
name: 'doc.ifc',
versionTag: 'v1',
projectId: 'proj-1',
});
const { init } = getCall(fetchMock);
const formData = init.body as FormData;
expect(formData).toBeInstanceOf(FormData);
expect(formData.get('projectId')).toBe('proj-1');
expect(formData.get('itemType')).toBe('FILE');
});
});
describe('error handling', () => {
it('throws when the server responds with a non-2xx status', async () => {
fetchMock.mockResolvedValue(errorResponse(403, 'Forbidden'));
const client = new EngineServicesClient(TOKEN, API);
await expect(
client.executeComponent('comp-1', { projectId: 'foreign' }),
).rejects.toThrow(/403/);
});
});
describe('version archive / recover / delete', () => {
it('listVersions GETs /item/:id/versions and forwards archived filter', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listVersions('item-1', { archived: true });
const { url, init } = getCall(fetchMock);
const { pathname, params } = parseUrl(url);
expect(init.method).toBe('GET');
expect(pathname).toBe('/api/item/item-1/versions');
expect(params.get('archived')).toBe('true');
});
it('listVersions omits archived param when not provided', async () => {
fetchMock.mockResolvedValue(okResponse([]));
const client = new EngineServicesClient(TOKEN, API);
await client.listVersions('item-1');
const { params } = parseUrl(getCall(fetchMock).url);
expect(params.get('archived')).toBeNull();
});
it('archiveVersion PUTs /item/:id/version/:tag/archive', async () => {
fetchMock.mockResolvedValue(okResponse({ tag: 'v2', archived: true }));
const client = new EngineServicesClient(TOKEN, API);
await client.archiveVersion('item-1', 'v2');
const { url, init } = getCall(fetchMock);
const { pathname } = parseUrl(url);
expect(init.method).toBe('PUT');
expect(pathname).toBe('/api/item/item-1/version/v2/archive');
});
it('recoverVersion PUTs /item/:id/version/:tag/recover', async () => {
fetchMock.mockResolvedValue(okResponse({ tag: 'v2', archived: false }));
const client = new EngineServicesClient(TOKEN, API);
await client.recoverVersion('item-1', 'v2');
const { url, init } = getCall(fetchMock);
const { pathname } = parseUrl(url);
expect(init.method).toBe('PUT');
expect(pathname).toBe('/api/item/item-1/version/v2/recover');
});
it('deleteVersion DELETEs /item/:id/version/:tag', async () => {
fetchMock.mockResolvedValue(okResponse({ success: true }));
const client = new EngineServicesClient(TOKEN, API);
await client.deleteVersion('item-1', 'v2');
const { url, init } = getCall(fetchMock);
const { pathname } = parseUrl(url);
expect(init.method).toBe('DELETE');
expect(pathname).toBe('/api/item/item-1/version/v2');
});
it('archiveVersion in bearer mode uses Authorization header', async () => {
fetchMock.mockResolvedValue(okResponse({ tag: 'v2', archived: true }));
const client = new EngineServicesClient(TOKEN, API, { useBearer: true });
await client.archiveVersion('item-1', 'v2');
const { url, init } = getCall(fetchMock);
const { params } = parseUrl(url);
expect(params.get('accessToken')).toBeNull();
expect((init.headers as Record<string, string>).Authorization).toBe(
`Bearer ${TOKEN}`,
);
});
it('deleteVersion throws when the server responds with a non-2xx', async () => {
fetchMock.mockResolvedValue(errorResponse(404, 'Not Found'));
const client = new EngineServicesClient(TOKEN, API);
await expect(client.deleteVersion('item-1', 'v2')).rejects.toThrow(/404/);
});
});
});