import { randomBytes } from 'crypto'; import pg from 'pg'; import { createPostgresStorage } from './index.js'; import { runUserRepositoryContract } from '../contract/users.contract.js'; import { runGitRepositoryContract } from '../contract/git.contract.js'; import { runAssetRepositoryContract } from '../contract/assets.contract.js'; import { runBrewRepositoryContract, runNotificationRepositoryContract } from '../contract/brews.contract.js'; const url = process.env.HB_TEST_POSTGRES_URL; const suite = url ? describe : describe.skip; if(!url) console.warn('HB_TEST_POSTGRES_URL fehlt: Postgres-Storage-Contract-Tests werden übersprungen.'); // Jede Suite bekommt ein frisches Schema test_ und räumt danach auf. const createStorage = async ()=>{ const schema = `test_${randomBytes(4).toString('hex')}`; const admin = new pg.Client({ connectionString: url }); await admin.connect(); await admin.query(`CREATE SCHEMA ${schema}`); const storage = createPostgresStorage(undefined, { url, searchPath: schema, poolMax: 3 }); await storage.connect(); return { storage, cleanup : async ()=>{ await storage.disconnect(); await admin.query(`DROP SCHEMA ${schema} CASCADE`); await admin.end(); } }; }; suite('Postgres UserRepository (Contract)', ()=>{ runUserRepositoryContract(async ()=>{ const { storage, cleanup } = await createStorage(); return { users: storage.users, cleanup }; }); }); suite('Postgres BrewRepository (Contract)', ()=>{ runBrewRepositoryContract(async ()=>{ const { storage, cleanup } = await createStorage(); return { brews: storage.brews, cleanup }; }); }); suite('Postgres NotificationRepository (Contract)', ()=>{ runNotificationRepositoryContract(async ()=>{ const { storage, cleanup } = await createStorage(); return { notifications: storage.notifications, cleanup }; }); }); suite('Postgres Git-Repositories (Contract)', ()=>{ runGitRepositoryContract(async ()=>{ const { storage, cleanup } = await createStorage(); return { users: storage.users, brews: storage.brews, gitConnections: storage.gitConnections, brewGitLinks: storage.brewGitLinks, cleanup }; }); }); suite('Postgres Asset-Repository (Contract)', ()=>{ runAssetRepositoryContract(async ()=>{ const { storage, cleanup } = await createStorage(); return { brews: storage.brews, brewAssets: storage.brewAssets, cleanup }; }); }); suite('Postgres Verbindung und Migrationen', ()=>{ test('Start ohne HB_POSTGRES_URL bricht mit klarer Meldung ab', async ()=>{ const storage = createPostgresStorage({ get: ()=>undefined }); await expect(storage.connect()).rejects.toThrow(/HB_POSTGRES_URL/); }); test('Migrationen sind idempotent, isReady folgt connect/disconnect', async ()=>{ const { storage, cleanup } = await createStorage(); expect(storage.isReady()).toBe(true); await cleanup(); expect(storage.isReady()).toBe(false); }); }); suite('Postgres Pool-Fehler', ()=>{ it('bleibt nach einem Idle-Client-Fehler bereit', async ()=>{ const { storage, cleanup } = await createStorage(); const spy = jest.spyOn(console, 'error').mockImplementation(()=>{}); try { expect(storage.isReady()).toBe(true); storage.pool.emit('error', new Error('Verbindung verloren')); expect(storage.isReady()).toBe(true); expect(spy).toHaveBeenCalled(); } finally { spy.mockRestore(); await cleanup(); } }); });