|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `GET /forms/:slug` translates a public form view that a `.custom` pair |
| 4 | +// overrides (ADR-0146 D2) in the same label order the `/meta/view` reads serve |
| 5 | +// it in (ruling record 6631250894, restated by 6942920667): a translation the |
| 6 | +// pair ships, then the override's own text, then the base's translation. |
| 7 | +// |
| 8 | +// The ORDER is `@objectstack/spec/system`'s (`labelCatalogs`), and the pair's |
| 9 | +// translations are the protocol's to answer (`servedOverrideBaseOf` + |
| 10 | +// `getPairTranslationBundle`, asked through `pairTranslationBundleOf`). What is |
| 11 | +// pinned here is the ROUTE's half: that the view it serves is handed the |
| 12 | +// pair's translations beside its packaged base, so the anonymous visitor reads |
| 13 | +// the text the authenticated `/meta` reader reads. |
| 14 | +// |
| 15 | +// Every cell carries a CONTROL leg through `GET /meta/view/:name` on the same |
| 16 | +// fixture. The pair rank needs the override envelope the read stamped on the |
| 17 | +// served document (`_packageId` = the base, `overrides.package` = the base); a |
| 18 | +// fixture that lost it would leave both surfaces on the override's own text, |
| 19 | +// and the form pin would pass with the wiring absent. The control fails first. |
| 20 | +// |
| 21 | +// The object half of the form (its field labels) is out of this file: an |
| 22 | +// `object` is extended, never replaced (ADR-0146 D10), so it never answers |
| 23 | +// `servedOverrideBaseOf` and has no pair rank on `/meta` either. |
| 24 | + |
| 25 | +import { describe, it, expect, vi } from 'vitest'; |
| 26 | +import { SchemaRegistry, assertEngineFindOnePredicate } from '@objectstack/objectql'; |
| 27 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 28 | +import { expandViewContainer } from '@objectstack/spec/ui'; |
| 29 | +import { RestServer } from './rest-server.js'; |
| 30 | + |
| 31 | +// Pay the first transform of the dist-resolved workspace dep the handlers reach |
| 32 | +// through a dynamic `import()` at MODULE LOAD, not inside a clocked `it()` body |
| 33 | +// (`scripts/check-test-source-alias.mjs`). |
| 34 | +import '@objectstack/spec/system'; |
| 35 | + |
| 36 | +const OBJ = 'ticket'; |
| 37 | +const BASE_PKG = 'com.example.helpdesk'; |
| 38 | +const PAIR_PKG = `${BASE_PKG}.custom`; |
| 39 | +const VIEW = `${OBJ}.contact`; |
| 40 | +const SLUG = 'ticket-contact'; |
| 41 | + |
| 42 | +const TITLE_EN = 'Contact us'; |
| 43 | +const TITLE_ZH = '联系我们'; |
| 44 | +const ABOUT_EN = 'Tell us about yourself'; |
| 45 | +const ABOUT_ZH = '介绍一下你自己'; |
| 46 | +const ABOUT_DESC_EN = 'We will get back to you within a day.'; |
| 47 | +const ABOUT_DESC_ZH = '我们会在一天内回复你。'; |
| 48 | + |
| 49 | +/** The override's own text: what the pair's declared override wrote over the base. */ |
| 50 | +const TITLE_OV = 'Contact the helpdesk (pair override)'; |
| 51 | +const ABOUT_OV = 'About you (pair override)'; |
| 52 | +/** The pair's own zh-CN translation of the text its override wrote. */ |
| 53 | +const TITLE_PAIR_ZH = '联系服务台(配对包译文)'; |
| 54 | +const ABOUT_PAIR_ZH = '关于你(配对包译文)'; |
| 55 | + |
| 56 | +const ticketObject = { |
| 57 | + name: OBJ, |
| 58 | + label: 'Ticket', |
| 59 | + fields: { |
| 60 | + subject: { type: 'text', label: 'Subject' }, |
| 61 | + email: { type: 'text', label: 'Email' }, |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +/** The `defineView` container as the code package ships it: one public form under `formViews.contact`. */ |
| 66 | +const ticketContainer = () => ({ |
| 67 | + object: OBJ, |
| 68 | + formViews: { |
| 69 | + contact: { |
| 70 | + type: 'simple', |
| 71 | + title: TITLE_EN, |
| 72 | + data: { provider: 'object', object: OBJ }, |
| 73 | + sharing: { enabled: true, allowAnonymous: true, publicLink: `/forms/${SLUG}` }, |
| 74 | + sections: [ |
| 75 | + { name: 'about', label: ABOUT_EN, description: ABOUT_DESC_EN, fields: ['subject'] }, |
| 76 | + { name: 'details', label: 'Details', fields: ['email'] }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }, |
| 80 | +}); |
| 81 | + |
| 82 | +/** The packaged form item as the boot registers it (`expandViewContainer`). */ |
| 83 | +const shippedFormItem = (): any => { |
| 84 | + const item = expandViewContainer(OBJ, ticketContainer()).find((v) => v.name === VIEW); |
| 85 | + if (!item) throw new Error(`fixture missing ${VIEW}`); |
| 86 | + return item; |
| 87 | +}; |
| 88 | + |
| 89 | +/** The deployment's code catalog: `en` repeats the shipped strings, `zh-CN` translates them. */ |
| 90 | +const CODE_CATALOG: Record<string, Record<string, any>> = { |
| 91 | + en: { |
| 92 | + objects: { |
| 93 | + [OBJ]: { |
| 94 | + _views: { contact: { title: TITLE_EN } }, |
| 95 | + _sections: { about: { label: ABOUT_EN, description: ABOUT_DESC_EN } }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + 'zh-CN': { |
| 100 | + objects: { |
| 101 | + [OBJ]: { |
| 102 | + _views: { contact: { title: TITLE_ZH } }, |
| 103 | + _sections: { about: { label: ABOUT_ZH, description: ABOUT_DESC_ZH } }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | +}; |
| 108 | + |
| 109 | +/** The pair's own zh-CN rows for the two strings its override changed. */ |
| 110 | +const PAIR_ZH_DATA = { |
| 111 | + objects: { |
| 112 | + [OBJ]: { |
| 113 | + _views: { contact: { title: TITLE_PAIR_ZH } }, |
| 114 | + _sections: { about: { label: ABOUT_PAIR_ZH } }, |
| 115 | + }, |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
| 119 | +/** Deep merge, `top` over `under`, of two translation-data groups. */ |
| 120 | +const mergeData = (under: Record<string, any>, top: Record<string, any>): Record<string, any> => { |
| 121 | + const out: Record<string, any> = { ...under }; |
| 122 | + for (const [k, v] of Object.entries(top)) { |
| 123 | + out[k] = v && typeof v === 'object' && !Array.isArray(v) && out[k] && typeof out[k] === 'object' |
| 124 | + ? mergeData(out[k], v) |
| 125 | + : v; |
| 126 | + } |
| 127 | + return out; |
| 128 | +}; |
| 129 | + |
| 130 | +/** One `where` value as the store reads it: `{ $in: [...] }` or equality. */ |
| 131 | +const whereValueMatches = (actual: unknown, want: unknown): boolean => |
| 132 | + want !== null && typeof want === 'object' && Array.isArray((want as { $in?: unknown }).$in) |
| 133 | + ? (want as { $in: unknown[] }).$in.includes(actual) |
| 134 | + : actual === want; |
| 135 | + |
| 136 | +function createMockServer() { |
| 137 | + return { |
| 138 | + get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), patch: vi.fn(), |
| 139 | + use: vi.fn(), |
| 140 | + listen: vi.fn().mockResolvedValue(undefined), |
| 141 | + close: vi.fn().mockResolvedValue(undefined), |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +interface Seed { |
| 146 | + /** Seed the pair's declared override of the form view. */ |
| 147 | + readonly override: boolean; |
| 148 | + /** Seed the pair's own zh-CN translation of the text its override wrote. */ |
| 149 | + readonly pairZh: boolean; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * The REAL protocol over a real `SchemaRegistry`, the container registered the |
| 154 | + * way the boot registers a `defineView` container, and the `.custom` pair's |
| 155 | + * rows seeded the way Studio's Customize publishes them: the override row bound |
| 156 | + * to the pair (`package_id: <base>.custom`) declaring |
| 157 | + * `overrides: { package: <base> }`, and the pair's translation an active |
| 158 | + * `translation` row bound to the pair. The i18n service serves the deployment |
| 159 | + * catalog: the code catalog with every authored row merged over it, the pair's |
| 160 | + * row included, its package dropped. |
| 161 | + */ |
| 162 | +function makeHost(seed: Seed) { |
| 163 | + const registry = new SchemaRegistry({ multiTenant: false }); |
| 164 | + registry.logLevel = 'silent'; |
| 165 | + registry.registerObject(ticketObject as any, BASE_PKG); |
| 166 | + const container = ticketContainer(); |
| 167 | + registry.registerItem('view', { name: OBJ, ...container }, 'name', BASE_PKG); |
| 168 | + for (const item of expandViewContainer(OBJ, container)) registry.registerItem('view', item, 'name', BASE_PKG); |
| 169 | + |
| 170 | + const rows: Record<string, unknown>[] = []; |
| 171 | + if (seed.override) { |
| 172 | + const shipped = shippedFormItem(); |
| 173 | + const overridden = { |
| 174 | + ...shipped, |
| 175 | + config: { |
| 176 | + ...shipped.config, |
| 177 | + title: TITLE_OV, |
| 178 | + sections: shipped.config.sections.map((s: any) => (s.name === 'about' ? { ...s, label: ABOUT_OV } : s)), |
| 179 | + }, |
| 180 | + overrides: { package: BASE_PKG, basedOn: '1.0.0' }, |
| 181 | + }; |
| 182 | + rows.push({ |
| 183 | + id: 'ov_form', type: 'view', name: VIEW, package_id: PAIR_PKG, organization_id: null, state: 'active', |
| 184 | + metadata: JSON.stringify(overridden), |
| 185 | + }); |
| 186 | + } |
| 187 | + if (seed.pairZh) { |
| 188 | + rows.push({ |
| 189 | + id: 'tr_pair_zh', type: 'translation', name: 'helpdesk_custom_zh', package_id: PAIR_PKG, organization_id: null, |
| 190 | + state: 'active', metadata: JSON.stringify({ name: 'helpdesk_custom_zh', locale: 'zh-CN', ...PAIR_ZH_DATA }), |
| 191 | + }); |
| 192 | + } |
| 193 | + |
| 194 | + const merged: Record<string, any> = { |
| 195 | + en: CODE_CATALOG.en, |
| 196 | + 'zh-CN': seed.pairZh ? mergeData(CODE_CATALOG['zh-CN'], PAIR_ZH_DATA) : CODE_CATALOG['zh-CN'], |
| 197 | + }; |
| 198 | + const i18n = { |
| 199 | + getLocales: () => ['en', 'zh-CN'], |
| 200 | + getTranslations: (locale: string) => merged[locale], |
| 201 | + getDefaultLocale: () => 'en', |
| 202 | + }; |
| 203 | + |
| 204 | + const matches = (r: Record<string, unknown>, w: Record<string, unknown>) => |
| 205 | + Object.entries(w ?? {}).every(([k, v]) => { |
| 206 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 207 | + return whereValueMatches(r[k] ?? null, v ?? null); |
| 208 | + }); |
| 209 | + const engine = { |
| 210 | + registry, |
| 211 | + find: async (table: string, q: { where: Record<string, unknown>; limit?: number }) => { |
| 212 | + if (table !== 'sys_metadata') return []; |
| 213 | + const matched = rows.filter((r) => matches(r, q.where)); |
| 214 | + return typeof q?.limit === 'number' ? matched.slice(0, q.limit) : matched; |
| 215 | + }, |
| 216 | + findOne: async (table: string, q: { where: Record<string, unknown> }) => { |
| 217 | + assertEngineFindOnePredicate(table, q); |
| 218 | + return table === 'sys_metadata' ? rows.find((r) => matches(r, q.where)) ?? null : null; |
| 219 | + }, |
| 220 | + }; |
| 221 | + const protocol = new ObjectStackProtocolImplementation(engine as never, undefined, 'env_test'); |
| 222 | + const rest = new RestServer( |
| 223 | + createMockServer() as never, protocol as never, { api: { requireAuth: false } } as never, |
| 224 | + undefined, undefined, undefined, undefined, undefined, |
| 225 | + undefined, undefined, undefined, undefined, undefined, |
| 226 | + // i18nServiceProvider — the 14th constructor argument. |
| 227 | + async () => i18n as never, |
| 228 | + ); |
| 229 | + (rest as unknown as { resolveExecCtx: () => Promise<unknown> }).resolveExecCtx = async () => ({ |
| 230 | + userId: 'u_admin', systemPermissions: ['manage_metadata', 'studio.access', 'setup.access'], |
| 231 | + }); |
| 232 | + rest.registerRoutes(); |
| 233 | + const routes = rest.getRouteManager(); |
| 234 | + const run = async (path: string, params: Record<string, string>, locale: string) => { |
| 235 | + const entry = routes.get('GET', path); |
| 236 | + if (!entry) throw new Error(`route not registered: GET ${path}`); |
| 237 | + let body: any; |
| 238 | + let status = 200; |
| 239 | + const res = { |
| 240 | + status: (s: number) => { status = s; return res; }, |
| 241 | + header: () => res, |
| 242 | + setHeader: () => res, |
| 243 | + json: (b: unknown) => { body = b; }, |
| 244 | + send: (b: unknown) => { body = b; }, |
| 245 | + } as unknown as Parameters<typeof entry.handler>[1]; |
| 246 | + await entry.handler( |
| 247 | + { params, query: {}, body: {}, headers: { 'accept-language': locale }, method: 'GET', path } as unknown as Parameters<typeof entry.handler>[0], |
| 248 | + res, |
| 249 | + ); |
| 250 | + expect(status, `GET ${path} ${JSON.stringify(params)}: ${JSON.stringify(body)}`).toBe(200); |
| 251 | + return body; |
| 252 | + }; |
| 253 | + /** The two strings a reader sees, plus the about section's description (a string the override left alone). */ |
| 254 | + const strings = (config: any) => ({ |
| 255 | + title: config?.title, |
| 256 | + about: config?.sections?.find((s: any) => s?.name === 'about')?.label, |
| 257 | + aboutDescription: config?.sections?.find((s: any) => s?.name === 'about')?.description, |
| 258 | + }); |
| 259 | + return { |
| 260 | + protocol, |
| 261 | + /** `GET /forms/:slug` — the anonymous visitor's read. */ |
| 262 | + form: async (locale: string) => strings((await run('/api/v1/forms/:slug', { slug: SLUG }, locale))?.form), |
| 263 | + /** `GET /meta/view/:name` — the CONTROL leg: the authenticated read of the same document. */ |
| 264 | + meta: async (locale: string) => strings((await run('/api/v1/meta/:type/:name', { type: 'view', name: VIEW }, locale))?.item?.config), |
| 265 | + }; |
| 266 | +} |
| 267 | + |
| 268 | +describe('GET /forms/:slug serves a .custom pair override of the form view in the ruled label order', () => { |
| 269 | + it('the view the route matches carries the override envelope the pair rank reads', async () => { |
| 270 | + // The route matches its form in `getMetaItems({ type: 'view' })`; the pair |
| 271 | + // rank reads `servedOverrideBaseOf` off that served document. |
| 272 | + const { protocol } = makeHost({ override: true, pairZh: true }); |
| 273 | + const views: any[] = ((await protocol.getMetaItems({ type: 'view' } as never)) as any).items; |
| 274 | + const served = views.find((v) => v?.name === VIEW); |
| 275 | + expect(served?._packageId).toBe(BASE_PKG); |
| 276 | + expect(served?.overrides?.package).toBe(BASE_PKG); |
| 277 | + expect(protocol.servedOverrideBaseOf('view', served)).toBe(BASE_PKG); |
| 278 | + }); |
| 279 | + |
| 280 | + it('zh-CN: a translation the pair ships beats the override\'s own text, as /meta serves it', async () => { |
| 281 | + const host = makeHost({ override: true, pairZh: true }); |
| 282 | + const expected = { title: TITLE_PAIR_ZH, about: ABOUT_PAIR_ZH, aboutDescription: ABOUT_DESC_ZH }; |
| 283 | + // CONTROL: the authenticated read of the same document reads the pair's rank. |
| 284 | + expect(await host.meta('zh-CN'), '/meta/view zh-CN').toEqual(expected); |
| 285 | + expect(await host.form('zh-CN'), '/forms zh-CN').toEqual(expected); |
| 286 | + }); |
| 287 | + |
| 288 | + it('en: the pair ships no en row, so both surfaces serve the override\'s own text', async () => { |
| 289 | + const host = makeHost({ override: true, pairZh: true }); |
| 290 | + const expected = { title: TITLE_OV, about: ABOUT_OV, aboutDescription: ABOUT_DESC_EN }; |
| 291 | + expect(await host.meta('en'), '/meta/view en').toEqual(expected); |
| 292 | + expect(await host.form('en'), '/forms en').toEqual(expected); |
| 293 | + }); |
| 294 | + |
| 295 | + it('zh-CN with no pair translation: the override\'s own text beats the base\'s translation on both surfaces', async () => { |
| 296 | + const host = makeHost({ override: true, pairZh: false }); |
| 297 | + const expected = { title: TITLE_OV, about: ABOUT_OV, aboutDescription: ABOUT_DESC_ZH }; |
| 298 | + expect(await host.meta('zh-CN'), '/meta/view zh-CN').toEqual(expected); |
| 299 | + expect(await host.form('zh-CN'), '/forms zh-CN').toEqual(expected); |
| 300 | + }); |
| 301 | + |
| 302 | + it('zh-CN with no override: the base\'s form reads the base\'s translation on both surfaces', async () => { |
| 303 | + const host = makeHost({ override: false, pairZh: false }); |
| 304 | + const expected = { title: TITLE_ZH, about: ABOUT_ZH, aboutDescription: ABOUT_DESC_ZH }; |
| 305 | + expect(await host.meta('zh-CN'), '/meta/view zh-CN').toEqual(expected); |
| 306 | + expect(await host.form('zh-CN'), '/forms zh-CN').toEqual(expected); |
| 307 | + }); |
| 308 | +}); |
0 commit comments