// Contract-Suite für gitConnections und brewGitLinks (ADR 0002 §5, ADR 0003 §2). // Aufruf: runGitRepositoryContract(async ()=>({ users, brews, gitConnections, brewGitLinks, cleanup })) export const runGitRepositoryContract = (createRepos)=>{ let r; let alice; let bob; let conn; let brew; beforeAll(async ()=>{ r = await createRepos(); alice = await r.users.create({ username: 'alice-git' }); bob = await r.users.create({ username: 'bob-git' }); brew = await r.brews.create({ title: 'Git-Brew', text: 'Hallo', authors: ['alice-git'] }); }); afterAll(async ()=>{ await r.cleanup?.(); }); test('Verbindung anlegen: Standardwerte, plain object, Token nur als Chiffrat', async ()=>{ conn = await r.gitConnections.create({ ownerUserId: alice.id, label: 'Gitea', repoUrl: 'https://git.example.test/a/b.git', tokenEnc: 'v1.x.y.z.t' }); expect(conn.id).toEqual(expect.any(String)); expect(conn).toMatchObject({ ownerUserId: alice.id, label: 'Gitea', authUsername: 'git', defaultBranch: 'main', brewDir: 'brews', assetsDir: 'assets', tokenEnc: 'v1.x.y.z.t', lastVerifiedAt: null }); expect(conn.constructor).toBe(Object); }); test('Besitz: findByIdForOwner mit fremder userId liefert null, listByOwner nur eigene', async ()=>{ expect((await r.gitConnections.findByIdForOwner(conn.id, alice.id)).id).toBe(conn.id); expect(await r.gitConnections.findByIdForOwner(conn.id, bob.id)).toBeNull(); expect(await r.gitConnections.findByIdForOwner('unbekannt', alice.id)).toBeNull(); expect((await r.gitConnections.listByOwner(alice.id)).map((c)=>c.id)).toEqual([conn.id]); expect(await r.gitConnections.listByOwner(bob.id)).toEqual([]); expect(await r.gitConnections.findById('unbekannt')).toBeNull(); }); test('update ändert Felder und updatedAt, nie den Besitzer', async ()=>{ const upd = await r.gitConnections.update(conn.id, { label: 'Neu', ownerUserId: bob.id, lastVerifiedAt: new Date(), defaultBranch: 'dev' }); expect(upd).toMatchObject({ label: 'Neu', ownerUserId: alice.id, defaultBranch: 'dev' }); expect(upd.lastVerifiedAt).toBeInstanceOf(Date); expect(await r.gitConnections.update('unbekannt', { label: 'x' })).toBeNull(); }); test('Link: upsert, find, (Verbindung, Branch, Pfad) ist eindeutig', async ()=>{ const link = await r.brewGitLinks.upsert({ brewId: brew.id, connectionId: conn.id, branch: 'main', path: 'brews/a.md', lastCommitSha: 'c1', lastBlobSha: 'b1', lastSyncedAt: new Date() }); expect(link).toMatchObject({ brewId: brew.id, connectionId: conn.id, branch: 'main', path: 'brews/a.md', lastCommitSha: 'c1', lastBlobSha: 'b1', lastError: null }); expect((await r.brewGitLinks.findByBrewId(brew.id)).path).toBe('brews/a.md'); expect((await r.brewGitLinks.findByLocation(conn.id, 'main', 'brews/a.md')).brewId).toBe(brew.id); expect(await r.brewGitLinks.findByLocation(conn.id, 'main', 'andere.md')).toBeNull(); const other = await r.brews.create({ title: 'Zweiter', text: 'x', authors: ['alice-git'] }); await expect(r.brewGitLinks.upsert({ brewId: other.id, connectionId: conn.id, branch: 'main', path: 'brews/a.md' })).rejects.toBeDefined(); await r.brewGitLinks.upsert({ brewId: other.id, connectionId: conn.id, branch: 'main', path: 'brews/b.md' }); expect((await r.brewGitLinks.listByConnection(conn.id)).length).toBe(2); await r.brews.deleteById(other.id); }); test('upsert ersetzt den Link des Brews (höchstens einer pro Brew)', async ()=>{ await r.brewGitLinks.upsert({ brewId: brew.id, connectionId: conn.id, branch: 'dev', path: 'brews/a.md' }); expect(await r.brewGitLinks.findByLocation(conn.id, 'main', 'brews/a.md')).toBeNull(); expect((await r.brewGitLinks.findByBrewId(brew.id)).branch).toBe('dev'); expect((await r.brewGitLinks.listByConnection(conn.id)).length).toBe(1); }); test('update des Links setzt SHA und Fehlercode', async ()=>{ const upd = await r.brewGitLinks.update(brew.id, { lastCommitSha: 'c2', lastBlobSha: 'b2', lastError: 'G04' }); expect(upd).toMatchObject({ lastCommitSha: 'c2', lastBlobSha: 'b2', lastError: 'G04' }); expect((await r.brewGitLinks.update(brew.id, { lastError: null })).lastError).toBeNull(); expect(await r.brewGitLinks.update('unbekannt', { lastError: 'x' })).toBeNull(); }); test('Rename eines Autors lässt Verbindungen und Links unverändert', async ()=>{ await r.brews.renameAuthor('alice-git', 'alice-neu'); expect((await r.gitConnections.listByOwner(alice.id)).map((c)=>c.id)).toEqual([conn.id]); expect((await r.brewGitLinks.findByBrewId(brew.id)).connectionId).toBe(conn.id); }); test('Brew löschen entfernt den Link, Verbindung bleibt', async ()=>{ const tmp = await r.brews.create({ title: 'Tmp', text: 'x', authors: ['alice-git'] }); await r.brewGitLinks.upsert({ brewId: tmp.id, connectionId: conn.id, branch: 'main', path: 'brews/tmp.md' }); expect(await r.brews.deleteById(tmp.id)).toBe(true); expect(await r.brewGitLinks.findByBrewId(tmp.id)).toBeNull(); const tmp2 = await r.brews.create({ title: 'Tmp2', text: 'x', authors: ['alice-git'], editId: 'tmp2editid12' }); await r.brewGitLinks.upsert({ brewId: tmp2.id, connectionId: conn.id, branch: 'main', path: 'brews/tmp2.md' }); expect(await r.brews.deleteByEditId('tmp2editid12')).toBe(true); expect(await r.brewGitLinks.findByBrewId(tmp2.id)).toBeNull(); expect(await r.gitConnections.findById(conn.id)).not.toBeNull(); }); test('Verbindung löschen entfernt ihre Links, der Brew bleibt', async ()=>{ const c2 = await r.gitConnections.create({ ownerUserId: bob.id, label: 'B', repoUrl: 'https://git.example.test/b/c.git', tokenEnc: 't' }); const b2 = await r.brews.create({ title: 'B2', text: 'x', authors: ['bob-git'] }); await r.brewGitLinks.upsert({ brewId: b2.id, connectionId: c2.id, branch: 'main', path: 'x.md' }); expect(await r.gitConnections.delete(c2.id)).toBe(true); expect(await r.gitConnections.delete(c2.id)).toBe(false); expect(await r.brewGitLinks.findByBrewId(b2.id)).toBeNull(); expect(await r.brews.findByEditId(b2.editId)).not.toBeNull(); }); test('Benutzer löschen entfernt Verbindungen und Links, Brews bleiben', async ()=>{ expect(await r.users.delete(alice.id)).toBe(true); expect(await r.gitConnections.findById(conn.id)).toBeNull(); expect(await r.brewGitLinks.findByBrewId(brew.id)).toBeNull(); expect(await r.brews.findByEditId(brew.editId)).not.toBeNull(); }); };