bunsane 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/scheduled_tasks.lock +1 -0
- package/CHANGELOG.md +52 -0
- package/config/cache.config.ts +35 -1
- package/core/App.ts +24 -1064
- package/core/ArcheType.ts +78 -2110
- package/core/Entity.ts +10 -33
- package/core/RequestContext.ts +85 -36
- package/core/RequestLoaders.ts +89 -31
- package/core/app/bootstrap.ts +133 -0
- package/core/app/cors.ts +94 -0
- package/core/app/graphqlSetup.ts +56 -0
- package/core/app/healthEndpoints.ts +31 -0
- package/core/app/metricsCollector.ts +27 -0
- package/core/app/preparedStatementWarmup.ts +55 -0
- package/core/app/processHandlers.ts +43 -0
- package/core/app/requestRouter.ts +309 -0
- package/core/app/restRegistry.ts +72 -0
- package/core/app/shutdown.ts +97 -0
- package/core/app/studioRouter.ts +83 -0
- package/core/archetype/customTypes.ts +100 -0
- package/core/archetype/decorators.ts +171 -0
- package/core/archetype/fieldResolvers.ts +621 -0
- package/core/archetype/helpers.ts +29 -0
- package/core/archetype/relationLoader.ts +118 -0
- package/core/archetype/schemaBuilder.ts +141 -0
- package/core/archetype/weaver.ts +218 -0
- package/core/archetype/zodSchemaBuilder.ts +527 -0
- package/core/cache/CacheManager.ts +126 -9
- package/core/middleware/AccessLog.ts +8 -1
- package/database/PreparedStatementCache.ts +12 -3
- package/database/cancellable.ts +22 -0
- package/database/instrumentedDb.ts +141 -0
- package/docs/RFC_APP_REFACTOR.md +248 -0
- package/docs/RFC_REFACTOR_TARGETS.md +251 -0
- package/package.json +1 -1
- package/query/Query.ts +53 -20
- package/tests/integration/loaders/RequestLoaders.abort.test.ts +82 -0
- package/tests/integration/query/Query.abort.test.ts +66 -0
- package/tests/unit/cache/CacheManager.test.ts +132 -1
- package/tests/unit/database/cancellable.test.ts +81 -0
- package/tests/unit/database/instrumentedDb.test.ts +160 -0
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Tests cache configuration and management
|
|
4
4
|
*/
|
|
5
5
|
import { describe, test, expect, beforeEach, afterEach, mock } from 'bun:test';
|
|
6
|
-
import { CacheManager } from '../../../core/cache/CacheManager';
|
|
6
|
+
import { CacheManager, COMPONENT_TOMBSTONE } from '../../../core/cache/CacheManager';
|
|
7
7
|
import { MemoryCache } from '../../../core/cache/MemoryCache';
|
|
8
8
|
import { MultiLevelCache } from '../../../core/cache/MultiLevelCache';
|
|
9
9
|
|
|
@@ -210,6 +210,137 @@ describe('CacheManager', () => {
|
|
|
210
210
|
});
|
|
211
211
|
});
|
|
212
212
|
|
|
213
|
+
describe('component negative cache (tombstones)', () => {
|
|
214
|
+
beforeEach(async () => {
|
|
215
|
+
await cacheManager.initialize({
|
|
216
|
+
enabled: true,
|
|
217
|
+
provider: 'memory',
|
|
218
|
+
strategy: 'write-through',
|
|
219
|
+
defaultTTL: 3600000,
|
|
220
|
+
entity: { enabled: true, ttl: 3600000 },
|
|
221
|
+
component: {
|
|
222
|
+
enabled: true,
|
|
223
|
+
ttl: 1800000,
|
|
224
|
+
negativeCacheEnabled: true,
|
|
225
|
+
negativeCacheTtl: 60_000,
|
|
226
|
+
},
|
|
227
|
+
query: { enabled: false, ttl: 300000, maxSize: 10000 },
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('setComponentsWriteThrough writes tombstones for absent requested keys', async () => {
|
|
232
|
+
const requested = [
|
|
233
|
+
{ entityId: 'e1', typeId: 't1' },
|
|
234
|
+
{ entityId: 'e2', typeId: 't2' },
|
|
235
|
+
];
|
|
236
|
+
await cacheManager.setComponentsWriteThrough([], requested);
|
|
237
|
+
const results = await cacheManager.getComponents(requested);
|
|
238
|
+
expect(results[0]).toBe(COMPONENT_TOMBSTONE);
|
|
239
|
+
expect(results[1]).toBe(COMPONENT_TOMBSTONE);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test('found rows are written, absent are tombstoned, single setMany', async () => {
|
|
243
|
+
const requested = [
|
|
244
|
+
{ entityId: 'e1', typeId: 't1' },
|
|
245
|
+
{ entityId: 'e2', typeId: 't2' },
|
|
246
|
+
];
|
|
247
|
+
const found = [{
|
|
248
|
+
id: 'c1', entityId: 'e1', typeId: 't1',
|
|
249
|
+
data: { x: 1 }, createdAt: new Date(), updatedAt: new Date(), deletedAt: null,
|
|
250
|
+
}];
|
|
251
|
+
await cacheManager.setComponentsWriteThrough(found, requested);
|
|
252
|
+
const results = await cacheManager.getComponents(requested);
|
|
253
|
+
expect((results[0] as any)?.id).toBe('c1');
|
|
254
|
+
expect(results[1]).toBe(COMPONENT_TOMBSTONE);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test('invalidateComponent drops tombstone', async () => {
|
|
258
|
+
await cacheManager.setComponentsWriteThrough([], [{ entityId: 'e1', typeId: 't1' }]);
|
|
259
|
+
await cacheManager.invalidateComponent('e1', 't1');
|
|
260
|
+
const results = await cacheManager.getComponents([{ entityId: 'e1', typeId: 't1' }]);
|
|
261
|
+
expect(results[0]).toBeNull();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('negativeCacheEnabled=false skips tombstone writes (backward compat)', async () => {
|
|
265
|
+
await cacheManager.initialize({
|
|
266
|
+
enabled: true,
|
|
267
|
+
provider: 'memory',
|
|
268
|
+
strategy: 'write-through',
|
|
269
|
+
defaultTTL: 3600000,
|
|
270
|
+
entity: { enabled: true, ttl: 3600000 },
|
|
271
|
+
component: { enabled: true, ttl: 1800000 },
|
|
272
|
+
query: { enabled: false, ttl: 300000, maxSize: 10000 },
|
|
273
|
+
});
|
|
274
|
+
await cacheManager.setComponentsWriteThrough([], [{ entityId: 'e1', typeId: 't1' }]);
|
|
275
|
+
const results = await cacheManager.getComponents([{ entityId: 'e1', typeId: 't1' }]);
|
|
276
|
+
expect(results[0]).toBeNull();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test('legacy 2-arg signature (components, ttl) still works', async () => {
|
|
280
|
+
const data = [{
|
|
281
|
+
id: 'c1', entityId: 'e1', typeId: 't1',
|
|
282
|
+
data: { x: 1 }, createdAt: new Date(), updatedAt: new Date(), deletedAt: null,
|
|
283
|
+
}];
|
|
284
|
+
await cacheManager.setComponentsWriteThrough(data, 3600_000);
|
|
285
|
+
const results = await cacheManager.getComponents([{ entityId: 'e1', typeId: 't1' }]);
|
|
286
|
+
expect((results[0] as any)?.id).toBe('c1');
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe('relation negative cache', () => {
|
|
291
|
+
beforeEach(async () => {
|
|
292
|
+
await cacheManager.initialize({
|
|
293
|
+
enabled: true,
|
|
294
|
+
provider: 'memory',
|
|
295
|
+
strategy: 'write-through',
|
|
296
|
+
defaultTTL: 3600000,
|
|
297
|
+
entity: { enabled: true, ttl: 3600000 },
|
|
298
|
+
component: { enabled: true, ttl: 1800000 },
|
|
299
|
+
relation: { negativeCacheEnabled: true, negativeCacheTtl: 60_000 },
|
|
300
|
+
query: { enabled: false, ttl: 300000, maxSize: 10000 },
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test('setRelationsEmpty + getRelationsEmpty round trip', async () => {
|
|
305
|
+
const keys = [
|
|
306
|
+
{ entityId: 'u1', relationField: 'orders', relatedType: 'Order', foreignKey: 'user_id' },
|
|
307
|
+
{ entityId: 'u2', relationField: 'orders', relatedType: 'Order', foreignKey: 'user_id' },
|
|
308
|
+
];
|
|
309
|
+
await cacheManager.setRelationsEmpty(keys);
|
|
310
|
+
const flags = await cacheManager.getRelationsEmpty(keys);
|
|
311
|
+
expect(flags).toEqual([true, true]);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('getRelationsEmpty returns false for keys never tombstoned', async () => {
|
|
315
|
+
const flags = await cacheManager.getRelationsEmpty([
|
|
316
|
+
{ entityId: 'u-fresh', relationField: 'orders', relatedType: 'Order', foreignKey: 'user_id' },
|
|
317
|
+
]);
|
|
318
|
+
expect(flags).toEqual([false]);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test('invalidateRelation drops tombstone', async () => {
|
|
322
|
+
const key = { entityId: 'u1', relationField: 'orders', relatedType: 'Order', foreignKey: 'user_id' };
|
|
323
|
+
await cacheManager.setRelationsEmpty([key]);
|
|
324
|
+
await cacheManager.invalidateRelation(key.entityId, key.relationField, key.relatedType, key.foreignKey);
|
|
325
|
+
const flags = await cacheManager.getRelationsEmpty([key]);
|
|
326
|
+
expect(flags).toEqual([false]);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test('relation cache disabled returns all-false (no-op)', async () => {
|
|
330
|
+
await cacheManager.initialize({
|
|
331
|
+
enabled: true,
|
|
332
|
+
provider: 'memory',
|
|
333
|
+
strategy: 'write-through',
|
|
334
|
+
defaultTTL: 3600000,
|
|
335
|
+
relation: { negativeCacheEnabled: false },
|
|
336
|
+
});
|
|
337
|
+
const key = { entityId: 'u1', relationField: 'orders', relatedType: 'Order', foreignKey: 'user_id' };
|
|
338
|
+
await cacheManager.setRelationsEmpty([key]);
|
|
339
|
+
const flags = await cacheManager.getRelationsEmpty([key]);
|
|
340
|
+
expect(flags).toEqual([false]);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
213
344
|
describe('cache disabled', () => {
|
|
214
345
|
beforeEach(async () => {
|
|
215
346
|
await cacheManager.initialize({ enabled: false });
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for `database/cancellable.ts` — the shared `runWithSignal`
|
|
3
|
+
* helper extracted from Entity.ts so every framework call-site uses the
|
|
4
|
+
* same abort-on-cancel pattern.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, test, expect } from 'bun:test';
|
|
7
|
+
import { runWithSignal } from '../../../database/cancellable';
|
|
8
|
+
|
|
9
|
+
function makeFakeQuery<T>(opts: { delayMs?: number; value?: T; rejectWith?: Error }) {
|
|
10
|
+
let cancelFn: () => void = () => {};
|
|
11
|
+
const promise: any = new Promise<T>((resolve, reject) => {
|
|
12
|
+
const handle = setTimeout(() => {
|
|
13
|
+
if (opts.rejectWith) reject(opts.rejectWith);
|
|
14
|
+
else resolve(opts.value as T);
|
|
15
|
+
}, opts.delayMs ?? 1);
|
|
16
|
+
cancelFn = () => {
|
|
17
|
+
clearTimeout(handle);
|
|
18
|
+
promise.cancelled = true;
|
|
19
|
+
reject(Object.assign(new Error('Query cancelled'), { name: 'AbortError' }));
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
// Swallow unhandled rejection when the helper throws on pre-abort before
|
|
23
|
+
// awaiting `q`. Real Bun SQL Query objects don't surface this as an
|
|
24
|
+
// unhandled rejection because the runtime captures the cancel reason.
|
|
25
|
+
promise.catch(() => {});
|
|
26
|
+
promise.cancel = cancelFn;
|
|
27
|
+
promise.cancelled = false;
|
|
28
|
+
return promise;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('runWithSignal', () => {
|
|
32
|
+
test('resolves normally without signal', async () => {
|
|
33
|
+
const q = makeFakeQuery({ value: [1, 2, 3] });
|
|
34
|
+
const r = await runWithSignal<number[]>(q);
|
|
35
|
+
expect(r).toEqual([1, 2, 3]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('resolves normally when signal never fires', async () => {
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const q = makeFakeQuery({ value: 'done' });
|
|
41
|
+
const r = await runWithSignal<string>(q, controller.signal);
|
|
42
|
+
expect(r).toBe('done');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('rejects immediately when signal is pre-aborted', async () => {
|
|
46
|
+
const controller = new AbortController();
|
|
47
|
+
controller.abort(new Error('pre-aborted'));
|
|
48
|
+
const q = makeFakeQuery({ delayMs: 5000 });
|
|
49
|
+
await expect(runWithSignal(q, controller.signal)).rejects.toBeDefined();
|
|
50
|
+
// Cancelling the underlying query is best-effort; verify cancel ran.
|
|
51
|
+
expect(q.cancelled).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('cancels query mid-flight when signal aborts', async () => {
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
const q = makeFakeQuery({ delayMs: 5000 });
|
|
57
|
+
queueMicrotask(() => controller.abort(new Error('mid-flight')));
|
|
58
|
+
|
|
59
|
+
await expect(runWithSignal(q, controller.signal)).rejects.toBeDefined();
|
|
60
|
+
expect(q.cancelled).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('removes abort listener on success', async () => {
|
|
64
|
+
const controller = new AbortController();
|
|
65
|
+
let listenerCount = 0;
|
|
66
|
+
const origAdd = controller.signal.addEventListener.bind(controller.signal);
|
|
67
|
+
const origRemove = controller.signal.removeEventListener.bind(controller.signal);
|
|
68
|
+
controller.signal.addEventListener = ((...args: any[]) => {
|
|
69
|
+
listenerCount++;
|
|
70
|
+
return (origAdd as any)(...args);
|
|
71
|
+
}) as any;
|
|
72
|
+
controller.signal.removeEventListener = ((...args: any[]) => {
|
|
73
|
+
listenerCount--;
|
|
74
|
+
return (origRemove as any)(...args);
|
|
75
|
+
}) as any;
|
|
76
|
+
|
|
77
|
+
const q = makeFakeQuery({ value: 1 });
|
|
78
|
+
await runWithSignal(q, controller.signal);
|
|
79
|
+
expect(listenerCount).toBe(0);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for database/instrumentedDb.ts.
|
|
3
|
+
*
|
|
4
|
+
* Uses a fake `db` that returns a `cancel()`able promise-like to exercise
|
|
5
|
+
* the timing + abort path without a real Postgres backend.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, test, expect, beforeEach } from 'bun:test';
|
|
8
|
+
import {
|
|
9
|
+
timedUnsafe,
|
|
10
|
+
incrementDataLoaderCall,
|
|
11
|
+
getDbStats,
|
|
12
|
+
resetDbStats,
|
|
13
|
+
type PerRequestCounters,
|
|
14
|
+
} from '../../../database/instrumentedDb';
|
|
15
|
+
|
|
16
|
+
interface FakeQuery<T> extends Promise<T> {
|
|
17
|
+
cancel(): void;
|
|
18
|
+
cancelled: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function makeFakeDb(opts: { delayMs?: number; rejectWith?: Error; rows?: any[] } = {}) {
|
|
22
|
+
return {
|
|
23
|
+
unsafe(_sql: string, _params: any[]): FakeQuery<any[]> {
|
|
24
|
+
let cancelFn: () => void = () => {};
|
|
25
|
+
const promise: any = new Promise<any[]>((resolve, reject) => {
|
|
26
|
+
const handle = setTimeout(() => {
|
|
27
|
+
if (opts.rejectWith) reject(opts.rejectWith);
|
|
28
|
+
else resolve(opts.rows ?? []);
|
|
29
|
+
}, opts.delayMs ?? 1);
|
|
30
|
+
cancelFn = () => {
|
|
31
|
+
clearTimeout(handle);
|
|
32
|
+
promise.cancelled = true;
|
|
33
|
+
reject(Object.assign(new Error('Query cancelled'), { name: 'AbortError' }));
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
// Swallow unhandled rejection on pre-abort cancel path.
|
|
37
|
+
promise.catch(() => {});
|
|
38
|
+
promise.cancel = cancelFn;
|
|
39
|
+
promise.cancelled = false;
|
|
40
|
+
return promise as FakeQuery<any[]>;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('timedUnsafe', () => {
|
|
46
|
+
beforeEach(() => resetDbStats());
|
|
47
|
+
|
|
48
|
+
test('records totalCount and totalMs on success', async () => {
|
|
49
|
+
const db = makeFakeDb({ rows: [{ id: 1 }], delayMs: 5 });
|
|
50
|
+
const result = await timedUnsafe(db as any, 'SELECT 1', []);
|
|
51
|
+
expect(result).toEqual([{ id: 1 }]);
|
|
52
|
+
|
|
53
|
+
const stats = getDbStats();
|
|
54
|
+
expect(stats.totalCount).toBe(1);
|
|
55
|
+
expect(stats.totalMs).toBeGreaterThanOrEqual(1);
|
|
56
|
+
expect(stats.maxMs).toBeGreaterThanOrEqual(1);
|
|
57
|
+
expect(stats.abortedCount).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('increments dbQueryCount on perRequest counters', async () => {
|
|
61
|
+
const db = makeFakeDb();
|
|
62
|
+
const perReq: PerRequestCounters = { dbQueryCount: 0 };
|
|
63
|
+
await timedUnsafe(db as any, 'SELECT 1', [], undefined, perReq);
|
|
64
|
+
await timedUnsafe(db as any, 'SELECT 1', [], undefined, perReq);
|
|
65
|
+
expect(perReq.dbQueryCount).toBe(2);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('aborted query rejects with AbortError and records abortedCount', async () => {
|
|
69
|
+
const db = makeFakeDb({ delayMs: 1000 });
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
const promise = timedUnsafe(db as any, 'SELECT pg_sleep(10)', [], controller.signal);
|
|
72
|
+
|
|
73
|
+
queueMicrotask(() => controller.abort(new Error('test abort')));
|
|
74
|
+
|
|
75
|
+
await expect(promise).rejects.toBeDefined();
|
|
76
|
+
|
|
77
|
+
const stats = getDbStats();
|
|
78
|
+
expect(stats.totalCount).toBe(1);
|
|
79
|
+
expect(stats.abortedCount).toBe(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('pre-aborted signal cancels before await', async () => {
|
|
83
|
+
const db = makeFakeDb({ delayMs: 1000 });
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
controller.abort(new Error('pre-aborted'));
|
|
86
|
+
|
|
87
|
+
const promise = timedUnsafe(db as any, 'SELECT 1', [], controller.signal);
|
|
88
|
+
await expect(promise).rejects.toBeDefined();
|
|
89
|
+
|
|
90
|
+
const stats = getDbStats();
|
|
91
|
+
expect(stats.abortedCount).toBe(1);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('slow query above threshold increments slowCount', async () => {
|
|
95
|
+
// Slow threshold defaults to 500ms; we cannot reasonably wait that long
|
|
96
|
+
// in a unit test, so we override via env at module load — fall back to
|
|
97
|
+
// asserting the slow path is reachable by passing a tiny synthetic
|
|
98
|
+
// delay larger than zero and checking that the slowCount path triggers
|
|
99
|
+
// only when over threshold. Direct assertion: with default threshold,
|
|
100
|
+
// a 5ms query should NOT mark slow.
|
|
101
|
+
const db = makeFakeDb({ delayMs: 5 });
|
|
102
|
+
await timedUnsafe(db as any, 'SELECT 1', []);
|
|
103
|
+
const stats = getDbStats();
|
|
104
|
+
expect(stats.slowCount).toBe(0);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('incrementDataLoaderCall', () => {
|
|
109
|
+
beforeEach(() => resetDbStats());
|
|
110
|
+
|
|
111
|
+
test('increments per-kind global stats', () => {
|
|
112
|
+
incrementDataLoaderCall('entity');
|
|
113
|
+
incrementDataLoaderCall('component');
|
|
114
|
+
incrementDataLoaderCall('component');
|
|
115
|
+
incrementDataLoaderCall('relation');
|
|
116
|
+
|
|
117
|
+
const stats = getDbStats();
|
|
118
|
+
expect(stats.dataLoaderCalls.entity).toBe(1);
|
|
119
|
+
expect(stats.dataLoaderCalls.component).toBe(2);
|
|
120
|
+
expect(stats.dataLoaderCalls.relation).toBe(1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('increments perRequest.dataLoaderCalls when supplied', () => {
|
|
124
|
+
const perReq = {
|
|
125
|
+
dbQueryCount: 0,
|
|
126
|
+
dataLoaderCalls: { entity: 0, component: 0, relation: 0 },
|
|
127
|
+
};
|
|
128
|
+
incrementDataLoaderCall('entity', perReq);
|
|
129
|
+
incrementDataLoaderCall('component', perReq);
|
|
130
|
+
incrementDataLoaderCall('component', perReq);
|
|
131
|
+
expect(perReq.dataLoaderCalls.entity).toBe(1);
|
|
132
|
+
expect(perReq.dataLoaderCalls.component).toBe(2);
|
|
133
|
+
expect(perReq.dataLoaderCalls.relation).toBe(0);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe('getDbStats', () => {
|
|
138
|
+
beforeEach(() => resetDbStats());
|
|
139
|
+
|
|
140
|
+
test('reports avgMs as totalMs / totalCount', async () => {
|
|
141
|
+
const db = makeFakeDb({ delayMs: 4 });
|
|
142
|
+
await timedUnsafe(db as any, 'SELECT 1', []);
|
|
143
|
+
await timedUnsafe(db as any, 'SELECT 1', []);
|
|
144
|
+
const stats = getDbStats();
|
|
145
|
+
expect(stats.totalCount).toBe(2);
|
|
146
|
+
// avgMs is rounded to 2 decimals while totalMs is integer-rounded,
|
|
147
|
+
// so allow ±0.5ms tolerance against the unrounded computation.
|
|
148
|
+
expect(Math.abs(stats.avgMs - stats.totalMs / 2)).toBeLessThan(1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('returns zero state after reset', () => {
|
|
152
|
+
const stats = getDbStats();
|
|
153
|
+
expect(stats.totalCount).toBe(0);
|
|
154
|
+
expect(stats.totalMs).toBe(0);
|
|
155
|
+
expect(stats.maxMs).toBe(0);
|
|
156
|
+
expect(stats.slowCount).toBe(0);
|
|
157
|
+
expect(stats.abortedCount).toBe(0);
|
|
158
|
+
expect(stats.dataLoaderCalls).toEqual({ entity: 0, component: 0, relation: 0 });
|
|
159
|
+
});
|
|
160
|
+
});
|