/**
 * PR-A Baseline Test 11: Compaction Trigger
 * Verifies: compaction mechanism exists in the codebase
 * Freezes: compaction contract
 */

import { describe, it } from "node:test";
import assert from "node:assert";
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";

const OPENCLAW_HOME = "/opt/homebrew/lib/node_modules/openclaw";
const DIST_DIR = join(OPENCLAW_HOME, "dist");
const DOCS_DIR = join(OPENCLAW_HOME, "docs");

describe("test-11: Compaction Baseline", () => {
  it("11.01 - compaction module exists in dist", () => {
    // Look for compaction-related files
    const compactionPaths = [
      join(DIST_DIR, "bundled", "compaction-notifier"),
      join(DIST_DIR, "commitments"),
    ];
    let found = false;
    for (const p of compactionPaths) {
      if (existsSync(p)) {
        console.log("[BASELINE] found:", p);
        const files = readdirSync(p);
        console.log("[BASELINE] contents:", files.join(", "));
        found = true;
      }
    }
    // Also check for compaction in main dist files
    const indexJs = join(DIST_DIR, "index.js");
    if (existsSync(indexJs)) {
      const content = readFileSync(indexJs, "utf8");
      const hasCompaction = content.includes("compaction") || content.includes("Compaction");
      console.log("[BASELINE] main index.js contains 'compaction':", hasCompaction);
      if (hasCompaction) found = true;
    }
    assert.ok(found, "compaction module must exist somewhere in dist");
  });

  it("11.02 - compaction documentation exists", () => {
    const docPath = join(DOCS_DIR, "concepts", "compaction.md");
    assert.ok(existsSync(docPath), "compaction docs must exist");
    const content = readFileSync(docPath, "utf8");
    console.log("[BASELINE] compaction docs size:", content.length);
    const hasConfig = content.includes("config") || content.includes("Config");
    const hasReserve = content.includes("reserve") || content.includes("Reserve");
    const hasThreshold = content.includes("threshold") || content.includes("budget");
    console.log("[BASELINE] compaction docs covers config:", hasConfig);
    console.log("[BASELINE] compaction docs covers reserve:", hasReserve);
    console.log("[BASELINE] compaction docs covers threshold:", hasThreshold);
    assert.ok(content.length > 100, "compaction docs must be substantial");
  });

  it("11.03 - compaction-notifier bundled module", () => {
    const notifierPath = join(DIST_DIR, "bundled", "compaction-notifier");
    if (existsSync(notifierPath)) {
      const files = readdirSync(notifierPath);
      console.log("[BASELINE] compaction-notifier files:", files.join(", "));
      // Should have at least handler and metadata
      const hasHandler = files.some(f => f.includes("handler") || f.includes("HOOK"));
      console.log("[BASELINE] has handler:", hasHandler);
      assert.ok(files.length > 0, "compaction-notifier must have files");
    } else {
      console.log("[BASELINE] compaction-notifier not at expected path");
    }
    assert.ok(true, "baseline frozen: compaction notifier check");
  });

  it("11.04 - compaction config schema recorded", () => {
    // Check config schema for compaction settings
    const configPath = join(homedir(), ".openclaw", "openclaw.json");
    const config = JSON.parse(readFileSync(configPath, "utf8"));
    
    const hasCompactionConfig = 
      config?.agents?.defaults?.compaction !== undefined ||
      config?.compaction !== undefined;
    console.log("[BASELINE] explicit compaction config:", hasCompactionConfig);
    
    // Check docs for compaction parameter names
    const docContent = readFileSync(
      join(DOCS_DIR, "concepts", "compaction.md"), "utf8"
    );
    const mentions = [
      "reserveTokens", "reserve", "threshold", "budget", 
      "memoryFlush", "autoCompact", "auto-compact"
    ];
    for (const m of mentions) {
      if (docContent.includes(m)) {
        console.log(`[BASELINE] compaction param "${m}": documented`);
      }
    }
    assert.ok(true, "baseline frozen: compaction config schema");
  });

  it("11.05 - memory flush before compaction is documented", () => {
    const docContent = readFileSync(
      join(DOCS_DIR, "concepts", "compaction.md"), "utf8"
    );
    const hasMemoryFlush = docContent.includes("memoryFlush") || 
                           docContent.includes("memory flush") ||
                           docContent.includes("Memory Flush");
    console.log("[BASELINE] memory flush documented:", hasMemoryFlush);
    assert.ok(hasMemoryFlush, "memory flush before compaction must be documented");
  });
});

