Files
core/test/mesh/teacup.test.ts
chrnx d6c5cee0ee feat: beta-hardening — routing, degradation, mesh gate, uploads, @header
Restructure first: split the two oversized files by cohesion (http.ts 449
→ http/{router,request,stream,ws,server,types}; app.ts 556 → app/{types,
introspect,index}), rename cryptic locals, JSDoc the public API, and
extract helpers so no function exceeds the complexity gate (createApp
187 → <15, listen 113 → 6). Public import paths are unchanged (barrels).

On that structure, five features that close the biggest gaps vs
Express/Fastify/Nest for a comparable beta:

- routing: `:name*` catch-all, specificity precedence (static ▸ param ▸
  catch-all, not registration order), and 405 + Allow when a path exists
  under a different method (was 404)
- visible degradation: optional providers that fail boot are summarized on
  listen() and queryable via app.degraded(), instead of a silent warn
- mesh is alpha — gated behind `experimental: true`; createApp throws if
  `mesh` is configured without it
- multipart moves to the optional `busboy` peer dependency (clear 501 when
  absent), keeping the core at one runtime dependency
- `@header('name')` as a singular alias of `@headers`

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-07-07 12:00:15 -05:00

34 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { buildRemote, envelopeFrom } from '../../src/mesh/teacup';
import type { Link } from '../../src/mesh/link';
const fakeLink = (calls: any[]): Link => ({
manifest: { scopes: [{ token: 'config', scope: 'app' }, { token: 'auth', scope: 'request' }], routes: [{ method: 'GET', pattern: '/u/:id' }] },
rpc: async (kind, name, ctx) => { calls.push({ kind, name, ctx }); return name === 'config' ? { url: 'db://x' } : { id: 'u1' }; },
close() {},
});
describe('teacup buildRemote', () => {
it('makes a provider node (app) and a step node (request) with RPC runners returning merge objects', async () => {
const calls: any[] = [];
const { providers, steps } = buildRemote(fakeLink(calls));
expect(providers.map((p) => p.name)).toEqual(['config']);
expect(steps.map((s) => s.name)).toEqual(['auth']);
expect(await providers[0].run({})).toEqual({ config: { url: 'db://x' } });
const ctx = { req: {}, params: { id: '9' }, query: {}, body: undefined, headers: { x: '1' } };
expect(await steps[0].run(ctx)).toEqual({ auth: { id: 'u1' } });
expect(calls.find((c) => c.name === 'auth').ctx).toMatchObject({ params: { id: '9' }, headers: { x: '1' } });
});
it('makes a proxy route entry with method+pattern', async () => {
const calls: any[] = [];
const { routes } = buildRemote(fakeLink(calls));
expect(routes[0]).toMatchObject({ method: 'GET', pattern: '/u/:id' });
});
it('envelopeFrom extracts only serializable request fields', () => {
const env = envelopeFrom({ method: 'POST', params: { a: '1' }, query: { q: 'x' }, body: { n: 1 }, headers: { h: 'v' }, db: { find() {} } } as any);
expect(env).toEqual({ method: 'POST', params: { a: '1' }, query: { q: 'x' }, body: { n: 1 }, headers: { h: 'v' } });
});
});