tuturuuu 0.0.1

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.
@@ -0,0 +1,523 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { NotFoundError, ValidationError } from './errors';
3
+ import { TuturuuuClient } from './storage';
4
+ import type {
5
+ DeleteDocumentResponse,
6
+ Document,
7
+ DocumentResponse,
8
+ GetDocumentResponse,
9
+ ListDocumentsResponse,
10
+ } from './types';
11
+
12
+ // Mock fetch globally
13
+ const mockFetch = vi.fn();
14
+
15
+ // Helper to create mock response with proper headers
16
+ const createMockResponse = (data: unknown, status = 200) => ({
17
+ ok: status >= 200 && status < 300,
18
+ status,
19
+ headers: new Headers({ 'content-type': 'application/json' }),
20
+ json: async () => data,
21
+ text: async () => (typeof data === 'string' ? data : JSON.stringify(data)),
22
+ blob: async () => (data instanceof Blob ? data : new Blob()),
23
+ });
24
+
25
+ describe('DocumentsClient', () => {
26
+ let client: TuturuuuClient;
27
+
28
+ beforeEach(() => {
29
+ vi.stubGlobal('fetch', mockFetch);
30
+ mockFetch.mockClear();
31
+ client = new TuturuuuClient('ttr_test_key');
32
+ });
33
+
34
+ afterEach(() => {
35
+ vi.unstubAllGlobals();
36
+ });
37
+
38
+ const mockDocument: Document = {
39
+ id: 'doc-123',
40
+ name: 'Test Document',
41
+ content: 'Test content',
42
+ isPublic: false,
43
+ createdAt: '2024-01-01T00:00:00Z',
44
+ };
45
+
46
+ describe('list', () => {
47
+ it('should list documents with default options', async () => {
48
+ const mockResponse: ListDocumentsResponse = {
49
+ data: [mockDocument],
50
+ pagination: { limit: 50, offset: 0, total: 1 },
51
+ };
52
+
53
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
54
+
55
+ const result = await client.documents.list();
56
+
57
+ expect(result).toEqual(mockResponse);
58
+ expect(mockFetch).toHaveBeenCalledTimes(1);
59
+ expect(mockFetch).toHaveBeenCalledWith(
60
+ expect.stringContaining('/documents?'),
61
+ expect.any(Object)
62
+ );
63
+
64
+ // Verify request details
65
+ const callOptions = mockFetch.mock.calls[0]?.[1];
66
+ expect(callOptions?.method).toBe('GET');
67
+
68
+ const headers = callOptions?.headers;
69
+ const authHeader =
70
+ headers instanceof Headers
71
+ ? headers.get('Authorization')
72
+ : headers?.Authorization;
73
+ expect(authHeader).toBe('Bearer ttr_test_key');
74
+
75
+ const contentType =
76
+ headers instanceof Headers
77
+ ? headers.get('Content-Type')
78
+ : headers?.['Content-Type'];
79
+ expect(contentType).toBeUndefined();
80
+ });
81
+
82
+ it('should list documents with search query', async () => {
83
+ const mockResponse: ListDocumentsResponse = {
84
+ data: [mockDocument],
85
+ pagination: { limit: 50, offset: 0, total: 1 },
86
+ };
87
+
88
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
89
+
90
+ await client.documents.list({ search: 'test' });
91
+
92
+ expect(mockFetch).toHaveBeenCalledWith(
93
+ expect.stringContaining('search=test'),
94
+ expect.any(Object)
95
+ );
96
+ });
97
+
98
+ it('should list documents with pagination', async () => {
99
+ const mockResponse: ListDocumentsResponse = {
100
+ data: [],
101
+ pagination: { limit: 20, offset: 10, total: 0 },
102
+ };
103
+
104
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
105
+
106
+ await client.documents.list({ limit: 20, offset: 10 });
107
+
108
+ expect(mockFetch).toHaveBeenCalledWith(
109
+ expect.stringContaining('limit=20'),
110
+ expect.any(Object)
111
+ );
112
+ expect(mockFetch).toHaveBeenCalledWith(
113
+ expect.stringContaining('offset=10'),
114
+ expect.any(Object)
115
+ );
116
+ });
117
+
118
+ it('should list documents filtered by isPublic', async () => {
119
+ const mockResponse: ListDocumentsResponse = {
120
+ data: [mockDocument],
121
+ pagination: { limit: 50, offset: 0, total: 1 },
122
+ };
123
+
124
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
125
+
126
+ await client.documents.list({ isPublic: true });
127
+
128
+ expect(mockFetch).toHaveBeenCalledWith(
129
+ expect.stringContaining('isPublic=true'),
130
+ expect.any(Object)
131
+ );
132
+ });
133
+
134
+ it('should list documents with all options', async () => {
135
+ const mockResponse: ListDocumentsResponse = {
136
+ data: [],
137
+ pagination: { limit: 10, offset: 5, total: 0 },
138
+ };
139
+
140
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
141
+
142
+ await client.documents.list({
143
+ search: 'meeting',
144
+ limit: 10,
145
+ offset: 5,
146
+ isPublic: false,
147
+ });
148
+
149
+ expect(mockFetch).toHaveBeenCalledWith(
150
+ expect.stringContaining('search=meeting'),
151
+ expect.any(Object)
152
+ );
153
+ expect(mockFetch).toHaveBeenCalledWith(
154
+ expect.stringContaining('limit=10'),
155
+ expect.any(Object)
156
+ );
157
+ expect(mockFetch).toHaveBeenCalledWith(
158
+ expect.stringContaining('offset=5'),
159
+ expect.any(Object)
160
+ );
161
+ expect(mockFetch).toHaveBeenCalledWith(
162
+ expect.stringContaining('isPublic=false'),
163
+ expect.any(Object)
164
+ );
165
+ });
166
+
167
+ it('should reject invalid limit', async () => {
168
+ await expect(client.documents.list({ limit: 0 })).rejects.toThrow(
169
+ ValidationError
170
+ );
171
+ await expect(client.documents.list({ limit: 101 })).rejects.toThrow(
172
+ ValidationError
173
+ );
174
+ });
175
+
176
+ it('should reject invalid offset', async () => {
177
+ await expect(client.documents.list({ offset: -1 })).rejects.toThrow(
178
+ ValidationError
179
+ );
180
+ });
181
+ });
182
+
183
+ describe('create', () => {
184
+ it('should create document with minimal data', async () => {
185
+ const mockResponse: DocumentResponse = {
186
+ message: 'Document created',
187
+ data: mockDocument,
188
+ };
189
+
190
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
191
+
192
+ const result = await client.documents.create({
193
+ name: 'Test Document',
194
+ });
195
+
196
+ expect(result).toEqual(mockResponse);
197
+ expect(mockFetch).toHaveBeenCalledWith(
198
+ expect.stringContaining('/documents'),
199
+ expect.objectContaining({
200
+ method: 'POST',
201
+ })
202
+ );
203
+
204
+ // Verify request body and headers
205
+ const callOptions = mockFetch.mock.calls[0]?.[1];
206
+ expect(callOptions?.method).toBe('POST');
207
+
208
+ const headers = callOptions?.headers;
209
+ const contentType =
210
+ headers instanceof Headers
211
+ ? headers.get('Content-Type')
212
+ : headers?.['Content-Type'];
213
+ expect(contentType).toBe('application/json');
214
+
215
+ const authHeader =
216
+ headers instanceof Headers
217
+ ? headers.get('Authorization')
218
+ : headers?.Authorization;
219
+ expect(authHeader).toBe('Bearer ttr_test_key');
220
+
221
+ const requestBody = JSON.parse(callOptions?.body as string);
222
+ expect(requestBody).toMatchObject({ name: 'Test Document' });
223
+ });
224
+
225
+ it('should create document with all fields', async () => {
226
+ const mockResponse: DocumentResponse = {
227
+ message: 'Document created',
228
+ data: mockDocument,
229
+ };
230
+
231
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
232
+
233
+ const result = await client.documents.create({
234
+ name: 'Test Document',
235
+ content: 'Test content',
236
+ isPublic: false,
237
+ });
238
+
239
+ expect(result).toEqual(mockResponse);
240
+
241
+ // Assert request body contains only provided fields
242
+ const callOptions = mockFetch.mock.calls[0]?.[1];
243
+ const requestBody = JSON.parse(callOptions?.body as string);
244
+ expect(requestBody).toStrictEqual({
245
+ name: 'Test Document',
246
+ content: 'Test content',
247
+ isPublic: false,
248
+ });
249
+
250
+ // Verify headers
251
+ const headers = callOptions?.headers;
252
+ const contentType =
253
+ headers instanceof Headers
254
+ ? headers.get('Content-Type')
255
+ : headers?.['Content-Type'];
256
+ expect(contentType).toBe('application/json');
257
+ expect(callOptions?.method).toBe('POST');
258
+ });
259
+
260
+ it('should reject empty name', async () => {
261
+ await expect(client.documents.create({ name: '' })).rejects.toThrow(
262
+ ValidationError
263
+ );
264
+ });
265
+
266
+ it('should reject name longer than 255 characters', async () => {
267
+ const longName = 'a'.repeat(256);
268
+ await expect(client.documents.create({ name: longName })).rejects.toThrow(
269
+ ValidationError
270
+ );
271
+ });
272
+ });
273
+
274
+ describe('get', () => {
275
+ it('should get document by ID', async () => {
276
+ const mockResponse: GetDocumentResponse = {
277
+ data: mockDocument,
278
+ };
279
+
280
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
281
+
282
+ const result = await client.documents.get('doc-123');
283
+
284
+ expect(result).toEqual(mockResponse);
285
+ expect(mockFetch).toHaveBeenCalledWith(
286
+ expect.stringContaining('/documents/doc-123'),
287
+ expect.any(Object)
288
+ );
289
+ });
290
+
291
+ it('should throw ValidationError for empty ID', async () => {
292
+ await expect(client.documents.get('')).rejects.toThrow(ValidationError);
293
+ });
294
+
295
+ it('should throw NotFoundError when server returns 404', async () => {
296
+ mockFetch.mockResolvedValueOnce(
297
+ createMockResponse(
298
+ {
299
+ error: 'Not Found',
300
+ message: 'Document not found',
301
+ code: 'DOCUMENT_NOT_FOUND',
302
+ },
303
+ 404
304
+ )
305
+ );
306
+
307
+ await expect(client.documents.get('doc-123')).rejects.toThrow(
308
+ NotFoundError
309
+ );
310
+
311
+ expect(mockFetch).toHaveBeenCalledWith(
312
+ expect.stringContaining('/documents/doc-123'),
313
+ expect.objectContaining({
314
+ method: 'GET',
315
+ })
316
+ );
317
+ });
318
+ });
319
+
320
+ describe('update', () => {
321
+ it('should update document name', async () => {
322
+ const mockResponse: DocumentResponse = {
323
+ message: 'Document updated',
324
+ data: { ...mockDocument, name: 'Updated Name' },
325
+ };
326
+
327
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
328
+
329
+ const result = await client.documents.update('doc-123', {
330
+ name: 'Updated Name',
331
+ });
332
+
333
+ expect(result).toEqual(mockResponse);
334
+ expect(mockFetch).toHaveBeenCalledWith(
335
+ expect.stringContaining('/documents/doc-123'),
336
+ expect.objectContaining({
337
+ method: 'PATCH',
338
+ })
339
+ );
340
+ });
341
+
342
+ it('should update document content', async () => {
343
+ const mockResponse: DocumentResponse = {
344
+ message: 'Document updated',
345
+ data: { ...mockDocument, content: 'New content' },
346
+ };
347
+
348
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
349
+
350
+ await client.documents.update('doc-123', {
351
+ content: 'New content',
352
+ });
353
+
354
+ expect(mockFetch).toHaveBeenCalled();
355
+ });
356
+
357
+ it('should update document visibility', async () => {
358
+ const mockResponse: DocumentResponse = {
359
+ message: 'Document updated',
360
+ data: { ...mockDocument, isPublic: true },
361
+ };
362
+
363
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
364
+
365
+ await client.documents.update('doc-123', {
366
+ isPublic: true,
367
+ });
368
+
369
+ expect(mockFetch).toHaveBeenCalled();
370
+ });
371
+
372
+ it('should update multiple fields', async () => {
373
+ const mockResponse: DocumentResponse = {
374
+ message: 'Document updated',
375
+ data: {
376
+ ...mockDocument,
377
+ name: 'New Name',
378
+ content: 'New content',
379
+ isPublic: true,
380
+ },
381
+ };
382
+
383
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
384
+
385
+ await client.documents.update('doc-123', {
386
+ name: 'New Name',
387
+ content: 'New content',
388
+ isPublic: true,
389
+ });
390
+
391
+ expect(mockFetch).toHaveBeenCalled();
392
+ });
393
+
394
+ it('should throw ValidationError for empty ID', async () => {
395
+ await expect(
396
+ client.documents.update('', { name: 'Test' })
397
+ ).rejects.toThrow(ValidationError);
398
+ });
399
+
400
+ it('should reject empty name', async () => {
401
+ await expect(
402
+ client.documents.update('doc-123', { name: '' })
403
+ ).rejects.toThrow(ValidationError);
404
+ });
405
+
406
+ it('should reject name longer than 255 characters', async () => {
407
+ const longName = 'a'.repeat(256);
408
+ await expect(
409
+ client.documents.update('doc-123', { name: longName })
410
+ ).rejects.toThrow(ValidationError);
411
+ });
412
+ });
413
+
414
+ describe('delete', () => {
415
+ it('should delete document', async () => {
416
+ const mockResponse: DeleteDocumentResponse = {
417
+ message: 'Document deleted',
418
+ };
419
+
420
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
421
+
422
+ const result = await client.documents.delete('doc-123');
423
+
424
+ expect(result).toEqual(mockResponse);
425
+ expect(mockFetch).toHaveBeenCalledWith(
426
+ expect.stringContaining('/documents/doc-123'),
427
+ expect.objectContaining({
428
+ method: 'DELETE',
429
+ })
430
+ );
431
+ });
432
+
433
+ it('should throw ValidationError for empty ID', async () => {
434
+ await expect(client.documents.delete('')).rejects.toThrow(
435
+ ValidationError
436
+ );
437
+ });
438
+
439
+ it('should throw NotFoundError when server returns 404', async () => {
440
+ mockFetch.mockResolvedValueOnce(
441
+ createMockResponse(
442
+ {
443
+ error: 'Not Found',
444
+ message: 'Document not found',
445
+ code: 'DOCUMENT_NOT_FOUND',
446
+ },
447
+ 404
448
+ )
449
+ );
450
+
451
+ await expect(client.documents.delete('doc-123')).rejects.toThrow(
452
+ NotFoundError
453
+ );
454
+
455
+ expect(mockFetch).toHaveBeenCalledWith(
456
+ expect.stringContaining('/documents/doc-123'),
457
+ expect.objectContaining({
458
+ method: 'DELETE',
459
+ })
460
+ );
461
+ });
462
+ });
463
+
464
+ describe('search', () => {
465
+ it('should search documents', async () => {
466
+ const mockResponse: ListDocumentsResponse = {
467
+ data: [mockDocument],
468
+ pagination: { limit: 50, offset: 0, total: 1 },
469
+ };
470
+
471
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
472
+
473
+ const result = await client.documents.search('test query');
474
+
475
+ expect(result).toEqual(mockResponse);
476
+ expect(mockFetch).toHaveBeenCalledWith(
477
+ expect.stringContaining('search=test+query'),
478
+ expect.any(Object)
479
+ );
480
+ });
481
+
482
+ it('should search documents with additional options', async () => {
483
+ const mockResponse: ListDocumentsResponse = {
484
+ data: [mockDocument],
485
+ pagination: { limit: 10, offset: 0, total: 1 },
486
+ };
487
+
488
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
489
+
490
+ await client.documents.search('test', {
491
+ limit: 10,
492
+ isPublic: true,
493
+ });
494
+
495
+ expect(mockFetch).toHaveBeenCalledWith(
496
+ expect.stringContaining('search=test'),
497
+ expect.any(Object)
498
+ );
499
+ expect(mockFetch).toHaveBeenCalledWith(
500
+ expect.stringContaining('limit=10'),
501
+ expect.any(Object)
502
+ );
503
+ expect(mockFetch).toHaveBeenCalledWith(
504
+ expect.stringContaining('isPublic=true'),
505
+ expect.any(Object)
506
+ );
507
+ });
508
+
509
+ it('should handle empty search query', async () => {
510
+ const mockResponse: ListDocumentsResponse = {
511
+ data: [],
512
+ pagination: { limit: 50, offset: 0, total: 0 },
513
+ };
514
+
515
+ mockFetch.mockResolvedValueOnce(createMockResponse(mockResponse));
516
+
517
+ const result = await client.documents.search('');
518
+
519
+ expect(result).toEqual(mockResponse);
520
+ expect(mockFetch).toHaveBeenCalled();
521
+ });
522
+ });
523
+ });