Skip to content

Commit b73f58e

Browse files
committed
test(objectql): the seed pins key their rows on 'ref', not a field named 'code'
check:error-code-casing reads a lowercase literal under a 'code' key as an ADR-0112 error code. The pins' external-id field was named 'code', so every row key ('bad', 'iso', ...) read as one. The field is arbitrary; renamed. Claude-Session: https://claude.ai/code/session_017ErfyP2Rx7XWHJA27QjyUi Co-authored-by: Claude <noreply@anthropic.com>
1 parent e6b5281 commit b73f58e

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

‎packages/objectql/src/seed-readonly-value-shape.test.ts‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ const LOAD_CONFIG = {
159159

160160
const seed = (object: string, records: Array<Record<string, unknown>>) => ({
161161
object,
162-
externalId: 'code',
162+
externalId: 'ref',
163163
mode: 'upsert',
164164
env: ['prod', 'dev', 'test'],
165165
records,
166166
});
167167

168-
/** The validation sentence a seed error quotes, after its `(code=…): ` lead. */
168+
/** The validation sentence a seed error quotes, after its `(ref=…): ` lead. */
169169
const quotedSentence = (message: string) => message.slice(message.indexOf('): ') + 3);
170170

171171
/** The thrown refusal, read the way an HTTP boundary reads it. */
@@ -208,7 +208,7 @@ describe('a system writer is exempt from the readonly strip, never from the valu
208208
label: name,
209209
datasource: 'seed-store',
210210
fields: {
211-
code: { name: 'code', label: 'Code', type: 'text' },
211+
ref: { name: 'ref', label: 'Ref', type: 'text' },
212212
subject: { name: 'subject', label: 'Subject', type: 'text' },
213213
run_at: { name: 'run_at', label: 'Run At', type: 'datetime', readonly },
214214
// The constraint arms a readonly value does NOT reach (see
@@ -229,20 +229,20 @@ describe('a system writer is exempt from the readonly strip, never from the valu
229229
const loader = () => new SeedLoaderService(objectql as never, emptyMetadata() as never, quietLogger as never);
230230
const load = (...seeds: ReturnType<typeof seed>[]) =>
231231
loader().load({ seeds: seeds as never, config: LOAD_CONFIG });
232-
const rows = (object: string, code: string) =>
233-
Array.from(storeFor(object).values()).filter((r) => r.code === code);
234-
const stored = (object: string, code: string) => {
235-
const found = rows(object, code);
236-
expect(found.length, `exactly one stored ${object} row for code ${code}`).toBe(1);
232+
const rows = (object: string, ref: string) =>
233+
Array.from(storeFor(object).values()).filter((r) => r.ref === ref);
234+
const stored = (object: string, ref: string) => {
235+
const found = rows(object, ref);
236+
expect(found.length, `exactly one stored ${object} row for ref ${ref}`).toBe(1);
237237
return found[0];
238238
};
239239

240240
// ── Pin 1 ────────────────────────────────────────────────────────────────
241241
describe("pin 1: 'yesterday' on a readonly datetime in a seed is refused, as the non-readonly path refuses it", () => {
242242
it('the seed loader counts it as a seed error, quotes the non-readonly sentence, and stores no row', async () => {
243243
const result = await load(
244-
seed(RO, [{ code: 'bad', run_at: 'yesterday' }]),
245-
seed(RW, [{ code: 'bad', run_at: 'yesterday' }]),
244+
seed(RO, [{ ref: 'bad', run_at: 'yesterday' }]),
245+
seed(RW, [{ ref: 'bad', run_at: 'yesterday' }]),
246246
);
247247

248248
expect(result.summary.totalErrored).toBe(2);
@@ -255,23 +255,23 @@ describe('a system writer is exempt from the readonly strip, never from the valu
255255
});
256256

257257
it('the replay (UPDATE) of an existing seed row is refused too, and the stored value stands', async () => {
258-
const first = await load(seed(RO, [{ code: 'r', run_at: ISO }]));
258+
const first = await load(seed(RO, [{ ref: 'r', run_at: ISO }]));
259259
expect(first.errors).toEqual([]);
260260

261-
const replay = await load(seed(RO, [{ code: 'r', run_at: 'yesterday' }]));
261+
const replay = await load(seed(RO, [{ ref: 'r', run_at: 'yesterday' }]));
262262
expect(replay.summary.totalErrored).toBe(1);
263263
expect(quotedSentence(replay.errors[0].message)).toBe('Run At must be a valid datetime (ISO-8601)');
264264
expect(stored(RO, 'r').run_at).toBe(ISO);
265265
});
266266

267267
it('the refusal is VALIDATION_FAILED / 400 on all four write seams, with the non-readonly field envelope', async () => {
268268
const ctx = { context: SEED_WRITE_EXECUTION_CONTEXT };
269-
const [existing] = await objectql.insert(RO, [{ code: 'e', run_at: ISO }], ctx);
269+
const [existing] = await objectql.insert(RO, [{ ref: 'e', run_at: ISO }], ctx);
270270

271271
const seams: Array<[string, () => Promise<unknown>]> = [
272-
['insert', () => objectql.insert(RO, { code: 'i', run_at: 'yesterday' }, ctx)],
272+
['insert', () => objectql.insert(RO, { ref: 'i', run_at: 'yesterday' }, ctx)],
273273
['update by id', () => objectql.update(RO, { id: existing.id, run_at: 'yesterday' }, ctx)],
274-
['update by predicate', () => objectql.update(RO, { run_at: 'yesterday' }, { ...ctx, where: { code: 'e' }, multi: true } as any)],
274+
['update by predicate', () => objectql.update(RO, { run_at: 'yesterday' }, { ...ctx, where: { ref: 'e' }, multi: true } as any)],
275275
];
276276
for (const [seam, write] of seams) {
277277
const r = await refusal(write);
@@ -281,18 +281,18 @@ describe('a system writer is exempt from the readonly strip, never from the valu
281281
]);
282282
}
283283
// The control: the SAME write on the non-readonly twin answers the same envelope.
284-
const control = await refusal(() => objectql.insert(RW, { code: 'i', run_at: 'yesterday' }, ctx));
284+
const control = await refusal(() => objectql.insert(RW, { ref: 'i', run_at: 'yesterday' }, ctx));
285285
expect([control.code, control.status]).toEqual(['VALIDATION_FAILED', 400]);
286286
expect(control.fields.map((f) => [f.field, f.code, f.message])).toEqual([
287287
['run_at', 'invalid_date', 'Run At must be a valid datetime (ISO-8601)'],
288288
]);
289289
expect(stored(RO, 'e').run_at).toBe(ISO);
290290
// Positive control on the predicate seam: a valid value lands through it.
291-
await objectql.update(RO, { run_at: DAYS_AGO_5 }, { ...ctx, where: { code: 'e' }, multi: true } as any);
291+
await objectql.update(RO, { run_at: DAYS_AGO_5 }, { ...ctx, where: { ref: 'e' }, multi: true } as any);
292292
expect(stored(RO, 'e').run_at).toBe(DAYS_AGO_5);
293293

294294
// The dry run (fourth seam) reports what the write refuses.
295-
const preview = await objectql.validate(RO, { code: 'p', run_at: 'yesterday' }, { mode: 'insert', context: SEED_WRITE_EXECUTION_CONTEXT });
295+
const preview = await objectql.validate(RO, { ref: 'p', run_at: 'yesterday' }, { mode: 'insert', context: SEED_WRITE_EXECUTION_CONTEXT });
296296
expect(preview.valid).toBe(false);
297297
expect(preview.results[0].errors.map((f: any) => [f.field, f.code])).toEqual([['run_at', 'invalid_date']]);
298298
});
@@ -303,15 +303,15 @@ describe('a system writer is exempt from the readonly strip, never from the valu
303303
// call shape, single-row and array.
304304
const ctx = { context: SEED_WRITE_EXECUTION_CONTEXT };
305305
for (const write of [
306-
() => objectql.insert(RO, { code: 'c1', run_at: cel`daysAgo(5)` }, ctx),
307-
() => objectql.insert(RO, [{ code: 'c2', run_at: cel`daysAgo(5)` }], ctx),
306+
() => objectql.insert(RO, { ref: 'c1', run_at: cel`daysAgo(5)` }, ctx),
307+
() => objectql.insert(RO, [{ ref: 'c2', run_at: cel`daysAgo(5)` }], ctx),
308308
]) {
309309
const r = await refusal(write);
310310
expect([r.code, r.status]).toEqual(['VALIDATION_FAILED', 400]);
311311
expect(r.fields.map((f) => [f.field, f.code])).toEqual([['run_at', 'invalid_date']]);
312312
}
313313
// The injected audit column the seed keeps since #21646.
314-
const audit = await refusal(() => objectql.insert(RO, { code: 'ca', created_at: 'yesterday' }, ctx));
314+
const audit = await refusal(() => objectql.insert(RO, { ref: 'ca', created_at: 'yesterday' }, ctx));
315315
expect([audit.code, audit.status]).toEqual(['VALIDATION_FAILED', 400]);
316316
expect(audit.fields.map((f) => [f.field, f.code])).toEqual([['created_at', 'invalid_date']]);
317317
expect(rows(RO, 'c1')).toEqual([]);
@@ -323,8 +323,8 @@ describe('a system writer is exempt from the readonly strip, never from the valu
323323
// ── Pin 2 ────────────────────────────────────────────────────────────────
324324
it('pin 2: a valid ISO value on a readonly field under the seed context is kept — authored, evaluated from `cel`, or on `created_at`', async () => {
325325
const result = await load(seed(RO, [
326-
{ code: 'iso', run_at: ISO, created_at: ISO },
327-
{ code: 'cel', run_at: cel`daysAgo(5)`, created_at: cel`daysAgo(5)` },
326+
{ ref: 'iso', run_at: ISO, created_at: ISO },
327+
{ ref: 'cel', run_at: cel`daysAgo(5)`, created_at: cel`daysAgo(5)` },
328328
]));
329329
expect(result.errors, JSON.stringify(result.errors)).toEqual([]);
330330
expect(stored(RO, 'iso').run_at).toBe(ISO);
@@ -333,7 +333,7 @@ describe('a system writer is exempt from the readonly strip, never from the valu
333333
expect(new Date(stored(RO, 'cel').created_at).toISOString()).toBe(DAYS_AGO_5);
334334

335335
// The replay keeps it too.
336-
const replay = await load(seed(RO, [{ code: 'iso', subject: 'v2', run_at: ISO, created_at: ISO }]));
336+
const replay = await load(seed(RO, [{ ref: 'iso', subject: 'v2', run_at: ISO, created_at: ISO }]));
337337
expect(replay.errors).toEqual([]);
338338
expect(stored(RO, 'iso').subject).toBe('v2');
339339
expect(stored(RO, 'iso').run_at).toBe(ISO);
@@ -342,17 +342,17 @@ describe('a system writer is exempt from the readonly strip, never from the valu
342342
// ── Pin 3 ────────────────────────────────────────────────────────────────
343343
describe('pin 3: the non-readonly path is unchanged', () => {
344344
it('a non-readonly datetime takes a valid value and refuses a malformed one, as before', async () => {
345-
const result = await load(seed(RW, [{ code: 'ok', run_at: ISO }]));
345+
const result = await load(seed(RW, [{ ref: 'ok', run_at: ISO }]));
346346
expect(result.errors).toEqual([]);
347347
expect(stored(RW, 'ok').run_at).toBe(ISO);
348-
const r = await refusal(() => objectql.insert(RW, { code: 'no', run_at: 'yesterday' }, { context: { isSystem: true } }));
348+
const r = await refusal(() => objectql.insert(RW, { ref: 'no', run_at: 'yesterday' }, { context: { isSystem: true } }));
349349
expect([r.code, r.status]).toEqual(['VALIDATION_FAILED', 400]);
350350
expect(r.fields.map((f) => [f.field, f.code])).toEqual([['run_at', 'invalid_date']]);
351351
});
352352

353353
it('a NON-system caller\'s readonly value is still dropped by the strip, never refused — on insert and on a whole-record write-back', async () => {
354354
const user = { context: { userId: 'user-1' } };
355-
const [row] = await objectql.insert(RO, [{ code: 'u', run_at: 'yesterday' }], user);
355+
const [row] = await objectql.insert(RO, [{ ref: 'u', run_at: 'yesterday' }], user);
356356
expect(stored(RO, 'u').run_at).toBeUndefined();
357357

358358
// A form round-trip echoes every key it read, a malformed legacy one
@@ -367,11 +367,11 @@ describe('a system writer is exempt from the readonly strip, never from the valu
367367
it('a readonly value reaches the SHAPE arms only: an undeclared option and an out-of-bound number are stored, as before', async () => {
368368
// The open-vocabulary ruling on `sys_activity.type` (commit 88b9d749a):
369369
// a readonly option set is the built-in set, not a closed enum.
370-
await objectql.insert(RO, { code: 'k', kind: 'author_value', score: 9 }, { context: SEED_WRITE_EXECUTION_CONTEXT });
370+
await objectql.insert(RO, { ref: 'k', kind: 'author_value', score: 9 }, { context: SEED_WRITE_EXECUTION_CONTEXT });
371371
expect(stored(RO, 'k').kind).toBe('author_value');
372372
expect(stored(RO, 'k').score).toBe(9);
373373
// …while the non-readonly twin refuses both, unchanged.
374-
const r = await refusal(() => objectql.insert(RW, { code: 'k', kind: 'author_value', score: 9 }, { context: SEED_WRITE_EXECUTION_CONTEXT }));
374+
const r = await refusal(() => objectql.insert(RW, { ref: 'k', kind: 'author_value', score: 9 }, { context: SEED_WRITE_EXECUTION_CONTEXT }));
375375
expect([r.code, r.status]).toEqual(['VALIDATION_FAILED', 400]);
376376
expect(r.fields.map((f) => [f.field, f.code]).sort()).toEqual([['kind', 'invalid_option'], ['score', 'max_value']]);
377377
});

0 commit comments

Comments
 (0)