Container side: - agent-runner switches to Bun. Drops better-sqlite3 (native compile gone), drops tsc build step in-image AND the tsc-on-every-session-wake in the entrypoint — bun runs src/index.ts directly. bun:sqlite replaces better-sqlite3; cross-mount DB invariants (journal_mode=DELETE, busy_timeout) preserved. Named params converted from @name to $name because bun:sqlite does not auto-strip the prefix the way better-sqlite3 does. - Tests ported from vitest to bun:test (only describe/it/expect/before/afterEach used, API-compatible). vitest.config.ts excludes container/agent-runner/. - bun.lock replaces pnpm-lock.yaml + pnpm-workspace.yaml under container/agent-runner/. Host pnpm workspace does NOT include this tree. Dockerfile improvements (independent of Bun but bundled while touching the file): - tini as PID 1 for correct SIGTERM propagation (prevents half-written outbound.db on shutdown). - Extracted entrypoint.sh — readable and diffable vs the old inline printf. - BuildKit cache mounts for apt + bun install + pnpm install. - --no-install-recommends on apt, pinned CLAUDE_CODE_VERSION, AGENT_BROWSER, VERCEL, BUN_VERSION. - CJK fonts (~200MB) behind ARG INSTALL_CJK_FONTS=false; build.sh reads from .env; setup/container.ts reads the same .env so /setup and manual rebuild stay in sync. - PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 in case any postinstall tries to pull a redundant Chromium. - /home/node 755 (was 777). Host side: - src/container-runner.ts dynamic spawn command collapses from `pnpm exec tsc --outDir /tmp/dist … && node /tmp/dist/index.js` to `exec bun run /app/src/index.ts` — cold start ~200-500ms faster per wake. CI: - oven-sh/setup-bun@v2 alongside Node/pnpm. Adds explicit container typecheck (was documented in CLAUDE.md, not enforced) and `bun test` for agent-runner tests.
121 lines
4.4 KiB
TypeScript
121 lines
4.4 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
|
|
|
import { initTestSessionDb, closeSessionDb, getInboundDb, getOutboundDb } from './db/connection.js';
|
|
import { getUndeliveredMessages } from './db/messages-out.js';
|
|
import { getPendingMessages } from './db/messages-in.js';
|
|
import { MockProvider } from './providers/mock.js';
|
|
import { runPollLoop } from './poll-loop.js';
|
|
|
|
beforeEach(() => {
|
|
initTestSessionDb();
|
|
// Seed a destination so output parsing can resolve "discord-test" → routing
|
|
getInboundDb()
|
|
.prepare(
|
|
`INSERT INTO destinations (name, display_name, type, channel_type, platform_id, agent_group_id)
|
|
VALUES ('discord-test', 'Discord Test', 'channel', 'discord', 'chan-1', NULL)`,
|
|
)
|
|
.run();
|
|
});
|
|
|
|
afterEach(() => {
|
|
closeSessionDb();
|
|
});
|
|
|
|
function insertMessage(id: string, content: object, opts?: { platformId?: string; channelType?: string; threadId?: string }) {
|
|
getInboundDb()
|
|
.prepare(
|
|
`INSERT INTO messages_in (id, kind, timestamp, status, platform_id, channel_type, thread_id, content)
|
|
VALUES (?, 'chat', datetime('now'), 'pending', ?, ?, ?, ?)`,
|
|
)
|
|
.run(id, opts?.platformId ?? null, opts?.channelType ?? null, opts?.threadId ?? null, JSON.stringify(content));
|
|
}
|
|
|
|
describe('poll loop integration', () => {
|
|
it('should pick up a message, process it, and write a response', async () => {
|
|
insertMessage('m1', { sender: 'Alice', text: 'What is the meaning of life?' }, { platformId: 'chan-1', channelType: 'discord', threadId: 'thread-1' });
|
|
|
|
const provider = new MockProvider({}, () => '<message to="discord-test">42</message>');
|
|
|
|
const controller = new AbortController();
|
|
const loopPromise = runPollLoopWithTimeout(provider, controller.signal, 2000);
|
|
|
|
await waitFor(() => getUndeliveredMessages().length > 0, 2000);
|
|
controller.abort();
|
|
|
|
const out = getUndeliveredMessages();
|
|
expect(out).toHaveLength(1);
|
|
expect(JSON.parse(out[0].content).text).toBe('42');
|
|
expect(out[0].platform_id).toBe('chan-1');
|
|
expect(out[0].channel_type).toBe('discord');
|
|
expect(out[0].in_reply_to).toBe('m1');
|
|
|
|
// Input message should be acked (not pending)
|
|
const pending = getPendingMessages();
|
|
expect(pending).toHaveLength(0);
|
|
|
|
await loopPromise.catch(() => {});
|
|
});
|
|
|
|
it('should process multiple messages in a batch', async () => {
|
|
insertMessage('m1', { sender: 'Alice', text: 'Hello' });
|
|
insertMessage('m2', { sender: 'Bob', text: 'World' });
|
|
|
|
const provider = new MockProvider({}, () => '<message to="discord-test">Got both messages</message>');
|
|
const controller = new AbortController();
|
|
const loopPromise = runPollLoopWithTimeout(provider, controller.signal, 2000);
|
|
|
|
await waitFor(() => getUndeliveredMessages().length > 0, 2000);
|
|
controller.abort();
|
|
|
|
const out = getUndeliveredMessages();
|
|
expect(out).toHaveLength(1);
|
|
expect(JSON.parse(out[0].content).text).toBe('Got both messages');
|
|
|
|
await loopPromise.catch(() => {});
|
|
});
|
|
|
|
it('should process messages arriving after loop starts', async () => {
|
|
const provider = new MockProvider({}, () => '<message to="discord-test">Processed</message>');
|
|
const controller = new AbortController();
|
|
const loopPromise = runPollLoopWithTimeout(provider, controller.signal, 3000);
|
|
|
|
// Insert message after loop has started
|
|
await sleep(200);
|
|
insertMessage('m-late', { sender: 'Charlie', text: 'Late arrival' });
|
|
|
|
await waitFor(() => getUndeliveredMessages().length > 0, 2000);
|
|
controller.abort();
|
|
|
|
const out = getUndeliveredMessages();
|
|
expect(out.length).toBeGreaterThanOrEqual(1);
|
|
|
|
await loopPromise.catch(() => {});
|
|
});
|
|
});
|
|
|
|
// Helper: run poll loop until aborted or timeout
|
|
async function runPollLoopWithTimeout(provider: MockProvider, signal: AbortSignal, timeoutMs: number): Promise<void> {
|
|
return Promise.race([
|
|
runPollLoop({
|
|
provider,
|
|
cwd: '/tmp',
|
|
}),
|
|
new Promise<void>((_, reject) => {
|
|
signal.addEventListener('abort', () => reject(new Error('aborted')));
|
|
}),
|
|
new Promise<void>((_, reject) => setTimeout(() => reject(new Error('timeout')), timeoutMs)),
|
|
]);
|
|
}
|
|
|
|
async function waitFor(condition: () => boolean, timeoutMs: number): Promise<void> {
|
|
const start = Date.now();
|
|
while (!condition()) {
|
|
if (Date.now() - start > timeoutMs) throw new Error('waitFor timeout');
|
|
await sleep(50);
|
|
}
|
|
}
|
|
|
|
function sleep(ms: number): Promise<void> {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|