test(server): await historical cleanup page completion

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-24 23:30:17 -05:00
1 parent 400f4c0fc8
commit 0c7109c103
2 files changed
+20 -5

No files matched your search

+7
View File
@@ -136,3 +136,10 @@ route suite separately exposed cold imports inside timed tests. Moving those
imports to suite loading preserves assertions and prevents timed-out requests imports to suite loading preserves assertions and prevents timed-out requests
from leaking into later mock state. The watchdog fixture collision from CI uses from leaking into later mock state. The watchdog fixture collision from CI uses
monotonic identifiers instead; its 19 real-PostgreSQL cases pass. monotonic identifiers instead; its 19 real-PostgreSQL cases pass.
The next CI run passed the security gate and the two originally failing shards.
Its remaining server shard caught a historical cleanup test checking 33 provider
inspections after only 32 had completed. The test advanced fake time before the
previous asynchronous page had necessarily published its next deadline. It now
waits for all 33 inspections, then retains the zero-further-work assertions. All
14 environment cleanup cases pass on real PostgreSQL; production code is unchanged.
@@ -17,17 +17,20 @@ import { historicalCleanupVerificationService } from "../services/historical-cle
import { heartbeatService } from "../services/heartbeat.js"; import { heartbeatService } from "../services/heartbeat.js";
import { environmentRuntimeService } from "../services/environment-runtime.js"; import { environmentRuntimeService } from "../services/environment-runtime.js";
const support = await getEmbeddedPostgresTestSupport(); import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
const external = resolveExternalTestDatabaseUrl("PAPERCLIP_ENVIRONMENT_CLEANUP_TEST_DATABASE_URL");
const support = external ? { supported: true } : await getEmbeddedPostgresTestSupport();
describe.skipIf(!support.supported)("environment cleanup scheduling", () => { describe.skipIf(!support.supported)("environment cleanup scheduling", () => {
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>, db: ReturnType<typeof createDb>; let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | undefined, db: ReturnType<typeof createDb>;
let companyId: string, environmentId: string, draining: boolean, failing: boolean; let companyId: string, environmentId: string, draining: boolean, failing: boolean;
let coordinator: ReturnType<typeof createDeliveryWorkCoordinator> | undefined; let coordinator: ReturnType<typeof createDeliveryWorkCoordinator> | undefined;
let worker: ReturnType<typeof registerEnvironmentCleanupWork> | undefined; let worker: ReturnType<typeof registerEnvironmentCleanupWork> | undefined;
let releases: ReturnType<typeof vi.fn>, inspections: ReturnType<typeof vi.fn>, reads: ReturnType<typeof vi.spyOn>; let releases: ReturnType<typeof vi.fn>, inspections: ReturnType<typeof vi.fn>, reads: ReturnType<typeof vi.spyOn>;
const errors: unknown[] = [], disposers: Array<() => void> = []; const errors: unknown[] = [], disposers: Array<() => void> = [];
beforeAll(async () => { beforeAll(async () => {
database = await startEmbeddedPostgresTestDatabase("environment-cleanup-work-"); if (!external) database = await startEmbeddedPostgresTestDatabase("environment-cleanup-work-");
db = createDb(database.connectionString, { maxConnections: 6, idleTimeoutSeconds: 0, maxLifetimeSeconds: 0 }); db = createDb(external ?? database!.connectionString, { maxConnections: 6, idleTimeoutSeconds: 0, maxLifetimeSeconds: 0 });
await Promise.all(Array.from({ length: 6 }, () => db.execute(sql`select pg_sleep(0.01)`))); await Promise.all(Array.from({ length: 6 }, () => db.execute(sql`select pg_sleep(0.01)`)));
}, 90_000); }, 90_000);
beforeEach(async () => { beforeEach(async () => {
@@ -225,7 +228,12 @@ describe.skipIf(!support.supported)("environment cleanup scheduling", () => {
for (let i = 0; i < 33; i++) { for (let i = 0; i < 33; i++) {
const lease = await pending(); await environmentService(db).releaseLease(lease.id, "failed", { cleanupStatus: "success" }); const lease = await pending(); await environmentService(db).releaseLease(lease.id, "failed", { cleanupStatus: "success" });
} }
await start(true); await settle(300_000); await settle(300_000); expect(inspections).toHaveBeenCalledTimes(33); await quiet(); await start(true);
await vi.waitFor(async () => {
await settle(300_000);
expect(inspections).toHaveBeenCalledTimes(33);
}, { timeout: 5000 });
await quiet();
}); });
it("keeps unknown historical resources scheduled until provider confirmation", async () => { it("keeps unknown historical resources scheduled until provider confirmation", async () => {
const lease = await pending(); await environmentService(db).releaseLease(lease.id, "failed", { cleanupStatus: "success" }); const lease = await pending(); await environmentService(db).releaseLease(lease.id, "failed", { cleanupStatus: "success" });