Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/verify-managed-datasource-stand-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@objectstack/verify': patch
---

Clause-②: no
Metadata-protocol: none

`os verify` no longer writes test rows into a declared managed datasource's real database. A datasource the app declares with `schemaMode: 'managed'` (or none) is now built as a fresh in-memory store of the same kind as verify's own default store, so its objects are still exercised (CRUD, reference shapes, delete behaviours, unique scopes) and nothing reaches the database the datasource names. A managed datasource whose engine has no faithful in-memory stand-in (`mongodb`, or a driver a plugin supplies) keeps its real driver, and verify leaves its objects out, reported as `managed datasource "NAME" (driver "DRIVER"): not exercised`. An object that requires a reference to a withheld object (external, or one of those) is now reported as withheld on purpose with the target's reason, instead of "required relational target X is not synthesizable".
13 changes: 10 additions & 3 deletions content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3029,9 +3029,16 @@ os verify --flow notify_on_new_case --flow-record '{"subject":"Printer jam"}'
object): every stage reports it as not exercised, and the CRUD stage lists
it `skipped` as `external datasource "NAME" (writes allowed)` or
`(read-only)`, which does not fail the run. An object that requires a
reference to it is skipped too. A write the app itself makes there, its seed
or a hook or flow that a stage's write fires, goes where the app sends it,
as under `os dev`. With `--rls`, a second fresh boot proves the
reference to it is skipped too, and its reason says the target was withheld
on purpose. A write the app itself makes there, its seed or a hook or flow
that a stage's write fires, goes where the app sends it, as under `os dev`.
A declared managed datasource (`schemaMode: 'managed'`, or none declared) is
not connected to the database it names: verify runs it on a fresh in-memory
store, as it runs its own default one, so every write to its objects stays
in memory and they are still exercised. One whose engine has no in-memory
stand-in (`mongodb`, or a driver a plugin supplies) keeps its real driver,
and its objects are `skipped` as `managed datasource "NAME" (driver
"DRIVER"): not exercised`. With `--rls`, a second fresh boot proves the
cross-owner invariant — a member must not write what it cannot read — for a
probe persona and for one persona per declared position, and a hole fails
the run. With `--reference-shapes`, another fresh boot proves that every
Expand Down
20 changes: 13 additions & 7 deletions packages/verify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ don't exercise, silently breaks at runtime." Point this at *your* app.
> datasource: every derived write family leaves it out, even one whose write
> gate is open (`external.allowWrites` and the object's `external.writable`),
> and reports it `skipped` as `external datasource "<name>" (writes allowed |
> read-only): not exercised`. An object bound to a declared MANAGED datasource
> (or one that declares no `schemaMode`) is written through that datasource,
> as under `objectstack dev`. A write the app itself makes to an external
> object (its seed, or a hook or flow
> that one of verify's writes fires) goes where the app sends it, as under
> `objectstack dev`; only the `--flow` run refuses every write routed away
> from the in-memory database.
> read-only): not exercised`; an object that requires a reference to it is
> skipped as withheld on purpose, with the target's reason. A write the app
> itself makes to an external object (its seed, or a hook or flow that one of
> verify's writes fires) goes where the app sends it, as under
> `objectstack dev`. A declared MANAGED datasource (or one that declares no
> `schemaMode`) is never connected to the database it names: verify builds it
> as a fresh in-memory store of the same kind as its own default store, so its
> objects are still exercised and every write to them, the app's included,
> stays in memory. A managed datasource whose engine has no faithful in-memory
> stand-in (`mongodb`, or a driver a plugin supplies) keeps its real driver,
> and every derived write family leaves its objects out as `managed datasource
> "<name>" (driver "<driver>"): not exercised`. Only the `--flow` run refuses
> every write routed away from the in-memory default database.

## CLI (zero-config)

Expand Down
76 changes: 76 additions & 0 deletions packages/verify/src/derive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ describe('deriveCrudCases — objects on an external datasource are not exercise
expect(c?.blocked).toContain('external datasource "wh" (read-only): not exercised');
});

// One reader with verify's driver factory (`isManagedDatasource`): a mode
// spelled only inside `config` or `external` gets the real driver at boot, so
// derive must withhold its objects too, or verify's rows reach that database.
it.each([
['config', { driver: 'postgres', config: { host: 'wh.internal', schemaMode: 'external' } }],
['external', { driver: 'postgres', config: { host: 'wh.internal' }, external: { schemaMode: 'validate-only' } }],
])('withholds an object whose datasource spells schemaMode only inside `%s`', (_where, ds) => {
const c = deriveCrudCases({
datasources: [{ name: 'wh', ...ds }],
objects: [{ name: 'wh_order', datasource: 'wh', fields: { amount: { type: 'number' } } }],
}).find((x) => x.object === 'wh_order');
expect(c?.blocked).toContain('external datasource "wh" (read-only): not exercised');
expect(c?.body).toBeUndefined();
});

it('a required relation to an external object is blocked too, so nothing is written for it', () => {
const config = external({ allowWrites: true }, { remoteName: 'orders', writable: true });
config.objects.push({
Expand All @@ -60,6 +75,35 @@ describe('deriveCrudCases — objects on an external datasource are not exercise
);
});

it('a managed object requiring a withheld target says it was withheld on purpose, and why', () => {
const config = external({ allowWrites: false }, { remoteName: 'orders' });
config.objects.push(
{ name: 'wh_note', fields: { order: { type: 'lookup', reference: 'wh_order', required: true } } } as any,
{ name: 'wh_reply', fields: { note: { type: 'lookup', reference: 'wh_note', required: true } } } as any,
);
const cases = deriveCrudCases(config);
const note = cases.find((x) => x.object === 'wh_note')?.blocked;
expect(note).toBe(
'required relational target "wh_order" is withheld on purpose — external datasource "wh" (read-only): ' +
'not exercised — verify writes only to its ephemeral store',
);
expect(note).not.toContain('not synthesizable');
// Transitively too: the dependent of a dependent is not called unsynthesizable.
const reply = cases.find((x) => x.object === 'wh_reply')?.blocked;
expect(reply).toContain('required relational target "wh_note" is withheld on purpose');
expect(reply).not.toContain('not synthesizable');
});

it('a required target blocked for a field shape keeps the generic sentence', () => {
const cases = deriveCrudCases({
objects: [
{ name: 'p', fields: { blob: { type: 'composite', required: true } } },
{ name: 'c', fields: { parent: { type: 'lookup', reference: 'p', required: true } } },
],
});
expect(cases.find((x) => x.object === 'c')?.blocked).toBe('required relational target "p" is not synthesizable');
});

it('leaves managed objects unaffected, including one on a declared managed datasource', () => {
const config = {
datasources: [{ name: 'side', schemaMode: 'managed' }],
Expand All @@ -72,6 +116,38 @@ describe('deriveCrudCases — objects on an external datasource are not exercise
expect(cases.find((x) => x.object === 'task')?.blocked).toBeFalsy();
expect(cases.find((x) => x.object === 'side_task')?.blocked).toBeFalsy();
});

// A managed SQL or memory datasource runs on verify's stand-in, so its
// objects are exercised (`verify.managed-datasource-stand-in.test.ts`). One
// whose engine has no stand-in keeps its real driver, so verify withholds
// its objects instead of writing through.
it('exercises an object on a managed SQL datasource, and withholds one on an engine with no stand-in', () => {
const config = {
datasources: [
{ name: 'pg', driver: 'postgres', schemaMode: 'managed' },
{ name: 'docs', driver: 'mongodb' },
{ name: 'plug', driver: 'acme-plugin-driver', schemaMode: 'managed' },
],
objects: [
{ name: 'pg_task', datasource: 'pg', fields: { title: { type: 'text' } } },
{ name: 'doc_task', datasource: 'docs', fields: { title: { type: 'text' } } },
{ name: 'plug_task', datasource: 'plug', fields: { title: { type: 'text' } } },
{ name: 'doc_note', fields: { doc: { type: 'lookup', reference: 'doc_task', required: true } } },
],
};
const cases = deriveCrudCases(config);
expect(cases.find((x) => x.object === 'pg_task')?.blocked).toBeFalsy();
expect(cases.find((x) => x.object === 'doc_task')?.blocked).toBe(
'managed datasource "docs" (driver "mongodb"): not exercised — verify has no ephemeral stand-in for ' +
'this engine and never writes through to the real one',
);
expect(cases.find((x) => x.object === 'plug_task')?.blocked).toContain(
'managed datasource "plug" (driver "acme-plugin-driver"): not exercised',
);
expect(cases.find((x) => x.object === 'doc_note')?.blocked).toContain(
'required relational target "doc_task" is withheld on purpose — managed datasource "docs"',
);
});
});

/**
Expand Down
44 changes: 39 additions & 5 deletions packages/verify/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import { isMultiValueField, referenceCarrierOf } from '@objectstack/spec/data';
import { declaredCollection } from './artifact-collections.js';
import { hasManagedStandIn, isManagedDatasource } from './managed-datasource-stand-in.js';

const COMPUTED = new Set(['formula', 'summary', 'autonumber', 'rollup', 'vector']);
const RELATIONAL = new Set(['lookup', 'master_detail', 'master-detail', 'masterdetail', 'tree']);
Expand Down Expand Up @@ -197,6 +198,8 @@ interface Draft {
relationalRefs: RelationalRef[];
requiredTargets: string[]; // referenced objects that MUST exist + be ordered-before
blocked?: string;
/** Blocked on purpose: verify withholds writes from it (or from a required target of it). */
withheld?: boolean;
}

/**
Expand All @@ -207,6 +210,8 @@ interface Draft {
* in. Only genuinely unsatisfiable shapes are `blocked`:
* - an object on an external datasource (never written: verify writes only to
* its ephemeral store), or
* - an object on a declared managed datasource whose engine verify has no
* ephemeral stand-in for (never written through), or
* - a required relation whose target is missing from the app config (external), or
* - a required-reference cycle (incl. a required self-reference), or
* - a required relation whose target is itself blocked (cascade), or
Expand Down Expand Up @@ -244,16 +249,37 @@ export function deriveCrudCases(config: any): CrudCase[] {
// Every write family (CRUD, RLS, reference shapes, delete behaviours,
// unique scopes, parent write reach, dashboard windows) builds its records
// from this case, so one blocked case keeps all of them off the object.
// The datasource counts as external the way the engine's write gate counts
// it: a declared `schemaMode` other than `managed`, or an object carrying
// an `external` block. The reason names the gate's state, so the report
// says when a write was possible and was withheld.
// Two triggers count the object as external. A datasource that is not
// managed is the half the engine's write gate reads too — read here by
// `isManagedDatasource`, the same reader verify's driver factory decides
// stand-in-or-real on, so a datasource the boot connects for real is never
// one whose objects verify writes (`./managed-datasource-stand-in.ts`).
// That reader also takes a `schemaMode` spelled inside `external` or
// `config`, which the engine's gate does not read. An object carrying an
// `external` block is verify's own trigger: the engine does not gate on
// it, verify withholds the object anyway. The reason names the gate's
// state, so the report says when a write was possible and was withheld.
const ds = obj.datasource ? dsByName.get(obj.datasource) : undefined;
if (obj.external || (typeof ds?.schemaMode === 'string' && ds.schemaMode !== 'managed')) {
if (obj.external || (ds && !isManagedDatasource(ds))) {
const writable = ds?.external?.allowWrites === true && obj.external?.writable === true;
d.blocked =
`external datasource "${obj.datasource ?? 'default'}" (${writable ? 'writes allowed' : 'read-only'}): ` +
'not exercised — verify writes only to its ephemeral store';
d.withheld = true;
drafts.set(obj.name, d);
continue;
}

// An object on a declared MANAGED datasource runs on the ephemeral store
// verify builds in that datasource's place (`./managed-datasource-stand-in.ts`),
// unless its engine has no faithful stand-in: then the boot keeps the real
// driver, and verify withholds the object rather than write through to it.
// `default` is verify's own store whatever the app declares under that name.
if (ds && obj.datasource !== 'default' && typeof ds.driver === 'string' && !hasManagedStandIn(ds.driver)) {
d.blocked =
`managed datasource "${obj.datasource}" (driver "${ds.driver}"): not exercised — verify has no ` +
'ephemeral stand-in for this engine and never writes through to the real one';
d.withheld = true;
drafts.set(obj.name, d);
continue;
}
Expand Down Expand Up @@ -325,6 +351,14 @@ export function deriveCrudCases(config: any): CrudCase[] {
if (d.blocked) continue;
for (const t of d.requiredTargets) {
const td = drafts.get(t);
if (td?.withheld) {
// Say the target was left out on purpose, and why: "not
// synthesizable" would send the reader after a field shape.
d.blocked = `required relational target "${t}" is withheld on purpose — ${td.blocked}`;
d.withheld = true;
changed = true;
break;
}
if (!td || td.blocked) {
d.blocked = `required relational target "${t}" is ${!td ? 'missing' : 'not synthesizable'}`;
changed = true;
Expand Down
33 changes: 27 additions & 6 deletions packages/verify/src/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@
// writes such an object when one of verify's writes fires it. Verify's own
// writes never reach an object on an EXTERNAL datasource: every derived write
// family leaves it out and reports it "not exercised", whatever its write gate
// says (`deriveCrudCases`, `derive.ts`). An object bound to a declared MANAGED
// datasource (or one that declares no `schemaMode`) is written through that
// datasource, as under `objectstack dev`; only the local flow run refuses a
// write to any object routed away from this database (`flow-run.ts`).
// `verify.external-datasource-no-writes.test.ts` pins the external half.
// says (`deriveCrudCases`, `derive.ts`).
//
// A declared MANAGED datasource (or one that declares no `schemaMode`) is NOT
// connected to the database it names. Ruling A of the composition rule
// (`bootStack`, #22301) already swaps the app's own default store for an
// ephemeral one; the same swap now covers every declared managed store:
// verify's driver factory builds it as a fresh in-memory store of the default
// store's engine (`managed-datasource-stand-in.ts`), so every write that
// reaches it — verify's derived writes, a REST create, the app's seed — lands
// in memory and dies with the kernel. A managed datasource whose engine has no
// faithful stand-in (`mongodb`, or a driver a plugin supplies) keeps its real
// driver, and every derived write family leaves its objects out and names
// them, as for an external one. Only the local flow run refuses a write to any
// object routed away from the default store (`flow-run.ts`).
// `verify.external-datasource-no-writes.test.ts` pins the external half and
// `verify.managed-datasource-stand-in.test.ts` the managed one.

import { randomBytes } from 'node:crypto';
import { resolve as resolvePath } from 'node:path';
Expand Down Expand Up @@ -61,6 +72,7 @@ import { MetadataPlugin } from '@objectstack/metadata';
import { createHostImporter, hostImportFailureKind } from '@objectstack/types/node';
import { materializeStackPlugin, resolveCapabilityArgument } from '@objectstack/core';
import { createHandle, type VerifyHandle } from './handle.js';
import { createVerifyDatasourceDriverFactory } from './managed-datasource-stand-in.js';
import { constructServedProviders } from './required-providers.js';
import { registryLevelFor, resolveBootLogLevel, scopeRegistryLogLevel } from './boot-log-level.js';

Expand Down Expand Up @@ -1168,7 +1180,16 @@ async function bootClaimed(
const { DatasourceAdminServicePlugin, createDefaultDatasourceDriverFactory } = await import(
'@objectstack/service-datasource'
);
await kernel.use(new DatasourceAdminServicePlugin({ driverFactory: createDefaultDatasourceDriverFactory() }));
// Verify's own factory over the one `objectstack dev` uses: a declared
// MANAGED datasource is built as an ephemeral store of the default
// store's engine instead of its real connection, the same swap ruling A
// makes for `default` (`./managed-datasource-stand-in.ts`).
await kernel.use(new DatasourceAdminServicePlugin({
driverFactory: createVerifyDatasourceDriverFactory({
real: createDefaultDatasourceDriverFactory(),
defaultStore: databaseDriver,
}),
}));
}
}

Expand Down
Loading
Loading