|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27041] The generic data door names a request's flow and step in the |
| 5 | + * READER's language, by the lookup the approvals door applies. |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * Setup's request list (`sys_approval_request`, view `all_requests`) reads the |
| 10 | + * generic data door, which served the stored `process_label` / |
| 11 | + * `current_step_label` to every reader: a zh-CN user saw "Account Approval" / |
| 12 | + * "Manager Review" there, while the approvals door served the same rows in |
| 13 | + * zh-CN from the app's language pack (#24863). |
| 14 | + * |
| 15 | + * ## The rig |
| 16 | + * |
| 17 | + * The rig of `reader-locale-display-labels.integration.test.ts`: a real |
| 18 | + * `ObjectQL` over `SqlDriver` (better-sqlite3, in-memory), the real |
| 19 | + * `AutomationEngine` opening the request through the real approval node, and |
| 20 | + * the real `ApprovalsServicePlugin` over a kernel whose `i18n` service is the |
| 21 | + * real in-memory one loaded with a zh-CN pack. The data door is |
| 22 | + * `ObjectStackProtocolImplementation.findData` / `getData`, the calls |
| 23 | + * `GET /api/v1/data/sys_approval_request` and `GET …/:id` make, with the |
| 24 | + * reader's `ExecutionContext.locale` as the REST door's shared assembler |
| 25 | + * resolves it from `Accept-Language`. |
| 26 | + * |
| 27 | + * ## What it pins |
| 28 | + * |
| 29 | + * - a zh-CN reader is served the zh-CN flow and step labels on the list as |
| 30 | + * Setup projects it (the labels selected, the machine columns not), on the |
| 31 | + * full-column list, and on the by-id read; |
| 32 | + * - the projection is kept: a label selected without its key column is served |
| 33 | + * without that column; |
| 34 | + * - parity: the same row read through both doors carries the same label text; |
| 35 | + * - a step the pack does not carry is served as stored, beside a translated |
| 36 | + * flow label; |
| 37 | + * - a reader in a locale no pack holds, an en reader and a reader with no |
| 38 | + * locale are served the stored text, and a system read is served as stored; |
| 39 | + * - the door adds no column: no `step_label`, no `object_label`; |
| 40 | + * - the stored row keeps the authored text. |
| 41 | + * |
| 42 | + * ⚠️ Direction, predicted before running: every zh-CN pin is RED on the unfixed |
| 43 | + * door (it served the stored text whatever the locale); every "as stored" pin is |
| 44 | + * green on both sides on purpose, fencing the fallback. |
| 45 | + */ |
| 46 | + |
| 47 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 48 | +import { ObjectQL } from '@objectstack/objectql'; |
| 49 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 50 | +import { AutomationEngine } from '@objectstack/service-automation'; |
| 51 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 52 | +import { createMemoryI18n } from '@objectstack/core'; |
| 53 | +import { TranslationDataSchema } from '@objectstack/spec/system'; |
| 54 | +import type { EngineQueryOptions } from '@objectstack/spec/data'; |
| 55 | +import type { ApprovalService } from './approval-service.js'; |
| 56 | +import { ApprovalsServicePlugin } from './approvals-plugin.js'; |
| 57 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 58 | + |
| 59 | +const SYSTEM = { isSystem: true, positions: [], permissions: [] } as any; |
| 60 | +const reader = (userId: string, locale?: string) => |
| 61 | + ({ userId, positions: [], permissions: [], ...(locale ? { locale } : {}) }) as any; |
| 62 | +const silent = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } as any; |
| 63 | + |
| 64 | +const REQUEST = 'sys_approval_request'; |
| 65 | +const SUBMITTER = 'u_sub'; |
| 66 | +const MANAGER = 'u_mgr'; |
| 67 | +const DIRECTOR = 'u_dir'; |
| 68 | +const OBJECT = 'crm_account'; |
| 69 | + |
| 70 | +const sysUser = { |
| 71 | + name: 'sys_user', |
| 72 | + label: 'User', |
| 73 | + fields: { |
| 74 | + id: { name: 'id', label: 'Id', type: 'text' as const, primaryKey: true }, |
| 75 | + name: { name: 'name', label: 'Name', type: 'text' as const }, |
| 76 | + email: { name: 'email', label: 'Email', type: 'text' as const }, |
| 77 | + }, |
| 78 | +}; |
| 79 | +const account = { |
| 80 | + name: OBJECT, |
| 81 | + label: 'Account', |
| 82 | + fields: { |
| 83 | + id: { name: 'id', label: 'Id', type: 'text' as const, primaryKey: true }, |
| 84 | + name: { name: 'name', label: 'Name', type: 'text' as const }, |
| 85 | + }, |
| 86 | +}; |
| 87 | + |
| 88 | +/** Two approval steps; the pack translates the first step and not the second. */ |
| 89 | +const FLOW = { |
| 90 | + name: 'account_approval', label: 'Account Approval', type: 'autolaunched', |
| 91 | + nodes: [ |
| 92 | + { id: 'start', type: 'start', label: 'Start' }, |
| 93 | + { id: 'manager_review', type: 'approval', label: 'Manager Review', config: { approvers: [{ type: 'user', value: MANAGER }] } }, |
| 94 | + { id: 'director_review', type: 'approval', label: 'Director Review', config: { approvers: [{ type: 'user', value: DIRECTOR }] } }, |
| 95 | + { id: 'ok', type: 'end', label: 'Approved' }, |
| 96 | + { id: 'no', type: 'end', label: 'Rejected' }, |
| 97 | + ], |
| 98 | + edges: [ |
| 99 | + { id: 'e1', source: 'start', target: 'manager_review' }, |
| 100 | + { id: 'e2', source: 'manager_review', target: 'director_review', label: 'approve' }, |
| 101 | + { id: 'e3', source: 'manager_review', target: 'no', label: 'reject' }, |
| 102 | + { id: 'e4', source: 'director_review', target: 'ok', label: 'approve' }, |
| 103 | + { id: 'e5', source: 'director_review', target: 'no', label: 'reject' }, |
| 104 | + ], |
| 105 | +}; |
| 106 | + |
| 107 | +/** The app's zh-CN language pack: the flow, its first step, and the subject object. */ |
| 108 | +const ZH_PACK = { |
| 109 | + objects: { [OBJECT]: { label: '客户' } }, |
| 110 | + flows: { |
| 111 | + account_approval: { |
| 112 | + label: '客户审批', |
| 113 | + approvals: { manager_review: { label: '经理审核' } }, |
| 114 | + }, |
| 115 | + }, |
| 116 | +}; |
| 117 | + |
| 118 | +const AS_STORED = { process_label: 'Account Approval', current_step_label: 'Manager Review' }; |
| 119 | +const IN_ZH = { process_label: '客户审批', current_step_label: '经理审核' }; |
| 120 | + |
| 121 | +const labelsOf = (row: any) => ({ |
| 122 | + process_label: row?.process_label, |
| 123 | + current_step_label: row?.current_step_label, |
| 124 | +}); |
| 125 | + |
| 126 | +/** Setup's request list as the console grid reads it: `all_requests`' columns, nothing else. */ |
| 127 | +const ALL_REQUESTS_SELECT = 'process_label,record_id,status,current_step_label,pending_approver_labels,submitter_id,updated_at'; |
| 128 | + |
| 129 | +/** The plugin's own object definitions, as its manifest registers them. */ |
| 130 | +async function packageObjects(): Promise<any[]> { |
| 131 | + let manifest: any; |
| 132 | + const ctx: any = { |
| 133 | + getService: (name: string) => { |
| 134 | + if (name === 'manifest') return { register: (m: any) => { manifest = m; } }; |
| 135 | + throw new Error(`no service '${name}'`); |
| 136 | + }, |
| 137 | + logger: silent, |
| 138 | + }; |
| 139 | + await new ApprovalsServicePlugin().init(ctx); |
| 140 | + return manifest.objects; |
| 141 | +} |
| 142 | + |
| 143 | +describe('[#27041] the data door serves a request\'s labels in the reader\'s locale', () => { |
| 144 | + let engine: ObjectQL; |
| 145 | + let automation: AutomationEngine; |
| 146 | + let svc: ApprovalService; |
| 147 | + let protocol: ObjectStackProtocolImplementation; |
| 148 | + let seq = 0; |
| 149 | + |
| 150 | + /** Start the flow on a fresh account as the submitter; answer the request it paused on. */ |
| 151 | + const submit = async (): Promise<any> => { |
| 152 | + const id = `acc_${++seq}`; |
| 153 | + const record = { id, name: `Acme ${seq}` }; |
| 154 | + await engine.insert(OBJECT, record, { context: SYSTEM } as any); |
| 155 | + const run = await automation.execute(FLOW.name, { object: OBJECT, record, userId: SUBMITTER } as any); |
| 156 | + expect(run.status, 'the run paused on its first approval step').toBe('paused'); |
| 157 | + const rows: any[] = await engine.find(REQUEST, { |
| 158 | + where: { record_id: id, status: 'pending' }, context: SYSTEM, |
| 159 | + } satisfies EngineQueryOptions); |
| 160 | + expect(rows).toHaveLength(1); |
| 161 | + return rows[0]; |
| 162 | + }; |
| 163 | + |
| 164 | + /** `GET /data/sys_approval_request?…`: the rows the door hands back for `id`. */ |
| 165 | + const listRow = async (context: any, id: string, query: Record<string, unknown> = {}): Promise<any> => { |
| 166 | + const res: any = await protocol.findData({ object: REQUEST, query: { id, ...query }, context }); |
| 167 | + expect(res.records, 'the reader is served the request').toHaveLength(1); |
| 168 | + return res.records[0]; |
| 169 | + }; |
| 170 | + /** `GET /data/sys_approval_request/:id`. */ |
| 171 | + const byId = async (context: any, id: string): Promise<any> => |
| 172 | + (await protocol.getData({ object: REQUEST, id, context } as any)).record; |
| 173 | + |
| 174 | + beforeAll(async () => { |
| 175 | + const parsed = TranslationDataSchema.safeParse(ZH_PACK) as { success: boolean; error?: unknown }; |
| 176 | + expect(parsed.success, JSON.stringify(parsed.error)).toBe(true); |
| 177 | + |
| 178 | + engine = new ObjectQL(); |
| 179 | + engine.registerDriver(new SqlDriver({ |
| 180 | + client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true, |
| 181 | + }), true); |
| 182 | + await engine.init(); |
| 183 | + for (const def of [sysUser, account, ...(await packageObjects())]) { |
| 184 | + engine.registry.registerObject(def as any, 'approvals-data-door-locale-test', 'approvals-data-door-locale-test'); |
| 185 | + } |
| 186 | + await engine.syncSchemas(); |
| 187 | + await installDefaultOrganization(engine); |
| 188 | + for (const [id, name] of [[SUBMITTER, 'Sam Submitter'], [MANAGER, 'Mona Manager'], [DIRECTOR, 'Dina Director']]) { |
| 189 | + await engine.insert('sys_user', { id, name, email: `${id}@example.com` }, { context: SYSTEM } as any); |
| 190 | + } |
| 191 | + |
| 192 | + automation = new AutomationEngine(silent); |
| 193 | + const i18n = createMemoryI18n(); |
| 194 | + i18n.loadTranslations('zh-CN', ZH_PACK); |
| 195 | + |
| 196 | + const services: Record<string, unknown> = { objectql: engine, automation, i18n }; |
| 197 | + const ctx: any = { |
| 198 | + getService: (name: string) => { |
| 199 | + if (!(name in services)) throw new Error(`[Kernel] Service '${name}' not found`); |
| 200 | + return services[name]; |
| 201 | + }, |
| 202 | + registerService: (name: string, service: unknown) => { services[name] = service; }, |
| 203 | + hook: () => {}, |
| 204 | + logger: silent, |
| 205 | + }; |
| 206 | + await new ApprovalsServicePlugin().start(ctx); |
| 207 | + svc = services.approvals as ApprovalService; |
| 208 | + automation.registerFlow(FLOW.name, FLOW as any); |
| 209 | + protocol = new ObjectStackProtocolImplementation(engine as any); |
| 210 | + }); |
| 211 | + |
| 212 | + afterAll(async () => { |
| 213 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 214 | + }); |
| 215 | + |
| 216 | + describe('a zh-CN reader', () => { |
| 217 | + it('Setup\'s list, projected to its columns: the labels in zh-CN, and only the columns it selected', async () => { |
| 218 | + const req = await submit(); |
| 219 | + const row = await listRow(reader(SUBMITTER, 'zh-CN'), req.id, { $select: ALL_REQUESTS_SELECT }); |
| 220 | + expect(labelsOf(row)).toEqual(IN_ZH); |
| 221 | + // The key columns the lookup borrowed are not served. |
| 222 | + expect(Object.keys(row)).not.toContain('process_name'); |
| 223 | + expect(Object.keys(row)).not.toContain('current_step'); |
| 224 | + }); |
| 225 | + |
| 226 | + it('the full-column list: the labels in zh-CN, the machine columns as stored', async () => { |
| 227 | + const req = await submit(); |
| 228 | + const row = await listRow(reader(SUBMITTER, 'zh-CN'), req.id); |
| 229 | + expect(labelsOf(row)).toEqual(IN_ZH); |
| 230 | + expect([row.process_name, row.current_step]).toEqual(['flow:account_approval', 'manager_review']); |
| 231 | + }); |
| 232 | + |
| 233 | + it('the by-id read', async () => { |
| 234 | + const req = await submit(); |
| 235 | + expect(labelsOf(await byId(reader(SUBMITTER, 'zh-CN'), req.id))).toEqual(IN_ZH); |
| 236 | + }); |
| 237 | + |
| 238 | + it('a bare `zh` reaches the zh-CN pack', async () => { |
| 239 | + const req = await submit(); |
| 240 | + expect(labelsOf(await listRow(reader(SUBMITTER, 'zh'), req.id))).toEqual(IN_ZH); |
| 241 | + }); |
| 242 | + |
| 243 | + it('parity: the data door and the approvals door serve the same label text for the same row', async () => { |
| 244 | + const req = await submit(); |
| 245 | + const ctx = reader(SUBMITTER, 'zh-CN'); |
| 246 | + const viaApprovals = await svc.getRequest(req.id, ctx); |
| 247 | + const viaData = await byId(ctx, req.id); |
| 248 | + const viaList = await listRow(ctx, req.id, { $select: ALL_REQUESTS_SELECT }); |
| 249 | + expect(labelsOf(viaData)).toEqual(labelsOf(viaApprovals)); |
| 250 | + expect(labelsOf(viaList)).toEqual(labelsOf(viaApprovals)); |
| 251 | + }); |
| 252 | + |
| 253 | + it('a step the pack does not carry is served as stored, beside the flow label it does', async () => { |
| 254 | + const req = await submit(); |
| 255 | + await svc.decide(req.id, { decision: 'approve', actorId: MANAGER }, reader(MANAGER)); |
| 256 | + const next: any[] = await engine.find(REQUEST, { |
| 257 | + where: { record_id: req.record_id, status: 'pending' }, context: SYSTEM, |
| 258 | + } satisfies EngineQueryOptions); |
| 259 | + expect(next.map((r) => r.current_step)).toEqual(['director_review']); |
| 260 | + const row = await listRow(reader(SUBMITTER, 'zh-CN'), next[0].id, { $select: ALL_REQUESTS_SELECT }); |
| 261 | + expect(labelsOf(row)).toEqual({ process_label: '客户审批', current_step_label: 'Director Review' }); |
| 262 | + }); |
| 263 | + |
| 264 | + it('the door adds no column the object does not declare', async () => { |
| 265 | + const req = await submit(); |
| 266 | + const row = await byId(reader(SUBMITTER, 'zh-CN'), req.id); |
| 267 | + expect(Object.keys(row)).not.toContain('step_label'); |
| 268 | + expect(Object.keys(row)).not.toContain('object_label'); |
| 269 | + }); |
| 270 | + }); |
| 271 | + |
| 272 | + it('a reader in a locale no pack holds, an en reader and a reader with no locale are served the stored text', async () => { |
| 273 | + const req = await submit(); |
| 274 | + for (const locale of ['ja-JP', 'en', undefined]) { |
| 275 | + expect(labelsOf(await listRow(reader(SUBMITTER, locale), req.id, { $select: ALL_REQUESTS_SELECT })), String(locale)) |
| 276 | + .toEqual(AS_STORED); |
| 277 | + expect(labelsOf(await byId(reader(SUBMITTER, locale), req.id)), String(locale)).toEqual(AS_STORED); |
| 278 | + } |
| 279 | + }); |
| 280 | + |
| 281 | + it('a system read is served as stored, a locale on it notwithstanding', async () => { |
| 282 | + const req = await submit(); |
| 283 | + const row = await engine.findOne(REQUEST, { where: { id: req.id } }, { context: { ...SYSTEM, locale: 'zh-CN' } }); |
| 284 | + expect(labelsOf(row)).toEqual(AS_STORED); |
| 285 | + }); |
| 286 | + |
| 287 | + it('the stored row keeps the authored text it opened with, after every read', async () => { |
| 288 | + const req = await submit(); |
| 289 | + await listRow(reader(SUBMITTER, 'zh-CN'), req.id, { $select: ALL_REQUESTS_SELECT }); |
| 290 | + await byId(reader(SUBMITTER, 'zh-CN'), req.id); |
| 291 | + const row = await engine.findOne(REQUEST, { where: { id: req.id } }, { context: SYSTEM }); |
| 292 | + expect(labelsOf(row)).toEqual(AS_STORED); |
| 293 | + }); |
| 294 | +}); |
0 commit comments