|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# test_chat_inline_map_attribution_text.py |
| 3 | +""" |
| 4 | +Functional test for the inline map attribution text fix. |
| 5 | +Version: 0.261.049 |
| 6 | +Implemented in: 0.261.049 |
| 7 | +
|
| 8 | +This test ensures that the chat's inline map card shows a tool result's tile attribution as |
| 9 | +text rather than HTML, and draws a tool result as a map only when its tile template points at |
| 10 | +SimpleChat's Azure Maps tile proxy. |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | +import re |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +ROOT = Path(__file__).resolve().parents[1] |
| 20 | +INLINE_MAPS_JS = ROOT / "application" / "single_app" / "static" / "js" / "chat" / "chat-inline-maps.js" |
| 21 | + |
| 22 | +sys.path.insert(0, str(ROOT / "functional_tests")) |
| 23 | + |
| 24 | +from test_support.versioning import assert_app_version_at_least # noqa: E402 |
| 25 | + |
| 26 | +IMPLEMENTED_IN = "0.261.049" |
| 27 | + |
| 28 | + |
| 29 | +def _source(): |
| 30 | + return INLINE_MAPS_JS.read_text(encoding="utf-8") |
| 31 | + |
| 32 | + |
| 33 | +def test_attribution_is_escaped_before_openlayers_renders_it(): |
| 34 | + """OpenLayers writes attributions as HTML, so the tool result's text is escaped first.""" |
| 35 | + print("Testing inline map attribution escaping...") |
| 36 | + source = _source() |
| 37 | + |
| 38 | + assert 'import { escapeHtml } from "./chat-utils.js";' in source |
| 39 | + assert 'attributions: escapeHtml(payload.tile_attribution || ""),' in source |
| 40 | + assert "attributions: payload.tile_attribution" not in source |
| 41 | + print("Attribution escaping passed!") |
| 42 | + |
| 43 | + |
| 44 | +def test_maps_require_the_tile_proxy_template(): |
| 45 | + """A tool result is drawn as a map only when its tiles come through the tile proxy.""" |
| 46 | + print("Testing the tile template requirement...") |
| 47 | + source = _source() |
| 48 | + |
| 49 | + assert 'const AZURE_MAPS_TILE_PROXY_PATH = "/api/azure-maps/tile";' in source |
| 50 | + assert "&& isTileProxyTemplate(result.map_payload.tile_url_template)" in source |
| 51 | + assert "&& result.map_payload.tile_url_template\n" not in source |
| 52 | + print("Tile template requirement passed!") |
| 53 | + |
| 54 | + |
| 55 | +def test_tile_proxy_template_check_behaves(): |
| 56 | + """Run the real isTileProxyTemplate function in Node against accepted and refused templates.""" |
| 57 | + print("Testing isTileProxyTemplate in Node...") |
| 58 | + source = _source() |
| 59 | + |
| 60 | + match = re.search(r"function isTileProxyTemplate\(template\) \{.*?\n\}\n", source, re.S) |
| 61 | + assert match, "isTileProxyTemplate was not found" |
| 62 | + |
| 63 | + proxy = ( |
| 64 | + "/api/azure-maps/tile?token=abc&api-version=2024-04-01&tilesetId=microsoft.base.road" |
| 65 | + "&zoom={z}&x={x}&y={y}&tileSize=256" |
| 66 | + ) |
| 67 | + cases = { |
| 68 | + proxy: True, |
| 69 | + f" {proxy} ": True, |
| 70 | + "https://tiles.example.test/{z}/{x}/{y}.png": False, |
| 71 | + "//tiles.example.test/api/azure-maps/tile?{z}{x}{y}": False, |
| 72 | + "/api/azure-maps/tiles?{z}{x}{y}": False, |
| 73 | + "/api/azure-maps/tile?zoom={z}&x={x}": False, |
| 74 | + '/api/azure-maps/tile?token="><img src=x>&{z}{x}{y}': False, |
| 75 | + "": False, |
| 76 | + } |
| 77 | + script = ( |
| 78 | + 'const AZURE_MAPS_TILE_PROXY_PATH = "/api/azure-maps/tile";\n' |
| 79 | + + match.group(0) |
| 80 | + + f"const cases = {json.dumps(list(cases.items()))};\n" |
| 81 | + + "const wrong = cases.filter(([value, expected]) => isTileProxyTemplate(value) !== expected);\n" |
| 82 | + + "for (const value of [undefined, null, 42, {}]) { if (isTileProxyTemplate(value)) wrong.push([String(value), false]); }\n" |
| 83 | + + "if (wrong.length) { console.error(JSON.stringify(wrong)); process.exit(1); }\n" |
| 84 | + + "console.log(`${cases.length + 4} template checks passed`);\n" |
| 85 | + ) |
| 86 | + result = subprocess.run(["node", "-e", script], capture_output=True, text=True) |
| 87 | + assert result.returncode == 0, f"isTileProxyTemplate returned the wrong answer for: {result.stderr}" |
| 88 | + print(f" {result.stdout.strip()}") |
| 89 | + print("isTileProxyTemplate passed!") |
| 90 | + |
| 91 | + |
| 92 | +def test_version_was_incremented(): |
| 93 | + assert_app_version_at_least(IMPLEMENTED_IN) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + tests = [ |
| 98 | + test_attribution_is_escaped_before_openlayers_renders_it, |
| 99 | + test_maps_require_the_tile_proxy_template, |
| 100 | + test_tile_proxy_template_check_behaves, |
| 101 | + test_version_was_incremented, |
| 102 | + ] |
| 103 | + failures = 0 |
| 104 | + for test in tests: |
| 105 | + try: |
| 106 | + test() |
| 107 | + except Exception as exc: # noqa: BLE001 - report every failing check |
| 108 | + failures += 1 |
| 109 | + print(f"FAILED {test.__name__}: {exc}") |
| 110 | + print(f"\n{len(tests) - failures}/{len(tests)} tests passed") |
| 111 | + sys.exit(1 if failures else 0) |
0 commit comments