|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for RuntimeConfigPlugin's per-request capability gating. |
| 5 | + * |
| 6 | + * The tenant runtime serves `GET /api/v1/runtime/config`. `features.aiStudio` |
| 7 | + * must follow the resolved environment's billing plan: free → off, paid → on, |
| 8 | + * and the static default must survive when the plan can't be resolved. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect } from 'vitest'; |
| 12 | +import { RuntimeConfigPlugin } from './runtime-config-plugin.js'; |
| 13 | + |
| 14 | +/** Drive the plugin's start() and capture the mounted `/runtime/config` handler. */ |
| 15 | +async function mountAndGetHandler(opts: { |
| 16 | + pluginConfig?: ConstructorParameters<typeof RuntimeConfigPlugin>[0]; |
| 17 | + resolveByHostname?: (host: string) => Promise<any>; |
| 18 | +}): Promise<(c: any) => Promise<any>> { |
| 19 | + let handler: ((c: any) => Promise<any>) | undefined; |
| 20 | + const rawApp = { |
| 21 | + get(path: string, h: (c: any) => Promise<any>) { |
| 22 | + if (path === '/api/v1/runtime/config') handler = h; |
| 23 | + }, |
| 24 | + }; |
| 25 | + const services: Record<string, any> = { |
| 26 | + 'http-server': { getRawApp: () => rawApp }, |
| 27 | + }; |
| 28 | + if (opts.resolveByHostname) { |
| 29 | + services['env-registry'] = { resolveByHostname: opts.resolveByHostname }; |
| 30 | + } |
| 31 | + const ctx: any = { |
| 32 | + logger: { info() {}, warn() {} }, |
| 33 | + getService: (name: string) => { |
| 34 | + const s = services[name]; |
| 35 | + if (!s) throw new Error(`no service ${name}`); |
| 36 | + return s; |
| 37 | + }, |
| 38 | + hooks: [] as Array<() => Promise<void>>, |
| 39 | + hook(_event: string, cb: () => Promise<void>) { this.hooks.push(cb); }, |
| 40 | + }; |
| 41 | + const plugin = new RuntimeConfigPlugin(opts.pluginConfig ?? {}); |
| 42 | + await plugin.start(ctx); |
| 43 | + for (const cb of ctx.hooks) await cb(); // fire kernel:ready |
| 44 | + if (!handler) throw new Error('handler not mounted'); |
| 45 | + return handler; |
| 46 | +} |
| 47 | + |
| 48 | +function fakeCtx(host: string) { |
| 49 | + let captured: any; |
| 50 | + return { |
| 51 | + c: { req: { header: (n: string) => (n.toLowerCase() === 'host' ? host : undefined) }, json: (b: any) => { captured = b; return b; } }, |
| 52 | + get payload() { return captured; }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +describe('RuntimeConfigPlugin — aiStudio plan gating', () => { |
| 57 | + it('disables aiStudio for a free-plan environment', async () => { |
| 58 | + const handler = await mountAndGetHandler({ |
| 59 | + resolveByHostname: async () => ({ environmentId: 'env1', organizationId: 'org1', plan: 'free' }), |
| 60 | + }); |
| 61 | + const { c } = fakeCtx('acme.objectos.ai'); |
| 62 | + const body = await handler(c); |
| 63 | + expect(body.features.aiStudio).toBe(false); |
| 64 | + expect(body.defaultEnvironmentId).toBe('env1'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('enables aiStudio for a paid-plan environment', async () => { |
| 68 | + const handler = await mountAndGetHandler({ |
| 69 | + resolveByHostname: async () => ({ environmentId: 'env2', plan: 'pro' }), |
| 70 | + }); |
| 71 | + const { c } = fakeCtx('acme.objectos.ai'); |
| 72 | + const body = await handler(c); |
| 73 | + expect(body.features.aiStudio).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('keeps the static default when the plan is absent', async () => { |
| 77 | + const handler = await mountAndGetHandler({ |
| 78 | + resolveByHostname: async () => ({ environmentId: 'env3' }), // no plan |
| 79 | + }); |
| 80 | + const { c } = fakeCtx('acme.objectos.ai'); |
| 81 | + const body = await handler(c); |
| 82 | + expect(body.features.aiStudio).toBe(true); // default is true |
| 83 | + }); |
| 84 | + |
| 85 | + it('honours an explicit aiStudio=false default when plan is absent', async () => { |
| 86 | + const handler = await mountAndGetHandler({ |
| 87 | + pluginConfig: { aiStudio: false }, |
| 88 | + resolveByHostname: async () => ({ environmentId: 'env4' }), |
| 89 | + }); |
| 90 | + const { c } = fakeCtx('acme.objectos.ai'); |
| 91 | + const body = await handler(c); |
| 92 | + expect(body.features.aiStudio).toBe(false); |
| 93 | + }); |
| 94 | + |
| 95 | + it('a free plan overrides even an aiStudio=true default', async () => { |
| 96 | + const handler = await mountAndGetHandler({ |
| 97 | + pluginConfig: { aiStudio: true }, |
| 98 | + resolveByHostname: async () => ({ environmentId: 'env5', plan: 'FREE' }), |
| 99 | + }); |
| 100 | + const { c } = fakeCtx('acme.objectos.ai'); |
| 101 | + const body = await handler(c); |
| 102 | + expect(body.features.aiStudio).toBe(false); |
| 103 | + }); |
| 104 | +}); |
0 commit comments