Skip to content

Commit 8aeb321

Browse files
committed
test(lint): pin the shared extension fold on junk input; say it does not mirror the legacy-name fold
objectExtensionsByTarget and objectsAsServed get the junk-input cases their neighbours have: a null stack, non-record packages entries and bodies, a non-array objectExtensions, non-record entries, junk fields and a string priority. The fold's docblock says it names the target verbatim and does not mirror the registry's short-name to legacy ns__name filing (#28858), so such an entry folds onto nothing and a key naming its field stays refused. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzfKwxFVA5m6bAjSpxf
1 parent f85110d commit 8aeb321

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎packages/lint/src/object-graph.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
nearestName,
1616
listNames,
1717
packagesOf,
18+
objectExtensionsByTarget,
19+
objectsAsServed,
1820
RELATIONSHIP_FIELD_TYPES,
1921
} from './object-graph.js';
2022
import { walkFilterFieldKeys, type FilterFieldKey } from './filter-walk.js';
@@ -345,6 +347,93 @@ describe('object-graph — packagesOf (#20206, ruling A on #15293 `5634034754`)'
345347
});
346348
});
347349

350+
/**
351+
* [#30303] The shared extension fold on junk input, beside the
352+
* `indexObjectGraph` and `packagesOf` cases above. Neither export refuses
353+
* anything: a malformed `packages` is {@link packagesOf}'s refusal, met by a
354+
* caller that reads the bodies through it (`validateSecurityPosture` does,
355+
* before either fold runs). Each shape below reads as "nothing to fold" for
356+
* the part that is junk, and never throws.
357+
*/
358+
describe('[#30303] object-graph — objectExtensionsByTarget / objectsAsServed on junk input', () => {
359+
const OBJ = { name: 'crm_contract', fields: { contract_no: { type: 'text' } } };
360+
const EXT = { extend: 'crm_contract', fields: { note: { type: 'text' } } };
361+
const served = (stack: unknown) =>
362+
objectsAsServed(stack).objects.map((o) => [o.name, (o.fields as Array<{ name: string }> | Record<string, unknown>)]);
363+
364+
it('a null or non-record stack: no extensions, no objects', () => {
365+
for (const stack of [null, undefined, 'junk', 42, []]) {
366+
expect(objectExtensionsByTarget(stack).size, String(stack)).toBe(0);
367+
expect(objectsAsServed(stack), String(stack)).toEqual({ objects: [], tied: new Map() });
368+
}
369+
});
370+
371+
it('packages: [null] and other non-record entries or bodies contribute nothing; a record body beside them still does', () => {
372+
expect(objectExtensionsByTarget({ packages: [null] }).size).toBe(0);
373+
const stack = {
374+
objects: [OBJ],
375+
packages: [null, 'junk', 42, [], { manifest: null }, { manifest: 'junk' }, { manifest: { objectExtensions: [EXT] } }],
376+
};
377+
expect([...objectExtensionsByTarget(stack).entries()].map(([t, list]) => [t, list.map((e) => [e.carrier, e.path])])).toEqual([
378+
['crm_contract', [[6, 'packages[6].manifest.objectExtensions[0]']]],
379+
]);
380+
expect(objectsAsServed(stack).objects[0].fields).toEqual([
381+
{ name: 'contract_no', type: 'text' },
382+
{ name: 'note', type: 'text' },
383+
]);
384+
});
385+
386+
it('a non-array objectExtensions folds nothing', () => {
387+
for (const objectExtensions of [null, 'junk', 42, { crm_contract: EXT }]) {
388+
const stack = { objects: [OBJ], objectExtensions };
389+
expect(objectExtensionsByTarget(stack).size, JSON.stringify(objectExtensions)).toBe(0);
390+
expect(objectsAsServed(stack).objects, JSON.stringify(objectExtensions)).toEqual([OBJ]);
391+
}
392+
});
393+
394+
it('a non-record entry, or one whose extend is not a string, is skipped; the record entry beside it is read', () => {
395+
const stack = {
396+
objects: [OBJ],
397+
objectExtensions: [null, 'junk', 42, [], { extend: 7, fields: { x: { type: 'text' } } }, { fields: { y: {} } }, EXT],
398+
};
399+
expect([...objectExtensionsByTarget(stack).keys()]).toEqual(['crm_contract']);
400+
expect(objectExtensionsByTarget(stack).get('crm_contract')!.map((e) => e.path)).toEqual(['objectExtensions[6]']);
401+
expect(served(stack)).toEqual([['crm_contract', [{ name: 'contract_no', type: 'text' }, { name: 'note', type: 'text' }]]]);
402+
});
403+
404+
it('junk fields add nothing: a non-collection, non-record members, a member with no name', () => {
405+
for (const fields of [null, 'junk', 42, [null, 'x', 3, { type: 'text' }, { name: '', type: 'text' }]]) {
406+
const stack = { objects: [OBJ], objectExtensions: [{ extend: 'crm_contract', fields }] };
407+
expect(() => objectsAsServed(stack), JSON.stringify(fields)).not.toThrow();
408+
expect(objectsAsServed(stack).objects[0].fields, JSON.stringify(fields)).toEqual([{ name: 'contract_no', type: 'text' }]);
409+
}
410+
// The array form is read by name, as the map form is.
411+
const arrayForm = { objects: [OBJ], objectExtensions: [{ extend: 'crm_contract', fields: [{ name: 'note', type: 'text' }] }] };
412+
expect(served(arrayForm)).toEqual([['crm_contract', [{ name: 'contract_no', type: 'text' }, { name: 'note', type: 'text' }]]]);
413+
});
414+
415+
// `ObjectExtensionSchema.priority` is a number, so a parsed stack never
416+
// carries this shape; the fold reads a non-number as absent (the default 200,
417+
// as `SchemaRegistry.registerObjectExtension` does) rather than guessing.
418+
it('a string priority reads as the default 200 and never throws', () => {
419+
const stack = {
420+
objects: [OBJ],
421+
packages: [
422+
{ manifest: { objectExtensions: [{ extend: 'crm_contract', priority: '100', fields: { note: { type: 'text', label: 'A' } } }] } },
423+
{ manifest: { objectExtensions: [{ extend: 'crm_contract', priority: 150, fields: { note: { type: 'text', label: 'B' } } }] } },
424+
],
425+
};
426+
expect(objectExtensionsByTarget(stack).get('crm_contract')!.map((e) => [e.carrier, e.priority])).toEqual([
427+
[1, 150],
428+
[0, 200],
429+
]);
430+
// So the string-priority entry folds LAST and its definition wins, untied.
431+
const { objects, tied } = objectsAsServed(stack);
432+
expect((objects[0].fields as Array<Record<string, unknown>>).find((f) => f.name === 'note')?.label).toBe('A');
433+
expect(tied.size).toBe(0);
434+
});
435+
});
436+
348437
/**
349438
* [#19289] A `{ type: 'user' }` field with no `reference` is TRAVERSABLE — the
350439
* third defect found by the implicit-target census, and the widest-reaching of

‎packages/lint/src/object-graph.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,14 @@ export interface VisibleObjectExtension {
397397
* (`packages[j].manifest`, `carrier` = `j`, the artifact's registration
398398
* order); without it, the stack's own (`carrier` 0). An artifact's top level
399399
* repeats its bodies' collections, so it is not read beside them.
400-
* - `extend` names its target verbatim.
400+
* - `extend` names its target verbatim. ⚠️ The registry does one more thing
401+
* this fold does not mirror (#28858, `SchemaRegistry.extensionTargetKey`):
402+
* a short name that nothing is registered under but that `getObject` folds
403+
* to a legacy `<ns>__<name>` key is filed under that key, with a warning.
404+
* Lint reads the stack's own object names and knows no namespace, so such
405+
* an entry folds onto nothing here: a key naming its field reads as
406+
* unknown, the refusing direction, and the warning's remedy (write the key
407+
* the object is registered under) is also what makes the fold see it.
401408
* - ONE entry per package per target: registering an extension removes that
402409
* package's earlier one, so a later entry of the same package replaces an
403410
* earlier one whole, its fields included.

0 commit comments

Comments
 (0)