import unittest
from src import project_manager

class TestDiscussionTakes(unittest.TestCase):
 def setUp(self):
  self.project_dict = project_manager.default_project("test_branching")
  # Populate initial history in 'main'
  self.project_dict["discussion"]["discussions"]["main"]["history"] = [
   "User: Message 0",
   "AI: Response 0",
   "User: Message 1",
   "AI: Response 1",
   "User: Message 2"
  ]

 def test_branch_discussion_creates_new_take(self):
  """Verify that branch_discussion copies history up to index and sets active."""
  source_id = "main"
  new_id = "take_1"
  message_index = 1
  
  # This will fail with AttributeError until implemented in project_manager.py
  project_manager.branch_discussion(self.project_dict, source_id, new_id, message_index)
  
  # Asserts
  self.assertIn(new_id, self.project_dict["discussion"]["discussions"])
  new_history = self.project_dict["discussion"]["discussions"][new_id]["history"]
  self.assertEqual(len(new_history), 2)
  self.assertEqual(new_history[0], "User: Message 0")
  self.assertEqual(new_history[1], "AI: Response 0")
  self.assertEqual(self.project_dict["discussion"]["active"], new_id)

 def test_promote_take_renames_discussion(self):
  """Verify that promote_take renames a discussion key."""
  take_id = "take_experimental"
  self.project_dict["discussion"]["discussions"][take_id] = project_manager.default_discussion()
  self.project_dict["discussion"]["discussions"][take_id]["history"] = ["User: Experimental"]
  
  new_id = "feature_refined"
  
  # This will fail with AttributeError until implemented in project_manager.py
  project_manager.promote_take(self.project_dict, take_id, new_id)
  
  # Asserts
  self.assertNotIn(take_id, self.project_dict["discussion"]["discussions"])
  self.assertIn(new_id, self.project_dict["discussion"]["discussions"])
  self.assertEqual(self.project_dict["discussion"]["discussions"][new_id]["history"], ["User: Experimental"])

if __name__ == "__main__":
 unittest.main()