feat(KB-648): enable parallel test execution and optimize test performance

- Enable parallel file execution in vitest configs for core, engine, CLI, and dashboard packages
- Optimize backup tests with fake timers for faster, deterministic execution
- Fix SettingsModal temporal dead zone by moving declaration before useCallback
- Fix engine tests with missing git branch delete mock and correct branch names
- Fix git worktree list mock and mission store mock in CLI tests
- Add changeset documenting test optimization patterns
- Update AGENTS.md with test optimization best practices
This commit is contained in:
gsxdsm
2026-03-31 23:19:52 -07:00
parent be373a396b
commit 07e471db5c
8 changed files with 91 additions and 45 deletions

View File

@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { mkdtempSync, writeFileSync, existsSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -14,17 +14,16 @@ import {
} from "./backup.js";
import type { ProjectSettings } from "./types.js";
// Helper to wait with a delay that ensures different timestamps
async function waitForNextSecond(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 1100));
}
describe("BackupManager", () => {
let tempDir: string;
let kbDir: string;
let backupManager: BackupManager;
beforeEach(async () => {
// Use fake timers for deterministic timestamp control
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
tempDir = mkdtempSync(join(tmpdir(), "kb-backup-test-"));
kbDir = join(tempDir, ".fusion");
await mkdir(kbDir, { recursive: true });
@@ -34,6 +33,7 @@ describe("BackupManager", () => {
});
afterEach(async () => {
vi.useRealTimers();
await rm(tempDir, { recursive: true, force: true });
});
@@ -83,20 +83,25 @@ describe("BackupManager", () => {
});
it("should return sorted array newest-first", async () => {
// Create multiple backups with delays to ensure different timestamps
await backupManager.createBackup();
await waitForNextSecond();
await backupManager.createBackup();
await waitForNextSecond();
await backupManager.createBackup();
// Create multiple backups by advancing system time deterministically
const backup1 = await backupManager.createBackup();
vi.setSystemTime(new Date("2026-01-01T00:00:01.000Z"));
const backup2 = await backupManager.createBackup();
vi.setSystemTime(new Date("2026-01-01T00:00:02.000Z"));
const backup3 = await backupManager.createBackup();
const backups = await backupManager.listBackups();
expect(backups).toHaveLength(3);
// Verify sorted by createdAt descending
for (let i = 0; i < backups.length - 1; i++) {
expect(backups[i].createdAt >= backups[i + 1].createdAt).toBe(true);
}
// Verify sorted by createdAt descending (newest first)
expect(backups[0].createdAt >= backups[1].createdAt).toBe(true);
expect(backups[1].createdAt >= backups[2].createdAt).toBe(true);
// Verify correct ordering by filename
expect(backups[0].filename).toBe(backup3.filename);
expect(backups[1].filename).toBe(backup2.filename);
expect(backups[2].filename).toBe(backup1.filename);
});
it("should only list files matching backup pattern", async () => {
@@ -123,10 +128,10 @@ describe("BackupManager", () => {
describe("cleanupOldBackups", () => {
it("should not delete when backup count is within retention", async () => {
// Create 3 backups with retention of 7
// Create 3 backups with retention of 7 by advancing time
for (let i = 0; i < 3; i++) {
vi.setSystemTime(new Date(`2026-01-01T00:00:0${i}.000Z`));
await backupManager.createBackup();
await waitForNextSecond();
}
const deleted = await backupManager.cleanupOldBackups();
@@ -139,10 +144,10 @@ describe("BackupManager", () => {
it("should delete oldest backups exceeding retention", async () => {
const manager = new BackupManager(kbDir, { retention: 2 });
// Create 4 backups with 1-second delays to ensure different timestamps
// Create 4 backups by advancing time deterministically
for (let i = 0; i < 4; i++) {
vi.setSystemTime(new Date(`2026-01-01T00:00:0${i}.000Z`));
await manager.createBackup();
await waitForNextSecond();
}
const deleted = await manager.cleanupOldBackups();
@@ -150,17 +155,17 @@ describe("BackupManager", () => {
expect(deleted).toBe(2); // 4 - 2 = 2 deleted
const backups = await manager.listBackups();
expect(backups).toHaveLength(2);
}, 10000);
});
it("should keep the newest backups after cleanup", async () => {
const manager = new BackupManager(kbDir, { retention: 2 });
// Create 4 backups and record their names
// Create 4 backups and record their names by advancing time
const backupNames: string[] = [];
for (let i = 0; i < 4; i++) {
vi.setSystemTime(new Date(`2026-01-01T00:00:0${i}.000Z`));
const backup = await manager.createBackup();
backupNames.push(backup.filename);
await waitForNextSecond();
}
await manager.cleanupOldBackups();
@@ -200,8 +205,8 @@ describe("BackupManager", () => {
it("should create pre-restore backup by default", async () => {
const backup = await backupManager.createBackup();
// Wait to ensure different timestamp
await waitForNextSecond();
// Advance time to ensure different timestamp for pre-restore backup
vi.setSystemTime(new Date("2026-01-01T00:00:01.000Z"));
// Restore with default options (should create pre-restore backup)
await backupManager.restoreBackup(backup.filename);
@@ -221,12 +226,19 @@ describe("generateBackupFilename", () => {
expect(filename).toMatch(/^kb-\d{4}-\d{2}-\d{2}-\d{6}\.db$/);
});
it("should generate unique filenames for different timestamps", async () => {
it("should generate unique filenames for different timestamps", () => {
// Use fake timers for deterministic time control
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
const filename1 = generateBackupFilename();
await waitForNextSecond();
vi.setSystemTime(new Date("2026-01-01T00:00:01.000Z"));
const filename2 = generateBackupFilename();
expect(filename1).not.toBe(filename2);
vi.useRealTimers();
});
});
@@ -294,6 +306,10 @@ describe("createBackupManager", () => {
});
it("should use settings when provided", async () => {
// Use fake timers for deterministic time control
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
const tempDir = mkdtempSync(join(tmpdir(), "kb-backup-test-"));
const kbDir = join(tempDir, ".fusion");
await mkdir(kbDir, { recursive: true });
@@ -306,18 +322,19 @@ describe("createBackupManager", () => {
const manager = createBackupManager(kbDir, settings);
// Create 4 backups with 1-second delays
// Create 4 backups by advancing time
for (let i = 0; i < 4; i++) {
vi.setSystemTime(new Date(`2026-01-01T00:00:0${i}.000Z`));
await manager.createBackup();
await waitForNextSecond();
}
// Cleanup should leave only 2
const deleted = await manager.cleanupOldBackups();
expect(deleted).toBe(2);
vi.useRealTimers();
await rm(tempDir, { recursive: true, force: true });
}, 10000);
});
});
describe("runBackupCommand", () => {
@@ -325,6 +342,10 @@ describe("runBackupCommand", () => {
let kbDir: string;
beforeEach(async () => {
// Use fake timers for deterministic timestamp control
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
tempDir = mkdtempSync(join(tmpdir(), "kb-backup-test-"));
kbDir = join(tempDir, ".fusion");
await mkdir(kbDir, { recursive: true });
@@ -332,6 +353,7 @@ describe("runBackupCommand", () => {
});
afterEach(async () => {
vi.useRealTimers();
await rm(tempDir, { recursive: true, force: true });
});
@@ -398,14 +420,15 @@ describe("runBackupCommand", () => {
autoBackupRetention: 2,
};
// Create 3 backups first (manually to test cleanup) with delays
// Create 3 backups first (manually to test cleanup) by advancing time
const manager = createBackupManager(kbDir, settings);
for (let i = 0; i < 3; i++) {
vi.setSystemTime(new Date(`2026-01-01T00:00:0${i}.000Z`));
await manager.createBackup();
await waitForNextSecond();
}
// Now run backup command
vi.setSystemTime(new Date("2026-01-01T00:00:03.000Z"));
const result = await runBackupCommand(kbDir, settings);
expect(result.success).toBe(true);