Repository navigation
Expand file tree
/
Copy pathpackageless-overlays.integration.test.ts
More file actions
438 lines (401 loc) · 20.6 KB
/
Copy pathpackageless-overlays.integration.test.ts
File metadata and controls
438 lines (401 loc) · 20.6 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* `os migrate packageless-overlays` against a real SQLite database: the
* ceremony that moves package-less overlay rows of a shipped name into the
* base's paired `<base>.custom` as declared overrides (ADR-0146 D11, N-16a).
*
* ## The fixture
*
* A compiled artifact (`dist/objectstack.json`, the shape a deployed app boots
* from) whose package ships an object and its list view, over a database an
* older release left behind with, bound to no package:
*
* - MOVED: an active row and a draft row of the package's view;
* - CONTROL, never moved: a view row of a name no package ships (environment
* metadata), and a draft `permission` row over a name the package ships
* (the security catalog's, ADR-0146 D6 — not an overlay type).
*
* ## What each case measures
*
* The command runs in-process through oclif, as the binary runs it. The
* preview writes nothing; `--apply` creates the pair through its door
* (`sys_packages` holds it), moves both rows with the declaration, leaves the
* controls, and a FRESH boot of the same database then serves the moved
* content under the base's name on the package-less read, the read naming the
* base, the layered read and the base's list; a second `--apply` moves 0.
*
* [#29392] A second template holds a row bound to the package itself (a save
* naming it, before that write was closed): listed and moved the same way.
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { SqlDriver } from '@objectstack/driver-sql';
import { PackageServicePlugin } from '@objectstack/service-package';
import { isExitSignal } from '../../utils/format.js';
import { bootSchemaStack } from '../../utils/schema-migrate.js';
import MigratePackagelessOverlays from './packageless-overlays.js';
// [#10126] Pay the first transform of these dist-resolved workspace deps at
// MODULE LOAD: the command reaches them through dynamic `import()`s inside
// `run()`, which vitest clocks (`scripts/check-test-source-alias.mjs`).
import '@objectstack/runtime';
import '@objectstack/metadata-protocol';
import '@objectstack/platform-objects/plugin';
const HERE = dirname(fileURLToPath(import.meta.url));
const CLI_ROOT = resolve(HERE, '..', '..', '..');
const CASE_TIMEOUT_MS = 180_000;
const PKG = 'com.example.m28413';
const PAIR = `${PKG}.custom`;
const VIEW = 'm28413_task.default';
const ENV_VIEW = 'm28413_env.default';
const SHIPPED_COLUMNS = ['title', 'status', 'owner'];
const STORED_COLUMNS = ['title', 'owner'];
const DRAFT_COLUMNS = ['title'];
const ARTIFACT = {
manifest: { id: PKG, name: 'm28413', version: '1.4.0', type: 'app', namespace: 'm28413' },
objects: [{
name: 'm28413_task',
label: 'Task',
sharingModel: 'private',
fields: Object.fromEntries(SHIPPED_COLUMNS.map((f) => [f, { name: f, label: f, type: 'text' }])),
}],
views: [{
object: 'm28413_task',
list: { label: 'All', type: 'grid', data: { provider: 'object', object: 'm28413_task' }, columns: SHIPPED_COLUMNS.map((field) => ({ field })) },
}],
permissions: [{ name: 'm28413_rep', label: 'Rep', objects: {} }],
};
const viewBody = (name: string, columns: readonly string[]) => ({
name,
object: 'm28413_task',
viewKind: 'list',
label: 'Saved in Setup',
config: { type: 'grid', data: { provider: 'object', object: 'm28413_task' }, columns: columns.map((field) => ({ field })) },
});
interface SeedRow { type: string; name: string; state: string; body: Record<string, unknown>; packageId?: string }
const SEED: SeedRow[] = [
{ type: 'view', name: VIEW, state: 'active', body: viewBody(VIEW, STORED_COLUMNS) },
{ type: 'view', name: VIEW, state: 'draft', body: viewBody(VIEW, DRAFT_COLUMNS) },
// Controls.
{ type: 'view', name: ENV_VIEW, state: 'active', body: viewBody(ENV_VIEW, STORED_COLUMNS) },
// A DRAFT: an active one over a package's name refuses the cold boot, which
// is \`os migrate security-catalog-overlays\`' to clear, not this ceremony's.
{ type: 'permission', name: 'm28413_rep', state: 'draft', body: { name: 'm28413_rep', label: 'Saved in the environment', objects: {} } },
];
/**
* [#29392] A database whose customization a save naming the package itself
* stored before that write was closed: an active row of the package's view
* bound to the package, beside the package-less environment row (control).
*/
const BOUND_SEED: SeedRow[] = [
{ type: 'view', name: VIEW, state: 'active', body: viewBody(VIEW, STORED_COLUMNS), packageId: PKG },
{ type: 'view', name: ENV_VIEW, state: 'active', body: viewBody(ENV_VIEW, STORED_COLUMNS) },
];
/**
* [#30261] A database whose customization of the package's view container (a
* list view the package does not ship added beside its default list) was
* stored as a package-less CONTAINER row, beside the environment row (control).
*/
const CONTAINER = 'm28413_task';
const CONTAINER_SEED: SeedRow[] = [
{
type: 'view', name: CONTAINER, state: 'active', body: {
object: 'm28413_task',
list: { label: 'All', type: 'grid', data: { provider: 'object', object: 'm28413_task' }, columns: STORED_COLUMNS.map((field) => ({ field })) },
listViews: { mine: { label: 'Mine', type: 'grid', data: { provider: 'object', object: 'm28413_task' }, columns: DRAFT_COLUMNS.map((field) => ({ field })) } },
},
},
{ type: 'view', name: ENV_VIEW, state: 'active', body: viewBody(ENV_VIEW, STORED_COLUMNS) },
];
let dir: string;
let templateDb: string;
let boundTemplateDb: string;
let containerTemplateDb: string;
const savedCwd = process.cwd();
const ENV_KEYS = ['OS_ARTIFACT_PATH', 'OS_DATABASE_URL', 'DATABASE_URL', 'OS_DATABASE_DRIVER', 'OS_HOME', 'NODE_ENV',
'OS_AUTH_SECRET', 'AUTH_SECRET', 'BETTER_AUTH_SECRET', 'OS_ENVIRONMENT_ID'] as const;
const savedEnv: Record<string, string | undefined> = {};
let caseSeq = 0;
function caseDb(template = templateDb): string {
caseSeq += 1;
const file = join(dir, 'cases', `${caseSeq}.db`);
mkdirSync(dirname(file), { recursive: true });
copyFileSync(template, file);
return file;
}
/** The rows an older release left, written at the driver the way that release stored them. */
async function insertRows(dbFile: string, rows: readonly SeedRow[]): Promise<void> {
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: dbFile }, useNullAsDefault: true });
try {
const k = (driver as any).knex;
const now = new Date().toISOString();
let n = 0;
for (const row of rows) {
n += 1;
await k('sys_metadata').insert({
id: `seed_${n}`, type: row.type, name: row.name, package_id: row.packageId ?? null, state: row.state,
version: 1, checksum: null, created_at: now, updated_at: now, metadata: JSON.stringify(row.body),
});
}
} finally {
await driver.disconnect();
}
}
/** Every `sys_metadata` row, `type/name@binding/state`, read on a connection of our own. */
async function storedRows(dbFile: string): Promise<string[]> {
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: dbFile }, useNullAsDefault: true });
try {
const rows = await (driver as any).knex('sys_metadata').select('type', 'name', 'package_id', 'state');
return rows.map((r: any) => `${r.type}/${r.name}@${r.package_id ?? '-'}/${r.state}`).sort();
} finally {
await driver.disconnect();
}
}
/** One stored body, parsed. */
async function storedBody(dbFile: string, where: Record<string, unknown>): Promise<any> {
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: dbFile }, useNullAsDefault: true });
try {
const row = await (driver as any).knex('sys_metadata').where(where).first();
return row ? JSON.parse(row.metadata) : undefined;
} finally {
await driver.disconnect();
}
}
/** The `sys_packages` ids stored. */
async function storedPackages(dbFile: string): Promise<string[]> {
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: dbFile }, useNullAsDefault: true });
try {
const rows = await (driver as any).knex('sys_packages').select('id');
return rows.map((r: any) => String(r.id)).sort();
} finally {
await driver.disconnect();
}
}
interface RunResult { payload: any; exitCode: number }
/** Run the command through oclif and capture its one JSON document. */
async function runJson(argv: string[]): Promise<RunResult> {
const savedExit = process.exitCode;
const swallow = ((_chunk: unknown, ...rest: unknown[]) => {
const cb = rest.find((a) => typeof a === 'function') as (() => void) | undefined;
if (cb) cb();
return true;
}) as typeof process.stdout.write;
const stdout = vi.spyOn(process.stdout, 'write').mockImplementation(swallow);
vi.spyOn(process.stderr, 'write').mockImplementation(swallow);
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
let thrownExit: number | undefined;
try {
try {
await MigratePackagelessOverlays.run(argv, { root: CLI_ROOT });
} catch (error) {
if (!isExitSignal(error)) throw error;
thrownExit = (error as { oclif?: { exit?: number } }).oclif?.exit;
}
const out = stdout.mock.calls.map((c) => String(c[0])).join('').trim();
let payload: any;
try { payload = JSON.parse(out); } catch { payload = { unparsed: out.slice(-400) }; }
return { payload, exitCode: thrownExit ?? (process.exitCode as number | undefined) ?? 0 };
} finally {
process.exitCode = savedExit;
vi.restoreAllMocks();
}
}
const columnsOf = (item: any): string[] => (item?.config?.columns ?? item?.columns ?? []).map((c: any) => c?.field ?? c);
/** What a fresh boot of `dbFile` serves for `VIEW`: the package-less read, the read naming the base, the layered effective, the base's list. */
async function servedColumns(dbFile: string): Promise<string[][]> {
const stack = await bootSchemaStack({
jsonOutput: false, databaseUrl: `file:${dbFile}`, projectRoot: dir, composeHostStack: true,
// The package registry `os serve` composes, so the pair installs from `sys_packages` as it does there.
extraPlugins: [new PackageServicePlugin()],
});
try {
const protocol = stack.kernel.getService('protocol') as any;
const read = async (packageId?: string) =>
(await protocol.getMetaItem({ type: 'view', name: VIEW, ...(packageId ? { packageId } : {}) }))?.item;
const layered = await protocol.getMetaItemLayered({ type: 'view', name: VIEW });
const list = await protocol.getMetaItems({ type: 'view', packageId: PKG });
return [
columnsOf(await read()),
columnsOf(await read(PKG)),
columnsOf(layered?.effective),
...((list?.items ?? []) as any[]).filter((i) => i?.name === VIEW).map((i) => columnsOf(i)),
];
} finally {
await stack.shutdown();
}
}
beforeAll(async () => {
for (const key of ENV_KEYS) savedEnv[key] = process.env[key];
for (const key of ENV_KEYS) delete process.env[key];
process.env.NODE_ENV = 'production';
dir = mkdtempSync(join(tmpdir(), 'os-28413-'));
mkdirSync(join(dir, 'dist'), { recursive: true });
process.env.OS_HOME = join(dir, 'home');
process.env.OS_ARTIFACT_PATH = join(dir, 'dist', 'objectstack.json');
writeFileSync(process.env.OS_ARTIFACT_PATH, JSON.stringify(ARTIFACT));
// The database an older release left: tables provisioned, then the
// package-less rows written at the driver the way that release stored them.
templateDb = join(dir, 'template.db');
const seed = await bootSchemaStack({
jsonOutput: false, databaseUrl: `file:${templateDb}`, projectRoot: dir, hydrateMetadata: false,
extraPlugins: [new PackageServicePlugin()],
});
await seed.shutdown();
boundTemplateDb = join(dir, 'bound-template.db');
copyFileSync(templateDb, boundTemplateDb);
containerTemplateDb = join(dir, 'container-template.db');
copyFileSync(templateDb, containerTemplateDb);
await insertRows(templateDb, SEED);
await insertRows(boundTemplateDb, BOUND_SEED);
await insertRows(containerTemplateDb, CONTAINER_SEED);
}, CASE_TIMEOUT_MS);
afterAll(() => {
process.chdir(savedCwd);
for (const [key, value] of Object.entries(savedEnv)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
});
// The command takes `process.cwd()` as the project root, as a real invocation does.
beforeEach(() => { process.chdir(dir); });
afterEach(() => { process.chdir(savedCwd); });
describe('os migrate packageless-overlays', () => {
it('the preview lists the shipped name\'s rows to move, counts the environment row, and writes nothing', async () => {
const db = caseDb();
const before = await storedRows(db);
const { payload, exitCode } = await runJson(['--database-url', `file:${db}`, '--json']);
expect(payload.error, JSON.stringify(payload).slice(0, 600)).toBeUndefined();
expect(exitCode).toBe(0);
expect(payload.apply).toBe(false);
expect(payload.rows.map((r: any) => `${r.type}/${r.name}/${r.state} ${r.disposition} → ${r.pair}`)).toEqual([
`view/${VIEW}/active movable → ${PAIR}`,
`view/${VIEW}/draft movable → ${PAIR}`,
]);
expect(payload.environmentRows).toBe(1);
expect(await storedRows(db)).toEqual(before);
expect(await storedPackages(db)).not.toContain(PAIR);
}, CASE_TIMEOUT_MS);
it('--apply moves both rows into the pair with the declaration, keeps the controls, serves the same content, and a re-run moves 0', async () => {
const db = caseDb();
const served = await servedColumns(db);
expect(served).toEqual([STORED_COLUMNS, STORED_COLUMNS, STORED_COLUMNS, STORED_COLUMNS]);
const { payload, exitCode } = await runJson(['--apply', '--yes', '--database-url', `file:${db}`, '--json']);
expect(payload.error, JSON.stringify(payload).slice(0, 600)).toBeUndefined();
expect(exitCode).toBe(0);
expect(payload.moved).toBe(2);
expect(payload.failed).toBe(0);
expect(payload.bases).toEqual([{ base: PKG, pair: PAIR, pairCreated: true, moved: 2, declarationsCompleted: 0, failed: 0, shadowed: 0 }]);
expect(payload.rows.map((r: any) => `${r.state} ${r.outcome} ${r.packageId} ${r.basedOn}`)).toEqual([
`active moved ${PAIR} 1.4.0`,
`draft moved ${PAIR} 1.4.0`,
]);
expect(await storedRows(db)).toEqual([
`permission/m28413_rep@-/draft`,
`view/${ENV_VIEW}@-/active`,
`view/${VIEW}@${PAIR}/active`,
`view/${VIEW}@${PAIR}/draft`,
]);
for (const state of ['active', 'draft']) {
const body = await storedBody(db, { type: 'view', name: VIEW, package_id: PAIR, state });
expect(body.overrides, state).toEqual({ package: PKG, basedOn: '1.4.0' });
}
expect(columnsOf(await storedBody(db, { type: 'view', name: VIEW, package_id: PAIR, state: 'draft' }))).toEqual(DRAFT_COLUMNS);
// The pair came through its door, so it outlives this process.
expect(await storedPackages(db)).toContain(PAIR);
// A fresh boot serves the moved content under the base's name.
expect(await servedColumns(db)).toEqual(served);
const again = await runJson(['--apply', '--yes', '--database-url', `file:${db}`, '--json']);
expect(again.payload.error, JSON.stringify(again.payload).slice(0, 600)).toBeUndefined();
expect(again.exitCode).toBe(0);
expect(again.payload.moved).toBe(0);
expect(again.payload.rows).toEqual([]);
}, CASE_TIMEOUT_MS * 3);
it('--apply without --yes in --json refuses and moves nothing', async () => {
const db = caseDb();
const before = await storedRows(db);
const { payload, exitCode } = await runJson(['--apply', '--database-url', `file:${db}`, '--json']);
expect(payload.error, JSON.stringify(payload).slice(0, 600)).toBe('confirmation_required');
expect(exitCode).toBe(1);
expect(await storedRows(db)).toEqual(before);
}, CASE_TIMEOUT_MS);
});
/**
* [#29392] A row bound to the read-only package itself, stored by a save
* naming it before that write was closed: the census lists it beside the
* package-less rows (`boundToBase`), and `--apply` moves it into the pair with
* the declaration, the same outcome. Before, the census read package-less
* rows only: the preview listed nothing and the row's one exit was a delete.
*/
describe('os migrate packageless-overlays: a row bound to the read-only package', () => {
it('the preview lists it with its target pair and writes nothing', async () => {
const db = caseDb(boundTemplateDb);
const before = await storedRows(db);
const { payload, exitCode } = await runJson(['--database-url', `file:${db}`, '--json']);
expect(payload.error, JSON.stringify(payload).slice(0, 600)).toBeUndefined();
expect(exitCode).toBe(0);
expect(payload.rows.map((r: any) => `${r.type}/${r.name}/${r.state} ${r.disposition} ${r.base} → ${r.pair} ${r.boundToBase} ${r.outcome}`)).toEqual([
`view/${VIEW}/active movable ${PKG} → ${PAIR} true listed`,
]);
expect(payload.movable).toBe(1);
expect(payload.environmentRows).toBe(1);
expect(await storedRows(db)).toEqual(before);
expect(await storedPackages(db)).not.toContain(PAIR);
}, CASE_TIMEOUT_MS);
it('--apply moves it into the pair with its `overrides` declaration, keeps the control, serves the same customization over the package\'s document, and a re-run moves 0', async () => {
const db = caseDb(boundTemplateDb);
const served = await servedColumns(db);
expect(served[1]).toEqual(STORED_COLUMNS);
const { payload, exitCode } = await runJson(['--apply', '--yes', '--database-url', `file:${db}`, '--json']);
expect(payload.error, JSON.stringify(payload).slice(0, 600)).toBeUndefined();
expect(exitCode).toBe(0);
expect(payload.bases).toEqual([{ base: PKG, pair: PAIR, pairCreated: true, moved: 1, declarationsCompleted: 0, failed: 0, shadowed: 0 }]);
expect(payload.rows.map((r: any) => `${r.state} ${r.outcome} ${r.packageId} ${r.basedOn} ${r.boundToBase}`)).toEqual([
`active moved ${PAIR} 1.4.0 true`,
]);
expect(await storedRows(db)).toEqual([`view/${ENV_VIEW}@-/active`, `view/${VIEW}@${PAIR}/active`]);
expect((await storedBody(db, { type: 'view', name: VIEW, package_id: PAIR, state: 'active' })).overrides)
.toEqual({ package: PKG, basedOn: '1.4.0' });
expect(await storedPackages(db)).toContain(PAIR);
// A fresh boot serves the customization at every read of the package's item.
expect(await servedColumns(db)).toEqual([STORED_COLUMNS, STORED_COLUMNS, STORED_COLUMNS, STORED_COLUMNS]);
const again = await runJson(['--apply', '--yes', '--database-url', `file:${db}`, '--json']);
expect(again.payload.error, JSON.stringify(again.payload).slice(0, 600)).toBeUndefined();
expect(again.exitCode).toBe(0);
expect(again.payload.moved).toBe(0);
expect(again.payload.rows).toEqual([]);
}, CASE_TIMEOUT_MS * 3);
});
/**
* [#30261] A package-less view CONTAINER row of the package's view: its shape
* refuses the `overrides` key, so the declared save `--apply` makes could
* never land. The preview lists it `cannot_declare` (left), and `--apply`
* attempts nothing: no failure, exit 0, the row and the database as they were.
* Before, the preview listed it movable and every `--apply` failed it.
*/
describe('os migrate packageless-overlays: a package-less view container', () => {
it('the preview lists it `cannot_declare`, left; --apply attempts nothing, fails nothing, and leaves the row', async () => {
const db = caseDb(containerTemplateDb);
const before = await storedRows(db);
const preview = await runJson(['--database-url', `file:${db}`, '--json']);
expect(preview.payload.error, JSON.stringify(preview.payload).slice(0, 600)).toBeUndefined();
expect(preview.exitCode).toBe(0);
expect(preview.payload.rows.map((r: any) => `${r.type}/${r.name}/${r.state} ${r.disposition} ${r.base} → ${r.pair} ${r.outcome}`)).toEqual([
`view/${CONTAINER}/active cannot_declare ${PKG} → ${PAIR} left`,
]);
expect(preview.payload.movable).toBe(0);
expect(preview.payload.left).toBe(1);
const { payload, exitCode } = await runJson(['--apply', '--yes', '--database-url', `file:${db}`, '--json']);
expect(payload.error, JSON.stringify(payload).slice(0, 600)).toBeUndefined();
expect(exitCode).toBe(0);
expect(payload.failed).toBe(0);
expect(payload.moved).toBe(0);
expect(payload.rows.map((r: any) => `${r.name} ${r.disposition} ${r.outcome}`)).toEqual([`${CONTAINER} cannot_declare left`]);
expect(await storedRows(db)).toEqual(before);
expect(await storedPackages(db)).not.toContain(PAIR);
}, CASE_TIMEOUT_MS * 2);
});