test(git): E2E-Skripte gegen Gitea/beliebiges Remote, Doku zu Abweichungen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e40b4d1499
commit
e8d6e215e8
3 files changed
+142
-1
No files matched your search
@@ -0,0 +1,104 @@
|
||||
// E2E-Prüfung des Git-Backends gegen ein echtes Remote (Gitea, Forgejo, GitHub, GitLab, lokales Remote).
|
||||
// Nutzung: GIT_E2E_URL=https://host/owner/repo.git GIT_E2E_USER=name GIT_E2E_TOKEN=… node scripts/git-e2e.mjs
|
||||
// (lokale/private Ziele: GIT_E2E_ALLOW_PRIVATE=true, http://: GIT_E2E_ALLOW_HTTP=true). Das Repo darf leer sein.
|
||||
// Es werden nur Dateien unter hb-e2e-<zeit>/ angelegt. Der Token wird nie ausgegeben.
|
||||
import crypto from 'crypto';
|
||||
import os from 'os';
|
||||
import api from '../server/homebrew.api.js';
|
||||
import { splitTextStyleAndMetadata } from '../shared/helpers.js';
|
||||
import { readGitConfig } from '../server/fork/git/config.js';
|
||||
import { createGitService } from '../server/fork/git/brewSync.js';
|
||||
import { createMemoryStorage } from '../tests/fork/git-memory-storage.js';
|
||||
|
||||
const { GIT_E2E_URL: url, GIT_E2E_USER: user = 'git', GIT_E2E_TOKEN: token } = process.env;
|
||||
if(!url || !token) { console.error('GIT_E2E_URL und GIT_E2E_TOKEN setzen.'); process.exit(2); }
|
||||
|
||||
const cfg = { ...readGitConfig({ HB_GIT_TOKEN_KEY: crypto.randomBytes(32).toString('base64'), HB_PUBLIC_URL: 'https://hb.example.test', HB_GIT_WORKDIR: `${os.tmpdir()}/hb-git-e2e` }),
|
||||
allowHttp: process.env.GIT_E2E_ALLOW_HTTP === 'true', allowPrivate: process.env.GIT_E2E_ALLOW_PRIVATE === 'true', timeoutMs: 60000 };
|
||||
const storage = createMemoryStorage();
|
||||
const quiet = { warn: (...a)=>console.log(' [warn]', ...a), error: console.error, log: ()=>{} };
|
||||
const svc = createGitService({ cfg, getStorage: ()=>storage, api, splitTextStyleAndMetadata, logger: quiet });
|
||||
|
||||
let failed = 0;
|
||||
const step = async (name, fn)=>{
|
||||
process.stdout.write(`- ${name} … `);
|
||||
try { const r = await fn(); console.log('ok'); return r; } catch (err) { failed++; console.log(`FEHLER ${err.HBErrorCode ?? ''} ${err.message}`); }
|
||||
};
|
||||
const expectCode = async (code, fn)=>{
|
||||
try { await fn(); } catch (err) { if(err.HBErrorCode === code) return err; throw new Error(`erwartet ${code}, erhalten ${err.HBErrorCode ?? err.message}`); }
|
||||
throw new Error(`erwartet ${code}, aber kein Fehler`);
|
||||
};
|
||||
const assert = (cond, msg)=>{ if(!cond) throw new Error(msg); };
|
||||
|
||||
const dir = `hb-e2e-${Date.now()}`;
|
||||
const alice = await storage.users.create({ username: 'e2e', displayName: 'E2E Tester' });
|
||||
const account = { username: 'e2e', uid: alice.id };
|
||||
const author = { name: 'E2E Tester', email: 'e2e@users.noreply.hb.example.test' };
|
||||
|
||||
const conn = await step('Verbindung anlegen und testen', async ()=>{
|
||||
const c = await svc.createConnection(account, { label: 'e2e', repoUrl: url, authUsername: user, token });
|
||||
assert(c.hasToken && !JSON.stringify(c).includes(token), 'Token in Antwort');
|
||||
const t = await svc.testConnection(account, c.id);
|
||||
console.log(`(Standard-Branch ${t.defaultBranch}, ${t.branchCount} Branches) `);
|
||||
return c;
|
||||
});
|
||||
if(!conn) process.exit(1);
|
||||
const stored = await storage.gitConnections.findById(conn.id);
|
||||
|
||||
const brew = await step('Brew anlegen (Commit)', ()=>svc.createBrew(account, { connectionId: conn.id, path: `${dir}/a.md`, brew: { title: 'E2E Brew', text: 'Version 1', renderer: 'V3' } }));
|
||||
const save = async (text, query = {}, who = account)=>{
|
||||
const current = await storage.brews.findByEditId(brew.editId);
|
||||
const res = { code: undefined, status(c) { this.code = c; return this; }, json(b) { this.body = b; return this; } };
|
||||
const ctx = await svc.beforeBrewSave({ brew: { ...current, text }, req: { query, account: who }, res });
|
||||
if(ctx) await svc.afterBrewSave(ctx, await storage.brews.update({ ...current, text, version: current.version + 1 }));
|
||||
return { ctx, res };
|
||||
};
|
||||
const link = ()=>storage.brewGitLinks.findByBrewId(brew.id);
|
||||
|
||||
await step('Speichern erzeugt neuen Commit', async ()=>{
|
||||
const before = (await link()).lastCommitSha;
|
||||
const { ctx } = await save('Version 2');
|
||||
assert(ctx?.git?.commit && ctx.git.commit !== before, 'kein neuer Commit');
|
||||
});
|
||||
await step('Laden holt den Remote-Stand (Pull)', async ()=>{
|
||||
const l = await link();
|
||||
const conn2 = await storage.gitConnections.findById(conn.id);
|
||||
const state = await svc.readRemote(conn2, { branch: l.branch, filePath: l.path });
|
||||
assert(state.content.includes('Version 2'), 'Inhalt fehlt');
|
||||
assert(state.blob === l.lastBlobSha, 'Blob weicht ab');
|
||||
});
|
||||
await step('Fremder Commit auf derselben Datei → Konflikt G01', async ()=>{
|
||||
const l = await link();
|
||||
await svc.commitContent(await storage.gitConnections.findById(conn.id), { branch: l.branch, filePath: l.path, content: 'Extern geändert', expectedBlob: l.lastBlobSha, author: { name: 'Extern', email: 'x@example.test' } });
|
||||
const { ctx, res } = await save('Meine Version');
|
||||
assert(ctx === null && res.code === 409 && res.body.HBErrorCode === 'G01', 'kein Konflikt');
|
||||
globalThis.remoteBlob = res.body.remoteBlobSha;
|
||||
});
|
||||
await step('„Meine Version behalten“ (gitOverwrite) schreibt normalen Commit', async ()=>{
|
||||
const { ctx, res } = await save('Meine Version', { gitOverwrite: globalThis.remoteBlob });
|
||||
assert(ctx?.git && res.code === undefined, 'Overwrite fehlgeschlagen');
|
||||
});
|
||||
await step('Pull übernimmt externe Änderung in den Snapshot', async ()=>{
|
||||
const l = await link();
|
||||
await svc.commitContent(await storage.gitConnections.findById(conn.id), { branch: l.branch, filePath: l.path, content: '```metadata\ntitle: Extern 2\n```\n\nVon außen\n', expectedBlob: l.lastBlobSha, author });
|
||||
await svc.pull(account, brew.editId);
|
||||
const b = await storage.brews.findByEditId(brew.editId);
|
||||
assert(b.title === 'Extern 2' && b.text.includes('Von außen'), 'Snapshot nicht aktualisiert');
|
||||
});
|
||||
await step('Neuer Branch in nicht leerem Repo', async ()=>{
|
||||
const b2 = await svc.createBrew(account, { connectionId: conn.id, branch: `hb-e2e-${Date.now()}`, path: `${dir}/b.md`, brew: { title: 'Branch Brew', text: 'x' } });
|
||||
assert(b2.editId, 'kein Brew');
|
||||
});
|
||||
await step('Dateiliste und Import', async ()=>{
|
||||
const files = await svc.listFiles(account, conn.id, { branch: (await link()).branch, dir });
|
||||
assert(files.files.includes(`${dir}/a.md`), 'Datei fehlt in der Liste');
|
||||
const imported = await svc.importBrew(account, { connectionId: conn.id, branch: (await link()).branch, path: `${dir}/a.md` }).catch((e)=>e);
|
||||
assert(imported.HBErrorCode === 'G06', 'Doppel-Import nicht abgelehnt');
|
||||
});
|
||||
await step('Falscher Token → G02 ohne Token in der Meldung', async ()=>{
|
||||
await storage.gitConnections.update(conn.id, { tokenEnc: (await import('../server/fork/git/crypto.js')).createTokenCrypto({ key: cfg.key }).encrypt('falsch-xyz', conn.id) });
|
||||
const err = await expectCode('G02', ()=>svc.testConnection(account, conn.id));
|
||||
assert(!JSON.stringify(err.toBody()).includes(token) && !JSON.stringify(err.toBody()).includes('falsch-xyz'), 'Token in Fehlermeldung');
|
||||
});
|
||||
console.log(failed ? `\n${failed} Schritt(e) fehlgeschlagen` : `\nAlles ok (Dateien unter ${dir}/)`);
|
||||
process.exit(failed ? 1 : 0);
|
||||
Reference in new issue
Block a user