Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
95 lines
4.3 KiB
JavaScript
95 lines
4.3 KiB
JavaScript
// API-Smoke-Test (ADR 0002 §5): Neuanlage, Laden, Update, Teilen-Lookup, Löschen gegen das echte Backend.
|
|
// Läuft pro konfiguriertem Backend: HB_TEST_POSTGRES_URL (postgres) bzw. HB_TEST_MONGODB_URI (mongo).
|
|
import os from 'os';
|
|
import { randomBytes } from 'crypto';
|
|
import supertest from 'supertest';
|
|
import pg from 'pg';
|
|
import mongoose from 'mongoose';
|
|
import { md5 } from 'hash-wasm';
|
|
import { makePatches, stringifyPatches } from '@sanity/diff-match-patch';
|
|
import createApp from '../../server/app.js';
|
|
import storage from '../../server/storage/index.js';
|
|
import packageJSON from '../../package.json' with { type: 'json' };
|
|
|
|
const backends = [
|
|
{ driver: 'postgres', url: process.env.HB_TEST_POSTGRES_URL },
|
|
{ driver: 'mongo', url: process.env.HB_TEST_MONGODB_URI }
|
|
];
|
|
|
|
const auth = `Basic ${Buffer.from('admin:password3').toString('base64')}`;
|
|
const version = { 'Homebrewery-Version': packageJSON.version };
|
|
|
|
for (const { driver, url } of backends) {
|
|
const suite = url ? describe : describe.skip;
|
|
if(!url) console.warn(`API-Smoke (${driver}): HB_TEST_${driver === 'postgres' ? 'POSTGRES_URL' : 'MONGODB_URI'} fehlt, übersprungen.`);
|
|
|
|
suite(`API-Smoke gegen ${driver}`, ()=>{
|
|
let request;
|
|
let cleanup;
|
|
|
|
beforeAll(async ()=>{
|
|
const name = `test_${randomBytes(4).toString('hex')}`;
|
|
const values = { hb_db_driver: driver, node_env: 'test', local_environments: ['local'] };
|
|
if(driver === 'postgres') {
|
|
const admin = new pg.Client({ connectionString: url });
|
|
await admin.connect();
|
|
await admin.query(`CREATE SCHEMA ${name}`);
|
|
// Schema per search_path der Verbindung: hängt die URL-Option an
|
|
values.hb_postgres_url = `${url}${url.includes('?') ? '&' : '?'}options=-c%20search_path%3D${name}`;
|
|
cleanup = async ()=>{ await storage.disconnect(); await admin.query(`DROP SCHEMA ${name} CASCADE`); await admin.end(); };
|
|
} else {
|
|
values.mongodb_uri = url;
|
|
cleanup = async ()=>{ await mongoose.connection.dropDatabase(); await storage.disconnect(); };
|
|
}
|
|
// Unter Jest scheitert der dynamische import('os') des Mongo-Treibers (Handshake ohne Metadaten)
|
|
const connect = mongoose.connect.bind(mongoose);
|
|
jest.spyOn(mongoose, 'connect').mockImplementation((u, o)=>connect(u, { ...o, dbName: name, runtimeAdapters: { os } }));
|
|
await storage.connect({ get: (key)=>values[key] });
|
|
request = supertest.agent(await createApp()).set('X-Forwarded-Proto', 'https');
|
|
});
|
|
afterAll(async ()=>{ await cleanup?.(); });
|
|
|
|
test('Neuanlage, Laden, Update, Löschen', async ()=>{
|
|
// Neuanlage
|
|
const created = await request.post('/api').set(version).send({ text: 'Hallo Smoke', title: 'Smoke Brew', tags: ['meta:theme'], renderer: 'V3' });
|
|
expect(created.status).toBe(200);
|
|
const { editId, shareId } = created.body;
|
|
expect(editId).toHaveLength(12);
|
|
expect(shareId).toHaveLength(12);
|
|
|
|
// Laden (Admin-Lookup per Edit- oder Share-ID)
|
|
const byEdit = await request.get(`/admin/lookup/${editId}`).set('Authorization', auth);
|
|
expect(byEdit.status).toBe(200);
|
|
expect(byEdit.body).toMatchObject({ title: 'Smoke Brew', shareId, editId });
|
|
const byShare = await request.get(`/admin/lookup/${shareId}`).set('Authorization', auth);
|
|
expect(byShare.body.shareId).toBe(shareId);
|
|
|
|
// Teilen: Theme-Bundle lädt den Brew über die Share-ID
|
|
const shared = await request.get(`/api/theme/V3/${shareId}`);
|
|
expect(shared.status).toBe(200);
|
|
|
|
// Update mit Patches (wie der Editor)
|
|
const loaded = byEdit.body;
|
|
const oldText = loaded.text.normalize('NFC');
|
|
const newText = `${oldText}\nZweite Zeile`;
|
|
const updated = await request.put(`/api/update/${editId}`).set(version).send({
|
|
...loaded,
|
|
text : newText,
|
|
hash : await md5(oldText),
|
|
patches : stringifyPatches(makePatches(encodeURI(oldText), encodeURI(newText)))
|
|
});
|
|
expect(updated.status).toBe(200);
|
|
expect(updated.body.version).toBe(2);
|
|
const reloaded = await request.get(`/admin/lookup/${editId}`).set('Authorization', auth);
|
|
expect(reloaded.body.version).toBe(2);
|
|
expect(reloaded.body.text).toContain('Zweite Zeile');
|
|
|
|
// Löschen (anonymer Brew ohne Autoren)
|
|
const removed = await request.get(`/api/remove/${editId}`).set(version);
|
|
expect(removed.status).toBe(204);
|
|
const gone = await request.get(`/admin/lookup/${editId}`).set('Authorization', auth);
|
|
expect(gone.body.shareId).toBeUndefined(); // Fehlerseite statt Brew
|
|
});
|
|
});
|
|
}
|