|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Adapter provenance — `GET /api/v1/ai/status` and the settings-apply |
| 4 | +// status tracking on AIServicePlugin. Persisted settings silently override |
| 5 | +// env auto-detection, so the plugin must expose WHICH config is live and |
| 6 | +// WHY a saved config was not applied (broken settings used to be |
| 7 | +// indistinguishable from working ones). |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 10 | +import { AIServicePlugin } from '../plugin.js'; |
| 11 | +import { AIService } from '../ai-service.js'; |
| 12 | +import { buildAIRoutes } from '../routes/ai-routes.js'; |
| 13 | +import { InMemoryConversationService } from '../conversation/in-memory-conversation-service.js'; |
| 14 | +import { MemoryLLMAdapter } from '../adapters/memory-adapter.js'; |
| 15 | +import type { Logger } from '@objectstack/spec/contracts'; |
| 16 | + |
| 17 | +const silentLogger: Logger = { |
| 18 | + debug: vi.fn(), |
| 19 | + info: vi.fn(), |
| 20 | + warn: vi.fn(), |
| 21 | + error: vi.fn(), |
| 22 | + fatal: vi.fn(), |
| 23 | +}; |
| 24 | + |
| 25 | +function createMockContext() { |
| 26 | + const services = new Map<string, unknown>(); |
| 27 | + const hooks = new Map<string, Function[]>(); |
| 28 | + services.set('manifest', { register: vi.fn() }); |
| 29 | + |
| 30 | + return { |
| 31 | + services, |
| 32 | + hooks, |
| 33 | + registerService: vi.fn((name: string, service: unknown) => services.set(name, service)), |
| 34 | + replaceService: vi.fn((name: string, service: unknown) => services.set(name, service)), |
| 35 | + getService: vi.fn(<T,>(name: string): T => { |
| 36 | + if (!services.has(name)) throw new Error(`Service "${name}" not found`); |
| 37 | + return services.get(name) as T; |
| 38 | + }), |
| 39 | + getServices: vi.fn(() => services), |
| 40 | + hook: vi.fn((name: string, handler: Function) => { |
| 41 | + if (!hooks.has(name)) hooks.set(name, []); |
| 42 | + hooks.get(name)!.push(handler); |
| 43 | + }), |
| 44 | + trigger: vi.fn(async () => {}), |
| 45 | + logger: silentLogger, |
| 46 | + getKernel: vi.fn(), |
| 47 | + } as any; |
| 48 | +} |
| 49 | + |
| 50 | +async function fireHook(ctx: any, name: string): Promise<void> { |
| 51 | + for (const handler of ctx.hooks.get(name) ?? []) await handler(); |
| 52 | +} |
| 53 | + |
| 54 | +/** Settings-service stub returning the given `ai` namespace values. */ |
| 55 | +function settingsStub(values: Record<string, { value: unknown; source?: string }>) { |
| 56 | + const actions = new Map<string, Function>(); |
| 57 | + return { |
| 58 | + actions, |
| 59 | + getNamespace: vi.fn(async () => ({ values })), |
| 60 | + subscribe: vi.fn(), |
| 61 | + registerAction: vi.fn((ns: string, id: string, fn: Function) => actions.set(`${ns}/${id}`, fn)), |
| 62 | + resetNamespace: vi.fn(async () => 3), |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +const AI_ENV_KEYS = [ |
| 67 | + 'AI_GATEWAY_MODEL', |
| 68 | + 'AI_GATEWAY_API_KEY', |
| 69 | + 'OPENAI_API_KEY', |
| 70 | + 'ANTHROPIC_API_KEY', |
| 71 | + 'GOOGLE_GENERATIVE_AI_API_KEY', |
| 72 | +]; |
| 73 | + |
| 74 | +describe('GET /api/v1/ai/status route', () => { |
| 75 | + it('returns adapter name plus the provenance from getAdapterStatus', async () => { |
| 76 | + const service = new AIService({ |
| 77 | + adapter: new MemoryLLMAdapter(), |
| 78 | + conversationService: new InMemoryConversationService(), |
| 79 | + }); |
| 80 | + const routes = buildAIRoutes(service, service.conversationService, silentLogger, { |
| 81 | + getAdapterStatus: () => ({ |
| 82 | + description: 'Vercel AI Gateway (model: anthropic/claude-sonnet-4.6)', |
| 83 | + source: 'env', |
| 84 | + provider: 'gateway', |
| 85 | + model: 'anthropic/claude-sonnet-4.6', |
| 86 | + settingsError: null, |
| 87 | + }), |
| 88 | + }); |
| 89 | + const statusRoute = routes.find(r => r.method === 'GET' && r.path === '/api/v1/ai/status'); |
| 90 | + expect(statusRoute).toBeDefined(); |
| 91 | + expect(statusRoute!.permissions).toEqual(['ai:read']); |
| 92 | + |
| 93 | + const response = await statusRoute!.handler({}); |
| 94 | + expect(response.status).toBe(200); |
| 95 | + expect(response.body).toMatchObject({ |
| 96 | + adapter: 'memory', |
| 97 | + source: 'env', |
| 98 | + provider: 'gateway', |
| 99 | + model: 'anthropic/claude-sonnet-4.6', |
| 100 | + settingsError: null, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('still serves adapter name when no status getter is wired', async () => { |
| 105 | + const service = new AIService({ |
| 106 | + adapter: new MemoryLLMAdapter(), |
| 107 | + conversationService: new InMemoryConversationService(), |
| 108 | + }); |
| 109 | + const routes = buildAIRoutes(service, service.conversationService, silentLogger); |
| 110 | + const statusRoute = routes.find(r => r.path === '/api/v1/ai/status')!; |
| 111 | + const response = await statusRoute.handler({}); |
| 112 | + expect(response.status).toBe(200); |
| 113 | + expect((response.body as any).adapter).toBe('memory'); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('AIServicePlugin adapter provenance', () => { |
| 118 | + const savedEnv: Record<string, string | undefined> = {}; |
| 119 | + |
| 120 | + beforeEach(() => { |
| 121 | + for (const key of AI_ENV_KEYS) { |
| 122 | + savedEnv[key] = process.env[key]; |
| 123 | + delete process.env[key]; |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + afterEach(() => { |
| 128 | + for (const key of AI_ENV_KEYS) { |
| 129 | + if (savedEnv[key] === undefined) delete process.env[key]; |
| 130 | + else process.env[key] = savedEnv[key]; |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + async function statusOf(ctx: any): Promise<Record<string, unknown>> { |
| 135 | + // The plugin caches routes on trigger('ai:routes', routes). |
| 136 | + const call = ctx.trigger.mock.calls.find((c: unknown[]) => c[0] === 'ai:routes'); |
| 137 | + const routes = call![1] as Array<{ path: string; handler: (req: object) => Promise<{ body?: unknown }> }>; |
| 138 | + const route = routes.find(r => r.path === '/api/v1/ai/status')!; |
| 139 | + return (await route.handler({})).body as Record<string, unknown>; |
| 140 | + } |
| 141 | + |
| 142 | + it('reports source=fallback when nothing is configured', async () => { |
| 143 | + const plugin = new AIServicePlugin(); |
| 144 | + const ctx = createMockContext(); |
| 145 | + await plugin.init(ctx); |
| 146 | + await plugin.start!(ctx); |
| 147 | + |
| 148 | + const body = await statusOf(ctx); |
| 149 | + expect(body.adapter).toBe('memory'); |
| 150 | + expect(body.source).toBe('fallback'); |
| 151 | + expect(body.provider).toBe('memory'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('reports source=explicit for a constructor-supplied adapter', async () => { |
| 155 | + const plugin = new AIServicePlugin({ |
| 156 | + adapter: { |
| 157 | + name: 'custom-test', |
| 158 | + chat: async () => ({ content: 'ok' }), |
| 159 | + complete: async () => ({ content: '' }), |
| 160 | + }, |
| 161 | + }); |
| 162 | + const ctx = createMockContext(); |
| 163 | + await plugin.init(ctx); |
| 164 | + await plugin.start!(ctx); |
| 165 | + |
| 166 | + const body = await statusOf(ctx); |
| 167 | + expect(body.adapter).toBe('custom-test'); |
| 168 | + expect(body.source).toBe('explicit'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('flags settingsError when saved settings cannot build an adapter (broken cloudflare)', async () => { |
| 172 | + const plugin = new AIServicePlugin(); |
| 173 | + const ctx = createMockContext(); |
| 174 | + // provider=cloudflare with an empty key — exactly the broken leftover |
| 175 | + // a half-filled Setup form produces. |
| 176 | + ctx.services.set('settings', settingsStub({ |
| 177 | + provider: { value: 'cloudflare', source: 'database' }, |
| 178 | + cloudflare_account_id: { value: '2846eb40a60f4738e292b90dcd8cce10', source: 'database' }, |
| 179 | + cloudflare_api_key: { value: '', source: 'database' }, |
| 180 | + cloudflare_model: { value: 'claude/sonnet-4.6', source: 'database' }, |
| 181 | + })); |
| 182 | + |
| 183 | + await plugin.init(ctx); |
| 184 | + await plugin.start!(ctx); |
| 185 | + await fireHook(ctx, 'kernel:ready'); |
| 186 | + |
| 187 | + const body = await statusOf(ctx); |
| 188 | + // The broken settings must NOT have replaced the fallback adapter… |
| 189 | + expect(body.adapter).toBe('memory'); |
| 190 | + expect(body.source).toBe('fallback'); |
| 191 | + // …and the failure must be visible, naming the saved provider. |
| 192 | + expect(body.settingsProvider).toBe('cloudflare'); |
| 193 | + expect(String(body.settingsError)).toContain('cloudflare'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('reports source=settings when saved settings apply cleanly', async () => { |
| 197 | + const plugin = new AIServicePlugin(); |
| 198 | + const ctx = createMockContext(); |
| 199 | + // `memory` stored explicitly (source=database) is a valid, buildable choice. |
| 200 | + ctx.services.set('settings', settingsStub({ |
| 201 | + provider: { value: 'memory', source: 'database' }, |
| 202 | + })); |
| 203 | + |
| 204 | + await plugin.init(ctx); |
| 205 | + await plugin.start!(ctx); |
| 206 | + await fireHook(ctx, 'kernel:ready'); |
| 207 | + |
| 208 | + const body = await statusOf(ctx); |
| 209 | + expect(body.source).toBe('settings'); |
| 210 | + expect(body.settingsProvider).toBe('memory'); |
| 211 | + expect(body.settingsError).toBeNull(); |
| 212 | + }); |
| 213 | + |
| 214 | + it('ai/reset clears saved values and re-runs env adapter detection', async () => { |
| 215 | + const plugin = new AIServicePlugin(); |
| 216 | + const ctx = createMockContext(); |
| 217 | + const settings = settingsStub({ |
| 218 | + provider: { value: 'memory', source: 'database' }, |
| 219 | + }); |
| 220 | + ctx.services.set('settings', settings); |
| 221 | + |
| 222 | + await plugin.init(ctx); |
| 223 | + await plugin.start!(ctx); |
| 224 | + await fireHook(ctx, 'kernel:ready'); |
| 225 | + |
| 226 | + // Saved settings are in effect… |
| 227 | + expect((await statusOf(ctx)).source).toBe('settings'); |
| 228 | + |
| 229 | + // …then the operator hits "Reset to environment defaults". |
| 230 | + const reset = settings.actions.get('ai/reset'); |
| 231 | + expect(reset).toBeDefined(); |
| 232 | + const result = await reset!({ ctx: {} }); |
| 233 | + expect(result.ok).toBe(true); |
| 234 | + expect(result.message).toContain('Cleared 3'); |
| 235 | + expect(settings.resetNamespace).toHaveBeenCalledWith('ai', {}); |
| 236 | + |
| 237 | + const body = await statusOf(ctx); |
| 238 | + // No AI env vars in this test → detection falls back to echo mode. |
| 239 | + expect(body.source).toBe('fallback'); |
| 240 | + expect(body.settingsProvider).toBeUndefined(); |
| 241 | + expect(body.settingsError).toBeNull(); |
| 242 | + }); |
| 243 | + |
| 244 | + it('keeps env provenance and clears settingsError when no settings are saved', async () => { |
| 245 | + const plugin = new AIServicePlugin(); |
| 246 | + const ctx = createMockContext(); |
| 247 | + ctx.services.set('settings', settingsStub({ |
| 248 | + provider: { value: 'memory', source: 'default' }, |
| 249 | + })); |
| 250 | + |
| 251 | + await plugin.init(ctx); |
| 252 | + await plugin.start!(ctx); |
| 253 | + await fireHook(ctx, 'kernel:ready'); |
| 254 | + |
| 255 | + const body = await statusOf(ctx); |
| 256 | + expect(body.source).toBe('fallback'); |
| 257 | + expect(body.settingsProvider).toBeUndefined(); |
| 258 | + expect(body.settingsError).toBeNull(); |
| 259 | + }); |
| 260 | +}); |
0 commit comments