|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Test scaffolding: a settings door over the real `ObjectQL` engine, the real |
| 5 | + * platform setting objects and the real `PUT /api/settings/:namespace` route, |
| 6 | + * with a Map driver underneath. Shared by the door suites that judge a value |
| 7 | + * shape end to end (#31241 `json`, #30208 `map`). |
| 8 | + */ |
| 9 | + |
| 10 | +import { vi } from 'vitest'; |
| 11 | +import { ObjectQL } from '@objectstack/objectql'; |
| 12 | +import { SysPlatformSetting, SysSecret, SysSetting } from '@objectstack/platform-objects/system'; |
| 13 | +import type { IHttpServer, IHttpRequest, IHttpResponse, RouteHandler } from '@objectstack/spec/contracts'; |
| 14 | +import type { SettingsManifest } from '@objectstack/spec/system'; |
| 15 | +import { SettingsService } from '../settings-service.js'; |
| 16 | +import { SettingsServicePlugin, wrapEngineAsSettingsEngine } from '../settings-service-plugin.js'; |
| 17 | +import { withEngineRegistry } from './settings-organization.testkit.js'; |
| 18 | +import { LocalCryptoProvider } from '../local-crypto-provider.js'; |
| 19 | +import { registerSettingsRoutes } from '../settings-routes.js'; |
| 20 | + |
| 21 | +const OWNER_PACKAGE = 'com.objectstack.test.settings-door'; |
| 22 | + |
| 23 | +/** A driver over plain Maps — the verbs the settings path reaches through `ObjectQL`. */ |
| 24 | +export function makeMemoryDriver() { |
| 25 | + const store = new Map<string, Map<string, Record<string, unknown>>>(); |
| 26 | + let nextId = 0; |
| 27 | + const copy = (r: Record<string, unknown>) => ({ ...r }); |
| 28 | + const rowsOf = (object: string) => { |
| 29 | + let s = store.get(object); |
| 30 | + if (!s) { s = new Map(); store.set(object, s); } |
| 31 | + return s; |
| 32 | + }; |
| 33 | + // `$or` is the one combinator the settings reads emit; anything else refuses, |
| 34 | + // so a predicate this double cannot express never reads as "no such row". |
| 35 | + const matches = (row: Record<string, unknown>, where: any): boolean => { |
| 36 | + if (!where || typeof where !== 'object') return true; |
| 37 | + return Object.entries(where).every(([k, v]) => { |
| 38 | + if (k === '$or') return (v as any[]).some((b) => matches(row, b)); |
| 39 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 40 | + return (row[k] ?? null) === (v ?? null); |
| 41 | + }); |
| 42 | + }; |
| 43 | + const driver: any = { |
| 44 | + name: 'memory', version: '0.0.0', supports: {} as any, |
| 45 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, |
| 46 | + async execute() { return null; }, |
| 47 | + async find(object: string, ast: any) { |
| 48 | + // The caller's bound by PRESENCE, after the filter and before the copy. |
| 49 | + const hits = [...rowsOf(object).values()].filter((r) => matches(r, ast?.where)); |
| 50 | + const page = typeof ast?.limit === 'number' ? hits.slice(0, ast.limit) : hits; |
| 51 | + return page.map(copy); |
| 52 | + }, |
| 53 | + async findOne(object: string, ast: any) { |
| 54 | + for (const r of rowsOf(object).values()) if (matches(r, ast?.where)) return copy(r); |
| 55 | + return null; |
| 56 | + }, |
| 57 | + async create(object: string, data: Record<string, unknown>) { |
| 58 | + nextId += 1; |
| 59 | + const id = (data.id as string) ?? `row_${nextId}`; |
| 60 | + const row = { ...data, id }; |
| 61 | + rowsOf(object).set(id, row); |
| 62 | + return copy(row); |
| 63 | + }, |
| 64 | + async update(object: string, id: string, data: Record<string, unknown>) { |
| 65 | + const s = rowsOf(object); |
| 66 | + const cur = s.get(id); |
| 67 | + if (!cur) return null; |
| 68 | + const next = { ...cur, ...data, id }; |
| 69 | + s.set(id, next); |
| 70 | + return copy(next); |
| 71 | + }, |
| 72 | + async upsert(object: string, data: Record<string, unknown>) { |
| 73 | + const id = data.id as string | undefined; |
| 74 | + return id && rowsOf(object).has(id) ? this.update(object, id, data) : this.create(object, data); |
| 75 | + }, |
| 76 | + async delete(object: string, id: string) { return rowsOf(object).delete(id); }, |
| 77 | + async count(object: string, ast: any) { return (await this.find(object, ast)).length; }, |
| 78 | + async bulkCreate(object: string, rows: Record<string, unknown>[]) { |
| 79 | + return Promise.all(rows.map((r) => this.create(object, r))); |
| 80 | + }, |
| 81 | + async bulkUpdate() { return []; }, |
| 82 | + async bulkDelete() {}, |
| 83 | + async updateMany(object: string, ast: any, data: Record<string, unknown>) { |
| 84 | + const rows = await this.find(object, ast); |
| 85 | + const s = rowsOf(object); |
| 86 | + for (const r of rows) s.set(r.id as string, { ...s.get(r.id as string), ...data, id: r.id }); |
| 87 | + return rows.length; |
| 88 | + }, |
| 89 | + async deleteMany(object: string, ast: any) { |
| 90 | + const rows = await this.find(object, ast); |
| 91 | + for (const r of rows) rowsOf(object).delete(r.id as string); |
| 92 | + return rows.length; |
| 93 | + }, |
| 94 | + async syncSchema() {}, async dropTable() {}, |
| 95 | + async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; }, |
| 96 | + async commit() {}, async rollback() {}, |
| 97 | + }; |
| 98 | + return { driver, rowsOf }; |
| 99 | +} |
| 100 | + |
| 101 | +export class MockHttp implements IHttpServer { |
| 102 | + routes = new Map<string, RouteHandler>(); |
| 103 | + private add(method: string, path: string, h: RouteHandler) { this.routes.set(`${method} ${path}`, h); } |
| 104 | + get(p: string, h: RouteHandler) { this.add('GET', p, h); return this as any; } |
| 105 | + post(p: string, h: RouteHandler) { this.add('POST', p, h); return this as any; } |
| 106 | + put(p: string, h: RouteHandler) { this.add('PUT', p, h); return this as any; } |
| 107 | + delete(p: string, h: RouteHandler) { this.add('DELETE', p, h); return this as any; } |
| 108 | + patch(p: string, h: RouteHandler) { this.add('PATCH', p, h); return this as any; } |
| 109 | + use() { return this as any; } |
| 110 | + listen() { return Promise.resolve(); } |
| 111 | + close() { return Promise.resolve(); } |
| 112 | + getInstance() { return null; } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * The pieces the running server binds: the real `ObjectQL` engine over a Map |
| 117 | + * driver, the platform setting objects, a `SettingsService` with `manifest` |
| 118 | + * registered, and the settings routes. `before` runs on the service before the |
| 119 | + * manifest registers (an option source, as a plugin registers one). |
| 120 | + */ |
| 121 | +export async function bootSettingsDoor( |
| 122 | + manifest: SettingsManifest, |
| 123 | + options: { before?: (svc: SettingsService) => void } = {}, |
| 124 | +) { |
| 125 | + const engine = new ObjectQL(); |
| 126 | + const { driver, rowsOf } = makeMemoryDriver(); |
| 127 | + engine.registerDriver(driver, true); |
| 128 | + await engine.init(); |
| 129 | + for (const o of [SysSetting, SysPlatformSetting, SysSecret]) { |
| 130 | + engine.registry.registerObject(o as any, OWNER_PACKAGE); |
| 131 | + } |
| 132 | + const svc = new SettingsService({ |
| 133 | + env: {}, |
| 134 | + engine: withEngineRegistry(wrapEngineAsSettingsEngine(engine as any), engine), |
| 135 | + cryptoProvider: new LocalCryptoProvider(), |
| 136 | + secretStore: (new SettingsServicePlugin() as any).buildSecretStore(engine), |
| 137 | + }); |
| 138 | + options.before?.(svc); |
| 139 | + svc.registerManifest(manifest); |
| 140 | + const http = new MockHttp(); |
| 141 | + registerSettingsRoutes(http, svc, { |
| 142 | + contextFromRequest: () => ({ enforced: true, permissions: ['manage_platform_settings'], userId: 'usr_admin' }), |
| 143 | + }); |
| 144 | + const put = http.routes.get('PUT /api/settings/:namespace')!; |
| 145 | + const namespace = manifest.namespace; |
| 146 | + /** One `PUT /api/settings/:namespace` with `body`, as the console sends it (a flat patch). */ |
| 147 | + const save = async (body: Record<string, unknown>) => { |
| 148 | + const req: IHttpRequest = { params: { namespace }, query: {}, body, headers: {}, method: 'PUT', path: '/' }; |
| 149 | + const state: { status: number; body?: any } = { status: 200 }; |
| 150 | + const res: IHttpResponse = { |
| 151 | + json: vi.fn((d) => { state.body = d; }) as any, |
| 152 | + send: vi.fn() as any, |
| 153 | + status: vi.fn((c: number) => { state.status = c; return res; }) as any, |
| 154 | + header: vi.fn(() => res) as any, |
| 155 | + }; |
| 156 | + await put(req, res); |
| 157 | + return state; |
| 158 | + }; |
| 159 | + const stored = (key: string) => |
| 160 | + [...rowsOf('sys_platform_setting').values()].find((r) => r.namespace === namespace && r.key === key); |
| 161 | + const seed = (row: Record<string, unknown>) => |
| 162 | + rowsOf('sys_platform_setting').set(String(row.id), { encrypted: false, locked: false, value_enc: null, ...row }); |
| 163 | + return { svc, save, stored, seed }; |
| 164 | +} |
0 commit comments