# SPDX-License-Identifier: AGPL-3.0-or-later """Coverage for the operator-upload instrumenters. Each instrumenter is round-tripped against a small, real-shaped fixture. We assert: * the callback URL ends up somewhere in the mutated bytes; * the output still parses (zip stays a valid zip; HTML stays reasonable); * the rejection paths surface :class:`InstrumenterRejectedError` with a useful message. """ from __future__ import annotations import io import zipfile import pytest from decnet.canary import CanaryContext, get_instrumenter from decnet.canary.base import InstrumenterRejectedError def _ctx(slug: str = "slug-abc") -> CanaryContext: return CanaryContext( callback_token=slug, http_base="https://canary.example.test", dns_zone="canary.example.test", persona="linux", ) # ----------------------- passthrough ------------------------------------ def test_passthrough_preserves_bytes() -> None: ins = get_instrumenter("passthrough") out = ins.instrument(b"\x00\x01\x02bin", _ctx(), target_path="/tmp/x.bin") assert out.content == b"\x00\x01\x02bin" assert out.path == "/tmp/x.bin" assert out.instrumenter == "passthrough" # ----------------------- plain ------------------------------------------ def test_plain_substitutes_url_placeholder() -> None: ins = get_instrumenter("plain") blob = b"api: {{CANARY_URL}}\nhost: {{CANARY_HOST}}\n" out = ins.instrument(blob, _ctx("slugXYZ"), target_path="/etc/x.yaml") assert b"https://canary.example.test/c/slugXYZ" in out.content assert b"slugXYZ.canary.example.test" in out.content assert b"{{CANARY_URL}}" not in out.content def test_plain_appends_when_no_placeholder() -> None: ins = get_instrumenter("plain") out = ins.instrument(b"key=value\n", _ctx("s1"), target_path="/etc/x.env") assert b"https://canary.example.test/c/s1" in out.content # Original content survives. assert out.content.startswith(b"key=value\n") @pytest.mark.parametrize( "head, expect_prefix", [ (b"[default]\nfoo=1\n", b"; "), (b"// js code\nconst x = 1;\n", b"// "), (b"#!/bin/bash\necho hi\n", b"# "), ], ) def test_plain_picks_comment_prefix(head: bytes, expect_prefix: bytes) -> None: ins = get_instrumenter("plain") out = ins.instrument(head, _ctx(), target_path="/etc/x") # The appended comment line uses the matching prefix. appended = out.content[len(head):] assert appended.lstrip(b"\n").startswith(expect_prefix) # ----------------------- html ------------------------------------------- def test_html_injects_pixel_before_body_close() -> None: ins = get_instrumenter("html") blob = b"