From 65de74193a206b43c3cfc9923b5f3dc02fb49605 Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 24 Feb 2026 16:38:02 +0100 Subject: [PATCH] test: add _strip_html edge cases and config validation tests strip_html: nested tags, malformed HTML, comments, entities. config: unknown key filtering, missing file, mixed valid/invalid. --- tests/test_config.py | 49 ++++++++++++++++++++++++++++++++- tests/test_strip_html.py | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/test_strip_html.py diff --git a/tests/test_config.py b/tests/test_config.py index b7a4896..5683f34 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,6 +1,9 @@ """Tests for configuration module.""" -from tuimble.config import AudioConfig, Config, PttConfig, ServerConfig +from tuimble.config import ( + AudioConfig, Config, PttConfig, ServerConfig, + _load_section, load_config, +) def test_default_config(): @@ -45,3 +48,47 @@ def test_server_cert_custom(): srv = ServerConfig(certfile="/path/cert.pem", keyfile="/path/key.pem") assert srv.certfile == "/path/cert.pem" assert srv.keyfile == "/path/key.pem" + + +# -- _load_section tests ----------------------------------------------------- + + +def test_load_section_filters_unknown_keys(): + """Unknown keys are silently dropped, valid keys are kept.""" + result = _load_section(ServerConfig, { + "host": "example.com", + "typo_field": "oops", + "another_bad": 42, + }) + assert result.host == "example.com" + assert result.port == 64738 # default preserved + + +def test_load_section_empty_dict(): + result = _load_section(PttConfig, {}) + assert result.key == "f4" + assert result.mode == "toggle" + + +def test_load_section_all_valid(): + result = _load_section(PttConfig, {"key": "space", "mode": "hold"}) + assert result.key == "space" + assert result.mode == "hold" + + +def test_load_config_missing_file(tmp_path): + """Missing config file returns defaults.""" + cfg = load_config(tmp_path / "nonexistent.toml") + assert cfg.server.host == "localhost" + + +def test_load_config_with_unknown_keys(tmp_path): + """Config file with unknown keys loads without error.""" + toml = tmp_path / "config.toml" + toml.write_text( + '[server]\nhost = "example.com"\nbogus = true\n' + '[ptt]\nfuture_option = "x"\n' + ) + cfg = load_config(toml) + assert cfg.server.host == "example.com" + assert cfg.ptt.key == "f4" # default, not overwritten diff --git a/tests/test_strip_html.py b/tests/test_strip_html.py new file mode 100644 index 0000000..f7db619 --- /dev/null +++ b/tests/test_strip_html.py @@ -0,0 +1,59 @@ +"""Tests for _strip_html edge cases.""" + +from tuimble.app import _strip_html + + +def test_plain_text_unchanged(): + assert _strip_html("hello world") == "hello world" + + +def test_simple_tags_stripped(): + assert _strip_html("bold") == "bold" + + +def test_nested_tags(): + assert _strip_html("

text

") == "text" + + +def test_self_closing_tags(): + assert _strip_html("line
break") == "linebreak" + + +def test_entities_unescaped(): + assert _strip_html("& < >") == "& < >" + + +def test_html_entities_in_tags(): + assert _strip_html("&") == "&" + + +def test_mumble_style_message(): + """Typical Mumble chat message with anchor tag.""" + msg = 'link text and more' + assert _strip_html(msg) == "link text and more" + + +def test_img_tag_with_attributes(): + assert _strip_html('beforepicafter') == "beforeafter" + + +def test_comment_stripped(): + assert _strip_html("beforeafter") == "beforeafter" + + +def test_empty_string(): + assert _strip_html("") == "" + + +def test_only_tags(): + assert _strip_html("

") == "" + + +def test_unclosed_tag(): + """Malformed HTML should not crash.""" + result = _strip_html("unclosed") + assert "unclosed" in result + + +def test_multiple_entities(): + assert _strip_html(""quoted"") == '"quoted"'