From 8ccf9d7d89bc395ad7dd19dde840401a5b84623d Mon Sep 17 00:00:00 2001 From: Anionex <1005128408@qq.com> Date: Fri, 14 Aug 2026 13:11:35 +0800 Subject: [PATCH] fix: preserve trace SVG byte contract on Windows --- .github/workflows/ci.yml | 5 ++++- bin/trace | 10 ++++++++-- tests/test_trace.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 222c02f..9ae1e2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,6 +36,7 @@ jobs: python tests/smoke_test_egress_failover.py python tests/test_vision_client.py python tests/test_restore_ui_playbook.py + python tests/test_trace.py python-egress-windows: runs-on: windows-latest @@ -47,7 +48,9 @@ jobs: - name: Compile proxy entry points run: python -m py_compile vision_proxy.py vision_client.py tests/smoke_test_egress_failover.py - name: Run Windows egress smoke test - run: python tests/smoke_test_egress_failover.py + run: | + python tests/smoke_test_egress_failover.py + python tests/test_trace.py extensions: runs-on: ubuntu-latest diff --git a/bin/trace b/bin/trace index 4b2a967..39b4d97 100755 --- a/bin/trace +++ b/bin/trace @@ -34,6 +34,12 @@ def truncate_decimals(svg, places=2): return re.sub(r"-?\d+\.\d{3,}", lambda m: f"{float(m.group()):.{places}f}", svg) +def write_svg(path, svg): + """Write the exact UTF-8 payload whose byte count is reported to callers.""" + payload = svg.encode("utf-8") + return Path(path).expanduser().write_bytes(payload) + + def prepare_input(path, region, scale): """Crop/upscale with Pillow when asked; return the path vtracer reads and the scale used. @@ -113,8 +119,8 @@ def main(): "--color is a last resort: on an anti-aliased image it splits every gray " "level into its own path.", file=sys.stderr) if args.output: - Path(args.output).expanduser().write_text(svg) - print(f"wrote {args.output} ({len(svg)} bytes, {paths} paths, traced at {scale}x)") + bytes_written = write_svg(args.output, svg) + print(f"wrote {args.output} ({bytes_written} bytes, {paths} paths, traced at {scale}x)") else: print(svg) diff --git a/tests/test_trace.py b/tests/test_trace.py index d97d7eb..8431d34 100644 --- a/tests/test_trace.py +++ b/tests/test_trace.py @@ -37,6 +37,18 @@ def main(): assert "7.891011" not in truncated and "7.89" in truncated print("PASS: background stripping and decimal truncation") + with tempfile.TemporaryDirectory() as raw: + out = os.path.join(raw, "multiline.svg") + multiline = "\n几何\n\n\n" + reported = mod.write_svg(out, multiline) + payload = multiline.encode("utf-8") + with open(out, "rb") as handle: + written = handle.read() + assert written == payload, "SVG output must preserve LF bytes on every platform" + assert reported == len(written), "reported bytes must equal the file size on disk" + assert reported > len(multiline), "the byte contract must not use Unicode character count" + print("PASS: multiline SVG output preserves exact UTF-8 bytes") + try: import vtracer # noqa: F401 from PIL import Image