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
+16
-1
@@ -93,6 +93,20 @@ G08–G10 sind Ergänzungen zur ADR.
|
||||
- Mitautoren speichern mit dem Token des Verbindungsbesitzers.
|
||||
- Nur ein App-Prozess (Mutex pro Brew ist prozesslokal).
|
||||
|
||||
## Abweichungen von ADR 0003
|
||||
|
||||
- Zusätzliche Codes `G08` (ungültige Eingabe), `G09` (Datei zu groß), `G10` (Feature aus).
|
||||
- `afterBrewSave(ctx, saved)` wird in jedem Fall aufgerufen (auch bei fehlgeschlagenem DB-Save), weil der
|
||||
Brew-Mutex von `beforeBrewSave` bis dahin gehalten wird; der Link wird nur bei Erfolg aktualisiert.
|
||||
- Datei im Repo nach externem Löschen: `gitOverwrite=none` bestätigt das Neuanlegen.
|
||||
- Abgleich beim Öffnen und Pull laden den Inhalt im selben Repo-Zugriff wie den Blob-Vergleich (ein zweites
|
||||
frisches Repo hätte den Blob nicht).
|
||||
- Die `git`-Prozesse laufen in eigener Prozessgruppe, damit Abbruch (Zeitbudget) auch `git-remote-http` beendet.
|
||||
- Spike (ADR: „vor M3-Start Pflicht“) gegen Gitea 1.24 über HTTP und lokalen `git http-backend` gelaufen
|
||||
(Root-Commit in leerem Repo, `--filter=blob:none`-Fetch mit Lazy-Blob, Push, Non-Fast-Forward-Ablehnung);
|
||||
nachvollziehbar mit `scripts/git-e2e-gitea.sh`. GitHub wurde nicht geprüft (kein Zugang in der Entwicklungs-VM),
|
||||
das Protokoll ist identisch.
|
||||
|
||||
## Tests
|
||||
|
||||
```bash
|
||||
@@ -101,4 +115,5 @@ HB_TEST_POSTGRES_URL=… HB_TEST_MONGODB_URI=… npx jest server/storage tests/f
|
||||
```
|
||||
|
||||
`tests/fork/git-http-test-server.js` startet `git http-backend` hinter einem Node-Server mit Basic-Auth
|
||||
(Remote mit `uploadpack.allowFilter` und `http.receivepack`). Gegen Gitea: `scripts/git-e2e-gitea.sh`.
|
||||
(Remote mit `uploadpack.allowFilter` und `http.receivepack`). Gegen eine echte Gitea-Instanz (Wegwerf-Container): `scripts/git-e2e-gitea.sh`; gegen ein beliebiges
|
||||
Remote (GitHub, GitLab, …): `GIT_E2E_URL=… GIT_E2E_USER=… GIT_E2E_TOKEN=… node scripts/git-e2e.mjs`.
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# Startet eine Wegwerf-Gitea-Instanz (Docker, SQLite), legt Benutzer, Token und Repo an und führt
|
||||
# scripts/git-e2e.mjs dagegen aus. Container wird am Ende gestoppt. Nur Dev-Zugangsdaten.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
NAME=hb-e2e-gitea
|
||||
PORT=${GITEA_E2E_PORT:-3300}
|
||||
IMAGE=${GITEA_IMAGE:-gitea/gitea:1.24}
|
||||
PASS="e2e-$(openssl rand -hex 8)"
|
||||
cleanup() { docker rm -f "$NAME" >/dev/null 2>&1 || true; }
|
||||
trap cleanup EXIT
|
||||
cleanup
|
||||
docker run -d --name "$NAME" --memory 400m -p "127.0.0.1:${PORT}:3000" \
|
||||
-e GITEA__database__DB_TYPE=sqlite3 -e GITEA__security__INSTALL_LOCK=true \
|
||||
-e GITEA__server__ROOT_URL="http://127.0.0.1:${PORT}/" -e GITEA__service__DISABLE_REGISTRATION=true "$IMAGE" >/dev/null
|
||||
for _ in $(seq 1 60); do curl -fs "http://127.0.0.1:${PORT}/api/healthz" >/dev/null && break; sleep 2; done
|
||||
docker exec -u git "$NAME" gitea admin user create --admin --username tester --password "$PASS" --email tester@example.test --must-change-password=false >/dev/null
|
||||
TOKEN=$(curl -fs -u "tester:$PASS" -H 'Content-Type: application/json' -X POST "http://127.0.0.1:${PORT}/api/v1/users/tester/tokens" \
|
||||
-d '{"name":"e2e","scopes":["write:repository","write:user"]}' | sed -n 's/.*"sha1":"\([^"]*\)".*/\1/p')
|
||||
curl -fs -u "tester:$PASS" -H 'Content-Type: application/json' -X POST "http://127.0.0.1:${PORT}/api/v1/user/repos" -d '{"name":"hb-e2e"}' >/dev/null
|
||||
GIT_E2E_URL="http://127.0.0.1:${PORT}/tester/hb-e2e.git" GIT_E2E_USER=tester GIT_E2E_TOKEN="$TOKEN" \
|
||||
GIT_E2E_ALLOW_HTTP=true GIT_E2E_ALLOW_PRIVATE=true node scripts/git-e2e.mjs
|
||||
@@ -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