Skip to content

Commit 7a89f01

Browse files
committed
test(runtime): pin AppPlugin's boot seed and per-organization replay honouring the landed-seed record (wip)
Claude-Session: https://claude.ai/code/session_017xRBcTJTE7kSbcMoJQ4Txf Co-authored-by: Claude <noreply@anthropic.com>
1 parent ca16aba commit 7a89f01

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

‎packages/runtime/src/app-plugin.seed-landed.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,126 @@ describe('AppPlugin publishes the rows its seed landed on (#23354)', () => {
202202
expect(read()!.seededRecordIds!('crm_case')).toEqual([]);
203203
});
204204
});
205+
206+
/**
207+
* #31021 — every seed load AppPlugin runs keeps, and honours, the record of
208+
* which seed rows landed in the organization it seeds (`sys_seed_landed_key`):
209+
* a row a person deleted is not inserted again by the next boot (`serve`'s
210+
* inline seed) or the next per-organization replay (what a host runs on an
211+
* upgrade), and the replayer's `{ reseed: true }` is the deliberate re-seed
212+
* that brings it back.
213+
*
214+
* The record object is registered the way `@objectstack/platform-objects`
215+
* registers it; the engine is the faithful double above, so every write the
216+
* loader makes lands in `engine.store`.
217+
*/
218+
describe('AppPlugin seed loads honour the record of landed seed rows (#31021)', () => {
219+
const OLD_BUDGET = process.env.OS_INLINE_SEED_BUDGET_MS;
220+
const OLD_MULTI = process.env.OS_MULTI_ORG_ENABLED;
221+
222+
beforeEach(() => {
223+
delete process.env.OS_MULTI_ORG_ENABLED;
224+
process.env.OS_INLINE_SEED_BUDGET_MS = '8000';
225+
});
226+
afterEach(() => {
227+
if (OLD_BUDGET === undefined) delete process.env.OS_INLINE_SEED_BUDGET_MS;
228+
else process.env.OS_INLINE_SEED_BUDGET_MS = OLD_BUDGET;
229+
if (OLD_MULTI === undefined) delete process.env.OS_MULTI_ORG_ENABLED;
230+
else process.env.OS_MULTI_ORG_ENABLED = OLD_MULTI;
231+
});
232+
233+
const LEDGER = 'sys_seed_landed_key';
234+
const objects: Record<string, any> = {
235+
...OBJECTS,
236+
sys_organization: { name: 'sys_organization', systemFields: { tenant: false }, fields: { name: { type: 'text' } } },
237+
[LEDGER]: {
238+
name: LEDGER,
239+
fields: {
240+
object_name: { type: 'text' },
241+
seed_key: { type: 'textarea' },
242+
organization_id: { type: 'lookup', reference: 'sys_organization' },
243+
},
244+
},
245+
};
246+
const metadata = {
247+
getObject: vi.fn(async (name: string) => objects[name]),
248+
listObjects: vi.fn(async () => Object.values(objects)),
249+
get: vi.fn(async (_type: string, name: string) => objects[name]),
250+
list: vi.fn(async () => []),
251+
register: vi.fn(async () => {}),
252+
unregister: vi.fn(async () => {}),
253+
exists: vi.fn(async () => false),
254+
listNames: vi.fn(async () => []),
255+
};
256+
257+
function context(engine: any) {
258+
const services = new Map<string, unknown>();
259+
const ctx = {
260+
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
261+
registerService: vi.fn((name: string, svc: unknown) => {
262+
if (services.has(name)) throw new Error(`service '${name}' already registered`);
263+
services.set(name, svc);
264+
}),
265+
getService: vi.fn((name: string) => {
266+
if (name === 'objectql') return engine;
267+
if (name === 'metadata') return metadata;
268+
return services.get(name);
269+
}),
270+
getServices: vi.fn(() => new Map()),
271+
hook: vi.fn(),
272+
trigger: vi.fn(),
273+
} as unknown as PluginContext;
274+
return { ctx, services };
275+
}
276+
277+
const cases = (engine: any, org: string) =>
278+
(engine.store.crm_case ?? []).filter((r: any) => r.organization_id === org).map((r: any) => r.name).sort();
279+
const upgraded = () => ({
280+
...bundle(),
281+
data: [{ ...bundle().data[0], records: [...bundle().data[0].records, { name: 'CASE-3' }] }],
282+
});
283+
284+
it("`serve`'s inline seed: a deleted row stays deleted across a reboot and an upgrade, whose new row lands", async () => {
285+
const engine = createEngine();
286+
// The `single` posture's Default Organization, created before seeds load.
287+
engine.store.sys_organization = [{ id: 'org_default', name: 'Default' }];
288+
289+
await new AppPlugin(bundle()).start(context(engine).ctx);
290+
expect(cases(engine, 'org_default')).toEqual(['CASE-1', 'CASE-2']);
291+
expect(engine.store[LEDGER].map((r: any) => [r.organization_id, r.seed_key])).toEqual([
292+
['org_default', '[["name","CASE-1"]]'],
293+
['org_default', '[["name","CASE-2"]]'],
294+
]);
295+
296+
engine.store.crm_case = engine.store.crm_case.filter((r: any) => r.name !== 'CASE-2');
297+
await new AppPlugin(bundle()).start(context(engine).ctx);
298+
expect(cases(engine, 'org_default')).toEqual(['CASE-1']);
299+
300+
await new AppPlugin(upgraded()).start(context(engine).ctx);
301+
expect(cases(engine, 'org_default')).toEqual(['CASE-1', 'CASE-3']);
302+
});
303+
304+
it('the per-organization replay keeps it per organization, and `{ reseed: true }` is the door that brings a removed row back', async () => {
305+
process.env.OS_MULTI_ORG_ENABLED = 'true';
306+
const engine = createEngine();
307+
const { ctx, services } = context(engine);
308+
await new AppPlugin(bundle()).start(ctx);
309+
const replayer = services.get('seed-replayer') as (orgId: string, options?: { reseed?: boolean }) => Promise<any>;
310+
311+
await replayer('org_a');
312+
await replayer('org_b');
313+
engine.store.crm_case = engine.store.crm_case.filter((r: any) => !(r.organization_id === 'org_a' && r.name === 'CASE-1'));
314+
315+
// The host's upgrade replay: org_a keeps its removal, org_b is untouched.
316+
const replay = await replayer('org_a');
317+
await replayer('org_b');
318+
expect(cases(engine, 'org_a')).toEqual(['CASE-2']);
319+
expect(cases(engine, 'org_b')).toEqual(['CASE-1', 'CASE-2']);
320+
expect(replay).toMatchObject({ inserted: 0, skipped: 2, errors: [] });
321+
322+
// The deliberate re-seed.
323+
const reseed = await replayer('org_a', { reseed: true });
324+
expect(reseed).toMatchObject({ inserted: 1 });
325+
expect(cases(engine, 'org_a')).toEqual(['CASE-1', 'CASE-2']);
326+
});
327+
});

0 commit comments

Comments
 (0)