import test from 'node:test' import assert from 'node:assert/strict' import { PassThrough, Readable } from 'node:stream' import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { runGit, runGitText, rewriteFastExportRefs } from '../src/git.js' import { runRemoteHelper } from '../src/helper.js' import { loadLatestSnapshot, saveSnapshot } from '../src/store.js' async function makeTempDir(prefix) { return mkdtemp(join(tmpdir(), prefix)) } function createMemoryPds() { const records = [] const blobs = [] return { collectionPrefix: 'org.latha.gitproxy', snapshotCollection: 'org.latha.gitproxy.snapshot', chunkCollection: 'org.latha.gitproxy.chunk', async uploadBlob(buffer, { contentType = 'application/octet-stream' } = {}) { const bytes = Buffer.from(buffer) const cid = `cid-${blobs.length + 1}` const blob = { $type: 'blob', ref: { $link: cid }, mimeType: contentType, size: bytes.length, } blobs.push({ cid, bytes, blob }) return blob }, async getBlob(cid) { const entry = blobs.find((item) => item.cid === cid) if (!entry) { throw new Error(`missing blob ${cid}`) } return Buffer.from(entry.bytes) }, async createRecord(collection, record, rkey) { const entry = { collection, record, rkey, uri: `at://did:example:alice/${collection}/${rkey ?? `r${records.length + 1}`}`, cid: `cid-${records.length + 1}`, } records.push(entry) return { uri: entry.uri, cid: entry.cid, validationStatus: 'unknown' } }, async listAllRecords(collection) { return records .filter((record) => record.collection === collection) .map((record) => ({ uri: record.uri, cid: record.cid, value: record.record })) }, records, blobs, } } async function runHelperConversation({ commands, cwd, repoId = 'demo', handle = 'alice.example', pds }) { const stdin = Readable.from([commands]) const stdout = new PassThrough() const stderr = new PassThrough() const outChunks = [] const errChunks = [] stdout.on('data', (chunk) => outChunks.push(Buffer.from(chunk))) stderr.on('data', (chunk) => errChunks.push(Buffer.from(chunk))) await runRemoteHelper({ stdin, stdout, stderr, env: { ...process.env, GIT_DIR: join(cwd, '.git'), }, argv: ['origin', `atgit://${handle}/${repoId}`], createPdsClient: () => pds, }) return { stdout: Buffer.concat(outChunks).toString('utf8'), stderr: Buffer.concat(errChunks).toString('utf8'), } } function stripLines(text) { return text .split('\n') .map((line) => line.trim()) .filter(Boolean) } test('rewriteFastExportRefs preserves binary payloads', () => { const stream = Buffer.concat([ Buffer.from('commit refs/heads/main\nmark :1\ndata 3\n', 'utf8'), Buffer.from([0x61, 0x62, 0x63]), Buffer.from('\nreset refs/heads/main\n', 'utf8'), ]) const rewritten = rewriteFastExportRefs(stream, new Map([['refs/heads/main', 'refs/heads/trunk']])) assert.match(rewritten.toString('utf8'), /commit refs\/heads\/trunk/) assert.match(rewritten.toString('utf8'), /reset refs\/heads\/trunk/) assert.ok(rewritten.includes(Buffer.from('abc'))) }) test('saveSnapshot and loadLatestSnapshot round-trip', async () => { const pds = createMemoryPds() const stream = Buffer.from('commit refs/heads/main\nmark :1\ndata 4\ntest\n', 'utf8') const manifest = await saveSnapshot({ pds, repoId: 'demo', refs: [{ source: 'refs/heads/main', target: 'refs/heads/main', oid: 'abcd1234' }], stream, }) assert.equal(manifest.repoId, 'demo') assert.equal(manifest.blob.$type, 'blob') assert.equal(manifest.blob.mimeType, 'application/octet-stream') assert.equal(manifest.blob.size, stream.length) const loaded = await loadLatestSnapshot({ pds, repoId: 'demo' }) assert.ok(loaded) assert.equal(loaded.manifest.snapshotId, manifest.snapshotId) assert.equal(loaded.stream.toString('utf8'), stream.toString('utf8')) }) test('remote helper push and fetch use injected oauth client', async () => { const pds = createMemoryPds() const sourceDir = await makeTempDir('gitproxy-src-') const fetchDir = await makeTempDir('gitproxy-fetch-') try { await runGit(['init'], { cwd: sourceDir }) await runGit(['checkout', '-b', 'main'], { cwd: sourceDir }) await runGit(['config', 'user.name', 'Test User'], { cwd: sourceDir }) await runGit(['config', 'user.email', 'test@example.com'], { cwd: sourceDir }) await writeFile(join(sourceDir, 'hello.txt'), 'hello from aproto\n') await runGit(['add', 'hello.txt'], { cwd: sourceDir }) await runGit(['commit', '-m', 'initial'], { cwd: sourceDir }) const commitOid = (await runGitText(['rev-parse', 'HEAD'], { cwd: sourceDir })).trim() const pushInput = 'capabilities\nlist for-push\npush HEAD:refs/heads/main\n\n' const pushOutput = await runHelperConversation({ commands: pushInput, cwd: sourceDir, pds, }) assert.equal(stripLines(pushOutput.stdout).at(-1), 'ok refs/heads/main') assert.equal(pds.records.filter((record) => record.collection === 'org.latha.gitproxy.snapshot').length, 1) assert.equal(pds.blobs.length, 1) await runGit(['init'], { cwd: fetchDir }) const fetchInput = 'capabilities\nfetch refs/heads/main\n\n' const fetchOutput = await runHelperConversation({ commands: fetchInput, cwd: fetchDir, pds, }) assert.ok(fetchOutput.stdout.includes('\n')) const fetchedOid = (await runGitText(['rev-parse', 'refs/remotes/alice.example-demo/main'], { cwd: fetchDir })).trim() assert.equal(fetchedOid, commitOid) await runGit(['checkout', '-b', 'main', 'refs/remotes/alice.example-demo/main'], { cwd: fetchDir }) const fetchedText = await readFile(join(fetchDir, 'hello.txt'), 'utf8') assert.equal(fetchedText, 'hello from aproto\n') } finally { await rm(sourceDir, { recursive: true, force: true }) await rm(fetchDir, { recursive: true, force: true }) } })