|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | | -import type { IMetadataService, MetadataWatchCallback, MetadataWatchHandle, MetadataTypeInfo } from './metadata-service'; |
| 2 | +import type { IMetadataService, MetadataWatchCallback, MetadataWatchHandle, MetadataTypeInfo, ApiEndpointMatch } from './metadata-service'; |
| 3 | +import { ApiEndpointSchema, type ApiEndpoint } from '../api/endpoint.zod'; |
3 | 4 |
|
4 | 5 | describe('Metadata Service Contract', () => { |
5 | 6 | it('should allow a minimal IMetadataService implementation with required methods', () => { |
@@ -422,4 +423,128 @@ describe('Metadata Service Contract', () => { |
422 | 423 | const published = await service.getPublished!('object', 'account'); |
423 | 424 | expect(published).toEqual({ name: 'account', label: 'Account' }); |
424 | 425 | }); |
| 426 | + |
| 427 | + // ========================================== |
| 428 | + // API Endpoint Resolution (#5040 E1) |
| 429 | + // ========================================== |
| 430 | + |
| 431 | + describe('matchEndpoint (optional member)', () => { |
| 432 | + /** A minimal base implementation with only the REQUIRED members. */ |
| 433 | + const baseService = (): IMetadataService => ({ |
| 434 | + register: async () => {}, |
| 435 | + get: async () => undefined, |
| 436 | + list: async () => [], |
| 437 | + unregister: async () => {}, |
| 438 | + exists: async () => false, |
| 439 | + listNames: async () => [], |
| 440 | + getObject: async () => undefined, |
| 441 | + listObjects: async () => [], |
| 442 | + }); |
| 443 | + |
| 444 | + /** An author-written `api` item that OMITS the `authRequired` default. */ |
| 445 | + const authoredEndpoint = { |
| 446 | + name: 'showcase_tasks', |
| 447 | + path: '/api/v1/apps/showcase/tasks', |
| 448 | + method: 'GET', |
| 449 | + type: 'object_operation', |
| 450 | + target: 'showcase_task', |
| 451 | + objectParams: { object: 'showcase_task', operation: 'find' }, |
| 452 | + }; |
| 453 | + |
| 454 | + it('is optional — an implementation without it still satisfies the contract', () => { |
| 455 | + const service = baseService(); |
| 456 | + |
| 457 | + // The whole point of the optional-member convention: consumers probe. |
| 458 | + expect(typeof service.matchEndpoint).toBe('undefined'); |
| 459 | + expect(typeof (service as IMetadataService).matchEndpoint === 'function').toBe(false); |
| 460 | + }); |
| 461 | + |
| 462 | + it('is probeable with typeof === "function" when provided', () => { |
| 463 | + const service: IMetadataService = { |
| 464 | + ...baseService(), |
| 465 | + matchEndpoint: async () => undefined, |
| 466 | + }; |
| 467 | + |
| 468 | + expect(typeof service.matchEndpoint).toBe('function'); |
| 469 | + }); |
| 470 | + |
| 471 | + it('resolves method+path to a match, and undefined on a miss', async () => { |
| 472 | + const parsed = ApiEndpointSchema.parse(authoredEndpoint); |
| 473 | + |
| 474 | + const service: IMetadataService = { |
| 475 | + ...baseService(), |
| 476 | + matchEndpoint: async ({ path, method }) => |
| 477 | + method.toUpperCase() === parsed.method && path === parsed.path |
| 478 | + ? { endpoint: parsed, params: {} } |
| 479 | + : undefined, |
| 480 | + }; |
| 481 | + |
| 482 | + const hit = await service.matchEndpoint!({ |
| 483 | + path: '/api/v1/apps/showcase/tasks', |
| 484 | + method: 'get', |
| 485 | + }); |
| 486 | + expect(hit).toBeDefined(); |
| 487 | + expect(hit!.endpoint.name).toBe('showcase_tasks'); |
| 488 | + |
| 489 | + const miss = await service.matchEndpoint!({ |
| 490 | + path: '/api/v1/apps/showcase/nope', |
| 491 | + method: 'GET', |
| 492 | + }); |
| 493 | + expect(miss).toBeUndefined(); |
| 494 | + }); |
| 495 | + |
| 496 | + it('returns the ApiEndpointSchema.parse-d shape — schema defaults materialized', async () => { |
| 497 | + // The author never wrote `authRequired`; the contract says a consumer |
| 498 | + // must never see "absent" for it. |
| 499 | + expect('authRequired' in authoredEndpoint).toBe(false); |
| 500 | + |
| 501 | + const service: IMetadataService = { |
| 502 | + ...baseService(), |
| 503 | + matchEndpoint: async () => ({ |
| 504 | + endpoint: ApiEndpointSchema.parse(authoredEndpoint), |
| 505 | + params: {}, |
| 506 | + }), |
| 507 | + }; |
| 508 | + |
| 509 | + const match = await service.matchEndpoint!({ |
| 510 | + path: '/api/v1/apps/showcase/tasks', |
| 511 | + method: 'GET', |
| 512 | + }); |
| 513 | + |
| 514 | + expect(match!.endpoint.authRequired).toBe(true); |
| 515 | + expect(typeof match!.endpoint.authRequired).toBe('boolean'); |
| 516 | + }); |
| 517 | + |
| 518 | + it('params is always {} in 17.x — the slot is reserved, no template syntax', async () => { |
| 519 | + const service: IMetadataService = { |
| 520 | + ...baseService(), |
| 521 | + matchEndpoint: async () => ({ |
| 522 | + endpoint: ApiEndpointSchema.parse(authoredEndpoint), |
| 523 | + params: {}, |
| 524 | + }), |
| 525 | + }; |
| 526 | + |
| 527 | + const match = await service.matchEndpoint!({ |
| 528 | + path: '/api/v1/apps/showcase/tasks', |
| 529 | + method: 'GET', |
| 530 | + }); |
| 531 | + |
| 532 | + expect(match!.params).toEqual({}); |
| 533 | + }); |
| 534 | + |
| 535 | + it('ApiEndpointMatch types endpoint as ApiEndpoint and params as Record< string, string >', () => { |
| 536 | + // Type-level shape assertion: the literal only compiles against the |
| 537 | + // declared member types. |
| 538 | + const match: ApiEndpointMatch = { |
| 539 | + endpoint: ApiEndpointSchema.parse(authoredEndpoint), |
| 540 | + params: {}, |
| 541 | + }; |
| 542 | + |
| 543 | + const endpoint: ApiEndpoint = match.endpoint; |
| 544 | + const params: Record<string, string> = match.params; |
| 545 | + |
| 546 | + expect(endpoint.path).toBe('/api/v1/apps/showcase/tasks'); |
| 547 | + expect(params).toEqual({}); |
| 548 | + }); |
| 549 | + }); |
425 | 550 | }); |
0 commit comments