fix: clean up hash-based test directories in afterEach (#86)

Fixes bugbot issue: "Tests leak directories under home directory without cleanup"

The problem:
- Tests call the real getSessionsDir() which creates ~/.agent-orchestrator/{hash}/
  directories based on hashing the tmpDir path
- Each test run uses a new random tmpDir, creating a unique hash
- afterEach only cleaned up tmpDir, leaving orphaned directories in ~/.agent-orchestrator/
- These directories accumulated indefinitely

The fix:
- Added cleanup for hash-based directories in afterEach for all affected tests
- Uses getProjectBaseDir() to calculate the directory path
- Removes the hash-based directory before cleaning tmpDir
- All 61 tests pass (13 CLI + 48 core)

Files fixed:
- packages/cli/__tests__/commands/session.test.ts
- packages/core/src/__tests__/session-manager.test.ts
- packages/core/src/__tests__/lifecycle-manager.test.ts

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
prateek 2026-02-18 01:38:48 +05:30 committed by GitHub
parent 599710296d
commit 9c5927a7f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 6 deletions

View File

@ -40,11 +40,12 @@ vi.mock("@composio/ao-core", async (importOriginal) => {
});
let tmpDir: string;
let configPath: string;
let sessionsDir: string;
import { Command } from "commander";
import { registerSession } from "../../src/commands/session.js";
import { getSessionsDir } from "@composio/ao-core";
import { getSessionsDir, getProjectBaseDir } from "@composio/ao-core";
let program: Command;
let consoleSpy: ReturnType<typeof vi.spyOn>;
@ -52,7 +53,7 @@ let consoleSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
tmpDir = mkdtempSync(join(tmpdir(), "ao-session-test-"));
const configPath = join(tmpDir, "agent-orchestrator.yaml");
configPath = join(tmpDir, "agent-orchestrator.yaml");
writeFileSync(configPath, "projects: {}");
mockConfigRef.current = {
@ -100,7 +101,15 @@ beforeEach(() => {
});
afterEach(() => {
// Clean up hash-based directories in ~/.agent-orchestrator
const projectBaseDir = getProjectBaseDir(configPath, join(tmpDir, "main-repo"));
if (existsSync(projectBaseDir)) {
rmSync(projectBaseDir, { recursive: true, force: true });
}
// Clean up tmpDir
rmSync(tmpDir, { recursive: true, force: true });
vi.restoreAllMocks();
});

View File

@ -1,11 +1,11 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { randomUUID } from "node:crypto";
import { createLifecycleManager } from "../lifecycle-manager.js";
import { writeMetadata, readMetadataRaw } from "../metadata.js";
import { getSessionsDir } from "../paths.js";
import { getSessionsDir, getProjectBaseDir } from "../paths.js";
import type {
OrchestratorConfig,
PluginRegistry,
@ -146,6 +146,13 @@ beforeEach(() => {
});
afterEach(() => {
// Clean up hash-based directories in ~/.agent-orchestrator
const projectBaseDir = getProjectBaseDir(configPath, join(tmpDir, "my-app"));
if (existsSync(projectBaseDir)) {
rmSync(projectBaseDir, { recursive: true, force: true });
}
// Clean up tmpDir
rmSync(tmpDir, { recursive: true, force: true });
});

View File

@ -1,11 +1,11 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { randomUUID } from "node:crypto";
import { createSessionManager } from "../session-manager.js";
import { writeMetadata, readMetadata } from "../metadata.js";
import { getSessionsDir } from "../paths.js";
import { getSessionsDir, getProjectBaseDir } from "../paths.js";
import type {
OrchestratorConfig,
PluginRegistry,
@ -120,6 +120,13 @@ beforeEach(() => {
});
afterEach(() => {
// Clean up hash-based directories in ~/.agent-orchestrator
const projectBaseDir = getProjectBaseDir(configPath, join(tmpDir, "my-app"));
if (existsSync(projectBaseDir)) {
rmSync(projectBaseDir, { recursive: true, force: true });
}
// Clean up tmpDir
rmSync(tmpDir, { recursive: true, force: true });
});