|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #21276 — `SchemaRegistry.assertPackageUninstallable` asks the uninstall's |
| 5 | + * ADR-0029 refusal without performing the uninstall. |
| 6 | + * |
| 7 | + * `deletePackage` (`@objectstack/metadata-protocol`) must decide this refusal |
| 8 | + * before it deletes the stored `sys_packages` row, and the only place the |
| 9 | + * registry used to decide it was inside `unregisterObjectsByPackage`, which |
| 10 | + * mutates when it does not refuse. The refusal pass (#7970) is now this one |
| 11 | + * method, and `unregisterObjectsByPackage` calls it: ONE predicate. |
| 12 | + * |
| 13 | + * Pinned here: |
| 14 | + * 1. it refuses with the uninstall's exact sentence, and mutates nothing; |
| 15 | + * 2. it answers normally for an uninstallable package, and mutates nothing; |
| 16 | + * 3. `unregisterObjectsByPackage` still refuses with that same sentence, by |
| 17 | + * calling this method (not a copy of its predicate), and `force` still |
| 18 | + * means "do not ask". |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 22 | +import { SchemaRegistry } from './registry'; |
| 23 | + |
| 24 | +const REFUSAL = |
| 25 | + 'Cannot uninstall package "com.owner": object "alpha" is extended by com.ext1, com.ext2. Uninstall extenders first.'; |
| 26 | + |
| 27 | +/** Every contributor of every object the fixture registers, as plain data. */ |
| 28 | +function contributorsOf(registry: SchemaRegistry) { |
| 29 | + return ['free', 'alpha', 'beta'].map((name) => ({ |
| 30 | + name, |
| 31 | + resolves: registry.getObject(name) !== undefined, |
| 32 | + contributors: registry.getObjectContributors(name).map((c) => `${c.packageId}:${c.ownership}`), |
| 33 | + })); |
| 34 | +} |
| 35 | + |
| 36 | +describe('#21276 SchemaRegistry.assertPackageUninstallable', () => { |
| 37 | + let registry: SchemaRegistry; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + registry = new SchemaRegistry({ multiTenant: false }); |
| 41 | + // `free` is walked FIRST and is not extended; `alpha` is the first |
| 42 | + // refusable object, with two extenders; `beta` is refusable too. |
| 43 | + registry.registerObject({ name: 'free', fields: {} } as any, 'com.owner', 'base', 'own'); |
| 44 | + registry.registerObject({ name: 'alpha', fields: {} } as any, 'com.owner', 'base', 'own'); |
| 45 | + registry.registerObject({ name: 'beta', fields: {} } as any, 'com.owner', 'base', 'own'); |
| 46 | + registry.registerObject({ name: 'alpha', fields: {} } as any, 'com.ext1', undefined, 'extend'); |
| 47 | + registry.registerObject({ name: 'alpha', fields: {} } as any, 'com.ext2', undefined, 'extend'); |
| 48 | + registry.registerObject({ name: 'beta', fields: {} } as any, 'com.ext3', undefined, 'extend'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('refuses with the uninstall\'s exact sentence, and mutates nothing', () => { |
| 52 | + const before = contributorsOf(registry); |
| 53 | + |
| 54 | + expect(() => registry.assertPackageUninstallable('com.owner')).toThrow(REFUSAL); |
| 55 | + |
| 56 | + expect(contributorsOf(registry)).toEqual(before); |
| 57 | + expect(before.every((o) => o.resolves)).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('answers normally for a package nothing extends, and mutates nothing', () => { |
| 61 | + registry.registerObject({ name: 'free', fields: {} } as any, 'com.ext1', undefined, 'extend'); |
| 62 | + const before = contributorsOf(registry); |
| 63 | + |
| 64 | + expect(() => registry.assertPackageUninstallable('com.ext1')).not.toThrow(); |
| 65 | + expect(() => registry.assertPackageUninstallable('com.unknown')).not.toThrow(); |
| 66 | + |
| 67 | + expect(contributorsOf(registry)).toEqual(before); |
| 68 | + }); |
| 69 | + |
| 70 | + it('unregisterObjectsByPackage refuses with the same sentence BY calling it; force does not ask', () => { |
| 71 | + const ask = vi.spyOn(registry, 'assertPackageUninstallable'); |
| 72 | + |
| 73 | + expect(() => registry.unregisterObjectsByPackage('com.owner')).toThrow(REFUSAL); |
| 74 | + expect(ask).toHaveBeenCalledTimes(1); |
| 75 | + expect(ask).toHaveBeenCalledWith('com.owner'); |
| 76 | + expect(registry.getObject('free')).toBeDefined(); |
| 77 | + |
| 78 | + ask.mockClear(); |
| 79 | + registry.unregisterObjectsByPackage('com.owner', true); |
| 80 | + expect(ask).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments