Skip to content

Commit 37a78b4

Browse files
committed
test(example-todo): boot the overdue-escalation kernel once per file under a measured budget
Both async cases booted a full todo kernel inside vitest's 5 s per-test default; on a loaded Test Core shard the first was cut off at 5016 ms. The kernel now boots once in beforeAll under a measured 30 s budget, and beforeEach deletes the todo_task rows through the driver so every case starts from a fresh boot's table. Assertions are unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2etWp2cPA9ARe5c6hN
1 parent 877170f commit 37a78b4

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

‎examples/app-todo/test/overdue-escalation-days-overdue.test.ts‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@
2727
* the first test below asserts that precondition off the app's own flow rather
2828
* than trusting it, because adding a `fields` list to that step is precisely
2929
* the edit that would silently empty the day count again.
30+
*
31+
* ONE KERNEL PER FILE, CLEAN ROWS PER CASE. Booting a kernel inside each
32+
* async case put the boot under vitest's 5 s per-test default, and on a loaded
33+
* Test Core shard the first one was cut off at 5016 ms. So the kernel boots
34+
* once in `beforeAll`, under its own measured budget (below), and `beforeEach`
35+
* gives every case the table a fresh boot would: no `todo_task` rows (deleted
36+
* through the driver, so no hook observes the reset). Each case still passes
37+
* alone and in shuffled order.
3038
*/
3139

32-
import { describe, it, expect, afterEach } from 'vitest';
40+
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
3341
import { ObjectKernel } from '@objectstack/core';
3442
import { ObjectQLPlugin } from '@objectstack/objectql';
3543
import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm';
@@ -38,13 +46,6 @@ import { OverdueEscalationFlow } from '../src/flows/index.js';
3846
import { Task } from '../src/objects/task.object.js';
3947
import { installDefaultOrganization } from './default-organization.testkit.js';
4048

41-
const openDrivers: Array<{ disconnect?: () => Promise<void> }> = [];
42-
afterEach(async () => {
43-
while (openDrivers.length) {
44-
try { await openDrivers.pop()?.disconnect?.(); } catch { /* noop */ }
45-
}
46-
});
47-
4849
/**
4950
* A node of the app's real flow, by id — searched at EVERY depth.
5051
*
@@ -95,7 +96,7 @@ function daysAgoIso(n: number): string {
9596
}
9697

9798
/** A real kernel with the app's real `todo_task` over in-process sqlite-wasm. */
98-
async function bootTodoData(): Promise<any> {
99+
async function bootTodoData(): Promise<{ data: any; driver: any }> {
99100
const kernel = new ObjectKernel({ logger: { level: 'silent' } } as any);
100101
await kernel.use(new ObjectQLPlugin());
101102
await kernel.bootstrap();
@@ -104,16 +105,38 @@ async function bootTodoData(): Promise<any> {
104105
const driver: any = new SqliteWasmDriver({ filename: ':memory:' });
105106
await driver.connect();
106107
objectql.registerDriver(driver, true);
107-
openDrivers.push(driver);
108108
objectql.registry.registerObject(Task, 'todo', 'todo');
109109
await objectql.syncSchemas();
110110
// A real `single` install's Default Organization: the system writes below
111111
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
112112
await installDefaultOrganization(objectql);
113113

114-
return kernel.getService('data');
114+
return { data: kernel.getService('data'), driver };
115115
}
116116

117+
/**
118+
* The boot budget is measured, not vitest's 10 s hook default. Locally one boot
119+
* (kernel, sqlite-wasm connect, schema sync, Default Organization) took
120+
* 150–250 ms cold and ~45 ms warm, and the case that paid it took 192–279 ms
121+
* idle and 466–693 ms with 8 CPU-bound processes beside the suite on 4 cores.
122+
* On a Test Core shard that case was cut off at 5016 ms (job 128575054246) and
123+
* the next one, booting its own kernel, took 3822 ms: the shard's slowdown has
124+
* reached ~20x the idle reading. 30 s is 6x the 5 s the shard already exceeded.
125+
*/
126+
const BOOT_BUDGET_MS = 30_000;
127+
128+
let data: any;
129+
let driver: any;
130+
beforeAll(async () => { ({ data, driver } = await bootTodoData()); }, BOOT_BUDGET_MS);
131+
afterAll(async () => { await driver?.disconnect?.(); });
132+
133+
/** The table a fresh boot leaves: no task rows. */
134+
beforeEach(async () => {
135+
for (const row of await driver.find('todo_task', { where: {} })) {
136+
await driver.delete('todo_task', row.id);
137+
}
138+
});
139+
117140
const SYSTEM = { context: { isSystem: true } };
118141

119142
describe('#18584 — the overdue escalation projection carries a day count', () => {
@@ -127,7 +150,6 @@ describe('#18584 — the overdue escalation projection carries a day count', ()
127150
});
128151

129152
it('every `{{ currentTask.… }}` hole in the escalation body names a key the row carries', async () => {
130-
const data = await bootTodoData();
131153
const due = daysAgoIso(5);
132154
await data.insert('todo_task', {
133155
subject: 'Renew the domain',
@@ -163,7 +185,6 @@ describe('#18584 — the overdue escalation projection carries a day count', ()
163185
});
164186

165187
it('0 when the task is not overdue, and 0 when `due_date` is empty', async () => {
166-
const data = await bootTodoData();
167188
await data.insert('todo_task', {
168189
subject: 'Future task', status: 'not_started', priority: 'normal',
169190
due_date: daysAgoIso(-3), owner: 'usr_test_owner',

0 commit comments

Comments
 (0)