Merge pull request #413 from PR0M3TH3AN/jules/test-cmd-backup-16743310224352411678

Add tests for cmdBackup
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2026-02-21 14:09:25 -05:00
commit e242860fd6
2 files changed
+109 -7

No files matched your search

+10 -7
View File
@@ -2,8 +2,6 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import { execSync } from 'node:child_process';
const BACKUP_ROOT = path.resolve(process.cwd(), '.torch/backups');
// Paths to snapshot on each backup run
const STATE_SOURCES = [
'.scheduler-memory/memory-store.json',
@@ -22,19 +20,21 @@ const STATE_SOURCES = [
*
* @param {Object} [opts]
* @param {string|null} [opts.output] - Override destination directory (default: .torch/backups/<ts>)
* @param {string} [opts.cwd] - Working directory (default: process.cwd())
* @returns {Promise<{backupDir: string, manifest: Object}>}
*/
export async function cmdBackup({ output = null } = {}) {
export async function cmdBackup({ output = null, cwd = process.cwd() } = {}) {
const BACKUP_ROOT = path.resolve(cwd, '.torch/backups');
const ts = new Date().toISOString().replace(/[:.]/g, '-');
const backupDir = output
? path.resolve(process.cwd(), output)
? path.resolve(cwd, output)
: path.join(BACKUP_ROOT, ts);
await fs.mkdir(backupDir, { recursive: true });
let gitCommit = null;
try {
gitCommit = execSync('git rev-parse HEAD', { stdio: 'pipe' }).toString().trim();
gitCommit = execSync('git rev-parse HEAD', { stdio: 'pipe', cwd }).toString().trim();
} catch {
// Not in a git repo or git unavailable
}
@@ -43,7 +43,7 @@ export async function cmdBackup({ output = null } = {}) {
const skipped = [];
for (const relPath of STATE_SOURCES) {
const src = path.resolve(process.cwd(), relPath);
const src = path.resolve(cwd, relPath);
const dest = path.join(backupDir, relPath.replace(/\//g, '__'));
try {
await fs.copyFile(src, dest);
@@ -77,9 +77,12 @@ export async function cmdBackup({ output = null } = {}) {
/**
* Lists all available backups under .torch/backups/, newest first.
*
* @param {Object} [opts]
* @param {string} [opts.cwd] - Working directory (default: process.cwd())
* @returns {Promise<Array<{id: string, createdAt: string|null, backupDir: string}>>}
*/
export async function listBackups() {
export async function listBackups({ cwd = process.cwd() } = {}) {
const BACKUP_ROOT = path.resolve(cwd, '.torch/backups');
try {
const entries = await fs.readdir(BACKUP_ROOT, { withFileTypes: true });
const backups = [];