record in params.ts read the argument map with Reflect.getMetadata, which walks the prototype chain, then pushed into the list it got back and defined that same object on the subclass. On a subclass with no metadata of its own, that object was the base class's, so a @query on Child.list also showed up in getArgs(Base, 'list'). routeDecorator in metadata.ts had the same pattern with the routes array, so a @Get on a subclass was also served by the base controller. It is the same bug in the same shape, so it is fixed here too. Both now write a copy onto the decorated class: a shallow copy of the map and a new list for the method in params.ts, a new array in metadata.ts. getMetadata returns the class's own metadata once it has some, so later decorators on that class copy its own data, not the base's. A subclass still starts from what it inherits, as before. The other decorators in metadata.ts (@Provider, @Step, @Route, @Module, @Transformer, @Html) define a fresh value per class or per method and never mutate what they read, so they are unaffected. Refs: GitHub #44 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
128 lines
5.1 KiB
TypeScript
128 lines
5.1 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();
|
|
});
|
|
});
|
|
|
|
// 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']);
|
|
});
|
|
});
|