fix: isolate status history tests from real state

This commit is contained in:
Brad Groux 2026-03-22 14:24:15 -05:00
parent e9f63c9199
commit ddca1b6cb4
2 changed files with 22 additions and 20 deletions

View file

@ -20,9 +20,7 @@ describe('StatusHistoryService', () => {
await fs.mkdir(configDir, { recursive: true });
historyFile = path.join(configDir, 'status-history.json');
// Create service and override private fields
service = new StatusHistoryService();
(service as any).historyFile = historyFile;
service = new StatusHistoryService({ historyFile });
});
afterEach(async () => {

View file

@ -1,6 +1,6 @@
import { readFile, writeFile, mkdir } from 'fs/promises';
import { fileExists } from '../storage/fs-helpers.js';
import { join } from 'path';
import { dirname, join } from 'path';
import { createLogger } from '../lib/logger.js';
import { withFileLock } from './file-lock.js';
const log = createLogger('status-history-service');
@ -36,36 +36,39 @@ export interface StatusPeriod {
taskTitle?: string;
}
export interface StatusHistoryServiceOptions {
historyFile?: string;
}
export class StatusHistoryService {
private historyFile: string;
private readonly MAX_ENTRIES = 5000; // Keep more entries for historical analysis
private lastEntry: StatusHistoryEntry | null = null;
private initPromise: Promise<void>;
constructor() {
this.historyFile = join(process.cwd(), '.veritas-kanban', 'status-history.json');
this.initPromise = this.init();
}
private async init(): Promise<void> {
await this.ensureDir();
await this.loadLastEntry();
constructor(options: StatusHistoryServiceOptions = {}) {
this.historyFile =
options.historyFile || join(process.cwd(), '.veritas-kanban', 'status-history.json');
this.initPromise = this.ensureDir();
}
private async ensureDir(): Promise<void> {
const dir = join(process.cwd(), '.veritas-kanban');
await mkdir(dir, { recursive: true });
await mkdir(dirname(this.historyFile), { recursive: true });
}
private async loadLastEntry(): Promise<void> {
private async getLastEntry(): Promise<StatusHistoryEntry | null> {
if (this.lastEntry) {
return this.lastEntry;
}
try {
const entries = await this.getHistory(1);
if (entries.length > 0) {
this.lastEntry = entries[0];
}
this.lastEntry = entries[0] ?? null;
return this.lastEntry;
} catch {
// Intentionally silent: history file may not exist on first run
this.lastEntry = null;
return null;
}
}
@ -100,8 +103,9 @@ export class StatusHistoryService {
// Calculate duration of previous status
let durationMs: number | undefined;
if (this.lastEntry) {
const lastTime = new Date(this.lastEntry.timestamp).getTime();
const previousEntry = await this.getLastEntry();
if (previousEntry) {
const lastTime = new Date(previousEntry.timestamp).getTime();
durationMs = now.getTime() - lastTime;
}