Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test: protect cleanup completion and administrative finalization
Alfredo Lopez5d3af930cd94
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 901ef8fa0..9ddf1f33f 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.701",+ "version": "0.1.702","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.701",+ "version": "0.1.702","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 43026ba2d..535bbc6c5 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.701",+ "version": "0.1.702","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/test/realPostgresCleanup.contract.test.ts b/control-api/test/realPostgresCleanup.contract.test.tsnew file mode 100644index 000000000..873f8b20f--- /dev/null+++ b/control-api/test/realPostgresCleanup.contract.test.ts@@ -0,0 +1,135 @@+import { describe, expect, it, vi } from 'vitest'+import { readFileSync } from 'node:fs'+import { dirname, join } from 'node:path'+import { fileURLToPath } from 'node:url'+import { Script } from 'node:vm'+import * as ts from 'typescript'++const testDir = dirname(fileURLToPath(import.meta.url))+const suites = [+ 'services.adminAuthDelete.realPostgres.integration.test.ts',+ 'services.controlAdminReplaceInviter.realPostgres.integration.test.ts',+ 'services.directory.userRetirement.realPostgres.integration.test.ts',+]++// This test-only contract probe executes each actual source callback, not a+// copied teardown model. Imports, beforeAll and business tests never run; the+// administrative/query collaborators below cannot contact a real database.+function afterAllCallback(+ file: string,+ collaborators: Record<string, unknown>+): () => Promise<void> {+ const source = readFileSync(join(testDir, file), 'utf8')+ const ast = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)+ const callbacks: ts.ArrowFunction[] = []+ function visit(node: ts.Node): void {+ if (+ ts.isCallExpression(node) &&+ ts.isIdentifier(node.expression) &&+ node.expression.text === 'afterAll'+ ) {+ const callback = node.arguments[0]+ if (callback && ts.isArrowFunction(callback)) callbacks.push(callback)+ }+ ts.forEachChild(node, visit)+ }+ visit(ast)+ // Do not put AST nodes in failure output: their parents contain unrelated+ // fixture data. The witness is exactly one asynchronous source callback.+ expect(callbacks.length).toBe(1)+ const callback = callbacks[0]!+ expect(callback.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.AsyncKeyword)).toBe(+ true+ )+ const emitted = ts.transpileModule(`(${callback.getText(ast)})`, {+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None },+ }).outputText+ const hook = new Script(emitted, { filename: `${file}:afterAll-contract` }).runInNewContext(+ {+ querySpy: undefined,+ connectSpy: undefined,+ corePoolConnectSpy: undefined,+ database: 'teardown_contract',+ quoteIdent: (value: string) => `"${value}"`,+ ...collaborators,+ },+ { timeout: 1_000 }+ ) as () => Promise<void>+ expect(typeof hook).toBe('function')+ return hook+}++describe.each(suites)('%s teardown contract', file => {+ it('closes the administrative pool after the client-close helper rejects without waiting or dropping', async () => {+ const testPool = {}+ const error = new Error('client-close helper failed')+ const helper = vi.fn().mockRejectedValueOnce(error)+ const wait = vi.fn()+ const adminPool = { end: vi.fn().mockResolvedValue(undefined), query: vi.fn() }+ const hook = afterAllCallback(file, {+ testPool,+ adminPool,+ endPoolAndWaitForClients: helper,+ waitForDatabaseConnectionsToClose: wait,+ })++ await expect(hook()).rejects.toBe(error)++ expect(helper).toHaveBeenCalledExactlyOnceWith(testPool)+ expect(adminPool.end).toHaveBeenCalledExactlyOnceWith()+ expect(wait).not.toHaveBeenCalled()+ expect(adminPool.query).not.toHaveBeenCalled()+ })++ it('waits for client closure, then server closure, then drops before closing the admin pool', async () => {+ const events: string[] = []+ const testPool = {}+ const helper = vi.fn(async () => {+ events.push('clients-closed')+ })+ const wait = vi.fn(async () => {+ events.push('server-closed')+ })+ const adminPool = {+ query: vi.fn(async () => {+ events.push('drop')+ }),+ end: vi.fn(async () => {+ events.push('admin-closed')+ }),+ }+ const hook = afterAllCallback(file, {+ testPool,+ adminPool,+ endPoolAndWaitForClients: helper,+ waitForDatabaseConnectionsToClose: wait,+ })++ await expect(hook()).resolves.toBeUndefined()++ expect(helper).toHaveBeenCalledExactlyOnceWith(testPool)+ expect(wait).toHaveBeenCalledExactlyOnceWith(adminPool, 'teardown_contract')+ expect(adminPool.query).toHaveBeenCalledExactlyOnceWith(+ 'DROP DATABASE IF EXISTS "teardown_contract"'+ )+ expect(adminPool.end).toHaveBeenCalledExactlyOnceWith()+ expect(events).toEqual(['clients-closed', 'server-closed', 'drop', 'admin-closed'])+ })++ it('still closes the test pool when no administrative pool was created', async () => {+ const testPool = {}+ const helper = vi.fn().mockResolvedValue(undefined)+ const wait = vi.fn()+ const hook = afterAllCallback(file, {+ testPool,+ adminPool: undefined,+ endPoolAndWaitForClients: helper,+ waitForDatabaseConnectionsToClose: wait,+ })++ await expect(hook()).resolves.toBeUndefined()++ expect(helper).toHaveBeenCalledExactlyOnceWith(testPool)+ expect(wait).not.toHaveBeenCalled()+ })+})diff --git a/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts b/control-api/test/services.adminAuthDelete.realPostgres.integration.test.tsindex 0e688fd68..54da2bab6 100644--- a/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts+++ b/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts@@ -76,13 +76,13 @@ describeRealPostgres('deleteControlAdmin on real PostgreSQL', () => {afterAll(async () => {querySpy?.mockRestore()connectSpy?.mockRestore()- await endPoolAndWaitForClients(testPool)- if (!adminPool) returntry {+ await endPoolAndWaitForClients(testPool)+ if (!adminPool) returnawait waitForDatabaseConnectionsToClose(adminPool, database)await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)} finally {- await adminPool.end()+ await adminPool?.end()}})diff --git a/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts b/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.tsindex d1a208289..4b351bf6a 100644--- a/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts+++ b/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts@@ -183,13 +183,13 @@ describeRealPostgres('control admin replace-inviter invitations on real PostgreSafterAll(async () => {querySpy?.mockRestore()connectSpy?.mockRestore()- await endPoolAndWaitForClients(testPool)- if (!adminPool) returntry {+ await endPoolAndWaitForClients(testPool)+ if (!adminPool) returnawait waitForDatabaseConnectionsToClose(adminPool, database)await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)} finally {- await adminPool.end()+ await adminPool?.end()}})diff --git a/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts b/control-api/test/services.directory.userRetirement.realPostgres.integration.test.tsindex 455a46bbc..da9569d77 100644--- a/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts+++ b/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts@@ -58,13 +58,13 @@ describeRealPostgres('retireDesktopUser on real PostgreSQL', () => {afterAll(async () => {corePoolConnectSpy?.mockRestore()- await endPoolAndWaitForClients(testPool)- if (!adminPool) returntry {+ await endPoolAndWaitForClients(testPool)+ if (!adminPool) returnawait waitForDatabaseConnectionsToClose(adminPool, database)await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)} finally {- await adminPool.end()+ await adminPool?.end()}})diff --git a/workflow-recipes/package-lock.json b/workflow-recipes/package-lock.jsonindex c243fe8de..bc583ccf9 100644--- a/workflow-recipes/package-lock.json+++ b/workflow-recipes/package-lock.json@@ -1,12 +1,12 @@{"name": "workload-recipes-controller",- "version": "0.1.455",+ "version": "0.1.456","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "workload-recipes-controller",- "version": "0.1.455",+ "version": "0.1.456","license": "SEE LICENSE IN LICENSE","dependencies": {"@aws-sdk/client-s3": "^3.1009.0",diff --git a/workflow-recipes/package.json b/workflow-recipes/package.jsonindex 75dcad32e..ab4052271 100644--- a/workflow-recipes/package.json+++ b/workflow-recipes/package.json@@ -1,6 +1,6 @@{"name": "workload-recipes-controller",- "version": "0.1.455",+ "version": "0.1.456","description": "Workload Recipes Controller (WRC) - WorkloadRecipe lifecycle management and MCP server interface","main": "dist/main.js","engines": {diff --git a/workflow-recipes/src/workflow/workflowReconciler.test.ts b/workflow-recipes/src/workflow/workflowReconciler.test.tsindex e247d805d..6ceb2de09 100644--- a/workflow-recipes/src/workflow/workflowReconciler.test.ts+++ b/workflow-recipes/src/workflow/workflowReconciler.test.ts@@ -2303,6 +2303,53 @@ describe('WorkflowReconciler — Plugin Workload SDK eager mcp-host', () => {}})+ it('does not forget the finalizer ledger entry when a later cleanup phase fails', async () => {+ const lateError = new Error('late finalize failed')+ const revocationId = 'late-finalize-revocation'+ const deps = makeDeps() as unknown as {+ pluginWorkloadSdkRevocationClient?: unknown+ }+ const revocationClient = {+ revoke: vi.fn().mockResolvedValue({+ state: 'revoking',+ revocationId,+ revoked: 1,+ fencedInvocations: 1,+ }),+ finalize: vi.fn().mockRejectedValue(lateError),+ }+ deps.pluginWorkloadSdkRevocationClient = revocationClient+ const reconciler = new WorkflowReconciler(deps as WorkflowReconcilerDeps)+ const legacyDeletes = () =>+ mockNetworkingApi.deleteNamespacedNetworkPolicy.mock.calls.filter(+ ([arg]) => arg.name === 'sdk-only-mcp-servers-egress-internet'+ )++ await expect(+ reconciler.cleanupPluginWorkloadSdk('sdk-only', {+ recipeUid: 'uid-sdk-only',+ recipeDeleted: true,+ })+ ).rejects.toBe(lateError)++ // Positive witnesses: the central legacy DELETE succeeded, and the later+ // revocation-finalization phase—not an earlier network failure—rejected.+ expect(legacyDeletes()).toHaveLength(1)+ expect(revocationClient.finalize).toHaveBeenCalledWith(+ sandboxNamespace,+ 'sdk-only',+ revocationId+ )++ // The old-UID retry is a direct process-local ledger probe, not a+ // Kubernetes recreation. It must hit the recorded gone-set, not send a+ // second DELETE after only partial cleanup completion.+ await expect(+ reconciler.retryLegacyMcpServersInternetEgressDelete('sdk-only', 'uid-sdk-only')+ ).resolves.toBe('removed')+ expect(legacyDeletes()).toHaveLength(1)+ })+it('defers absence reads until pod deletion completes so 404s cannot become unhandled rejections', async () => {const events: string[] = []crashRecoveryMocks.waitForPodDeletion.mockImplementationOnce(async () => {