stoke/test/sync.test.js
codex-bot-andresmgsl ea6c1a4fe9
All checks were successful
labels / labels (pull_request) Successful in 10s
ci / test (pull_request) Successful in 15s
feat: resolve repository default branch
2026-08-31 16:54:34 +00:00

113 lines
3.4 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const { execFileSync, spawnSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const CLI = path.join(__dirname, '..', 'src', 'cli.js');
const TOKEN = 'stoke-secret-token-for-sync-tests';
function git(args, cwd) {
return execFileSync('git', args, { cwd, encoding: 'utf8' }).trim();
}
function commit(directory, message, contents) {
fs.writeFileSync(path.join(directory, 'content.txt'), `${contents}\n`);
git(['add', 'content.txt'], directory);
git(['-c', 'user.name=Tester', '-c', 'user.email=tester@example.com', 'commit', '-m', message], directory);
return git(['rev-parse', 'HEAD'], directory);
}
function fixture() {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'stoke-sync-test-'));
const forgeRoot = path.join(root, 'forge');
const forgeRepo = path.join(forgeRoot, 'o', 'r.git');
const seed = path.join(root, 'seed');
const upstreamWork = path.join(root, 'upstream-work');
const upstreamRepo = path.join(root, 'upstream.git');
fs.mkdirSync(path.dirname(forgeRepo), { recursive: true });
git(['init', '-b', 'main', seed], root);
const oldSha = commit(seed, 'initial', 'initial');
git(['clone', '--bare', seed, forgeRepo], root);
git(['clone', seed, upstreamWork], root);
const newSha = commit(upstreamWork, 'upstream advance', 'advanced');
git(['clone', '--bare', upstreamWork, upstreamRepo], root);
const config = path.join(root, 'config.json');
fs.writeFileSync(config, JSON.stringify({
url: `file://${forgeRoot}`,
token: TOKEN,
login: 'tester',
}));
return {
root,
forgeRepo,
upstreamRepo,
config,
oldSha,
newSha,
cleanup() {
fs.rmSync(root, { recursive: true, force: true });
},
};
}
function runSync(fx, extra = [], { branch = 'main' } = {}) {
const args = [
CLI,
'repo',
'sync',
'-o', 'o',
'-r', 'r',
'--from', `file://${fx.upstreamRepo}`,
];
if (branch) args.push('--branch', branch);
args.push(...extra);
return spawnSync(process.execPath, args, {
encoding: 'utf8',
env: { ...process.env, STOKE_CONFIG_FILE: fx.config },
});
}
test('repo sync fast-forwards an undiverged forge branch', () => {
const fx = fixture();
try {
const result = runSync(fx);
assert.equal(result.status, 0, result.stderr);
assert.equal(git(['rev-parse', 'refs/heads/main'], fx.forgeRepo), fx.newSha);
assert.match(result.stdout, new RegExp(`main ${fx.oldSha}\\.\\.${fx.newSha}`));
} finally {
fx.cleanup();
}
});
test('repo sync resolves an omitted branch from the forge symbolic HEAD', () => {
const fx = fixture();
try {
const result = runSync(fx, [], { branch: null });
assert.equal(result.status, 0, result.stderr);
assert.equal(git(['rev-parse', 'refs/heads/main'], fx.forgeRepo), fx.newSha);
assert.match(result.stdout, new RegExp(`main ${fx.oldSha}\\.\\.${fx.newSha}`));
} finally {
fx.cleanup();
}
});
test('repo sync reports an already-current branch as a no-op', () => {
const fx = fixture();
try {
assert.equal(runSync(fx).status, 0);
const result = runSync(fx);
assert.equal(result.status, 0, result.stderr);
assert.equal(git(['rev-parse', 'refs/heads/main'], fx.forgeRepo), fx.newSha);
assert.match(result.stdout, new RegExp(`main is up to date at ${fx.newSha}`));
} finally {
fx.cleanup();
}
});