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(); }); }); // GitHub #44: the route decorator had the same read-the-inherited-array-then-push pattern as // `record` in params.ts, so a route on a subclass was added to the base controller as well. describe('route inheritance', () => { class Base { @Get('/a') a() {} } class Child extends Base { @Get('/b') b() {} } it('a route on a subclass leaves the base class unchanged', () => { expect(getRoutes(Base).map((route) => route.handlerName)).toEqual(['a']); }); it('the subclass keeps the inherited routes and adds its own', () => { expect(getRoutes(Child).map((route) => route.handlerName)).toEqual(['a', 'b']); }); });