From 6c831d216db42397b4e74d799d23f98d05d91ed3 Mon Sep 17 00:00:00 2001 From: Wintermute Date: Sun, 3 May 2026 16:14:43 +0000 Subject: [PATCH] =?UTF-8?q?fix(test):=20e2e=20tests=20clean=20up=20after?= =?UTF-8?q?=20themselves=20=E2=80=94=20no=20more=20orphan=20clients?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: every test run left e2e-oauth-test, e2e-revoke-test, and e2e-revoke-key-test rows in oauth_clients and access_tokens. The CLI-based cleanup in afterAll was failing silently. Fix: - beforeAll: SQL DELETE of any e2e-* orphans from previous crashed runs - afterAll: direct SQL cleanup of oauth_tokens, oauth_clients, access_tokens, mcp_request_log — all rows matching 'e2e-%' pattern - No reliance on CLI commands for cleanup (they fail silently) Verified: 52 tests pass, 0 test rows remain after run. --- test/e2e/serve-http-oauth.test.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/test/e2e/serve-http-oauth.test.ts b/test/e2e/serve-http-oauth.test.ts index 0d1a64ce5..031970068 100644 --- a/test/e2e/serve-http-oauth.test.ts +++ b/test/e2e/serve-http-oauth.test.ts @@ -34,6 +34,16 @@ describeE2E('serve-http OAuth 2.1 E2E (v0.26.1)', () => { beforeAll(async () => { const { execSync, spawn } = await import('child_process'); + // Clean up orphans from any previous crashed test runs + try { + const postgres = (await import('postgres')).default; + const cleanSql = postgres(process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL || '', { prepare: false }); + await cleanSql`DELETE FROM oauth_tokens WHERE client_id IN (SELECT client_id FROM oauth_clients WHERE client_name LIKE 'e2e-%')`; + await cleanSql`DELETE FROM oauth_clients WHERE client_name LIKE 'e2e-%'`; + await cleanSql`DELETE FROM access_tokens WHERE name LIKE 'e2e-%'`; + await cleanSql.end(); + } catch {} + // Register a test OAuth client via CLI const regOutput = execSync( 'bun run src/cli.ts auth register-client e2e-oauth-test --grant-types client_credentials --scopes "read write"', @@ -96,12 +106,19 @@ describeE2E('serve-http OAuth 2.1 E2E (v0.26.1)', () => { await new Promise(r => setTimeout(r, 1000)); if (!serverProcess.killed) serverProcess.kill('SIGKILL'); } - // Revoke test client + // Nuclear cleanup via direct SQL — CLI revoke is unreliable try { - const { execSync } = await import('child_process'); - execSync(`bun run src/cli.ts auth revoke-client "${clientId}"`, - { cwd: process.cwd(), encoding: 'utf8', stdio: 'pipe' }); - } catch {} + const postgres = (await import('postgres')).default; + const sql = postgres(process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL || '', { prepare: false }); + await sql`DELETE FROM oauth_tokens WHERE client_id IN (SELECT client_id FROM oauth_clients WHERE client_name LIKE 'e2e-%')`; + await sql`DELETE FROM mcp_request_log WHERE token_name IN (SELECT client_id FROM oauth_clients WHERE client_name LIKE 'e2e-%')`; + await sql`DELETE FROM oauth_clients WHERE client_name LIKE 'e2e-%'`; + await sql`DELETE FROM mcp_request_log WHERE agent_name LIKE 'e2e-%'`; + await sql`DELETE FROM access_tokens WHERE name LIKE 'e2e-%'`; + await sql.end(); + } catch (e) { + console.error('[e2e] Cleanup failed:', e instanceof Error ? e.message : e); + } }); // Helper: mint a token with given scopes