import assert from 'node:assert/strict'; import test from 'node:test'; import { createPostgresUserDataSpaceService } from './postgres-user-data-space-service.mjs'; const USER_ID = '18a143d1-3ac5-42b3-a4af-d07f8445bce6'; test('existing PostgreSQL user spaces reconcile agent SET permission before tenant access', async () => { const queries = []; const client = { async query(sql) { queries.push(String(sql)); if (String(sql).includes('SELECT migration_state FROM mindspace_control.user_spaces')) { return { rows: [{ migration_state: 'cutover' }] }; } if (String(sql).includes('FROM information_schema.columns')) return { rows: [] }; return { rows: [] }; }, release() {}, }; const service = createPostgresUserDataSpaceService({ userId: USER_ID, pgPool: { connect: async () => client }, }); await service.getSchema(); const reconcileIndex = queries.findIndex((sql) => ( sql.includes("r.rolname = 'ms_u_18a143d13ac5_agent'") && sql.includes('WITH INHERIT FALSE, SET TRUE') )); const setRoleIndex = queries.findIndex((sql) => sql.includes('SET LOCAL ROLE "ms_u_18a143d13ac5_agent"')); assert.ok(reconcileIndex >= 0, 'existing space must reconcile the agent role membership'); assert.ok(setRoleIndex > reconcileIndex, 'role reconciliation must happen before SET LOCAL ROLE'); });