import fs from "node:fs";
import { createToolServer } from "../../tools/index.js";
import { loadConfig, loadOwnerConfig, getConfigDir, getMemoriesDir, getTokensFile } from "../../config/index.js";
import { hasLocalGoogleRefreshToken } from "../../google/token-file.js";
import { isDisplaySizeInitialized } from "../../tools/desktop-executor-factory.js";
import { ensureBrowser, getCdpEndpoint } from "../../agent/browser-launcher.js";
import { buildSuiteResult, ok, runTest, skipped } from "../suite-utils.js";
export const selfSuite = {
    id: "self",
    name: "Self check",
    run: async (context) => {
        const started = Date.now();
        const tests = await Promise.all([
            runTest("self", "self.config_load", "Config load", () => {
                loadConfig();
                return Promise.resolve(ok("Config loaded"));
            }),
            runTest("self", "self.owner_config_load", "Owner config load", () => {
                try {
                    loadOwnerConfig();
                    return Promise.resolve(ok("Owner config loaded"));
                }
                catch {
                    return Promise.resolve(skipped("Owner config not available"));
                }
            }),
            runTest("self", "self.required_directories", "Required directories", () => {
                for (const dir of [getConfigDir(), getMemoriesDir()]) {
                    fs.mkdirSync(dir, { recursive: true });
                    fs.accessSync(dir, fs.constants.W_OK);
                }
                return Promise.resolve(ok("Required directories writable"));
            }),
            runTest("self", "self.tool_server", "Tool server construction", () => {
                createToolServer();
                return Promise.resolve(ok("Tool server created"));
            }),
            runTest("self", "self.secrets_presence", "Secrets presence", () => Promise.resolve(ok("Secret presence checked", {
                googleTokens: hasLocalGoogleRefreshToken() ? "present" : "missing",
                anthropicApiKey: context.config.anthropicApiKey ? "present" : "missing",
                telegramBotToken: context.config.channels.telegram?.botToken ? "present" : "missing",
            }))),
            runTest("self", "self.display_size", "Display size status", () => {
                if (isDisplaySizeInitialized())
                    return Promise.resolve(ok("Display size initialized"));
                return Promise.resolve(skipped("Display size not initialized; CUA tests may be limited"));
            }),
            runTest("self", "self.chrome_cdp", "Chrome/CDP status", async () => {
                await ensureBrowser();
                const endpoint = getCdpEndpoint();
                const resp = await fetch(`${endpoint}/json/version`).catch(() => null);
                if (resp?.ok)
                    return ok("CDP endpoint reachable", { endpoint });
                return skipped("Chrome/CDP not reachable");
            }),
            runTest("self", "self.google_tokens_file", "Google tokens file", () => {
                if (fs.existsSync(getTokensFile()))
                    return Promise.resolve(ok("Google tokens file present"));
                return Promise.resolve(skipped("Google tokens file missing"));
            }),
        ]);
        return buildSuiteResult("self", "Self check", started, tests);
    },
};
//# sourceMappingURL=self.js.map