-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp-plugin.disabled-seed.test.ts
More file actions
227 lines (187 loc) · 8.74 KB
/
Copy pathapp-plugin.disabled-seed.test.ts
File metadata and controls
227 lines (187 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Persisted package disable-state must survive a restart of an EMPTY env (#5047).
*
* The seed that carries an operator's "disable this package" decision across a
* restart works by filling the registry's initial-disabled set BEFORE the first
* `installPackage` call, so that every registration path installs those
* packages disabled. It used to run AFTER `AppPlugin.init`'s empty-env early
* return — and an empty env (an artifact with no app payload) is precisely the
* environment whose packages ALL arrive later, from `sys_packages` hydration or
* an HTTP install. So on exactly those envs the set stayed empty and every
* disabled package came back ENABLED on each restart.
*
* These tests boot a REAL kernel (LiteKernel + ObjectQLPlugin + AppPlugin) over
* a REAL state file, and drive the REAL `PackageServicePlugin` rehydration, so
* the regression is pinned end to end rather than against a re-implementation.
*
* Reverse verification for the fix: move `seedPersistedDisabledPackages(ctx)`
* back below the `if (this.empty)` return in `app-plugin.ts` and every
* empty-env case here fails (`installed` / `enabled: true`), while the
* non-empty case stays green.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { LiteKernel } from '@objectstack/core';
import { ObjectQLPlugin } from '@objectstack/objectql';
import { PackageServicePlugin } from '@objectstack/service-package';
import { AppPlugin } from './app-plugin.js';
import { setPackageDisabled } from './package-state-store.js';
const ENVIRONMENT_ID = 'env_disabled_seed';
const DISABLED_ID = 'com.acme.reporting';
const ENABLED_ID = 'com.acme.billing';
interface InstalledPackageView {
status?: string;
enabled?: boolean;
}
interface TestRegistry {
installPackage(manifest: Record<string, unknown>): unknown;
getPackage(id: string): InstalledPackageView | undefined;
}
let home: string;
const envSnapshot = { OS_HOME: process.env.OS_HOME, OS_ENVIRONMENT_ID: process.env.OS_ENVIRONMENT_ID };
function manifestFor(id: string) {
return { id, name: id, version: '1.0.0', type: 'application' };
}
/**
* Boot the composition an empty environment actually runs: the engine plus an
* AppPlugin whose bundle carries no app payload.
*/
async function bootEmptyEnv(): Promise<{ kernel: LiteKernel; registry: TestRegistry }> {
const kernel = new LiteKernel({ logger: { level: 'error' } });
kernel.use(new ObjectQLPlugin({}));
kernel.use(new AppPlugin({}, { environmentId: ENVIRONMENT_ID, organizationId: 'org_test' }));
await kernel.bootstrap();
const ql = kernel.getService<{ registry: TestRegistry }>('objectql');
return { kernel, registry: ql.registry };
}
/** A PluginContext for PackageServicePlugin whose engine shares `registry`. */
function packageServiceCtx(registry: TestRegistry, rows: Array<Record<string, unknown>>) {
const execute = vi.fn(async ({ sql }: { sql: string }) => {
if (/SELECT \* FROM sys_packages/i.test(sql)) return { rows };
return { rows: [] }; // CREATE TABLE / INDEX / …
});
const services = new Map<string, unknown>([['objectql', { execute, registry }]]);
return {
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
getService: (n: string) => services.get(n),
registerService: (n: string, s: unknown) => services.set(n, s),
} as never;
}
function sysPackagesRow(manifest: Record<string, unknown>) {
return {
id: manifest.id,
version: manifest.version,
manifest: JSON.stringify(manifest),
metadata: '{}',
hash: 'h',
created_at: 't',
updated_at: 't',
};
}
beforeEach(() => {
home = mkdtempSync(join(tmpdir(), 'os-disabled-seed-'));
process.env.OS_HOME = home;
delete process.env.OS_ENVIRONMENT_ID;
});
afterEach(() => {
rmSync(home, { recursive: true, force: true });
if (envSnapshot.OS_HOME === undefined) delete process.env.OS_HOME;
else process.env.OS_HOME = envSnapshot.OS_HOME;
if (envSnapshot.OS_ENVIRONMENT_ID === undefined) delete process.env.OS_ENVIRONMENT_ID;
else process.env.OS_ENVIRONMENT_ID = envSnapshot.OS_ENVIRONMENT_ID;
});
describe('empty-env boot seeds persisted package disable-state (#5047)', () => {
it('a package registered after boot lands DISABLED — the hydration-only path', async () => {
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, true); // operator disabled it last run
const { kernel, registry } = await bootEmptyEnv();
// Nothing came from the (empty) artifact; this is the post-boot
// registration every package in such an env goes through.
registry.installPackage(manifestFor(DISABLED_ID));
expect(registry.getPackage(DISABLED_ID)).toMatchObject({
status: 'disabled',
enabled: false,
});
await kernel.shutdown();
});
it('seeds only the persisted ids — other packages still install enabled', async () => {
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, true);
const { kernel, registry } = await bootEmptyEnv();
registry.installPackage(manifestFor(ENABLED_ID));
expect(registry.getPackage(ENABLED_ID)).toMatchObject({
status: 'installed',
enabled: true,
});
await kernel.shutdown();
});
it('a package replayed from sys_packages by PackageServicePlugin lands DISABLED', async () => {
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, true);
// Phase 1: the empty-env kernel boots and seeds the registry.
const { kernel, registry } = await bootEmptyEnv();
// Phase 2: the real rehydration replays the durable row into that same
// registry (ADR-0033 consolidation).
await new PackageServicePlugin().start(
packageServiceCtx(registry, [
sysPackagesRow(manifestFor(DISABLED_ID)),
sysPackagesRow(manifestFor(ENABLED_ID)),
]),
);
expect(registry.getPackage(DISABLED_ID)).toMatchObject({
status: 'disabled',
enabled: false,
});
expect(registry.getPackage(ENABLED_ID)).toMatchObject({
status: 'installed',
enabled: true,
});
await kernel.shutdown();
});
it('re-enabling clears the persisted state — the package comes back enabled', async () => {
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, true);
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, false);
const { kernel, registry } = await bootEmptyEnv();
registry.installPackage(manifestFor(DISABLED_ID));
expect(registry.getPackage(DISABLED_ID)).toMatchObject({
status: 'installed',
enabled: true,
});
await kernel.shutdown();
});
it('boots an empty env with no persisted state at all (nothing to seed)', async () => {
const { kernel, registry } = await bootEmptyEnv();
registry.installPackage(manifestFor(DISABLED_ID));
expect(registry.getPackage(DISABLED_ID)).toMatchObject({ enabled: true });
await kernel.shutdown();
});
// Guards the other direction of the reorder: moving the seed earlier must
// not change what a NON-empty env already did.
it('non-empty env keeps its existing behavior — bundle package installs disabled', async () => {
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, true);
const kernel = new LiteKernel({ logger: { level: 'error' } });
kernel.use(new ObjectQLPlugin({}));
kernel.use(
new AppPlugin(
{ id: DISABLED_ID, name: DISABLED_ID, version: '1.0.0', objects: [] },
{ environmentId: ENVIRONMENT_ID, organizationId: 'org_test' },
),
);
await kernel.bootstrap();
const registry = kernel.getService<{ registry: TestRegistry }>('objectql').registry;
expect(registry.getPackage(DISABLED_ID)).toMatchObject({
status: 'disabled',
enabled: false,
});
await kernel.shutdown();
});
// The seed resolves `objectql` through getService; a kernel without an
// engine (metadata-only one-shot commands) must still boot.
it('degrades silently on a kernel with no engine at all', async () => {
setPackageDisabled(ENVIRONMENT_ID, DISABLED_ID, true);
const kernel = new LiteKernel({ logger: { level: 'error' } });
kernel.use(new AppPlugin({}, { environmentId: ENVIRONMENT_ID, organizationId: 'org_test' }));
await expect(kernel.bootstrap()).resolves.toBeUndefined();
await kernel.shutdown();
});
});