|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #30215 — an uninstalled package stops reading as a booted code package on |
| 5 | + * the running instance. |
| 6 | + * |
| 7 | + * `registerApp` puts a package's manifest in the engine's `manifests` map, and |
| 8 | + * ADR-0070's `isWritablePackage` reads that map FIRST: an id in it is a code |
| 9 | + * package, read-only. Every uninstall door (the protocol's `deletePackage`, |
| 10 | + * `DELETE /packages/:id`, the install-local DELETE) withdraws the package |
| 11 | + * through `SchemaRegistry.uninstallPackage`, and nothing took the id out of |
| 12 | + * the engine's map. So an administrator who uninstalled a package got a 200, |
| 13 | + * then `ensure-customization` on the same instance answered `writable: false` |
| 14 | + * for it until the server restarted. |
| 15 | + * |
| 16 | + * Pinned here, against a real engine and its own registry: |
| 17 | + * - the registry's uninstall drops the engine's manifest entry, so the |
| 18 | + * predicate answers writable again; |
| 19 | + * - an uninstall the registry REFUSES (ADR-0029: another package extends an |
| 20 | + * object this one owns) removes nothing, the manifest entry included; |
| 21 | + * - a reinstall of the same id puts the entry back (read-only again); |
| 22 | + * - the registry's announcement: once per withdrawn package, never for an id |
| 23 | + * it does not hold, never after unsubscribe, and a throwing listener does |
| 24 | + * not turn a completed uninstall into a refused one. |
| 25 | + */ |
| 26 | + |
| 27 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 28 | +import { isWritablePackage } from '@objectstack/metadata-protocol'; |
| 29 | +import { ObjectQL } from './engine.js'; |
| 30 | +import { SchemaRegistry } from './registry.js'; |
| 31 | + |
| 32 | +const PKG = 'com.example.tasksapp'; |
| 33 | + |
| 34 | +function manifestsOf(engine: ObjectQL): Map<string, unknown> { |
| 35 | + return (engine as unknown as { manifests: Map<string, unknown> }).manifests; |
| 36 | +} |
| 37 | + |
| 38 | +function bootWithPackage(): ObjectQL { |
| 39 | + const engine = new ObjectQL(); |
| 40 | + engine.registerApp({ |
| 41 | + id: PKG, |
| 42 | + namespace: 'tasks_app', |
| 43 | + objects: [{ name: 'tasks_app_task', fields: { name: { type: 'text' } } }], |
| 44 | + } as any); |
| 45 | + return engine; |
| 46 | +} |
| 47 | + |
| 48 | +describe('ObjectQL — an uninstall forgets the package\'s manifest (#30215)', () => { |
| 49 | + it('reads a registered code package as read-only, and writable once the registry uninstalls it', () => { |
| 50 | + const engine = bootWithPackage(); |
| 51 | + expect(manifestsOf(engine).has(PKG)).toBe(true); |
| 52 | + expect(isWritablePackage(engine, PKG)).toBe(false); |
| 53 | + |
| 54 | + expect(engine.registry.uninstallPackage(PKG)).toBe(true); |
| 55 | + |
| 56 | + expect(engine.registry.getPackage(PKG)).toBeUndefined(); |
| 57 | + expect(manifestsOf(engine).has(PKG)).toBe(false); |
| 58 | + expect(isWritablePackage(engine, PKG)).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('keeps the manifest entry when the registry refuses the uninstall (an extended object)', () => { |
| 62 | + const engine = bootWithPackage(); |
| 63 | + engine.registry.registerObject( |
| 64 | + { name: 'tasks_app_task', fields: {} } as any, 'com.example.addon', undefined, 'extend', |
| 65 | + ); |
| 66 | + |
| 67 | + expect(() => engine.registry.uninstallPackage(PKG)).toThrow(); |
| 68 | + |
| 69 | + expect(engine.registry.getPackage(PKG)).toBeDefined(); |
| 70 | + expect(manifestsOf(engine).has(PKG)).toBe(true); |
| 71 | + expect(isWritablePackage(engine, PKG)).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('puts the entry back on a reinstall of the same id', () => { |
| 75 | + const engine = bootWithPackage(); |
| 76 | + engine.registry.uninstallPackage(PKG); |
| 77 | + expect(isWritablePackage(engine, PKG)).toBe(true); |
| 78 | + |
| 79 | + engine.registerApp({ |
| 80 | + id: PKG, |
| 81 | + namespace: 'tasks_app', |
| 82 | + objects: [{ name: 'tasks_app_task', fields: { name: { type: 'text' } } }], |
| 83 | + } as any); |
| 84 | + |
| 85 | + expect(manifestsOf(engine).has(PKG)).toBe(true); |
| 86 | + expect(isWritablePackage(engine, PKG)).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it('leaves every other package\'s manifest entry in place', () => { |
| 90 | + const engine = bootWithPackage(); |
| 91 | + engine.registerApp({ id: 'com.example.notesapp', namespace: 'notes_app' } as any); |
| 92 | + |
| 93 | + engine.registry.uninstallPackage(PKG); |
| 94 | + |
| 95 | + expect(manifestsOf(engine).has('com.example.notesapp')).toBe(true); |
| 96 | + expect(isWritablePackage(engine, 'com.example.notesapp')).toBe(false); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('SchemaRegistry.onPackageUninstalled (#30215)', () => { |
| 101 | + afterEach(() => { vi.restoreAllMocks(); }); |
| 102 | + |
| 103 | + function registryWith(id: string): SchemaRegistry { |
| 104 | + const registry = new SchemaRegistry(); |
| 105 | + registry.installPackage({ id, name: id, version: '1.0.0', type: 'app' } as any, 'code'); |
| 106 | + return registry; |
| 107 | + } |
| 108 | + |
| 109 | + it('announces a withdrawn package once, after its record is gone', () => { |
| 110 | + const registry = registryWith(PKG); |
| 111 | + const seen: Array<{ id: string; recordGone: boolean }> = []; |
| 112 | + registry.onPackageUninstalled((id) => { |
| 113 | + seen.push({ id, recordGone: registry.getPackage(id) === undefined }); |
| 114 | + }); |
| 115 | + |
| 116 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 117 | + registry.uninstallPackage(PKG); |
| 118 | + registry.uninstallPackage(PKG); // nothing left to withdraw |
| 119 | + |
| 120 | + expect(seen).toEqual([{ id: PKG, recordGone: true }]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('announces nothing for an id the registry does not hold, or after unsubscribe', () => { |
| 124 | + const registry = registryWith(PKG); |
| 125 | + const listener = vi.fn(); |
| 126 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 127 | + const unsubscribe = registry.onPackageUninstalled(listener); |
| 128 | + |
| 129 | + registry.uninstallPackage('com.example.never-installed'); |
| 130 | + expect(listener).not.toHaveBeenCalled(); |
| 131 | + |
| 132 | + unsubscribe(); |
| 133 | + registry.uninstallPackage(PKG); |
| 134 | + expect(listener).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('a throwing listener neither refuses the uninstall nor starves the next listener, and is logged at error', () => { |
| 138 | + const registry = registryWith(PKG); |
| 139 | + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 140 | + const next = vi.fn(); |
| 141 | + registry.onPackageUninstalled(() => { throw new Error('boom'); }); |
| 142 | + registry.onPackageUninstalled(next); |
| 143 | + |
| 144 | + expect(registry.uninstallPackage(PKG)).toBe(true); |
| 145 | + |
| 146 | + expect(registry.getPackage(PKG)).toBeUndefined(); |
| 147 | + expect(next).toHaveBeenCalledWith(PKG); |
| 148 | + expect(error).toHaveBeenCalledTimes(1); |
| 149 | + expect(String(error.mock.calls[0][0])).toContain(`package "${PKG}" was uninstalled`); |
| 150 | + expect(String(error.mock.calls[0][0])).toContain('boom'); |
| 151 | + }); |
| 152 | +}); |
0 commit comments