Files
core/test/metadata.test.ts
chrnx bff8778112 refactor(mesh): @Provider's options no longer advertise export
The framework's public type offered an option whose only behaviour is to
throw at boot. Removing the field from the options type means a TypeScript
caller gets a compile error at the decorator, where the mistake is, instead
of a boot error a deploy away.

The runtime throw in collectProviders stays as the JavaScript backstop, so
ProviderMeta keeps the field and the decorator still records it through a
cast — a JS caller reaches the error naming the provider and its replacement
rather than having the option silently ignored.

The `as any` in the boot-throw test is now genuinely load-bearing, since no
TypeScript caller can write that decorator at all, and says so.

test/metadata.test.ts asserted the flag was stored for a provider. That half
described the option working, which is the message being removed, and the
boot throw it fed is already covered end to end in test/mesh/failure.test.ts.
The default is a different claim and stays: collectProviders throws on
anything truthy, so every plain provider recording `false` is what keeps
ordinary apps booting.

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

113 lines
4.4 KiB
TypeScript

import 'reflect-metadata';
import { expect, test, describe, it } from 'vitest';
import {
Provider, Step, Route, Get, Module,
getProviderMeta, getStepMeta, getRoutes, getModuleMeta,
Sse, Ws, Stream, Post, Head, Options,
} from '../src/metadata';
@Provider({ provides: 'db', needs: ['config'] })
class Db { provide() { return { db: 1 }; } }
@Step({ provides: 'user', needs: ['db', 'req'], optional: false })
class Auth { run() { return { user: 'x' }; } }
@Route('/users')
class Ctl { @Get('/:id') getUser() { return 1; } }
@Module({ mountpoint: '/api', providers: [Db], steps: [Auth], controllers: [Ctl] })
class ApiModule {}
test('provider metadata is readable', () => {
expect(getProviderMeta(Db)).toEqual({ provides: 'db', needs: ['config'], optional: false, export: false });
});
test('step metadata is readable', () => {
expect(getStepMeta(Auth)).toEqual({ provides: 'user', needs: ['db', 'req'], optional: false, export: false });
});
test('routes carry method, path and handler name', () => {
expect(getRoutes(Ctl)).toEqual([{ method: 'GET', path: '/users/:id', handlerName: 'getUser', transport: 'buffer', export: false }]);
});
test('module metadata lists members', () => {
const m = getModuleMeta(ApiModule)!;
expect(m.mountpoint).toBe('/api');
expect(m.providers).toEqual([Db]);
expect(m.steps).toEqual([Auth]);
expect(m.controllers).toEqual([Ctl]);
});
describe('stream route decorators', () => {
it('records transport + method per decorator', () => {
class StreamCtl {
@Sse('/feed') feed() {}
@Ws('/chat') chat() {}
@Stream('/either') either() {}
@Post('/make') make() {}
}
const byName = Object.fromEntries(getRoutes(StreamCtl).map((r) => [r.handlerName, r]));
expect(byName.feed).toMatchObject({ method: 'GET', transport: 'sse', path: '/feed' });
expect(byName.chat).toMatchObject({ method: 'GET', transport: 'ws', path: '/chat' });
expect(byName.either).toMatchObject({ method: 'GET', transport: 'negotiate', path: '/either' });
expect(byName.make).toMatchObject({ method: 'POST', transport: 'buffer', path: '/make' });
});
});
describe('HEAD and OPTIONS decorators', () => {
it('records both as buffered methods', () => {
class Methods {
@Head('/resource') head() {}
@Options('/resource') options() {}
}
const byName = Object.fromEntries(getRoutes(Methods).map((route) => [route.handlerName, route]));
expect(byName.head).toMatchObject({ method: 'HEAD', transport: 'buffer', path: '/resource' });
expect(byName.options).toMatchObject({ method: 'OPTIONS', transport: 'buffer', path: '/resource' });
});
});
describe('mesh export flags', () => {
it('steps carry export, and a provider defaults to not exported', () => {
@Step({ provides: 'auth', export: true }) class A { run() { return {}; } }
@Provider({ provides: 'priv' }) class P { provide() { return {}; } }
expect(getStepMeta(A)!.export).toBe(true);
// No `export: true` case for a provider: the option is gone from `@Provider`'s type, and its
// only behaviour is the boot throw that `test/mesh/failure.test.ts` covers end to end —
// asserting it is *stored* here would assert the option works. The default is a different
// claim and still load-bearing: `collectProviders` throws on anything truthy, so every plain
// provider recording `false` is what keeps ordinary apps booting.
expect(getProviderMeta(P)!.export).toBe(false);
});
it('routes carry export via decorator option (default false)', () => {
class Ctl2 {
@Get('/pub', { export: true }) pub() {}
@Get('/priv') priv() {}
}
const byName = Object.fromEntries(getRoutes(Ctl2).map((r) => [r.handlerName, r]));
expect(byName.pub.export).toBe(true);
expect(byName.priv.export).toBe(false);
});
});
import { Html, getHtmlMeta } from '../src/metadata';
describe('@Html', () => {
it('records empty meta for bare @Html', () => {
class A { @Html m() { return ''; } }
expect(getHtmlMeta(A, 'm')).toEqual({});
});
it('records path and template for called @Html', () => {
class B {
@Html('views/x.html') a() {}
@Html('views/y.html', { template: true }) c() { return {}; }
}
expect(getHtmlMeta(B, 'a')).toEqual({ path: 'views/x.html', template: undefined });
expect(getHtmlMeta(B, 'c')).toEqual({ path: 'views/y.html', template: true });
});
it('returns undefined when @Html is absent', () => {
class C { m() {} }
expect(getHtmlMeta(C, 'm')).toBeUndefined();
});
});