Skip to content

Commit 7d7ad20

Browse files
committed
wip(trigger-record-change): lean fixture engines hold a Default Organization
Claude-Session: https://claude.ai/code/session_018EszgtA3V2KXBX5tf59Hi4 Co-authored-by: Claude <noreply@anthropic.com>
1 parent f1111e1 commit 7d7ad20

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎packages/triggers/trigger-record-change/src/before-update-flow-payload-reach.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import type { IDataEngine, IObjectQLEngine } from '@objectstack/spec/contracts';
109109
// whole module graph measures loading, not behaviour.
110110
import { SqlDriver } from '@objectstack/driver-sql';
111111
import { RecordChangeTriggerPlugin } from './plugin.js';
112+
import { installDefaultOrganization } from './default-organization.testkit.js';
112113

113114
/**
114115
* `registerObject` / `registerHook` / `registerFunction` are concrete-engine
@@ -899,6 +900,9 @@ describe('[#15356/#14744] S5 on the real SQL driver — the mutation reaches no
899900
(objectql.registry as unknown as TestObjectRegistry).registerObject(o.def as never, o.owner);
900901
}
901902
await objectql.syncSchemas();
903+
// A real `single` install's Default Organization: the system writes below
904+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
905+
await installDefaultOrganization(objectql);
902906

903907
objectql.registerFunction('sq_mutate_nested', async (args: any) => {
904908
const tags = (args.automation?.record as any)?.tags;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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/trigger-record-change',
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+
}

‎packages/triggers/trigger-record-change/src/record-change-integration.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { RecordChangeTriggerPlugin } from './plugin.js';
3939
// `packages/runtime/src/**` (which is what `plugin-auth` and `dogfood`
4040
// declare) because the helper imports nothing — it is the whole read.
4141
import { captureExpectedReadRefusals } from '../../../runtime/src/expected-read-refusal-noise.js';
42+
import { installDefaultOrganization } from './default-organization.testkit.js';
4243

4344
/**
4445
* `check:slot-lookup` (#4251) — a NEW `kernel.getService(...)` site must carry
@@ -417,6 +418,9 @@ describe('a system write must not fire a record-change flow UNSCOPED (#3760)', (
417418
await attachSqlite(objectql);
418419
objectql.registry.registerObject(objectDef('sysw'), 'test', 'test');
419420
await objectql.syncSchemas();
421+
// A real `single` install's Default Organization: the system writes below
422+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
423+
await installDefaultOrganization(objectql);
420424
// No `runAs` — the spec default 'user'. This is the shape an author (very
421425
// often an AI) writes without realising it can run without a user.
422426
automation.registerFlow('sysw_stamp', stampFlow('sysw_stamp', 'sysw') as any);
@@ -462,6 +466,9 @@ describe('a system write must not fire a record-change flow UNSCOPED (#3760)', (
462466
await attachSqlite(objectql);
463467
objectql.registry.registerObject(objectDef('sysw2'), 'test', 'test');
464468
await objectql.syncSchemas();
469+
// A real `single` install's Default Organization: the system writes below
470+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
471+
await installDefaultOrganization(objectql);
465472
automation.registerFlow('sysw2_stamp', stampFlow('sysw2_stamp', 'sysw2') as any);
466473

467474
const created = await data.insert('sysw2', { status: 'new' }, { context: { userId: 'u_trigger' } });
@@ -490,6 +497,9 @@ describe('record-change trigger — end-to-end (#1491)', () => {
490497
await attachSqlite(objectql);
491498
objectql.registry.registerObject(objectDef('wid'), 'test', 'test');
492499
await objectql.syncSchemas();
500+
// A real `single` install's Default Organization: the system writes below
501+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
502+
await installDefaultOrganization(objectql);
493503
automation.registerFlow('stamp_flow', stampFlow('stamp_flow', 'wid') as any);
494504

495505
// The flow bound to the trigger…
@@ -525,6 +535,9 @@ describe('record-change trigger — end-to-end (#1491)', () => {
525535
ql.registry.registerObject(objectDef('wid2'), 'test', 'test');
526536
ql.registry.registerItem('flow', flowDef, 'name', 'test');
527537
await ql.syncSchemas();
538+
// A real `single` install's Default Organization: the system writes below
539+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
540+
await installDefaultOrganization(ql);
528541
},
529542
};
530543

@@ -566,6 +579,9 @@ describe('record-change trigger — end-to-end (#1491)', () => {
566579
await attachSqlite(objectql);
567580
objectql.registry.registerObject(objectDef('wid3'), 'test', 'test');
568581
await objectql.syncSchemas();
582+
// A real `single` install's Default Organization: the system writes below
583+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
584+
await installDefaultOrganization(objectql);
569585
automation.registerFlow('mirror_write', mirrorWriteFlow('mirror_write', 'wid3') as any);
570586

571587
expect((automation as any).getActiveTriggerBindings()).toContainEqual({
@@ -600,6 +616,9 @@ describe('record-change trigger — end-to-end (#1491)', () => {
600616
await attachSqlite(objectql);
601617
objectql.registry.registerObject(objectDef('wid5'), 'test', 'test');
602618
await objectql.syncSchemas();
619+
// A real `single` install's Default Organization: the system writes below
620+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
621+
await installDefaultOrganization(objectql);
603622
automation.registerFlow('urgent_alert', urgentAlertFlow('urgent_alert', 'wid5') as any);
604623

605624
// Create leg — a brand-new URGENT record: `previous == null` makes the
@@ -662,6 +681,9 @@ describe('record-change trigger — end-to-end (#1491)', () => {
662681
'test', 'test',
663682
);
664683
await objectql.syncSchemas();
684+
// A real `single` install's Default Organization: the system writes below
685+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
686+
await installDefaultOrganization(objectql);
665687

666688
automation.registerFlow('before_write_probe', {
667689
name: 'before_write_probe', label: 'Before-update probe', type: 'record_change',
@@ -812,6 +834,9 @@ describe('hydration schema gate — real ObjectQL engine findOne count (#8482)',
812834
objectql.registry.registerObject(plainObjectDef('gate_plain'), 'test', 'test');
813835
objectql.registry.registerObject(auditObjectDef('gate_plain_audit'), 'test', 'test');
814836
await objectql.syncSchemas();
837+
// A real `single` install's Default Organization: the system writes below
838+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
839+
await installDefaultOrganization(objectql);
815840
automation.registerFlow(
816841
'gate_plain_audit_flow',
817842
afterUpdateAuditFlow('gate_plain_audit_flow', 'gate_plain', 'gate_plain_audit') as any,
@@ -848,6 +873,9 @@ describe('hydration schema gate — real ObjectQL engine findOne count (#8482)',
848873
objectql.registry.registerObject(formulaObjectDef('gate_calc'), 'test', 'test');
849874
objectql.registry.registerObject(auditObjectDef('gate_calc_audit'), 'test', 'test');
850875
await objectql.syncSchemas();
876+
// A real `single` install's Default Organization: the system writes below
877+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
878+
await installDefaultOrganization(objectql);
851879
automation.registerFlow(
852880
'gate_calc_audit_flow',
853881
afterUpdateAuditFlow('gate_calc_audit_flow', 'gate_calc', 'gate_calc_audit') as any,

‎packages/triggers/trigger-record-change/src/reentrant-start-condition.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import { SqlDriver } from '@objectstack/driver-sql';
6464
import { AutomationServicePlugin, type AutomationEngine } from '@objectstack/service-automation';
6565
import type { IDataEngine, IObjectQLEngine } from '@objectstack/spec/contracts';
6666
import { RecordChangeTriggerPlugin } from './plugin.js';
67+
import { installDefaultOrganization } from './default-organization.testkit.js';
6768

6869
/** See `record-change-integration.test.ts` for why this shape is named. */
6970
type TestObjectQLEngine = IObjectQLEngine & {
@@ -271,6 +272,9 @@ describe("a record-change flow's start condition is evaluated on its own write-b
271272
objectql.registry.registerObject(o.def as never, o.owner);
272273
}
273274
await objectql.syncSchemas();
275+
// A real `single` install's Default Organization: the system writes below
276+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
277+
await installDefaultOrganization(objectql);
274278
automation.registerFlow('esc_escalation', escalationFlow('esc_escalation', 'esc_case') as never);
275279

276280
// Created OUTSIDE the condition (`priority` is not critical). The flow is

0 commit comments

Comments
 (0)