#!/usr/bin/env tsx
import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { SwarmCoordinator } from "../src/integrations/ralph/swarm/swarm-coordinator.js";
import { RalphStackMemoryBridge } from "../src/integrations/ralph/bridge/ralph-stackmemory-bridge.js";
import { GitWorkflowManager } from "../src/integrations/ralph/swarm/git-workflow-manager.js";
import { v4 as uuidv4 } from "uuid";
async function testDatabaseOptionalFix() {
  console.log("\n1. Testing database optional fix...");
  try {
    const bridgeWithoutDb = new RalphStackMemoryBridge({
      useStackMemory: false
      // This should prevent database error
    });
    await bridgeWithoutDb.initialize({
      task: "Test task without database",
      criteria: "Test criteria"
    });
    console.log(
      "\u2705 Bridge initialized successfully without database requirement"
    );
  } catch (error) {
    console.error(
      "\u274C Failed to initialize bridge without database:",
      error.message
    );
    return false;
  }
  return true;
}
async function testGitBranchConflictFix() {
  console.log("\n2. Testing git branch conflict fix...");
  const gitManager = new GitWorkflowManager({
    enableGitWorkflow: true,
    branchStrategy: "agent"
  });
  const mockAgent = {
    id: uuidv4(),
    role: "developer",
    status: "active",
    capabilities: ["coding"],
    workingDirectory: ".swarm/test",
    currentTask: null,
    performance: {
      tasksCompleted: 0,
      successRate: 1,
      averageTaskTime: 0,
      driftDetected: false,
      lastFreshStart: Date.now()
    },
    coordination: {
      communicationStyle: "collaborative",
      conflictResolution: "defer_to_expertise",
      collaborationPreferences: []
    }
  };
  const mockTask = {
    id: uuidv4(),
    type: "implementation",
    title: "implement-core-feature",
    description: "Test task",
    priority: 1,
    estimatedEffort: "medium",
    requiredRoles: ["developer"],
    dependencies: [],
    acceptanceCriteria: ["Test passes"]
  };
  try {
    await gitManager.initializeAgentWorkflow(mockAgent, mockTask);
    console.log("\u2705 First branch initialization successful");
    await gitManager.initializeAgentWorkflow(mockAgent, mockTask);
    console.log(
      "\u2705 Second branch initialization handled existing branch gracefully"
    );
  } catch (error) {
    console.error("\u274C Git branch conflict handling failed:", error.message);
    return false;
  }
  return true;
}
async function testStopSwarmMethod() {
  console.log("\n3. Testing stopSwarm method...");
  const coordinator = new SwarmCoordinator({
    maxAgents: 5,
    coordinationInterval: 3e4
  });
  try {
    if (typeof coordinator.stopSwarm !== "function") {
      console.error("\u274C stopSwarm method does not exist");
      return false;
    }
    await coordinator.stopSwarm();
    console.log("\u2705 stopSwarm method executed successfully");
  } catch (error) {
    console.error("\u274C stopSwarm method failed:", error.message);
    return false;
  }
  return true;
}
async function main() {
  console.log("Testing Swarm Orchestration Fixes");
  console.log("==================================");
  let allTestsPassed = true;
  const test1 = await testDatabaseOptionalFix();
  allTestsPassed = allTestsPassed && test1;
  const test2 = await testGitBranchConflictFix();
  allTestsPassed = allTestsPassed && test2;
  const test3 = await testStopSwarmMethod();
  allTestsPassed = allTestsPassed && test3;
  console.log("\n==================================");
  if (allTestsPassed) {
    console.log("\u2705 All tests passed! Fixes are working correctly.");
    process.exit(0);
  } else {
    console.log("\u274C Some tests failed. Please review the fixes.");
    process.exit(1);
  }
}
main().catch((error) => {
  console.error("Test script error:", error);
  process.exit(1);
});
//# sourceMappingURL=test-swarm-fixes.js.map
