-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmetadata-api.test.ts
More file actions
715 lines (628 loc) · 23.9 KB
/
Copy pathmetadata-api.test.ts
File metadata and controls
715 lines (628 loc) · 23.9 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* @objectstack/nextjs — Comprehensive Metadata API Integration Tests
*
* Validates that the Next.js adapter correctly routes ALL metadata API operations
* defined by the @objectstack/metadata package through the HttpDispatcher.
*
* Covers: CRUD, Query, Bulk, Overlay, Import/Export, Validation, Type Registry, Dependencies
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock dispatcher instance
const mockDispatcher = {
getDiscoveryInfo: vi.fn().mockReturnValue({ version: '1.0', endpoints: [] }),
handleAuth: vi.fn().mockResolvedValue({ handled: true, response: { body: { ok: true }, status: 200 } }),
handleGraphQL: vi.fn().mockResolvedValue({ data: {} }),
handleMetadata: vi.fn().mockResolvedValue({ handled: true, response: { body: { success: true }, status: 200 } }),
handleData: vi.fn().mockResolvedValue({ handled: true, response: { body: { records: [] }, status: 200 } }),
handleStorage: vi.fn().mockResolvedValue({ handled: true, response: { body: {}, status: 200 } }),
dispatch: vi.fn().mockResolvedValue({ handled: true, response: { body: { success: true }, status: 200 } }),
};
vi.mock('@objectstack/runtime', () => {
return {
HttpDispatcher: function HttpDispatcher() {
return mockDispatcher;
},
};
});
vi.mock('next/server', () => {
class MockNextRequest {
url: string;
method: string;
private _body: any;
constructor(url: string, init?: any) {
this.url = url;
this.method = init?.method || 'GET';
this._body = init?.body;
}
async json() {
return this._body ? JSON.parse(this._body) : {};
}
async formData() {
const map = new Map();
map.set('file', { name: 'test.txt', type: 'text/plain' });
return { get: (key: string) => map.get(key) };
}
}
class MockNextResponse {
body: any;
status: number;
headers: Record<string, string>;
constructor(body?: any, init?: any) {
this.body = body;
this.status = init?.status || 200;
this.headers = init?.headers || {};
}
async json() {
return typeof this.body === 'string' ? JSON.parse(this.body) : this.body;
}
static json(body: any, init?: any) {
return new MockNextResponse(body, init);
}
static redirect(url: string | URL) {
const res = new MockNextResponse(null, { status: 307 });
(res as any).redirectUrl = typeof url === 'string' ? url : url.toString();
return res;
}
}
return { NextRequest: MockNextRequest, NextResponse: MockNextResponse };
});
import { NextRequest } from 'next/server';
import { createRouteHandler } from './index';
const mockKernel = { name: 'test-kernel' } as any;
function makeReq(url: string, method = 'GET', body?: any) {
const init: any = { method };
if (body) init.body = JSON.stringify(body);
return new (NextRequest as any)(url, init);
}
describe('Next.js Metadata API Integration Tests', () => {
let handler: ReturnType<typeof createRouteHandler>;
beforeEach(() => {
vi.clearAllMocks();
handler = createRouteHandler({ kernel: mockKernel });
});
// ==========================================
// CRUD Operations
// ==========================================
describe('CRUD Operations', () => {
describe('GET meta/objects — List all objects', () => {
it('dispatches to dispatch with correct path', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: [
{ name: 'account', label: 'Account' },
{ name: 'contact', label: 'Contact' },
],
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects');
const res = await handler(req, { params: { objectstack: ['meta', 'objects'] } });
expect(res.status).toBe(200);
expect(res.body.data).toHaveLength(2);
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta/objects',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
});
describe('GET meta/objects/account — Get single object', () => {
it('dispatches to dispatch with item-level path', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: { type: 'object', name: 'account', definition: { label: 'Account' } },
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects/account');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account'] } });
expect(res.status).toBe(200);
expect(res.body.data.name).toBe('account');
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta/objects/account',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
});
describe('POST meta/objects — Register metadata', () => {
it('dispatches POST with JSON body', async () => {
const body = {
type: 'object',
name: 'project_task',
data: { label: 'Project Task', fields: {} },
};
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: { body: { success: true }, status: 201 },
});
const req = makeReq('http://localhost/api/meta/objects', 'POST', body);
const res = await handler(req, { params: { objectstack: ['meta', 'objects'] } });
expect(res.status).toBe(201);
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'POST',
'/meta/objects',
body,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
});
describe('PUT meta/objects/account — Update metadata', () => {
it('dispatches PUT with JSON body', async () => {
const body = { label: 'Updated Account' };
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: { body: { success: true }, status: 200 },
});
const req = makeReq('http://localhost/api/meta/objects/account', 'PUT', body);
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account'] } });
expect(res.status).toBe(200);
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'PUT',
'/meta/objects/account',
body,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
});
describe('DELETE meta/objects/old_entity — Delete metadata', () => {
it('dispatches DELETE to dispatch', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: { type: 'object', name: 'old_entity' } },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects/old_entity', 'DELETE');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'old_entity'] } });
expect(res.status).toBe(200);
expect(res.body.data.name).toBe('old_entity');
});
});
describe('Multiple metadata types', () => {
it('dispatches for views', async () => {
const req = makeReq('http://localhost/api/meta/views');
await handler(req, { params: { objectstack: ['meta', 'views'] } });
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta/views',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
it('dispatches for flows', async () => {
const req = makeReq('http://localhost/api/meta/flows');
await handler(req, { params: { objectstack: ['meta', 'flows'] } });
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta/flows',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
it('dispatches for agents', async () => {
const req = makeReq('http://localhost/api/meta/agents');
await handler(req, { params: { objectstack: ['meta', 'agents'] } });
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta/agents',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
});
});
// ==========================================
// Query / Search
// ==========================================
describe('Query / Search', () => {
describe('POST meta/query — Advanced search', () => {
it('dispatches query with full filter payload', async () => {
const queryBody = {
types: ['object', 'view'],
search: 'account',
page: 1,
pageSize: 25,
};
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: {
items: [{ type: 'object', name: 'account' }],
total: 1,
page: 1,
pageSize: 25,
},
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/query', 'POST', queryBody);
const res = await handler(req, { params: { objectstack: ['meta', 'query'] } });
expect(res.status).toBe(200);
expect(res.body.data.items).toHaveLength(1);
});
});
});
// ==========================================
// Bulk Operations
// ==========================================
describe('Bulk Operations', () => {
describe('POST meta/bulk/register — Bulk register', () => {
it('dispatches bulk register', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: { total: 2, succeeded: 2, failed: 0 } },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/bulk/register', 'POST', {
items: [
{ type: 'object', name: 'customer', data: {} },
{ type: 'view', name: 'customer_list', data: {} },
],
});
const res = await handler(req, { params: { objectstack: ['meta', 'bulk', 'register'] } });
expect(res.status).toBe(200);
expect(res.body.data.succeeded).toBe(2);
});
});
describe('POST meta/bulk/unregister — Bulk unregister', () => {
it('dispatches bulk unregister', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: { total: 2, succeeded: 2, failed: 0 } },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/bulk/unregister', 'POST', {
items: [{ type: 'object', name: 'old' }, { type: 'view', name: 'old_view' }],
});
const res = await handler(req, { params: { objectstack: ['meta', 'bulk', 'unregister'] } });
expect(res.status).toBe(200);
expect(res.body.data.succeeded).toBe(2);
});
});
describe('Bulk operation with partial failures', () => {
it('returns error details', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: {
total: 3,
succeeded: 2,
failed: 1,
errors: [{ type: 'object', name: 'bad', error: 'Validation failed' }],
},
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/bulk/register', 'POST', {
items: [
{ type: 'object', name: 'good', data: {} },
{ type: 'object', name: 'good2', data: {} },
{ type: 'object', name: 'bad', data: {} },
],
continueOnError: true,
});
const res = await handler(req, { params: { objectstack: ['meta', 'bulk', 'register'] } });
expect(res.body.data.failed).toBe(1);
expect(res.body.data.errors[0].name).toBe('bad');
});
});
});
// ==========================================
// Overlay / Customization
// ==========================================
describe('Overlay / Customization', () => {
describe('GET meta/objects/account/overlay — Get overlay', () => {
it('dispatches overlay retrieval', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: {
id: 'overlay-001',
baseType: 'object',
baseName: 'account',
scope: 'platform',
patch: {},
},
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects/account/overlay');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account', 'overlay'] } });
expect(res.status).toBe(200);
expect(res.body.data.scope).toBe('platform');
});
});
describe('PUT meta/objects/account/overlay — Save overlay', () => {
it('dispatches overlay save', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: { body: { success: true }, status: 200 },
});
const req = makeReq('http://localhost/api/meta/objects/account/overlay', 'PUT', {
id: 'overlay-002',
baseType: 'object',
baseName: 'account',
patch: { fields: { status: { label: 'Custom' } } },
});
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account', 'overlay'] } });
expect(res.status).toBe(200);
});
});
describe('GET meta/objects/account/effective — Get effective metadata', () => {
it('dispatches effective metadata retrieval', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: { name: 'account', fields: { status: { label: 'Custom Status' } } },
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects/account/effective');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account', 'effective'] } });
expect(res.status).toBe(200);
expect(res.body.data.fields.status.label).toBe('Custom Status');
});
});
});
// ==========================================
// Import / Export
// ==========================================
describe('Import / Export', () => {
describe('POST meta/export — Export metadata', () => {
it('dispatches export request', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: { version: '1.0', objects: {} } },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/export', 'POST', { types: ['object'], format: 'json' });
const res = await handler(req, { params: { objectstack: ['meta', 'export'] } });
expect(res.status).toBe(200);
expect(res.body.data.version).toBe('1.0');
});
});
describe('POST meta/import — Import metadata', () => {
it('dispatches import request', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: { total: 3, imported: 3, skipped: 0, failed: 0 } },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/import', 'POST', {
data: { objects: { a: {} } },
conflictResolution: 'merge',
});
const res = await handler(req, { params: { objectstack: ['meta', 'import'] } });
expect(res.status).toBe(200);
expect(res.body.data.imported).toBe(3);
});
});
});
// ==========================================
// Validation
// ==========================================
describe('Validation', () => {
describe('POST meta/validate — Validate metadata', () => {
it('dispatches validation', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: { valid: true } },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/validate', 'POST', { type: 'object', data: {} });
const res = await handler(req, { params: { objectstack: ['meta', 'validate'] } });
expect(res.status).toBe(200);
expect(res.body.data.valid).toBe(true);
});
it('returns errors for invalid metadata', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: {
valid: false,
errors: [{ path: 'name', message: 'Required', code: 'required' }],
},
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/validate', 'POST', { type: 'object', data: {} });
const res = await handler(req, { params: { objectstack: ['meta', 'validate'] } });
expect(res.body.data.valid).toBe(false);
expect(res.body.data.errors).toHaveLength(1);
});
});
});
// ==========================================
// Type Registry
// ==========================================
describe('Type Registry', () => {
describe('GET meta/types — List types', () => {
it('returns all registered types', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: { success: true, data: ['object', 'view', 'flow', 'agent'] },
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/types');
const res = await handler(req, { params: { objectstack: ['meta', 'types'] } });
expect(res.status).toBe(200);
expect(res.body.data).toContain('object');
});
});
describe('GET meta/types/object — Get type info', () => {
it('returns type metadata', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: {
type: 'object',
label: 'Object',
filePatterns: ['**/*.object.ts'],
supportsOverlay: true,
domain: 'data',
},
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/types/object');
const res = await handler(req, { params: { objectstack: ['meta', 'types', 'object'] } });
expect(res.status).toBe(200);
expect(res.body.data.domain).toBe('data');
});
});
});
// ==========================================
// Dependency Tracking
// ==========================================
describe('Dependency Tracking', () => {
describe('GET meta/objects/account/dependencies — Get dependencies', () => {
it('returns dependencies', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: [{
sourceType: 'object',
sourceName: 'account',
targetType: 'object',
targetName: 'organization',
kind: 'reference',
}],
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects/account/dependencies');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account', 'dependencies'] } });
expect(res.status).toBe(200);
expect(res.body.data).toHaveLength(1);
});
});
describe('GET meta/objects/account/dependents — Get dependents', () => {
it('returns dependents', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({
handled: true,
response: {
body: {
success: true,
data: [
{ sourceType: 'view', sourceName: 'account_list', targetType: 'object', targetName: 'account', kind: 'reference' },
{ sourceType: 'flow', sourceName: 'new_account', targetType: 'object', targetName: 'account', kind: 'triggers' },
],
},
status: 200,
},
});
const req = makeReq('http://localhost/api/meta/objects/account/dependents');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'account', 'dependents'] } });
expect(res.status).toBe(200);
expect(res.body.data).toHaveLength(2);
});
});
});
// ==========================================
// Error Handling
// ==========================================
describe('Error Handling', () => {
it('returns 404 when metadata not found', async () => {
mockDispatcher.dispatch.mockResolvedValueOnce({ handled: false });
const req = makeReq('http://localhost/api/meta/objects/nonexistent');
const res = await handler(req, { params: { objectstack: ['meta', 'objects', 'nonexistent'] } });
expect(res.status).toBe(404);
});
it('returns 500 on dispatcher exception', async () => {
mockDispatcher.dispatch.mockRejectedValueOnce(new Error('Internal error'));
const req = makeReq('http://localhost/api/meta/objects');
const res = await handler(req, { params: { objectstack: ['meta', 'objects'] } });
expect(res.status).toBe(500);
expect(res.body.error.message).toBe('Internal error');
});
it('returns custom status code from error', async () => {
mockDispatcher.dispatch.mockRejectedValueOnce(
Object.assign(new Error('Forbidden'), { statusCode: 403 }),
);
const req = makeReq('http://localhost/api/meta/objects');
const res = await handler(req, { params: { objectstack: ['meta', 'objects'] } });
expect(res.status).toBe(403);
});
});
// ==========================================
// Path Parsing
// ==========================================
describe('Path Parsing', () => {
it('correctly joins nested segments', async () => {
const req = makeReq('http://localhost/api/meta/objects/account/fields/name');
await handler(req, { params: { objectstack: ['meta', 'objects', 'account', 'fields', 'name'] } });
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta/objects/account/fields/name',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
it('handles single segment meta path', async () => {
const req = makeReq('http://localhost/api/meta');
// With just ['meta'], subPath becomes empty after slice(1)
await handler(req, { params: { objectstack: ['meta'] } });
expect(mockDispatcher.dispatch).toHaveBeenCalledWith(
'GET',
'/meta',
undefined,
{},
expect.objectContaining({ request: expect.anything() }),
);
});
});
});