From 6be8fbae450562e5aa24c4252c77211edde2f9df Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 16 Nov 2026 11:43:44 -0600 Subject: [PATCH] test: support bounded external PostgreSQL in incoming wake regressions --- .../heartbeat-comment-wake-batching.test.ts | 22 +++++++++++++---- .../heartbeat-person-tree-dispatch.test.ts | 24 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/server/src/__tests__/heartbeat-comment-wake-batching.test.ts b/server/src/__tests__/heartbeat-comment-wake-batching.test.ts index 2e7c35ed30b..e08a6c14f5a 100644 --- a/server/src/__tests__/heartbeat-comment-wake-batching.test.ts +++ b/server/src/__tests__/heartbeat-comment-wake-batching.test.ts @@ -20,6 +20,7 @@ import { companies, companyMemberships, createDb, + closeRegisteredClients, heartbeatRuns, issueComments, issueRecoveryActions, @@ -43,12 +44,15 @@ import { } from "./helpers/embedded-postgres.ts"; import { parseWakePayloadFromMessage } from "./helpers/wake-message.ts"; +import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js"; + +const externalDatabaseUrl = resolveExternalTestDatabaseUrl("PAPERCLIP_HEARTBEAT_COMMENT_WAKE_TEST_DATABASE_URL"); const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport(); -const describeEmbeddedPostgres = embeddedPostgresSupport.supported +const describeEmbeddedPostgres = externalDatabaseUrl || embeddedPostgresSupport.supported ? describe : describe.skip; -if (!embeddedPostgresSupport.supported) { +if (!externalDatabaseUrl && !embeddedPostgresSupport.supported) { console.warn( `Skipping embedded Postgres heartbeat comment wake batching tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`, ); @@ -295,6 +299,10 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => { } beforeAll(async () => { + if (externalDatabaseUrl) { + db = createDb(externalDatabaseUrl, { maxConnections: 4 }); + return; // The external owner applies the normal migrations once before this process. + } const started = await startEmbeddedPostgresTestDatabase( "paperclip-heartbeat-comment-wake-", ); @@ -303,9 +311,13 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => { }, 120_000); afterAll(async () => { - await heartbeatService(db).drainActiveRunExecutions(); - await closeDbClient(db); - await tempDb?.cleanup(); + const failures: unknown[] = []; + try { await heartbeatService(db).drainActiveRunExecutions(); } catch (error) { failures.push(error); } + try { await closeDbClient(db); } catch (error) { failures.push(error); } + try { if (externalDatabaseUrl) await closeRegisteredClients(externalDatabaseUrl, { scope: "database" }); } + catch (error) { failures.push(error); } + try { await tempDb?.cleanup(); } catch (error) { failures.push(error); } + if (failures.length) throw new AggregateError(failures, "Comment-wake fixture cleanup failed"); }); afterEach(() => { diff --git a/server/src/__tests__/heartbeat-person-tree-dispatch.test.ts b/server/src/__tests__/heartbeat-person-tree-dispatch.test.ts index f3fa82443c8..a88565bf83c 100644 --- a/server/src/__tests__/heartbeat-person-tree-dispatch.test.ts +++ b/server/src/__tests__/heartbeat-person-tree-dispatch.test.ts @@ -1,7 +1,7 @@ import { randomUUID } from "node:crypto"; import { and, eq, inArray, sql } from "drizzle-orm"; import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; -import { agentWakeupRequests, agents, authUsers, companies, companyMemberships, createDb, heartbeatRuns, issues } from "@paperclipai/db"; +import { agentWakeupRequests, agents, authUsers, companies, companyMemberships, closeRegisteredClients, createDb, heartbeatRuns, issues } from "@paperclipai/db"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; import { heartbeatService } from "../services/heartbeat.ts"; import { runningProcesses } from "../adapters/index.ts"; @@ -21,8 +21,11 @@ vi.mock("../adapters/index.ts", async () => { return { ...actual, getServerAdapter: vi.fn(() => ({ supportsLocalAgentJwt: false, execute: mockAdapterExecute })) }; }); +import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js"; + +const externalDatabaseUrl = resolveExternalTestDatabaseUrl("PAPERCLIP_PERSON_TREE_DISPATCH_TEST_DATABASE_URL"); const support = await getEmbeddedPostgresTestSupport(); -const suite = support.supported ? describe : describe.skip; +const suite = externalDatabaseUrl || support.supported ? describe : describe.skip; const MINUTE = 60_000; suite("dispatch of work on a task tree a person created, through the heartbeat queue", () => { @@ -32,8 +35,12 @@ suite("dispatch of work on a task tree a person created, through the heartbeat q const userId = randomUUID(); beforeAll(async () => { - temporary = await startEmbeddedPostgresTestDatabase("paperclip-person-tree-dispatch-"); - db = createDb(temporary.connectionString); + if (externalDatabaseUrl) { + db = createDb(externalDatabaseUrl, { maxConnections: 4 }); + } else { + temporary = await startEmbeddedPostgresTestDatabase("paperclip-person-tree-dispatch-"); + db = createDb(temporary.connectionString); + } heartbeat = heartbeatService(db); await db.insert(authUsers).values({ id: userId, name: "Board", email: `${userId}@example.test`, emailVerified: true, createdAt: new Date(), updatedAt: new Date() }); @@ -50,7 +57,14 @@ suite("dispatch of work on a task tree a person created, through the heartbeat q await db.execute(sql`truncate table ${companies} cascade`); }, 180_000); - afterAll(async () => { await db?.$client.end(); await temporary?.cleanup(); }, 120_000); + afterAll(async () => { + const failures: unknown[] = []; + try { await db?.$client.end(); } catch (error) { failures.push(error); } + try { if (externalDatabaseUrl) await closeRegisteredClients(externalDatabaseUrl, { scope: "database" }); } + catch (error) { failures.push(error); } + try { await temporary?.cleanup(); } catch (error) { failures.push(error); } + if (failures.length) throw new AggregateError(failures, "Person-tree fixture cleanup failed"); + }, 120_000); async function seed() { const companyId = randomUUID(), agentId = randomUUID();