import types
import importlib

def test_main_calls_logging_and_mainloop(monkeypatch):
    # Prepare dummy MainView with attributes and a no-op mainloop
    calls = {}

    class DummyApp:
        def __init__(self):
            self.log_text_widget = object()

        def mainloop(self):
            calls['mainloop'] = True
        
        def after(self, ms, func, *args):
            # Immediately call the function for tests and return a fake id
            try:
                func(*args)
            except Exception:
                pass
            return f"after-{ms}"

    # Monkeypatch MainView used by __main__ (comes from gui.main_view.MainView)
    monkeypatch.setattr('target_simulator.gui.main_view.MainView', DummyApp)

    def fake_setup(app, cfg):
        calls['setup_basic_logging'] = True

    def fake_add_tkhandler(widget, cfg):
        calls['add_tkinter_handler'] = True

    # Reload module to ensure imports are fresh
    mod = importlib.import_module('target_simulator.__main__')
    importlib.reload(mod)

    # Assign our fakes onto the reloaded module so main() will use them
    mod.setup_basic_logging = fake_setup
    mod.add_tkinter_handler = fake_add_tkhandler

    # Call main (should not block due to DummyApp.mainloop)
    mod.main()

    assert calls.get('setup_basic_logging')
    assert calls.get('add_tkinter_handler')
    assert calls.get('mainloop')
