69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
/// <reference types="jest" />
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
import { TransactionManager } from '../src/core/transaction';
|
|
|
|
describe('TransactionManager', () => {
|
|
let tm: TransactionManager;
|
|
let testDir: string;
|
|
|
|
beforeEach(() => {
|
|
tm = new TransactionManager();
|
|
testDir = path.join(os.tmpdir(), `g1-test-${Date.now()}`);
|
|
if (!fs.existsSync(testDir)) {
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (fs.existsSync(testDir)) {
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('should record and rollback file creation', () => {
|
|
const testFile = path.join(testDir, 'test.txt');
|
|
|
|
tm.begin();
|
|
tm.record(testFile);
|
|
fs.writeFileSync(testFile, 'hello world');
|
|
|
|
expect(fs.existsSync(testFile)).toBe(true);
|
|
|
|
tm.rollback();
|
|
|
|
expect(fs.existsSync(testFile)).toBe(false);
|
|
});
|
|
|
|
test('should record and rollback file modification', () => {
|
|
const testFile = path.join(testDir, 'edit.txt');
|
|
fs.writeFileSync(testFile, 'original');
|
|
|
|
tm.begin();
|
|
tm.record(testFile);
|
|
fs.writeFileSync(testFile, 'modified');
|
|
|
|
expect(fs.readFileSync(testFile, 'utf8')).toBe('modified');
|
|
|
|
tm.rollback();
|
|
|
|
expect(fs.readFileSync(testFile, 'utf8')).toBe('original');
|
|
});
|
|
|
|
test('should delete backup files after commit', () => {
|
|
const testFile = path.join(testDir, 'commit.txt');
|
|
fs.writeFileSync(testFile, 'data');
|
|
|
|
tm.begin();
|
|
tm.record(testFile);
|
|
fs.writeFileSync(testFile, 'new data');
|
|
|
|
tm.commit();
|
|
|
|
expect(fs.readFileSync(testFile, 'utf8')).toBe('new data');
|
|
// Backups are in ~/.g1nation-backups (hardcoded in TM, but let's assume it cleans up internal state)
|
|
expect(tm.isActive()).toBe(false);
|
|
});
|
|
});
|