const test = require('brittle');
const fs = require('fs');
const os = require('os');
const path = require('path');
const HyperSync = require('../');
// _ensureRemoteHelper self-provisions the git-remote-punch remote helper so
// `git fetch punch://...` works even when the helper isn't on the daemon's
// PATH (the common case on a fresh peer). Tested via Object.create so we don't
// spin up a Hyperswarm.
test('_ensureRemoteHelper writes a git-remote-punch shim + _gitEnv prepends it to PATH', (t) => {
const tmpRepos = fs.mkdtempSync(path.join(os.tmpdir(), 'hyper-sync-helper-'));
t.teardown(() => { try { fs.rmSync(tmpRepos, { recursive: true, force: true }); } catch {} });
const obj = Object.create(HyperSync.prototype);
obj._reposDir = tmpRepos;
const dir = obj._ensureRemoteHelper();
t.ok(dir, 'helper dir provisioned (git-remote-punch located as a sibling)');
t.is(dir, path.join(tmpRepos, '.remote-helpers'), 'helper dir lives under the repos dir (non-.git → repo scans ignore it)');
const shim = path.join(dir, 'git-remote-punch');
t.ok(fs.existsSync(shim), 'shim file written');
const content = fs.readFileSync(shim, 'utf8');
t.ok(content.startsWith('#!/bin/sh'), 'shim is a shell script');
t.ok(content.includes('exec "'), 'shim uses exec');
t.ok(content.includes('git-remote-punch'), 'shim references the git-remote-punch bin');
t.ok(content.trim().endsWith('"$@"'), 'shim forwards all args to the helper');
t.ok((fs.statSync(shim).mode & 0o111) !== 0, 'shim is executable');
t.is(obj._ensureRemoteHelper(), dir, 'idempotent — second call returns the cached dir');
const env = obj._gitEnv();
t.ok(env.PATH.startsWith(dir + path.delimiter), '_gitEnv prepends the helper dir to PATH');
t.ok(env.PATH.length > dir.length + 1, 'base PATH preserved after the helper dir');
});