#!/usr/bin/env python3 """ Comprehensive test to diagnose and fix punch button accumulation issue. """ import os import sys import tkinter as tk sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) import logging from src.ui_manager import UIManager def test_punch_button_step_by_step(): """Test punch button functionality step by step with detailed logging.""" print("šŸ”¬ Punch Button Step-by-Step Diagnosis") print("=" * 50) root = tk.Tk() root.title("Punch Button Diagnosis") root.geometry("800x600") logger = logging.getLogger("punch_diagnosis") logger.setLevel(logging.DEBUG) ui_manager = UIManager(root, logger) sample_values = ( "07/29/2025", 5, 3, 7, 6, 1, "", 0, "", 0, "", 0, "", "Punch diagnosis test", ) punch_calls = [] save_calls = [] def track_save(*args): save_calls.append(args) if len(args) >= 12: dose_data = args[-1] print("\nšŸ’¾ SAVE CAPTURED:") for med, doses in dose_data.items(): if doses: count = len(doses.split("|")) if "|" in doses else 1 print(f" {med}: {count} dose(s) - {doses}") else: print(f" {med}: No doses") if args and hasattr(args[0], "destroy"): args[0].destroy() callbacks = {"save": track_save, "delete": lambda x: x.destroy()} try: edit_window = ui_manager.create_edit_window(sample_values, callbacks) # Let's manually patch the _punch_dose_in_edit method to add logging original_punch = ui_manager._punch_dose_in_edit def logged_punch(medicine_name, dose_vars): print(f"\n🄊 PUNCH CALLED: {medicine_name}") dose_entry_var = dose_vars.get(f"{medicine_name}_entry_var") dose_text_widget = dose_vars.get(f"{medicine_name}_doses_text") if not dose_entry_var or not dose_text_widget: print(f"āŒ Missing variables for {medicine_name}") return dose = dose_entry_var.get().strip() print(f"šŸ“ Dose entered: '{dose}'") if not dose: print("āŒ No dose entered") return # Get current content BEFORE modification before_content = dose_text_widget.get(1.0, tk.END).strip() print(f"šŸ“‹ Content BEFORE: '{before_content}'") # Call original method result = original_punch(medicine_name, dose_vars) # Get content AFTER modification after_content = dose_text_widget.get(1.0, tk.END).strip() print(f"šŸ“‹ Content AFTER: '{after_content}'") punch_calls.append( { "medicine": medicine_name, "dose": dose, "before": before_content, "after": after_content, } ) return result # Patch the method ui_manager._punch_dose_in_edit = logged_punch print("\nšŸ“ TEST INSTRUCTIONS:") print("1. Enter '100mg' in Bupropion dose field") print("2. Click 'Take Bupropion' - watch for PUNCH CALLED message") print("3. Enter '200mg' in Bupropion dose field") print("4. Click 'Take Bupropion' again - watch content changes") print("5. Enter '300mg' in Bupropion dose field") print("6. Click 'Take Bupropion' a third time") print("7. Verify the text area shows all three doses") print("8. Click Save") print("\nā³ Please perform the test sequence...") edit_window.wait_window() print("\nšŸ“Š ANALYSIS:") print(f" Punch calls made: {len(punch_calls)}") print(f" Save calls made: {len(save_calls)}") if punch_calls: print("\n🄊 PUNCH CALL DETAILS:") for i, call in enumerate(punch_calls, 1): print(f" Call {i}: {call['medicine']} - {call['dose']}") print(f" Before: '{call['before']}'") print(f" After: '{call['after']}'") print() # Check if multiple punches accumulated properly if len(punch_calls) >= 2: last_call = punch_calls[-1] lines_in_final = ( last_call["after"].count("\n") + 1 if last_call["after"] else 0 ) print("šŸ” ACCUMULATION CHECK:") print(f" Final content has {lines_in_final} lines") print(f" Expected: {len(punch_calls)} lines") if lines_in_final >= len(punch_calls): print("āœ… Punch button accumulation appears to be working!") return True else: print("āŒ Punch button accumulation is NOT working correctly!") return False else: print("āš ļø Not enough punch calls to test accumulation") return False except Exception as e: print(f"āŒ Error during test: {e}") import traceback traceback.print_exc() return False finally: root.destroy() if __name__ == "__main__": os.chdir("/home/will/Code/thechart") success = test_punch_button_step_by_step() if success: print("\nšŸŽÆ Punch button test completed - accumulation working!") else: print("\n🚨 Punch button test revealed accumulation issues!")