|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Give a lean test engine what every real `single` install has: an |
| 5 | + * organization object holding its Default Organization. |
| 6 | + * |
| 7 | + * ADR-0131 D1: a table the SQL driver creates carries its tenant column NOT |
| 8 | + * NULL. The engine stamps an organization-less system write with the derived |
| 9 | + * organization under `single` (D9) — but only where the composition registers |
| 10 | + * the organization object; a bare `ObjectQL` + `SqlDriver` registers none, so |
| 11 | + * the write reaches the database with no organization and is refused there. |
| 12 | + * A suite whose subject is not tenancy (claims, acks, idle cost, at-rest |
| 13 | + * shapes) calls this once its own objects are registered, so its system |
| 14 | + * writes land the way they do on a real install. |
| 15 | + * |
| 16 | + * The object mirrors the platform's: managed by better-auth, so it carries no |
| 17 | + * tenant column of its own. |
| 18 | + */ |
| 19 | +export const DEFAULT_ORGANIZATION_ID = 'org_default'; |
| 20 | + |
| 21 | +export async function installDefaultOrganization(engine: { |
| 22 | + registry: { registerObject(def: unknown, owner: string): unknown }; |
| 23 | + getSchema(object: string): unknown; |
| 24 | + syncSchemas(): Promise<unknown>; |
| 25 | + getDriverForObject(object: string): |
| 26 | + | { |
| 27 | + create(object: string, data: Record<string, unknown>): Promise<unknown>; |
| 28 | + findOne(object: string, query: unknown): Promise<unknown>; |
| 29 | + } |
| 30 | + | undefined; |
| 31 | +}): Promise<void> { |
| 32 | + // Idempotent: a suite that syncs one engine more than once may call it each time. |
| 33 | + if (!engine.getSchema('sys_organization')) { |
| 34 | + engine.registry.registerObject( |
| 35 | + { name: 'sys_organization', label: 'Organization', managedBy: 'better-auth', fields: { name: { type: 'text', label: 'Name' } } }, |
| 36 | + '@objectstack/runtime', |
| 37 | + ); |
| 38 | + } |
| 39 | + await engine.syncSchemas(); |
| 40 | + // Seeded through the driver: the organization is data at rest, not a write |
| 41 | + // the suite under test should observe (an audit or activity mirror would). |
| 42 | + const driver = engine.getDriverForObject('sys_organization')!; |
| 43 | + if (await driver.findOne('sys_organization', { where: { id: DEFAULT_ORGANIZATION_ID } })) return; |
| 44 | + await driver.create('sys_organization', { id: DEFAULT_ORGANIZATION_ID, name: 'Default' }); |
| 45 | +} |
0 commit comments