/**
 * Shared test utilities for consistent test patterns across the codebase.
 *
 * These utilities reduce code duplication and provide a standardized approach
 * to common test operations like ANSI stripping, file existence checks, and
 * temp directory management.
 */
import * as fs from "fs/promises";
import * as os from "os";
import * as path from "path";
/**
 * Strip ANSI escape codes from a string for plain text comparison.
 *
 * Note: This test utility uses a simple string parameter rather than named args
 * for ergonomics, since it's called frequently in test assertions.
 *
 * @param str - The string containing ANSI codes
 *
 * @returns The string with ANSI codes removed
 */
export const stripAnsi = (str) => {
    // eslint-disable-next-line no-control-regex
    return str.replace(/\x1b\[[0-9;]*m/g, "");
};
/**
 * Check if a file or directory exists at the given path.
 *
 * @param args - The arguments object
 * @param args.filePath - The path to check
 *
 * @returns Promise resolving to true if the path exists, false otherwise
 */
export const pathExists = async (args) => {
    const { filePath } = args;
    return fs
        .access(filePath)
        .then(() => true)
        .catch(() => false);
};
/**
 * Create a temporary test context with standardized directory structure.
 *
 * @param args - The arguments object
 * @param args.prefix - Prefix for the temp directory name (e.g., "profiles-test")
 * @param args.agentDirName - Override the agent config directory name (defaults to ".claude")
 *
 * @returns Promise resolving to a TempTestContext with tempDir, agentDir, and cleanup function
 *
 * @example
 * ```typescript
 * let ctx: TempTestContext;
 *
 * beforeEach(async () => {
 *   ctx = await createTempTestContext({ prefix: "my-test" });
 * });
 *
 * afterEach(async () => {
 *   await ctx.cleanup();
 * });
 * ```
 */
export const createTempTestContext = async (args) => {
    const { prefix } = args;
    const agentDirName = args.agentDirName ?? ".claude";
    const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), `${prefix}-`));
    const agentDir = path.join(tempDir, agentDirName);
    await fs.mkdir(agentDir, { recursive: true });
    return {
        tempDir,
        agentDir,
        cleanup: async () => fs.rm(tempDir, { recursive: true, force: true }),
    };
};
/**
 * Create an isolated test context with HOME directory isolation.
 * Use this for integration tests that need complete isolation from the global
 * test setup (e.g., tests that need their own config state).
 *
 * @param args - The arguments object
 * @param args.prefix - Prefix for the temp directory name
 *
 * @returns Promise resolving to IsolatedTestContext
 *
 * @example
 * ```typescript
 * let ctx: IsolatedTestContext;
 *
 * beforeEach(async () => {
 *   ctx = await createIsolatedTestContext({ prefix: "config-test" });
 * });
 *
 * afterEach(async () => {
 *   await ctx.cleanup();
 * });
 * ```
 */
export const createIsolatedTestContext = async (args) => {
    const { prefix } = args;
    // Create base temp context
    const baseCtx = await createTempTestContext({ prefix });
    // Create HOME structure within temp dir
    const noriDir = path.join(baseCtx.tempDir, ".nori");
    const skillsetsDir = path.join(noriDir, "profiles");
    await fs.mkdir(skillsetsDir, { recursive: true });
    // Store original and set isolated HOME
    const originalGlobalConfig = process.env.NORI_GLOBAL_CONFIG;
    process.env.NORI_GLOBAL_CONFIG = baseCtx.tempDir;
    return {
        ...baseCtx,
        homeDir: baseCtx.tempDir,
        noriDir,
        originalGlobalConfig,
        cleanup: async () => {
            // Restore original NORI_GLOBAL_CONFIG
            if (originalGlobalConfig != null) {
                process.env.NORI_GLOBAL_CONFIG = originalGlobalConfig;
            }
            else {
                delete process.env.NORI_GLOBAL_CONFIG;
            }
            // Clean up temp directory
            await baseCtx.cleanup();
        },
    };
};
//# sourceMappingURL=index.js.map