| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Nodes; |
| 3 | |
| 4 | namespace CascadeIDE.Tests; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Убирает машинно-зависимые и «шумные» поля из <c>get_ide_state</c> для сравнения в тестах (ADR 0052). |
| 8 | /// </summary> |
| 9 | public static class AgentContractIdeStateRedaction |
| 10 | { |
| 11 | /// <summary>Детерминированный вид для assert: два подряд вызова headless после редукции должны совпасть.</summary> |
| 12 | public static string RedactForStableCompare(string ideStateJson) |
| 13 | { |
| 14 | var node = JsonNode.Parse(ideStateJson) as JsonObject |
| 15 | ?? throw new InvalidOperationException("Expected JSON object."); |
| 16 | RedactRoot(node); |
| 17 | return node.ToJsonString(new JsonSerializerOptions { WriteIndented = false }); |
| 18 | } |
| 19 | |
| 20 | private static void RedactRoot(JsonObject o) |
| 21 | { |
| 22 | o["solution_path"] = null; |
| 23 | o["current_file_path"] = ""; |
| 24 | o["selected_solution_path"] = null; |
| 25 | o["diagnostics"] = new JsonArray(); |
| 26 | |
| 27 | if (o["editor"] is JsonObject ed) |
| 28 | { |
| 29 | ed["content_length"] = 0; |
| 30 | ed["selection_start"] = 0; |
| 31 | ed["selection_length"] = 0; |
| 32 | } |
| 33 | |
| 34 | if (o["breakpoints"] is JsonObject bp) |
| 35 | { |
| 36 | if (bp["current_file"] is JsonArray cur) |
| 37 | cur.Clear(); |
| 38 | bp["debugger_count"] = 0; |
| 39 | } |
| 40 | |
| 41 | if (o["debug"] is JsonObject dbg) |
| 42 | { |
| 43 | dbg["position_file"] = null; |
| 44 | dbg["position_line"] = 0; |
| 45 | dbg["stack_count"] = 0; |
| 46 | dbg["variables_count"] = 0; |
| 47 | } |
| 48 | |
| 49 | if (o["build"] is JsonObject b) |
| 50 | { |
| 51 | b["output_preview"] = ""; |
| 52 | b["binlog_path"] = null; |
| 53 | } |
| 54 | |
| 55 | o["agent_trace_step_count"] = 0; |
| 56 | o["is_autonomous_running"] = false; |
| 57 | } |
| 58 | } |
| 59 | |