import pytest import json from pathlib import Path import sys from pathlib import Path as _Path sys.path.append(str(_Path(__file__).parent.parent)) from application_handler import ApplicationHandler @pytest.fixture def temp_applications_file(tmp_path): """Fixture to create a temporary applications file.""" file = tmp_path / "applications.json" file.write_text("{}", encoding="utf-8") return file @pytest.fixture def application_handler(temp_applications_file, monkeypatch): """Fixture to create an ApplicationHandler instance with a temporary applications file.""" class DummyContext: pass class DummyStateManager: pass monkeypatch.setattr("application_handler.APPLICATIONS_FILE", temp_applications_file) return ApplicationHandler(browser_context=DummyContext(), state_manager=DummyStateManager(), applications_file=temp_applications_file) import types class DummyContext: pass class DummyStateManager: pass def test_detect_company_domains(): handler = ApplicationHandler(browser_context=DummyContext(), state_manager=DummyStateManager()) assert handler._detect_company('https://howoge.de/abc') == 'howoge' assert handler._detect_company('https://www.howoge.de/abc') == 'howoge' assert handler._detect_company('https://portal.gewobag.de/') == 'gewobag' assert handler._detect_company('https://degewo.de/') == 'degewo' assert handler._detect_company('https://gesobau.de/') == 'gesobau' assert handler._detect_company('https://stadtundland.de/') == 'stadtundland' assert handler._detect_company('https://stadt-und-land.de/') == 'stadtundland' assert handler._detect_company('https://wbm.de/') == 'wbm' def test_detect_company_path_fallback(): handler = ApplicationHandler(browser_context=DummyContext(), state_manager=DummyStateManager()) assert handler._detect_company('https://example.com/howoge/abc') == 'howoge' assert handler._detect_company('https://foo.bar/gewobag') == 'gewobag' assert handler._detect_company('https://foo.bar/degewo') == 'degewo' assert handler._detect_company('https://foo.bar/gesobau') == 'gesobau' assert handler._detect_company('https://foo.bar/stadt-und-land') == 'stadtundland' assert handler._detect_company('https://foo.bar/wbm') == 'wbm' def test_detect_company_unknown(): handler = ApplicationHandler(browser_context=DummyContext(), state_manager=DummyStateManager()) assert handler._detect_company('https://example.com/') == 'unknown' assert handler._detect_company('') == 'unknown' assert handler._detect_company(None) == 'unknown' def test_load_applications_empty(application_handler): """Test loading applications when the file is empty.""" applications = application_handler.load_applications() assert applications == {} def test_save_application(application_handler): """Test saving an application.""" result = { "listing_id": "12345", "company": "test_company", "link": "http://example.com", "timestamp": "2025-12-27T12:00:00", "success": True, "message": "Application successful", "address": "Test Address", "rooms": "3", "price": "1000" } application_handler.save_application(result) applications = application_handler.load_applications() assert "12345" in applications assert applications["12345"] == result def test_has_applied(application_handler): """Test checking if an application exists.""" result = { "listing_id": "12345", "company": "test_company", "link": "http://example.com", "timestamp": "2025-12-27T12:00:00", "success": True, "message": "Application successful", "address": "Test Address", "rooms": "3", "price": "1000" } application_handler.save_application(result) assert application_handler.has_applied("12345") is True assert application_handler.has_applied("67890") is False