Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: poll for embedded usage rows instead of a fixed 200 ms sleep

garrytan2cc6af777155
diff --git a/test/e2e/engine-open-b3a-postgres.test.ts b/test/e2e/engine-open-b3a-postgres.test.ts
index 448fcc1951..84344ccd27 100644
--- a/test/e2e/engine-open-b3a-postgres.test.ts
+++ b/test/e2e/engine-open-b3a-postgres.test.ts
@@ -11,6 +11,7 @@ import { __setGenerateTextTransportForTests, chat, resetGateway } from '../../sr
import { openEngine, withEngineScope, type EngineSettings } from '../../src/core/engine-open.ts';
import { __aliasPolicyEngineCountForTests, readHonorAliases, remotePrivacyRelaxed } from '../../src/core/search/private-visibility.ts';
import type { BrainEngine } from '../../src/core/engine.ts';
+import { waitFor } from '../helpers/wait-for.ts';
const DATABASE_URL = process.env.DATABASE_URL;
const describeE2E = DATABASE_URL ? describe : describe.skip;
@@ -51,11 +52,11 @@ describeE2E('embedded engine on Postgres (B3a)', () => {
try {
const start = await rows();
await withEngineScope(engine, () => chat({ messages: [{ role: 'user', content: 'hello' }] }));
- await Bun.sleep(200);
+ await waitFor(async () => (await rows()) >= start + 1, { timeoutMs: 10_000, label: 'first usage row' });
expect(await rows()).toBe(start + 1);
await engine.reconnect();
await withEngineScope(engine, () => chat({ messages: [{ role: 'user', content: 'hello again' }] }));
- await Bun.sleep(200);
+ await waitFor(async () => (await rows()) >= start + 2, { timeoutMs: 10_000, label: 'usage row after reconnect' });
expect(await rows()).toBe(start + 2);
} finally {
await engine.disconnect();
diff --git a/test/engine-open-usage.test.ts b/test/engine-open-usage.test.ts
index 6d782b012e..547eaa9802 100644
--- a/test/engine-open-usage.test.ts
+++ b/test/engine-open-usage.test.ts
@@ -12,6 +12,7 @@ import { __setGenerateTextTransportForTests, chat, resetGateway } from '../src/c
import { __chatUsageSinkCountForTests } from '../src/core/ai/chat-usage.ts';
import { openEngine, withEngineScope, type EngineSettings } from '../src/core/engine-open.ts';
import { createEngine } from '../src/core/engine-factory.ts';
+import { waitFor } from './helpers/wait-for.ts';
import type { BrainEngine } from '../src/core/engine.ts';
const key = () => `sk-usage-${randomBytes(12).toString('hex')}`;
@@ -21,7 +22,7 @@ const settingsFor = (database: EngineSettings['database']): EngineSettings => ({
ai: { providers: { openai: { apiKey: key() } }, models: { chat: 'openai:gpt-5.6' } },
});
const usageRows = async (engine: BrainEngine) => Number((await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM chat_usage_log'))[0]!.n);
-const settled = () => Bun.sleep(200);
+const booked = (engine: BrainEngine, n: number) => waitFor(async () => (await usageRows(engine)) >= n, { timeoutMs: 10_000, label: `${n} usage row(s)` });
const hello = (engine: BrainEngine) => withEngineScope(engine, () => chat({ messages: [{ role: 'user', content: 'hello' }] }));
afterEach(() => {
@@ -54,11 +55,11 @@ describe('embedded chat usage', () => {
try {
await engine.initSchema();
await hello(engine);
- await settled();
+ await booked(engine, 1);
expect(await usageRows(engine)).toBe(1);
await engine.reconnect();
await hello(engine);
- await settled();
+ await booked(engine, 2);
expect(await usageRows(engine)).toBe(2);
} finally {
await engine.disconnect();