Files
homebrewery-oidc/server/storage/mongo/storage.spec.js
T
dev-bot BotandClaude Sonnet 5.5 a55c403527 feat(storage): Repository-Schicht für Brews/Notifications, PostgreSQL-18-Backend (Kysely) und Contract-Suite
HB_DB_DRIVER=mongo|postgres, versionierte Migrationen beim Start, Mongo-Backend ruft unverändert die Upstream-Modelle.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 10:16:55 +00:00

48 lines
2.4 KiB
JavaScript

import os from 'os';
import mongoose from 'mongoose';
import { randomBytes } from 'crypto';
import { createUserRepository } from './users.js';
import { createBrewRepository } from './brews.js';
import { createNotificationRepository } from './notifications.js';
import { model as HomebrewModel } from '../../homebrew.model.js';
import { model as NotificationModel } from '../../notifications.model.js';
import { runUserRepositoryContract } from '../contract/users.contract.js';
import { runBrewRepositoryContract, runNotificationRepositoryContract } from '../contract/brews.contract.js';
const uri = process.env.HB_TEST_MONGODB_URI;
const suite = uri ? describe : describe.skip;
// Unter Jest scheitert der dynamische import('os') des Mongo-Treibers; ohne Client-Metadaten lehnt der Server den Handshake ab.
const driverOptions = { runtimeAdapters: { os } };
if(!uri) console.warn('HB_TEST_MONGODB_URI fehlt: Mongo-Storage-Contract-Tests werden übersprungen.');
suite('Mongo UserRepository (Contract)', ()=>{
runUserRepositoryContract(async ()=>{
const dbName = `test_${randomBytes(4).toString('hex')}`;
const connection = await mongoose.createConnection(uri, { dbName, ...driverOptions }).asPromise();
const users = createUserRepository(connection);
await users.Model.init();
return { users, cleanup: async ()=>{ await connection.dropDatabase(); await connection.close(); } };
});
});
// Brews und Notifications laufen über die globalen Upstream-Modelle (mongoose.connect).
const connectGlobal = async ()=>{
const dbName = `test_${randomBytes(4).toString('hex')}`;
await mongoose.connect(uri, { dbName, ...driverOptions });
// Nicht Model.init()/syncIndexes(): Upstream definiert `{ lock: 1, sparse: true }`, das neuere Mongo-Server ablehnen.
await HomebrewModel.collection.createIndex({ shareId: 1 }, { unique: true });
await HomebrewModel.collection.createIndex({ editId: 1 }, { unique: true });
await HomebrewModel.collection.createIndex({ published: 1, title: 'text' });
await NotificationModel.init();
return async ()=>{ await mongoose.connection.dropDatabase(); await mongoose.disconnect(); };
};
suite('Mongo BrewRepository (Contract)', ()=>{
runBrewRepositoryContract(async ()=>({ brews: createBrewRepository(), cleanup: await connectGlobal() }));
});
suite('Mongo NotificationRepository (Contract)', ()=>{
runNotificationRepositoryContract(async ()=>({ notifications: createNotificationRepository(), cleanup: await connectGlobal() }));
});