|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #14367 — `SchemaRegistry.registerObject`'s cross-package ownership refusal |
| 5 | + * carries an ADR-0112 envelope. |
| 6 | + * |
| 7 | + * ## What this pins, and why an envelope rather than a throw |
| 8 | + * |
| 9 | + * The refusal (ADR-0029 D3, single owner per object name) used to be a bare |
| 10 | + * `Error`. Measured while reverse-verifying the install-time |
| 11 | + * `DUPLICATE_ARTIFACT_OBJECT_NAME` check one layer up (#14163): with that |
| 12 | + * check ablated, `expect(refused).toBeDefined()` STAYED GREEN, because this |
| 13 | + * refusal fired one step later and looked, to a throw-shaped assertion, |
| 14 | + * exactly like the check that had just been deleted. Only the envelope |
| 15 | + * assertion (`code` + `status`) went red. So every rejection test on this |
| 16 | + * path could only be a bare `toThrow()` — precisely the assertion ADR-0112 |
| 17 | + * and ADR-0130 D3 rule out by name. |
| 18 | + * |
| 19 | + * Three facts, each its own case so a failure reads as the specific |
| 20 | + * regression: |
| 21 | + * |
| 22 | + * 1. the refusal is `ObjectOwnershipConflictError` with `code` + |
| 23 | + * `status: 422` (and the two package ids + the object name as fields); |
| 24 | + * 2. the message text is byte-for-byte what the bare `Error` carried — |
| 25 | + * the fence that keeps every message-substring assertion and every |
| 26 | + * `console.warn` forwarder unchanged; |
| 27 | + * 3. the ADR-0029 D9 §6.1 late-install branch beside it (a TENANT-authored |
| 28 | + * sitting owner) is NOT a refusal and does not throw this class — or |
| 29 | + * anything. |
| 30 | + */ |
| 31 | + |
| 32 | +import { describe, it, expect } from 'vitest'; |
| 33 | +import { ObjectOwnershipConflictError, SchemaRegistry } from './registry.js'; |
| 34 | + |
| 35 | +const APP_PKG = 'app.myapp'; |
| 36 | +const OTHER_PKG = 'app.otherapp'; |
| 37 | + |
| 38 | +const packagedBody = (name: string) => ({ |
| 39 | + name, |
| 40 | + label: 'Invoice', |
| 41 | + fields: { |
| 42 | + name: { name: 'name', type: 'text', label: 'Name' }, |
| 43 | + packaged_only: { name: 'packaged_only', type: 'text', label: 'Packaged only' }, |
| 44 | + }, |
| 45 | +}); |
| 46 | + |
| 47 | +const silent = () => { |
| 48 | + const r = new SchemaRegistry({ multiTenant: false }); |
| 49 | + r.logLevel = 'silent'; |
| 50 | + return r; |
| 51 | +}; |
| 52 | + |
| 53 | +const kinds = (r: SchemaRegistry, name: string) => |
| 54 | + r.getObjectContributors(name).map((c) => c.ownership); |
| 55 | + |
| 56 | +/** What a synchronous registration REFUSED with, or `undefined` when it did not refuse. */ |
| 57 | +const refusalOf = (run: () => unknown): unknown => { |
| 58 | + try { |
| 59 | + run(); |
| 60 | + return undefined; |
| 61 | + } catch (e) { |
| 62 | + return e; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * The text the bare `Error` carried, spelled out in full rather than matched |
| 68 | + * by substring: a substring match would stay green through a rewording that |
| 69 | + * still contained the fragment, and the whole point of the fence is that the |
| 70 | + * forwarders' `console.warn` lines and the existing regex assertions read the |
| 71 | + * SAME bytes as before. |
| 72 | + */ |
| 73 | +const LEGACY_MESSAGE = |
| 74 | + 'Object "myapp_invoice" is already owned by package "app.myapp". ' + |
| 75 | + "Package \"app.otherapp\" cannot claim ownership. Use 'extend' to add fields."; |
| 76 | + |
| 77 | +describe('#14367 — the cross-package ownership refusal is an ADR-0112 envelope', () => { |
| 78 | + it('refuses a second code package with `ObjectOwnershipConflictError`: code + status 422, both packages and the object named', () => { |
| 79 | + const r = silent(); |
| 80 | + r.registerObject(packagedBody('myapp_invoice') as any, APP_PKG); |
| 81 | + |
| 82 | + const refused = refusalOf(() => r.registerObject(packagedBody('myapp_invoice') as any, OTHER_PKG)); |
| 83 | + |
| 84 | + expect(refused).toBeInstanceOf(ObjectOwnershipConflictError); |
| 85 | + // The envelope — never a bare `toThrow()`. |
| 86 | + expect(refused).toMatchObject({ code: 'OBJECT_OWNERSHIP_CONFLICT', status: 422 }); |
| 87 | + const err = refused as ObjectOwnershipConflictError; |
| 88 | + expect(err.name).toBe('ObjectOwnershipConflictError'); |
| 89 | + expect(err.objectName).toBe('myapp_invoice'); |
| 90 | + expect(err.existingPackageId).toBe(APP_PKG); |
| 91 | + expect(err.incomingPackageId).toBe(OTHER_PKG); |
| 92 | + // Nothing half-applied: the sitting owner is untouched. |
| 93 | + expect(kinds(r, 'myapp_invoice')).toEqual(['own']); |
| 94 | + expect(r.getObjectOwner('myapp_invoice')?.packageId).toBe(APP_PKG); |
| 95 | + }); |
| 96 | + |
| 97 | + it('keeps the message text byte-for-byte — the fence every substring assertion and forwarder relies on', () => { |
| 98 | + const r = silent(); |
| 99 | + r.registerObject(packagedBody('myapp_invoice') as any, APP_PKG); |
| 100 | + |
| 101 | + const refused = refusalOf(() => r.registerObject(packagedBody('myapp_invoice') as any, OTHER_PKG)); |
| 102 | + |
| 103 | + expect((refused as Error).message).toBe(LEGACY_MESSAGE); |
| 104 | + // …and the existing sites' regex still matches it, which is the same fact |
| 105 | + // from the other side. |
| 106 | + expect((refused as Error).message).toMatch(/already owned by package "app\.myapp"/); |
| 107 | + }); |
| 108 | + |
| 109 | + it('the class is constructible on its own with the same envelope and the same text', () => { |
| 110 | + // Pinned directly so a change to the constructor's message template is a |
| 111 | + // change to THIS line, not only to whatever registry path happens to |
| 112 | + // exercise it. |
| 113 | + const err = new ObjectOwnershipConflictError('myapp_invoice', APP_PKG, OTHER_PKG); |
| 114 | + expect(err).toBeInstanceOf(Error); |
| 115 | + expect(err.code).toBe('OBJECT_OWNERSHIP_CONFLICT'); |
| 116 | + expect(err.status).toBe(422); |
| 117 | + expect(err.message).toBe(LEGACY_MESSAGE); |
| 118 | + }); |
| 119 | + |
| 120 | + /** |
| 121 | + * THE FENCE (ADR-0029 D9 §6.1). A code package registering an object a |
| 122 | + * TENANT row already holds is a late install, not a refusal: the code layer |
| 123 | + * takes ownership and the tenant contribution becomes its overlay. Out of |
| 124 | + * scope for the envelope by ruling, and pinned here so the envelope cannot |
| 125 | + * creep onto it: the branch throws nothing at all. |
| 126 | + */ |
| 127 | + it('does NOT refuse the D9 §6.1 late install — a tenant-authored sitting owner is re-classified, nothing is thrown', () => { |
| 128 | + const r = silent(); |
| 129 | + r.registerObject( |
| 130 | + { ...packagedBody('myapp_invoice'), _provenance: 'org' } as any, |
| 131 | + 'sys_metadata', |
| 132 | + ); |
| 133 | + |
| 134 | + const refused = refusalOf(() => r.registerObject(packagedBody('myapp_invoice') as any, APP_PKG)); |
| 135 | + |
| 136 | + expect(refused).toBeUndefined(); |
| 137 | + expect(refused).not.toBeInstanceOf(ObjectOwnershipConflictError); |
| 138 | + expect(kinds(r, 'myapp_invoice')).toEqual(['own', 'overlay']); |
| 139 | + expect(r.getObjectOwner('myapp_invoice')?.packageId).toBe(APP_PKG); |
| 140 | + }); |
| 141 | + |
| 142 | + /** The remedy the message prescribes is not a claim: `extend` from another package is accepted. */ |
| 143 | + it("accepts the message's own remedy — an `extend` from the other package is not an ownership claim", () => { |
| 144 | + const r = silent(); |
| 145 | + r.registerObject(packagedBody('myapp_invoice') as any, APP_PKG); |
| 146 | + |
| 147 | + const refused = refusalOf(() => |
| 148 | + r.registerObject( |
| 149 | + { name: 'myapp_invoice', fields: { ext_field: { name: 'ext_field', type: 'text' } } } as any, |
| 150 | + OTHER_PKG, undefined, 'extend', |
| 151 | + ), |
| 152 | + ); |
| 153 | + |
| 154 | + expect(refused).toBeUndefined(); |
| 155 | + expect(kinds(r, 'myapp_invoice')).toEqual(['own', 'extend']); |
| 156 | + expect(r.getObjectOwner('myapp_invoice')?.packageId).toBe(APP_PKG); |
| 157 | + }); |
| 158 | +}); |
0 commit comments