Skip to content

Commit 1920c54

Browse files
committed
wip(cli): served test boots hold a Default Organization ahead of the app's seed
Claude-Session: https://claude.ai/code/session_018EszgtA3V2KXBX5tf59Hi4 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3bb0ddb commit 1920c54

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎packages/cli/src/commands/migrate/preview-read-only.integration.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { isExitSignal } from '../../utils/format.js';
6363
// MODULE LOAD: the fixture's served boot reaches it through a dynamic
6464
// `import()` inside a clocked hook (`scripts/check-test-source-alias.mjs`).
6565
import '@objectstack/runtime';
66+
import { withDefaultOrganization } from '../../utils/default-organization-plugin.testkit.js';
6667

6768
const HERE = dirname(fileURLToPath(import.meta.url));
6869
const CLI_ROOT = resolve(HERE, '..', '..', '..');
@@ -249,7 +250,9 @@ async function createFixture(cell: DialectCell): Promise<Fixture> {
249250
const served = await createStandaloneStack({ projectRoot: dir, databaseUrl });
250251
const runtime = new Runtime({ cluster: false });
251252
const kernel = runtime.getKernel();
252-
for (const plugin of served.plugins) await kernel.use(plugin as any);
253+
// A real served boot composes authentication, whose Default Organization
254+
// stamps the seed rows (ADR-0131 D9); the fresh tenant column is NOT NULL (D1).
255+
for (const plugin of withDefaultOrganization(served.plugins)) await kernel.use(plugin as any);
253256
for (const plugin of await buildDataMigrationPlugins({ automation: true, audit: true })) await kernel.use(plugin as any);
254257
await runtime.start();
255258
try {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* A fixture plugin that gives a served test boot what every real `single`
5+
* install has before its inline seed runs: an organization object holding the
6+
* Default Organization.
7+
*
8+
* ADR-0131 D1: a table the SQL driver creates carries its tenant column NOT
9+
* NULL. A real `os dev` / `os serve` composes authentication, which provisions
10+
* the Default Organization, and the engine stamps every organization-less seed
11+
* row with it (D9). `createStandaloneStack` alone composes no authentication,
12+
* so a suite that serves an artifact's seeds through it would have them refused
13+
* at the database. Spliced into the plugin list ahead of the app plugin (see
14+
* {@link withDefaultOrganization}), it starts after the default datasource and
15+
* before the app's `start()` seeds.
16+
*
17+
* The object mirrors the platform's: managed by better-auth, so it carries no
18+
* tenant column of its own; its row is seeded through the driver, as data at
19+
* rest.
20+
*/
21+
export const DEFAULT_ORGANIZATION_ID = 'org_default';
22+
23+
export function defaultOrganizationPlugin() {
24+
return {
25+
name: 'test.default-organization',
26+
version: '0.0.0',
27+
type: 'standard',
28+
dependencies: ['com.objectstack.runtime.default-datasource'],
29+
async init() {},
30+
async start(ctx: { getService(name: string): any }) {
31+
const ql = ctx.getService('objectql');
32+
if (!ql.getSchema('sys_organization')) {
33+
ql.registry.registerObject(
34+
{ name: 'sys_organization', label: 'Organization', managedBy: 'better-auth', fields: { name: { type: 'text', label: 'Name' } } },
35+
'test.default-organization',
36+
);
37+
}
38+
await ql.syncObjectSchema('sys_organization');
39+
const driver = ql.getDriverForObject('sys_organization');
40+
if (await driver.findOne('sys_organization', { where: { id: DEFAULT_ORGANIZATION_ID } })) return;
41+
await driver.create('sys_organization', { id: DEFAULT_ORGANIZATION_ID, name: 'Default' });
42+
},
43+
};
44+
}
45+
46+
/** The served plugin list with {@link defaultOrganizationPlugin} ahead of every app plugin. */
47+
export function withDefaultOrganization<T>(plugins: readonly T[]): T[] {
48+
const out = [...plugins];
49+
const datasource = out.findIndex((p: any) => p?.name === 'com.objectstack.runtime.default-datasource');
50+
out.splice(datasource + 1, 0, defaultOrganizationPlugin() as T);
51+
return out;
52+
}

‎packages/cli/src/utils/schema-migrate.one-shot-family.integration.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { PlatformObjectsPlugin } from '@objectstack/platform-objects/plugin';
8686
import { SettingsServicePlugin } from '@objectstack/service-settings';
8787
import '@objectstack/service-storage';
8888
import '@objectstack/plugin-audit';
89+
import { withDefaultOrganization } from './default-organization-plugin.testkit.js';
8990

9091
const HERE = dirname(fileURLToPath(import.meta.url));
9192
const SRC = resolve(HERE, '..');
@@ -406,7 +407,9 @@ async function bootServedStack(dbFile: string): Promise<void> {
406407
const stack = await createStandaloneStack({ projectRoot: dir, databaseUrl: `file:${dbFile}` });
407408
const runtime = new Runtime({ cluster: false });
408409
const kernel = runtime.getKernel();
409-
for (const plugin of stack.plugins) await kernel.use(plugin as any);
410+
// A real served boot composes authentication, whose Default Organization
411+
// stamps the seed rows (ADR-0131 D9); the fresh tenant column is NOT NULL (D1).
412+
for (const plugin of withDefaultOrganization(stack.plugins)) await kernel.use(plugin as any);
410413
for (const plugin of await buildDataMigrationPlugins({ storage: true, automation: true, audit: true })) {
411414
await kernel.use(plugin as any);
412415
}
@@ -613,7 +616,9 @@ describe('[#21391] a one-shot boot arms no lifecycle sweep', () => {
613616
const stack = await createStandaloneStack({ projectRoot: dir, databaseUrl: `file:${c.dbFile}`, skipSeedData: true });
614617
const runtime = new Runtime({ cluster: false });
615618
const kernel = runtime.getKernel();
616-
for (const plugin of stack.plugins) await kernel.use(plugin as any);
619+
// A real served boot composes authentication, whose Default Organization
620+
// stamps the seed rows (ADR-0131 D9); the fresh tenant column is NOT NULL (D1).
621+
for (const plugin of withDefaultOrganization(stack.plugins)) await kernel.use(plugin as any);
617622
await runtime.start();
618623
try {
619624
expect(armed(kernel)).toBe(true);

0 commit comments

Comments
 (0)