import unittest from datetime import datetime import time from sia.working_memory import WorkingMemory from sia.delete_command import DeleteCommand from sia.reasoning_entry import ReasoningEntry from sia.background_entry import BackgroundEntry class DeleteCommandTest(unittest.TestCase): def setUp(self): """Set up test cases with a fresh working memory""" self.memory = WorkingMemory() self.test_id = "test-id-1234" self.test_timestamp = datetime(2024, 10, 31, 12, 0, 0) def test_delete_existing_entry(self): """Test deleting an existing entry""" # Add test entry entry = ReasoningEntry("test content", self.test_id, self.test_timestamp) self.memory.add_entry(entry) # Execute delete command command = DeleteCommand(self.test_id) result = command.execute(self.memory) # Verify result and memory state self.assertTrue(result.success) self.assertFalse(result.should_stop) self.assertEqual(result.message, "") self.assertEqual(self.memory.get_entries_count(), 0) def test_delete_running_background_entry(self): """Test deleting a background entry that is still running""" # Create a background entry with a long-running command entry = BackgroundEntry("sleep 10", self.test_id, self.test_timestamp) self.memory.add_entry(entry) # Start the process entry.update() self.assertIsNotNone(entry.process) process_pid = entry.process.pid # Execute delete command command = DeleteCommand(self.test_id) result = command.execute(self.memory) # Verify process was terminated time.sleep(0.1) # Give process time to terminate self.assertTrue(result.success) self.assertIsNone(entry.process) # Verify process no longer exists with self.assertRaises(ProcessLookupError): import os os.kill(process_pid, 0) def test_delete_nonexistent_entry(self): """Test attempting to delete a nonexistent entry""" command = DeleteCommand("nonexistent-id") result = command.execute(self.memory) # Verify error result self.assertFalse(result.success) self.assertFalse(result.should_stop) self.assertIn("not found", result.message)