Skip to content

Commit e950e52

Browse files
committed
test(example-todo): boot the loop-body kernel once per file under a measured budget
Each of the five cases booted a full todo kernel inside vitest's 5 s per-test default; on loaded Test Core shards the first case was cut off at 5011-5033 ms. The kernel now boots once in beforeAll under a measured 30 s budget, and beforeEach restores a fresh boot's state (no todo_task rows, deleted through the driver; no captured notices). CONTROL removes its fixture flow in a finally. Assertions are unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2etWp2cPA9ARe5c6hN
1 parent 7c523aa commit e950e52

1 file changed

Lines changed: 53 additions & 18 deletions

File tree

‎examples/app-todo/test/loop-body-iteration.test.ts‎

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@
3434
* real flow by deleting `config.body` and re-wiring the flat edges, still dies
3535
* at `update_priority`. Without it a green suite could not distinguish "the
3636
* loop body ran" from "nothing in this harness can fail".
37+
*
38+
* ONE KERNEL PER FILE, CLEAN STATE PER CASE. The boot is ~80% of a case's wall
39+
* (measured locally: ~250 ms of boot against ~25 ms of `execute`), and booting
40+
* it inside every case put a whole kernel under vitest's 5 s per-test default:
41+
* on loaded Test Core shards the first case was cut off at 5011–5033 ms. So the
42+
* kernel boots once in `beforeAll`, under its own measured budget (below), and
43+
* `beforeEach` gives every case the state a fresh boot would: no `todo_task`
44+
* rows (deleted through the driver, so no hook or flow observes the reset) and
45+
* no captured notices. The only registration a case adds — CONTROL's fixture
46+
* flow — is removed in its own `finally`. Each case still passes alone and in
47+
* shuffled order.
3748
*/
3849

39-
import { describe, it, expect, afterEach } from 'vitest';
50+
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
4051
import { ObjectKernel } from '@objectstack/core';
4152
import { ObjectQLPlugin } from '@objectstack/objectql';
4253
import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm';
@@ -55,13 +66,6 @@ interface CapturedEmit {
5566
source?: { object: string; id: string };
5667
}
5768

58-
const openDrivers: Array<{ disconnect?: () => Promise<void> }> = [];
59-
afterEach(async () => {
60-
while (openDrivers.length) {
61-
try { await openDrivers.pop()?.disconnect?.(); } catch { /* noop */ }
62-
}
63-
});
64-
6569
/** `n` whole days before today, as a `Field.date` string (UTC); negative = future. */
6670
function daysAgoIso(n: number): string {
6771
const d = new Date();
@@ -74,11 +78,14 @@ function daysAgoIso(n: number): string {
7478
* A real kernel with the app's real `todo_task` over in-process sqlite-wasm,
7579
* the real automation engine, and a capturing `messaging` service.
7680
*/
77-
async function bootTodoKernel(): Promise<{
81+
interface TodoKernel {
7882
automation: AutomationEngine & Record<string, any>;
7983
data: any;
84+
driver: any;
8085
emits: CapturedEmit[];
81-
}> {
86+
}
87+
88+
async function bootTodoKernel(): Promise<TodoKernel> {
8289
const emits: CapturedEmit[] = [];
8390
const messaging = {
8491
async emit(input: CapturedEmit) {
@@ -107,7 +114,6 @@ async function bootTodoKernel(): Promise<{
107114
const driver: any = new SqliteWasmDriver({ filename: ':memory:' });
108115
await driver.connect();
109116
objectql.registerDriver(driver, true);
110-
openDrivers.push(driver);
111117
objectql.registry.registerObject(Task, 'todo', 'todo');
112118
await objectql.syncSchemas();
113119
// A real `single` install's Default Organization: the system writes below
@@ -116,9 +122,33 @@ async function bootTodoKernel(): Promise<{
116122

117123
automation.registerFlow(OverdueEscalationFlow.name, OverdueEscalationFlow);
118124
automation.registerFlow(TaskReminderFlow.name, TaskReminderFlow);
119-
return { automation, data, emits };
125+
return { automation, data, driver, emits };
120126
}
121127

128+
/**
129+
* The boot budget is measured, not vitest's 10 s hook default. Locally one boot
130+
* (kernel, sqlite-wasm connect, schema sync, Default Organization, flows) took
131+
* 230–330 ms idle and ~650 ms with 8 CPU-bound processes beside the suite on 4
132+
* cores. On Test Core shards the old first case — this boot plus one
133+
* `execute` — was cut off at 5011, 5015 and 5033 ms (jobs 128519296820,
134+
* 128555004931, 128565270519), and one warm case took 4620 ms, so the shard's
135+
* slowdown has reached ~20x the idle reading. 30 s is 6x the 5 s the shards
136+
* already exceeded.
137+
*/
138+
const BOOT_BUDGET_MS = 30_000;
139+
140+
let k: TodoKernel;
141+
beforeAll(async () => { k = await bootTodoKernel(); }, BOOT_BUDGET_MS);
142+
afterAll(async () => { await k?.driver.disconnect?.(); });
143+
144+
/** The state a fresh boot leaves: no task rows, no captured notices. */
145+
beforeEach(async () => {
146+
for (const row of await k.driver.find('todo_task', { where: {} })) {
147+
await k.driver.delete('todo_task', row.id);
148+
}
149+
k.emits.length = 0;
150+
});
151+
122152
const SYSTEM = { context: { isSystem: true } };
123153

124154
/** Seed one task `days` days past due, and return its id. */
@@ -132,7 +162,7 @@ async function seedOverdueTask(data: any, subject: string, days: number, owner =
132162

133163
describe('#19206 — the loop body runs and binds `currentTask`', () => {
134164
it('ESCALATION: the run reaches `notify_owner` and the notice renders the real day count', async () => {
135-
const { automation, data, emits } = await bootTodoKernel();
165+
const { automation, data, emits } = k;
136166
const id = await seedOverdueTask(data, 'Renew the domain', 5);
137167

138168
const result: any = await automation.execute('todo_overdue_escalation');
@@ -158,7 +188,7 @@ describe('#19206 — the loop body runs and binds `currentTask`', () => {
158188
});
159189

160190
it('EVERY item is processed, and each one carries its own values', async () => {
161-
const { automation, data, emits } = await bootTodoKernel();
191+
const { automation, data, emits } = k;
162192
await seedOverdueTask(data, 'Alpha', 4, 'usr_a');
163193
await seedOverdueTask(data, 'Bravo', 9, 'usr_b');
164194
await seedOverdueTask(data, 'Charlie', 12, 'usr_c');
@@ -184,7 +214,7 @@ describe('#19206 — the loop body runs and binds `currentTask`', () => {
184214
});
185215

186216
it('CONTAINMENT: one un-notifiable row does not end the sweep', async () => {
187-
const { automation, data, emits } = await bootTodoKernel();
217+
const { automation, data, emits } = k;
188218
await seedOverdueTask(data, 'Alpha', 4, 'usr_a');
189219
// `notify` refuses an empty resolved recipient set (`notify-node.ts`), so
190220
// this row's iteration fails INSIDE the body's `try_catch`.
@@ -200,7 +230,7 @@ describe('#19206 — the loop body runs and binds `currentTask`', () => {
200230
});
201231

202232
it('REMINDER: the second loop in the file binds its iterator too', async () => {
203-
const { automation, data, emits } = await bootTodoKernel();
233+
const { automation, data, emits } = k;
204234
await data.insert('todo_task', {
205235
subject: 'Pay the invoice', status: 'not_started', priority: 'high',
206236
due_date: daysAgoIso(-1), owner: 'usr_test_owner',
@@ -216,7 +246,7 @@ describe('#19206 — the loop body runs and binds `currentTask`', () => {
216246
});
217247

218248
it('CONTROL: the pre-fix body-less shape still dies at `update_priority`', async () => {
219-
const { automation, data, emits } = await bootTodoKernel();
249+
const { automation, data, emits } = k;
220250
await seedOverdueTask(data, 'Renew the domain', 5);
221251

222252
// The pre-#19206 shape, rebuilt from the real flow so it cannot drift: the
@@ -237,7 +267,12 @@ describe('#19206 — the loop body runs and binds `currentTask`', () => {
237267
];
238268
automation.registerFlow(legacy.name, legacy);
239269

240-
const result: any = await automation.execute(legacy.name);
270+
let result: any;
271+
try {
272+
result = await automation.execute(legacy.name);
273+
} finally {
274+
automation.unregisterFlow(legacy.name);
275+
}
241276
expect(result.success).toBe(false);
242277
// Loud rather than catastrophic: the filter operand reads `currentTask`,
243278
// which the body-less shape never binds, and the operand envelope fails the

0 commit comments

Comments
 (0)