| 1 | using System.Text.Json; |
| 2 | using CascadeIDE.Features.WebAiPortal.Application; |
| 3 | using Xunit; |
| 4 | |
| 5 | namespace CascadeIDE.Tests; |
| 6 | |
| 7 | public sealed class WebAiPortalBridgePayloadResolutionTests |
| 8 | { |
| 9 | [Fact] |
| 10 | public void TryResolve_prefers_fence_over_surrounding_noise() |
| 11 | { |
| 12 | const string md = """ |
| 13 | text |
| 14 | ```json-cascade |
| 15 | { "command_id": "codebase_index_search", "args": { "query": "x" } } |
| 16 | ``` |
| 17 | """; |
| 18 | Assert.True(WebAiPortalBridgePayloadResolution.TryResolvePayload(md, out var json, out var src)); |
| 19 | Assert.Equal(WebAiPortalBridgePayloadResolution.PayloadSourceHint.FencedMarkdown, src); |
| 20 | Assert.Contains("codebase_index_search", json, StringComparison.Ordinal); |
| 21 | } |
| 22 | |
| 23 | [Fact] |
| 24 | public void TryResolve_bare_json_when_no_fence() |
| 25 | { |
| 26 | const string s = """{"command_id":"get_editor_state","args":{"max_preview_chars":10000}}"""; |
| 27 | Assert.True(WebAiPortalBridgePayloadResolution.TryResolvePayload(s, out var json, out var src)); |
| 28 | Assert.Equal(WebAiPortalBridgePayloadResolution.PayloadSourceHint.BareJson, src); |
| 29 | Assert.Equal(s, json); |
| 30 | } |
| 31 | |
| 32 | [Fact] |
| 33 | public void TryResolve_rejects_plain_text() |
| 34 | { |
| 35 | Assert.False(WebAiPortalBridgePayloadResolution.TryResolvePayload("hello", out _, out _)); |
| 36 | Assert.False(WebAiPortalBridgePayloadResolution.TryParseBareExecuteCommand("{ \"no\": true }", out _)); |
| 37 | Assert.False(WebAiPortalBridgePayloadResolution.TryParseBareExecuteCommand("{\"command_id\":17}", out _)); |
| 38 | } |
| 39 | |
| 40 | [Fact] |
| 41 | public void Unwrap_invoke_result_strips_json_string_wrapper_like_webview2() |
| 42 | { |
| 43 | const string inner = """{"command_id":"get_editor_state","args":{}}"""; |
| 44 | var wrapped = JsonSerializer.Serialize(inner); |
| 45 | Assert.Equal(inner, WebAiPortalLastCommandDomProbe.UnwrapWrappedJsonString(wrapped)); |
| 46 | } |
| 47 | |
| 48 | [Fact] |
| 49 | public void Unwrap_passes_through_raw_json_object() |
| 50 | { |
| 51 | const string raw = """{"command_id":"x"}"""; |
| 52 | Assert.Equal(raw, WebAiPortalLastCommandDomProbe.UnwrapWrappedJsonString(raw)); |
| 53 | } |
| 54 | |
| 55 | [Fact] |
| 56 | public void TryGetCommandId_reads_trimmed_string_property() |
| 57 | { |
| 58 | const string s = """ {"command_id" : "get_editor_state" } """; |
| 59 | Assert.True(WebAiPortalBridgePayloadResolution.TryGetCommandId(s, out var id)); |
| 60 | Assert.Equal("get_editor_state", id); |
| 61 | |
| 62 | Assert.False(WebAiPortalBridgePayloadResolution.TryGetCommandId("{} ", out _), "нет command_id"); |
| 63 | Assert.False(WebAiPortalBridgePayloadResolution.TryGetCommandId("", out _), "пустой"); |
| 64 | } |
| 65 | } |
| 66 | |