Repository navigation
Expand file tree
/
Copy pathrecord-lock-user-facing-message.test.ts
More file actions
416 lines (369 loc) · 18 KB
/
Copy pathrecord-lock-user-facing-message.test.ts
File metadata and controls
416 lines (369 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* [#18153] The record lock's per-row refusal is END-USER copy — pinned as such,
* and pinned APART from the three operator-facing refusals beside it.
*
* ## What the card was
*
* A record held by a live approval refused the write with
* `record '<id>' of '<apiName>' is locked while an approval is in progress`.
* The console toast copies that sentence verbatim, so an end user read an
* opaque primary key and a machine identifier, and a deny-path toast is exactly
* the string that reaches screenshots, screen recordings and support tickets.
*
* ## The three pins, and why each is here
*
* 1. **The user-facing half.** Driven end to end — real {@link ObjectQL}, real
* sqlite driver, the real `beforeUpdate` hook — because the defect was about
* the string a real refusal produces, not about a helper in isolation. The
* assertion is stated as an ABSENCE (no record id, no object API name in the
* body) as well as a presence, because the presence alone would stay green
* on a sentence that named the record correctly and then appended the id.
*
* 2. **The three operator refusals, unchanged.** The cap refusals and the
* unanswerable-intersection refusal are raised about a WRITE SHAPE, and
* naming the object's API name there is the useful thing to say. The card
* ruled them out of scope; this pins that, so a later "harmonise the lock's
* messages" sweep goes red instead of quietly sweeping them into the
* end-user shape. They are driven through a FAKE engine rather than the real
* rig on purpose: two of the three need more than {@link PENDING_LOCK_LIMIT}
* locked rows and the third needs the engine's own read to fail, and neither
* is a state a sqlite fixture should be bent into to assert a string.
*
* 3. **`RECORD_LOCKED` / 409, both directions.** The card moved the human
* sentence only; the wire vocabulary is contract (ADR-0112) and the 409 is
* pinned across the batch-row protocol surfaces. Both directions means: the
* code and status are asserted to BE those values, and the `CODE: message`
* envelope is asserted to still be the envelope — a rewording that reached
* either would be a widening, not a copy fix.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { ObjectQL } from '@objectstack/objectql';
import { SqlDriver } from '@objectstack/driver-sql';
import { bindApprovalLockHook, APPROVALS_HOOK_PACKAGE } from './lifecycle-hooks.js';
import { BUILTIN_OPERATION_MESSAGES } from '@objectstack/spec/system';
import { installDefaultOrganization } from './default-organization.testkit.js';
const OPPORTUNITY = {
name: 'opportunity',
label: 'Opportunity',
fields: {
id: { name: 'id', type: 'text' as const, primaryKey: true },
name: { name: 'name', type: 'text' as const },
amount: { name: 'amount', type: 'number' as const },
approval_status: { name: 'approval_status', type: 'text' as const },
},
};
/** Same shape, no title-eligible field at all — the degradation leg. */
const LEDGER_ENTRY = {
name: 'ledger_entry',
label: 'Ledger Entry',
nameField: 'id',
fields: {
id: { name: 'id', type: 'text' as const, primaryKey: true },
amount: { name: 'amount', type: 'number' as const },
approval_status: { name: 'approval_status', type: 'text' as const },
},
};
const APPROVAL_REQUEST = {
name: 'sys_approval_request',
label: 'Approval Request',
fields: {
id: { name: 'id', type: 'text' as const, primaryKey: true },
object_name: { name: 'object_name', type: 'text' as const },
record_id: { name: 'record_id', type: 'text' as const },
status: { name: 'status', type: 'text' as const },
flow_run_id: { name: 'flow_run_id', type: 'text' as const },
node_config_json: { name: 'node_config_json', type: 'text' as const },
},
};
const NODE_CONFIG = JSON.stringify({ lockRecord: true, approvalStatusField: 'approval_status' });
describe('[#18153] the per-row refusal is user-facing copy', () => {
let engine: ObjectQL;
let opportunityId: string;
let ledgerId: string;
afterEach(async () => {
try { await engine?.destroy(); } catch { /* noop */ }
});
beforeEach(async () => {
engine = new ObjectQL();
engine.registerDriver(new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
}), true);
await engine.init();
// `packageId` is REQUIRED by `registerObject` — passed rather than elided so
// this file adds no raw `tsc` error to the package's TEST_DEBT ledger.
for (const o of [OPPORTUNITY, LEDGER_ENTRY, APPROVAL_REQUEST]) {
engine.registry.registerObject(o as any, 'com.objectstack.test.18153');
}
await engine.syncSchemas();
// A real `single` install's Default Organization: the system writes below
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
await installDefaultOrganization(engine);
opportunityId = String((await engine.insert('opportunity', { name: 'Acme renewal', amount: 100 })).id);
ledgerId = String((await engine.insert('ledger_entry', { amount: 100 })).id);
for (const [object, recordId] of [['opportunity', opportunityId], ['ledger_entry', ledgerId]] as const) {
await engine.insert('sys_approval_request', {
object_name: object, record_id: recordId, status: 'pending',
flow_run_id: 'run_1', node_config_json: NODE_CONFIG,
}, { context: { isSystem: true } } as any);
}
bindApprovalLockHook(engine as any);
});
async function refusal(object: string, id: string): Promise<any> {
let thrown: any = null;
try {
await engine.update(object, { amount: 999 }, { where: { id } } as any);
} catch (e) { thrown = e; }
expect(thrown).not.toBeNull();
return thrown;
}
it('names the record by its label and leaks neither the id nor the API name', async () => {
const thrown = await refusal('opportunity', opportunityId);
expect(thrown.message).toBe(
"RECORD_LOCKED: Opportunity 'Acme renewal' is locked while an approval is in progress, " +
'and cannot be edited until that approval is complete',
);
// Stated as an absence too: a sentence that named the record correctly and
// then appended the id would satisfy the equality above only by accident,
// and these two are what the card actually asked for.
expect(thrown.message).not.toContain(opportunityId);
expect(thrown.message).not.toContain(OPPORTUNITY.name);
});
it('degrades to the object label — ⛔ never back to the id — when no title resolves', async () => {
// `nameField: 'id'` is the pointer a title-less object ends up with, and it
// is exactly the shape that would re-introduce the defect if the resolver
// trusted the pointer.
const thrown = await refusal('ledger_entry', ledgerId);
expect(thrown.message).toBe(
'RECORD_LOCKED: This Ledger Entry is locked while an approval is in progress, ' +
'and cannot be edited until that approval is complete',
);
expect(thrown.message).not.toContain(ledgerId);
expect(thrown.message).not.toContain(LEDGER_ENTRY.name);
});
it('keeps the id and the API name reachable on the CONSOLE for support', async () => {
engine.unregisterHooksByPackage(APPROVALS_HOOK_PACKAGE);
const info: string[] = [];
bindApprovalLockHook(engine as any, { warn: () => {}, info: (m: any) => info.push(String(m)) });
await refusal('opportunity', opportunityId);
// The card's own remedy: "moved to the console … not simply deleted if a
// support path still needs them".
expect(info.some((line) => line.includes(opportunityId) && line.includes('opportunity'))).toBe(true);
});
it('pins `RECORD_LOCKED` and 409 in both directions', async () => {
const thrown = await refusal('opportunity', opportunityId);
expect(thrown.code).toBe('RECORD_LOCKED');
expect(thrown.statusCode).toBe(409);
// The other direction: the wire vocabulary did not acquire a second
// spelling, and the `CODE: message` envelope is still the envelope.
expect(thrown.status).toBeUndefined();
expect(String(thrown.message).startsWith('RECORD_LOCKED: ')).toBe(true);
});
});
/**
* CONTROL — the three operator-facing refusals, byte for byte.
*
* A fake engine, because these three are reached by write SHAPES, not by record
* states: two need more locked rows than {@link PENDING_LOCK_LIMIT} and the
* third needs the intersection read to throw. Each refusal is driven through
* the REAL handler `bindApprovalLockHook` registers.
*/
describe('[#18153] CONTROL — the operator-facing refusals are untouched', () => {
const LIMIT = 1_000;
/** Captures the handler the real binder registers, then drives it directly. */
function bindAgainst(find: (object: string, args: any) => Promise<any[]>) {
let handler!: (ctx: any) => Promise<void>;
bindApprovalLockHook({
registerHook: (_event: string, h: any) => { handler = h; },
unregisterHooksByPackage: () => 0,
find: find as any,
} as any);
return handler;
}
async function refusalFrom(handler: (ctx: any) => Promise<void>, ctx: any): Promise<any> {
let thrown: any = null;
try { await handler(ctx); } catch (e) { thrown = e; }
expect(thrown).not.toBeNull();
return thrown;
}
it('the named-id cap refusal still names the object', async () => {
const handler = bindAgainst(async () => []);
const ids = Array.from({ length: LIMIT + 1 }, (_v, i) => `r${i}`);
const thrown = await refusalFrom(handler, {
object: 'leave_request',
input: { id: { $in: ids }, data: { amount: 1 } },
});
expect(thrown.message).toBe(
`RECORD_LOCKED: refusing to authorize an update naming more than ${LIMIT} records of 'leave_request' — ` +
'the approval lock cannot check them row by row; scope the write',
);
expect(thrown.code).toBe('RECORD_LOCKED');
expect(thrown.statusCode).toBe(409);
});
it('the predicate cap refusal still names the object', async () => {
const pending = Array.from({ length: LIMIT + 1 }, (_v, i) => ({ record_id: `r${i}`, status: 'pending' }));
const handler = bindAgainst(async () => pending);
const thrown = await refusalFrom(handler, {
object: 'leave_request',
input: { data: { amount: 1 }, options: { where: { status: 'draft' } } },
});
expect(thrown.message).toBe(
`RECORD_LOCKED: refusing a predicate update on 'leave_request': more than ${LIMIT} of its records carry a ` +
'pending approval, so the lock cannot decide row by row; scope the write to the rows you mean',
);
expect(thrown.code).toBe('RECORD_LOCKED');
expect(thrown.statusCode).toBe(409);
});
it('the unanswerable-intersection refusal still names the object', async () => {
const handler = bindAgainst(async (object: string) => {
if (object === 'sys_approval_request') return [{ record_id: 'r1', status: 'pending' }];
throw new Error('driver exploded');
});
const thrown = await refusalFrom(handler, {
object: 'leave_request',
input: { data: { amount: 1 }, options: { where: { status: 'draft' } } },
});
expect(thrown.message).toBe(
"RECORD_LOCKED: cannot determine which rows a predicate update on 'leave_request' would touch " +
'(driver exploded); 1 record(s) of it carry a pending approval, so the write is refused',
);
expect(thrown.code).toBe('RECORD_LOCKED');
expect(thrown.statusCode).toBe(409);
});
});
/**
* [#34060] The per-row refusal is in the WRITER's locale.
*
* User-facing refusal text is in the request locale (ruling C on #32440), and
* the record door's other refusals already answer that way through the shared
* Operation Message Catalog. This refusal now does too, under the
* `record_locked*` keys: the object is named by its label in the writer's
* locale (the pack's `objects.<object>.label`, else the declared label), the
* record by its title, and a locale the catalog does not carry reads English.
* The wire vocabulary does not move: `RECORD_LOCKED` / 409 and the
* `CODE: message` envelope on every leg.
*
* Same real rig as the #18153 describe above. The `i18n` service is a double
* holding one zh-CN pack, shaped as `II18nService` answers: `t()` echoes the
* key back on a miss, and `getLocales()` lists what it holds.
*/
describe('[#34060] the per-row refusal is worded in the writer\'s locale', () => {
const TAIL_EN = 'is locked while an approval is in progress, and cannot be edited until that approval is complete';
const ZH = BUILTIN_OPERATION_MESSAGES['zh-CN'];
const PACKS: Record<string, Record<string, string>> = {
'zh-CN': {
'objects.opportunity.label': '商机',
'objects.ledger_entry.label': '分类账分录',
},
};
const i18n = {
getLocales: () => Object.keys(PACKS),
t: (key: string, locale: string) => PACKS[locale]?.[key] ?? key,
};
let engine: ObjectQL;
let opportunityId: string;
let ledgerId: string;
afterEach(async () => {
try { await engine?.destroy(); } catch { /* noop */ }
});
async function rig(service: (() => any) | undefined): Promise<void> {
engine = new ObjectQL();
engine.registerDriver(new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
}), true);
await engine.init();
for (const o of [OPPORTUNITY, LEDGER_ENTRY, APPROVAL_REQUEST]) {
engine.registry.registerObject(o as any, 'com.objectstack.test.34060');
}
await engine.syncSchemas();
await installDefaultOrganization(engine);
opportunityId = String((await engine.insert('opportunity', { name: 'Acme renewal', amount: 100 })).id);
ledgerId = String((await engine.insert('ledger_entry', { amount: 100 })).id);
for (const [object, recordId] of [['opportunity', opportunityId], ['ledger_entry', ledgerId]] as const) {
await engine.insert('sys_approval_request', {
object_name: object, record_id: recordId, status: 'pending',
flow_run_id: 'run_1', node_config_json: NODE_CONFIG,
}, { context: { isSystem: true } } as any);
}
bindApprovalLockHook(engine as any, undefined, undefined, service);
}
/** The refusal a writer in `locale` gets; the engine copies the envelope's locale onto the hook session. */
async function refusalIn(locale: string | undefined, object: string, id: string): Promise<any> {
let thrown: any = null;
try {
await engine.update(object, { amount: 999 }, {
where: { id },
context: { userId: 'u_writer', positions: [], permissions: [], ...(locale ? { locale } : {}) },
} as any);
} catch (e) { thrown = e; }
expect(thrown).not.toBeNull();
expect(thrown.code).toBe('RECORD_LOCKED');
expect(thrown.statusCode).toBe(409);
expect(thrown.message.startsWith('RECORD_LOCKED: ')).toBe(true);
// Nothing reached the store.
expect((await engine.findOne(object, { where: { id } }))?.amount).toBe(100);
return thrown;
}
it('zh-CN: the Chinese sentence, naming the object by its zh-CN label and the record by its title', async () => {
await rig(() => i18n);
const thrown = await refusalIn('zh-CN', 'opportunity', opportunityId);
expect(thrown.message).toBe(
`RECORD_LOCKED: ${ZH.record_locked.replace('{{object}}', '商机').replace('{{title}}', 'Acme renewal')}`,
);
expect(thrown.message).toContain('商机「Acme renewal」');
expect(thrown.message).not.toContain(TAIL_EN);
expect(thrown.message).not.toContain(opportunityId);
});
it('zh-CN, a record with no title: the label-only sentence, still in Chinese', async () => {
await rig(() => i18n);
const thrown = await refusalIn('zh-CN', 'ledger_entry', ledgerId);
expect(thrown.message).toBe(`RECORD_LOCKED: ${ZH.record_locked_untitled.replace('{{object}}', '分类账分录')}`);
expect(thrown.message).not.toContain(ledgerId);
});
it('a bare `zh` reaches the zh-CN sentence and label', async () => {
await rig(() => i18n);
const thrown = await refusalIn('zh', 'opportunity', opportunityId);
expect(thrown.message).toContain('商机「Acme renewal」');
});
it('en-US: the English sentence, byte-identical to the one this replaced', async () => {
await rig(() => i18n);
const thrown = await refusalIn('en-US', 'opportunity', opportunityId);
expect(thrown.message).toBe(`RECORD_LOCKED: Opportunity 'Acme renewal' ${TAIL_EN}`);
});
it('a locale the catalog does not carry falls back to English', async () => {
await rig(() => i18n);
const thrown = await refusalIn('xx-YY', 'opportunity', opportunityId);
expect(thrown.message).toBe(`RECORD_LOCKED: Opportunity 'Acme renewal' ${TAIL_EN}`);
});
it('no locale on the write: English', async () => {
await rig(() => i18n);
const thrown = await refusalIn(undefined, 'opportunity', opportunityId);
expect(thrown.message).toBe(`RECORD_LOCKED: Opportunity 'Acme renewal' ${TAIL_EN}`);
});
it('no i18n service: the built-in catalog still answers zh-CN, with the declared label', async () => {
await rig(undefined);
const thrown = await refusalIn('zh-CN', 'opportunity', opportunityId);
expect(thrown.message).toBe(
`RECORD_LOCKED: ${ZH.record_locked.replace('{{object}}', 'Opportunity').replace('{{title}}', 'Acme renewal')}`,
);
});
it('a deployment translation of `errors.record_locked` wins, interpolated', async () => {
const override = {
getLocales: () => ['zh-CN'],
t: (key: string, locale: string) =>
key === 'errors.record_locked' && locale === 'zh-CN' ? '{{object}}《{{title}}》审批中,暂不可改' : i18n.t(key, locale),
};
await rig(() => override);
const thrown = await refusalIn('zh-CN', 'opportunity', opportunityId);
expect(thrown.message).toBe('RECORD_LOCKED: 商机《Acme renewal》审批中,暂不可改');
});
it('a throwing i18n service degrades the wording, never the lock', async () => {
await rig(() => { throw new Error('i18n exploded'); });
const thrown = await refusalIn('zh-CN', 'opportunity', opportunityId);
expect(thrown.message).toContain('Opportunity「Acme renewal」');
});
});