|
38 | 38 | * the #22019 block of `packages/metadata-protocol/src/protocol.runtime-authoring-gate.test.ts`. |
39 | 39 | */ |
40 | 40 | import { describe, expect, it } from 'vitest'; |
| 41 | +import { applyInjectedSystemColumns } from '@objectstack/metadata-core'; |
41 | 42 | import { EXPRESSION_INVALID, runAuthoringRules } from './authoring-rules.js'; |
42 | 43 | import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor } from './runtime-gate.js'; |
43 | 44 | import { runStackExpressionPasses, validateStackExpressions } from './validate-expressions.js'; |
@@ -452,3 +453,148 @@ describe('#33072 — the object_extension door judges an extension field\'s form |
452 | 453 | expect(runtimeAuthoringRulesFor('object').map((r) => r.name)).toContain('validateStackExpressions'); |
453 | 454 | }); |
454 | 455 | }); |
| 456 | + |
| 457 | +describe('#33154 — an extension\'s validation rules are judged at the build and the object_extension door by the object\'s own rule pass', () => { |
| 458 | + /** The extended object, as the door's live universe carries it. */ |
| 459 | + const target = { |
| 460 | + name: 'fx_ticket', |
| 461 | + label: 'Ticket', |
| 462 | + sharingModel: 'private', |
| 463 | + fields: { |
| 464 | + employee: { type: 'lookup', label: 'Employee', reference: 'fx_staff' }, |
| 465 | + amount: { type: 'number', label: 'Amount' }, |
| 466 | + }, |
| 467 | + }; |
| 468 | + const staff = { |
| 469 | + name: 'fx_staff', label: 'Staff', sharingModel: 'private', |
| 470 | + fields: { daily_hours: { type: 'number', label: 'Daily Hours' } }, |
| 471 | + }; |
| 472 | + const script = (name: string, condition: string) => ({ name, type: 'script', condition, message: 'Refused.' }); |
| 473 | + /** One extension of `fx_ticket` adding the given rules (and fields). */ |
| 474 | + const extension = (validations: unknown[], fields: Record<string, unknown> = {}) => ({ extend: 'fx_ticket', fields, validations }); |
| 475 | + const RULE_WHERE = (rule: string) => `object 'fx_ticket' · objectExtensions[0] · validation '${rule}'`; |
| 476 | + const gateExtension = (item: unknown, objects: unknown[] = [target, staff]) => |
| 477 | + runRuntimeAuthoringRules({ type: 'object_extension', item, context: { objects } }); |
| 478 | + const buildFindings = (stack: Record<string, unknown>) => |
| 479 | + runAuthoringRules('build', { normalized: stack, parsed: stack }).filter((f) => f.where.includes('objectExtensions')); |
| 480 | + |
| 481 | + it('⭐ LIT — a rule reading a field the extended object does not serve is REFUSED, at the entry, with the slot\'s key path', () => { |
| 482 | + const result = gateExtension(extension([script('amount_positive', 'record.amont < 0')])); |
| 483 | + |
| 484 | + const errs = expressionFindings(result.errors); |
| 485 | + expect(errs, dump(result)).toHaveLength(1); |
| 486 | + expect(errs[0]).toMatchObject({ |
| 487 | + severity: 'error', |
| 488 | + where: RULE_WHERE('amount_positive'), |
| 489 | + path: 'objectExtensions[0].validations[0].condition', |
| 490 | + }); |
| 491 | + expect(errs[0]!.message).toContain('amont'); |
| 492 | + }); |
| 493 | + |
| 494 | + it('⭐ LIT — a rule calling an unregistered function (`sqrt`) is REFUSED, and a nested `then` branch is judged too', () => { |
| 495 | + const result = gateExtension(extension([ |
| 496 | + script('first_ok', 'record.amount != null && record.amount < 0'), |
| 497 | + { |
| 498 | + name: 'big_ticket', type: 'conditional', message: 'Refused.', |
| 499 | + when: 'record.amount != null && record.amount > 100', |
| 500 | + then: script('root_check', 'record.amount != null && sqrt(record.amount) > 10'), |
| 501 | + }, |
| 502 | + ])); |
| 503 | + |
| 504 | + const errs = expressionFindings(result.errors); |
| 505 | + expect(errs.map((f) => [f.where, f.path]), dump(result)).toEqual([ |
| 506 | + [`object 'fx_ticket' · objectExtensions[0] · validation rule 'big_ticket' then → 'root_check'`, 'objectExtensions[0].validations[1].then.condition'], |
| 507 | + ]); |
| 508 | + }); |
| 509 | + |
| 510 | + it('⭐ PARITY — on one shared input set, the door\'s findings deep-equal the build\'s', () => { |
| 511 | + for (const item of [ |
| 512 | + extension([script('typo', 'record.amont < 0')]), |
| 513 | + extension([script('fn', 'sqrt(record.amount) > 1')]), |
| 514 | + extension([script('bare', 'amount < 0')]), |
| 515 | + ]) { |
| 516 | + const atDoor = gateExtension(item); |
| 517 | + const atBuild = buildFindings({ objects: [target, staff], objectExtensions: [item] }); |
| 518 | + |
| 519 | + expect(atBuild.length, dump(atBuild)).toBeGreaterThan(0); |
| 520 | + expect([...atDoor.errors, ...atDoor.advisories]).toEqual(atBuild); |
| 521 | + } |
| 522 | + }); |
| 523 | + |
| 524 | + it('⭐ PARITY — the door reads the objects as served, injected system columns and all: an unguarded ordering over `created_at` is accepted there as at the build, one over a declared nullable field is refused at both', () => { |
| 525 | + // As the registry serves them: `created_at`, `owner_id`, … declared. |
| 526 | + const served = [applyInjectedSystemColumns(target), applyInjectedSystemColumns(staff)]; |
| 527 | + expect(Object.keys(served[0]!.fields)).toContain('created_at'); |
| 528 | + for (const [condition, refused] of [ |
| 529 | + ["record.created_at > timestamp('2020-01-01T00:00:00Z')", false], |
| 530 | + ['record.amount > 0', true], |
| 531 | + ] as const) { |
| 532 | + const item = extension([script('since', condition)]); |
| 533 | + const atDoor = gateExtension(item, served); |
| 534 | + const atBuild = buildFindings({ objects: [target, staff], objectExtensions: [item] }); |
| 535 | + |
| 536 | + expect([...atDoor.errors, ...atDoor.advisories], condition).toEqual(atBuild); |
| 537 | + expect(atBuild.length > 0, `${condition}: ${dump(atBuild)}`).toBe(refused); |
| 538 | + } |
| 539 | + }); |
| 540 | + |
| 541 | + it('⭐ CONTROL — a valid rule, a rule reading a field the same extension adds, and a one-hop read publish clean', () => { |
| 542 | + for (const item of [ |
| 543 | + extension([script('ok', 'record.amount != null && record.amount < 0')]), |
| 544 | + extension([script('bonus_ok', 'record.bonus != null && record.amount != null && record.bonus > record.amount')], { bonus: { type: 'number', label: 'Bonus' } }), |
| 545 | + // The one-hop read the rule pass serves, in the shape its traversal |
| 546 | + // check prescribes: gated on the reference being set. |
| 547 | + extension([{ |
| 548 | + name: 'hop_ok', type: 'conditional', message: 'Refused.', |
| 549 | + when: 'record.employee != null', |
| 550 | + then: script('long_day', 'record.employee.daily_hours > 12'), |
| 551 | + }]), |
| 552 | + ]) { |
| 553 | + const result = gateExtension(item); |
| 554 | + expect(result.errors, dump(result)).toEqual([]); |
| 555 | + expect(result.advisories, dump(result)).toEqual([]); |
| 556 | + expect(buildFindings({ objects: [target, staff], objectExtensions: [item] }), dump(item)).toEqual([]); |
| 557 | + } |
| 558 | + }); |
| 559 | + |
| 560 | + it('a rule reading a field a SIBLING package\'s extension adds resolves at the build; a misspelling of it is refused', () => { |
| 561 | + const sibling = { extend: 'fx_ticket', fields: { bonus: { type: 'number', label: 'Bonus' } } }; |
| 562 | + // An artifact: its top level repeats its bodies' objects, and the fold |
| 563 | + // reads each body's extensions. |
| 564 | + const stackWith = (item: unknown) => ({ |
| 565 | + objects: [target, staff], |
| 566 | + packages: [ |
| 567 | + { manifest: { objects: [target, staff] } }, |
| 568 | + { manifest: { objectExtensions: [sibling] } }, |
| 569 | + { manifest: { objectExtensions: [item] } }, |
| 570 | + ], |
| 571 | + }); |
| 572 | + |
| 573 | + expect(buildFindings(stackWith(extension([script('sib', 'record.bonus != null && record.bonus > 0')])))).toEqual([]); |
| 574 | + const typo = buildFindings(stackWith(extension([script('sib', 'record.bonsu != null && record.bonsu > 0')]))); |
| 575 | + expect(typo.map((f) => [f.rule, f.where]), dump(typo)).toEqual([ |
| 576 | + [EXPRESSION_INVALID, "object 'fx_ticket' · packages[2].manifest.objectExtensions[0] · validation 'sib'"], |
| 577 | + ]); |
| 578 | + }); |
| 579 | + |
| 580 | + it('an extension of an object the door does not carry is judged on what does not depend on its fields', () => { |
| 581 | + expect(expressionFindings(gateExtension(extension([script('fn', 'sqrt(record.amount) > 1')]), []).errors)).toHaveLength(1); |
| 582 | + const read = gateExtension(extension([script('read', 'record.amount != null && record.amount < 0')]), []); |
| 583 | + expect(read.errors, dump(read)).toEqual([]); |
| 584 | + }); |
| 585 | + |
| 586 | + it('on an extension write only the extension walk judges: the target\'s own broken rule is not this write\'s to answer for', () => { |
| 587 | + const broken = { ...target, validations: [script('own_typo', 'record.amont < 0')] }; |
| 588 | + const stack = { objects: [broken, staff], objectExtensions: [extension([script('ok', 'record.amount != null && record.amount < 0')])] }; |
| 589 | + // The build judges the object's own rule… |
| 590 | + expect(validateStackExpressions(stack).map((i) => i.where)).toEqual(["object 'fx_ticket' · validation 'own_typo'"]); |
| 591 | + // …the extension door's pass reads only the written entry. |
| 592 | + expect(runStackExpressionPasses(stack, { runtimeWriteType: 'object_extension' })).toEqual([]); |
| 593 | + }); |
| 594 | + |
| 595 | + it('CONTROL — the object save door still judges the object\'s own rule at its own location, and no extension rule', () => { |
| 596 | + const own = { ...target, validations: [script('own_typo', 'record.amont < 0')] }; |
| 597 | + const result = gateObject(own); |
| 598 | + expect(expressionFindings(result.errors).map((f) => f.where)).toEqual(["object 'fx_ticket' · validation 'own_typo'"]); |
| 599 | + }); |
| 600 | +}); |
0 commit comments