|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The knowledge settings live and die with the knowledge service (#33933). |
| 5 | + * |
| 6 | + * A kernel that mounts `KnowledgeServicePlugin` beside `SettingsServicePlugin` |
| 7 | + * offers the knowledge page, the `ai` embedder section, both test actions and |
| 8 | + * the Setup entry; the same kernel without it offers none of them. This kernel |
| 9 | + * stands in for an app that mounts the service (none in this repository does): |
| 10 | + * the knowledge plugin is `use`d BEFORE the settings plugin on purpose, so the |
| 11 | + * pins also prove that its `optionalDependencies` declaration orders the |
| 12 | + * settings plugin ahead rather than the `use` order doing it by luck. |
| 13 | + * |
| 14 | + * The Setup entry is judged on the merged Setup tree the console reads (the |
| 15 | + * stock contributions plus every bundle registered through the `manifest` |
| 16 | + * service, folded by the ObjectQL registry), and against the hub list: the |
| 17 | + * knowledge page has exactly one sidebar entry exactly when the hub lists it. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect } from 'vitest'; |
| 21 | +import { LiteKernel } from '@objectstack/core'; |
| 22 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 23 | +import { ObjectQL } from '@objectstack/objectql'; |
| 24 | +import { SETUP_APP, SETUP_NAV_CONTRIBUTIONS } from '@objectstack/platform-objects'; |
| 25 | +import type { IHttpRequest, IHttpResponse, IHttpServer, RouteHandler } from '@objectstack/spec/contracts'; |
| 26 | +import { |
| 27 | + SettingsServicePlugin, |
| 28 | + registerSettingsRoutes, |
| 29 | + aiEmbedderSettingsSpecifiers, |
| 30 | + aiTestEmbedderActionHandler, |
| 31 | + knowledgeTestActionHandler, |
| 32 | + type SettingsService, |
| 33 | +} from '@objectstack/service-settings'; |
| 34 | +import { KnowledgeServicePlugin } from '../knowledge-service-plugin.js'; |
| 35 | + |
| 36 | +const EMBEDDER_KEYS = aiEmbedderSettingsSpecifiers.map((s) => s.key).filter((k): k is string => !!k); |
| 37 | +const SETTINGS_PAGE = /^\/apps\/setup\/system\/settings\/([a-z_]+)$/; |
| 38 | + |
| 39 | +interface NavItem { id?: string; type?: string; url?: string; children?: NavItem[] } |
| 40 | + |
| 41 | +/** The `manifest` service ObjectQL provides, reduced to a recorder. */ |
| 42 | +class FakeManifestPlugin implements Plugin { |
| 43 | + name = 'test.fake.manifest'; |
| 44 | + version = '1.0.0'; |
| 45 | + type = 'standard' as const; |
| 46 | + providesServices = ['manifest']; |
| 47 | + constructor(private readonly registered: unknown[]) {} |
| 48 | + async init(ctx: PluginContext): Promise<void> { |
| 49 | + ctx.registerService('manifest', { register: (m: unknown) => { this.registered.push(m); } }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +async function boot(withKnowledge: boolean) { |
| 54 | + const registered: unknown[] = []; |
| 55 | + const kernel = new LiteKernel(); |
| 56 | + kernel.use(new FakeManifestPlugin(registered)); |
| 57 | + if (withKnowledge) kernel.use(new KnowledgeServicePlugin({ enableEventSync: false })); |
| 58 | + kernel.use(new SettingsServicePlugin({ env: {}, registerRoutes: false })); |
| 59 | + await kernel.bootstrap(); |
| 60 | + const settings = kernel.getService<SettingsService>('settings'); |
| 61 | + return { kernel, settings, registered }; |
| 62 | +} |
| 63 | + |
| 64 | +/** The Configuration group of the merged Setup tree. */ |
| 65 | +function configurationEntries(registered: unknown[]): NavItem[] { |
| 66 | + const engine = new ObjectQL(); |
| 67 | + engine.registry.logLevel = 'silent'; |
| 68 | + engine.registerApp({ |
| 69 | + id: 'com.objectstack.test.setup-shell', namespace: 'sys', version: '0.0.0', type: 'plugin', scope: 'system', |
| 70 | + name: 'Setup shell (fixture)', apps: [SETUP_APP], navigationContributions: SETUP_NAV_CONTRIBUTIONS, |
| 71 | + } as any); |
| 72 | + for (const m of registered) engine.registerApp(m as any); |
| 73 | + const navigation = (engine.registry.getApp('setup') as { navigation?: NavItem[] } | undefined)?.navigation ?? []; |
| 74 | + return navigation.find((item) => item.id === 'group_configuration')?.children ?? []; |
| 75 | +} |
| 76 | + |
| 77 | +const knowledgeEntries = (registered: unknown[]) => |
| 78 | + configurationEntries(registered).filter((i) => i.type === 'url' && SETTINGS_PAGE.exec(i.url ?? '')?.[1] === 'knowledge'); |
| 79 | + |
| 80 | +async function namespaceValues(settings: SettingsService, ns: string): Promise<Record<string, unknown>> { |
| 81 | + const payload = await settings.getNamespace(ns); |
| 82 | + return Object.fromEntries(Object.entries(payload.values).map(([k, v]) => [k, (v as { value?: unknown }).value])); |
| 83 | +} |
| 84 | + |
| 85 | +describe('#33933 — knowledge settings are contributed by the mounted knowledge service', async () => { |
| 86 | + const mounted = await boot(true); |
| 87 | + const bare = await boot(false); |
| 88 | + |
| 89 | + it('reads two real hubs: both serve the default slate', () => { |
| 90 | + for (const { settings } of [mounted, bare]) { |
| 91 | + expect(settings.listManifests().map((m) => m.namespace)).toEqual(expect.arrayContaining(['ai', 'storage', 'mail'])); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('the knowledge page is offered with the service, and only with it', () => { |
| 96 | + expect(mounted.settings.listManifests().map((m) => m.namespace)).toContain('knowledge'); |
| 97 | + expect(bare.settings.listManifests().map((m) => m.namespace)).not.toContain('knowledge'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('the ai page carries the embedder section with the service, and only with it', () => { |
| 101 | + const keysOf = (s: SettingsService) => |
| 102 | + (s.getManifest('ai').specifiers as Array<{ key?: string; id?: string }>).map((x) => x.key ?? x.id); |
| 103 | + expect(EMBEDDER_KEYS.length).toBeGreaterThanOrEqual(6); |
| 104 | + expect(keysOf(mounted.settings)).toEqual(expect.arrayContaining([...EMBEDDER_KEYS, 'embedder', 'test_embedder'])); |
| 105 | + const bareKeys = keysOf(bare.settings); |
| 106 | + expect(bareKeys).toContain('provider'); |
| 107 | + for (const key of [...EMBEDDER_KEYS, 'embedder', 'test_embedder']) expect(bareKeys, key).not.toContain(key); |
| 108 | + }); |
| 109 | + |
| 110 | + it('both test buttons reach their handlers when the service is mounted', async () => { |
| 111 | + // "Reached" is judged by the handler's own answer for the same inputs, |
| 112 | + // never by message text: the no-handler answer is a different result. |
| 113 | + const knowledgeValues = await namespaceValues(mounted.settings, 'knowledge'); |
| 114 | + expect(await mounted.settings.runAction('knowledge', 'test', {})).toEqual( |
| 115 | + await knowledgeTestActionHandler({ namespace: 'knowledge', actionId: 'test', values: knowledgeValues, payload: {}, ctx: {} }), |
| 116 | + ); |
| 117 | + const aiValues = await namespaceValues(mounted.settings, 'ai'); |
| 118 | + const embedder = await mounted.settings.runAction('ai', 'test_embedder', {}); |
| 119 | + expect(embedder).toEqual( |
| 120 | + await aiTestEmbedderActionHandler({ namespace: 'ai', actionId: 'test_embedder', values: aiValues, payload: {}, ctx: {} }), |
| 121 | + ); |
| 122 | + expect(embedder.severity).toBe('warning'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('the Setup sidebar opens the knowledge page exactly once with the service, and never without it', () => { |
| 126 | + const entries = knowledgeEntries(mounted.registered); |
| 127 | + expect(entries).toHaveLength(1); |
| 128 | + expect(entries[0].id).toBe('nav_settings_knowledge'); |
| 129 | + expect(knowledgeEntries(bare.registered)).toHaveLength(0); |
| 130 | + // Non-vacuity: the bare tree is a real Setup tree with its AI entry. |
| 131 | + expect(configurationEntries(bare.registered).some((i) => i.id === 'nav_settings_ai')).toBe(true); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +class MockHttp implements IHttpServer { |
| 136 | + routes = new Map<string, RouteHandler>(); |
| 137 | + private add(method: string, path: string, handler: RouteHandler) { |
| 138 | + this.routes.set(`${method} ${path}`, handler); |
| 139 | + } |
| 140 | + get(path: string, h: RouteHandler) { this.add('GET', path, h); return this as any; } |
| 141 | + post(path: string, h: RouteHandler) { this.add('POST', path, h); return this as any; } |
| 142 | + put(path: string, h: RouteHandler) { this.add('PUT', path, h); return this as any; } |
| 143 | + delete(path: string, h: RouteHandler) { this.add('DELETE', path, h); return this as any; } |
| 144 | + patch(path: string, h: RouteHandler) { this.add('PATCH', path, h); return this as any; } |
| 145 | + use() { return this as any; } |
| 146 | + listen() { return Promise.resolve(); } |
| 147 | + close() { return Promise.resolve(); } |
| 148 | + getInstance() { return null; } |
| 149 | +} |
| 150 | + |
| 151 | +async function post(http: MockHttp, namespace: string, actionId: string, body: unknown) { |
| 152 | + const req = { params: { namespace, actionId }, query: {}, body, headers: {}, method: 'POST', path: '/' } as IHttpRequest; |
| 153 | + const state: { status: number; body?: any } = { status: 200 }; |
| 154 | + const res = { |
| 155 | + json: (data: unknown) => { state.body = data; }, |
| 156 | + send: () => {}, |
| 157 | + status: (code: number) => { state.status = code; return res; }, |
| 158 | + header: () => res, |
| 159 | + } as unknown as IHttpResponse; |
| 160 | + await http.routes.get('POST /api/settings/:namespace/:actionId')!(req, res); |
| 161 | + return state; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * The two buttons #24769 measured, at the HTTP door — moved here from |
| 166 | + * `@objectstack/service-settings`' `settings-default-action-handlers.pin.test.ts` |
| 167 | + * with #33933, because the composition that serves them is now the one that |
| 168 | + * mounts the knowledge service. |
| 169 | + */ |
| 170 | +describe('[#24769] the two buttons the knowledge service contributes, at the HTTP door', async () => { |
| 171 | + // A platform admin: both namespaces gate reads and writes (and so actions) on |
| 172 | + // `manage_platform_settings`. |
| 173 | + const admin = () => ({ enforced: true, permissions: ['manage_platform_settings'], userId: 'usr_admin' }); |
| 174 | + const { settings } = await boot(true); |
| 175 | + const http = new MockHttp(); |
| 176 | + registerSettingsRoutes(http, settings, { contextFromRequest: admin }); |
| 177 | + |
| 178 | + it('POST /api/settings/ai/test_embedder is answered by the embedder check (200, not 400)', async () => { |
| 179 | + const answer = await post(http, 'ai', 'test_embedder', { |
| 180 | + values: { embedder_provider: 'openai', embedder_api_key: 'sk-test', embedder_model: 'text-embedding-3-small' }, |
| 181 | + }); |
| 182 | + expect(answer.status).toBe(200); |
| 183 | + expect(answer.body?.data?.ok).toBe(true); |
| 184 | + expect(answer.body?.data?.message).toContain('text-embedding-3-small'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('POST /api/settings/knowledge/test is answered by the adapter check (200, not 400)', async () => { |
| 188 | + const answer = await post(http, 'knowledge', 'test', { |
| 189 | + values: { adapter: 'ragflow', ragflow_base_url: 'http://localhost:9380', ragflow_api_key: 'k' }, |
| 190 | + }); |
| 191 | + expect(answer.status).toBe(200); |
| 192 | + expect(answer.body?.data?.ok).toBe(true); |
| 193 | + expect(answer.body?.data?.message).toContain('localhost:9380'); |
| 194 | + }); |
| 195 | +}); |
0 commit comments