From cf97e4c4e8bbbbb81ce76afaaa1f8e98c2d092c3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 24 Aug 2026 15:41:23 -0500 Subject: [PATCH] [noise] Add a session resume integration test --- .../fixtures/api_noise_resume.yaml | 9 ++++ tests/integration/test_api_noise_resume.py | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/integration/fixtures/api_noise_resume.yaml create mode 100644 tests/integration/test_api_noise_resume.py diff --git a/tests/integration/fixtures/api_noise_resume.yaml b/tests/integration/fixtures/api_noise_resume.yaml new file mode 100644 index 0000000000..2fa0da7b65 --- /dev/null +++ b/tests/integration/fixtures/api_noise_resume.yaml @@ -0,0 +1,9 @@ +esphome: + name: host-noise-resume +host: +api: + encryption: + key: N4Yle5YirwZhPiHHsdZLdOA73ndj/84veVaLhTvxCuU= +# VERY_VERBOSE so the frame helper logs "Session resumed!" +logger: + level: VERY_VERBOSE diff --git a/tests/integration/test_api_noise_resume.py b/tests/integration/test_api_noise_resume.py new file mode 100644 index 0000000000..149a9a535c --- /dev/null +++ b/tests/integration/test_api_noise_resume.py @@ -0,0 +1,46 @@ +"""Integration test for noise session resume.""" + +from __future__ import annotations + +import aioesphomeapi.core +import pytest + +from .types import APIClientConnectedFactory, RunCompiledFunction + +NOISE_KEY = "N4Yle5YirwZhPiHHsdZLdOA73ndj/84veVaLhTvxCuU=" + + +@pytest.mark.asyncio +async def test_api_noise_resume( + yaml_config: str, + run_compiled: RunCompiledFunction, + api_client_connected: APIClientConnectedFactory, +) -> None: + """A reconnect with the ticket from the first connection resumes the session.""" + if not hasattr(aioesphomeapi.core, "ResumeAPIError"): + pytest.skip("aioesphomeapi without noise session resume") + + device_lines: list[str] = [] + + async with ( + run_compiled(yaml_config, line_callback=device_lines.append), + api_client_connected(noise_psk=NOISE_KEY) as client, + ): + # First connection: full handshake, the device issues a ticket + info = await client.device_info() + assert info.name == "host-noise-resume" + assert not any("Session resumed" in line for line in device_lines) + + # Same client reconnects and offers the ticket + await client.disconnect() + await client.connect(login=True) + info = await client.device_info() + assert info.name == "host-noise-resume" + assert any("Session resumed" in line for line in device_lines) + + # The resumed session issued a fresh ticket, so it resumes again + await client.disconnect() + await client.connect(login=True) + info = await client.device_info() + assert info.name == "host-noise-resume" + assert sum("Session resumed" in line for line in device_lines) == 2