|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for compile_report.py and render_table.py. |
| 16 | +
|
| 17 | +Run with: python3 -m unittest discover -s .agents/skills/security-status-report/scripts |
| 18 | +""" |
| 19 | + |
| 20 | +import json |
| 21 | +import os |
| 22 | +import tempfile |
| 23 | +import unittest |
| 24 | + |
| 25 | +from compile_report import compile_report |
| 26 | +from render_table import render_table |
| 27 | + |
| 28 | + |
| 29 | +class CompileReportTest(unittest.TestCase): |
| 30 | + def test_copies_summaries(self): |
| 31 | + with tempfile.TemporaryDirectory() as d: |
| 32 | + threats = os.path.join(d, "threats.json") |
| 33 | + with open(threats, "w") as f: |
| 34 | + json.dump([{"threat_id": "T-01", "priority": "High", "threat": "x"}], f) |
| 35 | + with open(os.path.join(d, "T-01.json"), "w") as f: |
| 36 | + json.dump({"threat_id": "T-01", "quality": 0.5, "mitigation_summary": "m", "remaining_gap": "g"}, f) |
| 37 | + out = os.path.join(d, "final.json") |
| 38 | + compile_report(threats, d, out) |
| 39 | + with open(out) as f: |
| 40 | + got = json.load(f)[0] |
| 41 | + self.assertEqual(got["mitigation_summary"], "m") |
| 42 | + self.assertEqual(got["remaining_gap"], "g") |
| 43 | + |
| 44 | + |
| 45 | +class RenderTableTest(unittest.TestCase): |
| 46 | + def test_sorts_by_priority_then_id(self): |
| 47 | + data = [ |
| 48 | + {"threat_id": "T-10", "priority": "Low", "quality": 0.1}, |
| 49 | + {"threat_id": "T-03", "priority": "Critical", "quality": 0.2}, |
| 50 | + {"threat_id": "T-02", "priority": "High", "quality": 0.3}, |
| 51 | + {"threat_id": "T-01", "priority": "Critical", "quality": 0.4}, |
| 52 | + ] |
| 53 | + rows = [l.split(" | ")[0].lstrip("| ") for l in render_table(data).splitlines() if l.startswith("| T-")] |
| 54 | + self.assertEqual(rows, ["T-01", "T-03", "T-02", "T-10"]) |
| 55 | + |
| 56 | + def test_error_row_and_pipe_escaping(self): |
| 57 | + data = [ |
| 58 | + {"threat_id": "T-01", "priority": "High", "error": "Missing result file"}, |
| 59 | + {"threat_id": "T-02", "priority": "High", "quality": 1.0, "mitigation_summary": "a|b", "remaining_gap": "None"}, |
| 60 | + ] |
| 61 | + out = render_table(data) |
| 62 | + self.assertIn("| T-01 | High | ERROR | | Missing result file |", out) |
| 63 | + self.assertIn("| T-02 | High | 1.00 | a/b | None |", out) |
| 64 | + self.assertIn("Mean score: 1.00", out) |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + unittest.main() |
0 commit comments