import { it, expect } from 'vitest'; import { execFile } from 'node:child_process'; import { mkdtemp, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { promisify } from 'node:util'; import { branchesIn, installHooks, issueGitToken, verifyGitToken } from '../src/service.js'; // Runs without opening a port: the push goes over Git's local transport, which runs the same hook. const run = promisify(execFile); const git = (cwd: string, args: string[], env: Record = {}) => run('git', ['-c', 'user.name=Fixture', '-c', 'user.email=fixture@example.invalid', ...args], { cwd, env: { ...process.env, ...env }, }).then(result => result.stdout.trim()); it('lets a credential scoped to ide/ push only ide/ branches, and lists them', async () => { const root = await mkdtemp(path.join(os.tmpdir(), 'wishforge-git-hooks-')); try { const hooks = await installHooks(root); const repository = path.join(root, 'project.git'); await git(root, ['init', '--quiet', '--bare', '--initial-branch=main', repository]); // Over HTTP the service sets core.hooksPath for each request; a local push reads it from the repository. await git(repository, ['config', 'core.hooksPath', hooks]); const clone = path.join(root, 'clone'); await git(root, ['clone', '--quiet', repository, clone]); await writeFile(path.join(clone, 'a.txt'), 'a\n'); await git(clone, ['add', 'a.txt']); await git(clone, ['commit', '--quiet', '-m', 'A']); // Without a scope (the platform's own integration pushes) any branch may be updated. await git(clone, ['push', '--quiet', 'origin', 'HEAD:refs/heads/main']); const integrated = await git(repository, ['rev-parse', 'main']); await writeFile(path.join(clone, 'b.txt'), 'b\n'); await git(clone, ['add', 'b.txt']); await git(clone, ['commit', '--quiet', '-m', 'B']); const ide = { WISHFORGE_REF_PREFIX: 'refs/heads/ide/' }; await expect(git(clone, ['push', 'origin', 'HEAD:refs/heads/main'], ide)).rejects.toThrow( /只能推送 ide\/ 开头的分支/, ); expect(await git(repository, ['rev-parse', 'main'])).toBe(integrated); await git(clone, ['push', '--quiet', 'origin', 'HEAD:refs/heads/ide/work'], ide); expect(await branchesIn(repository, 'refs/heads/ide/')).toEqual([ { name: 'ide/work', commit: await git(clone, ['rev-parse', 'HEAD']), updatedAt: expect.any(String) }, ]); await expect(branchesIn(repository, 'refs/heads/')).rejects.toMatchObject({ code: 'INVALID_REF' }); } finally { await rm(root, { recursive: true, force: true }); } }); it('carries the push scope in the signed token and refuses a malformed one', () => { const secret = 's'.repeat(32); const claims = { aud: 'wishforge-git' as const, tenantId: 't', projectId: 'p', permission: 'write' as const }; const scoped = issueGitToken({ ...claims, iat: 100, exp: 400, refPrefix: 'refs/heads/ide/' }, secret); expect(verifyGitToken(scoped, secret, { tenantId: 't', projectId: 'p', write: true }, 200).refPrefix).toBe( 'refs/heads/ide/', ); const broad = issueGitToken({ ...claims, iat: 100, exp: 400, refPrefix: 'refs/heads/' }, secret); expect(() => verifyGitToken(broad, secret, { tenantId: 't', projectId: 'p', write: true }, 200)).toThrow(); });