| 1 | using CascadeIDE.Features.Chat; |
| 2 | using CascadeIDE.Services; |
| 3 | using CascadeIDE.Services.Intercom; |
| 4 | using Xunit; |
| 5 | |
| 6 | namespace CascadeIDE.Tests; |
| 7 | |
| 8 | public sealed class IntercomAttachAffordanceTests |
| 9 | { |
| 10 | [Fact] |
| 11 | public void AttachSelectionToComposer_InsertsMarker_WhenSelectionPresent() |
| 12 | { |
| 13 | var vm = CreateChatPanel( |
| 14 | filePath: @"D:\ws\src\Foo.cs", |
| 15 | editorText: "line1\nline2\n", |
| 16 | selectionStart: 0, |
| 17 | selectionLength: 5); |
| 18 | |
| 19 | var message = vm.AttachSelectionToComposer(); |
| 20 | |
| 21 | Assert.Contains("Прикреплено", message, StringComparison.Ordinal); |
| 22 | Assert.Contains("⟦a:", vm.ChatInput, StringComparison.Ordinal); |
| 23 | } |
| 24 | |
| 25 | [Fact] |
| 26 | public void AttachDiagnosticAtCaret_Fails_WhenNoDiagnostic() |
| 27 | { |
| 28 | var vm = CreateChatPanel( |
| 29 | filePath: @"D:\ws\src\Foo.cs", |
| 30 | editorText: "ok\n", |
| 31 | caretOffset: 1); |
| 32 | |
| 33 | var message = vm.AttachDiagnosticAtCaretToComposer(); |
| 34 | |
| 35 | Assert.Contains("диагност", message, StringComparison.OrdinalIgnoreCase); |
| 36 | } |
| 37 | |
| 38 | [Fact] |
| 39 | public void ApplyAttachDropPayload_ProblemJson_InsertsMarker() |
| 40 | { |
| 41 | var vm = CreateChatPanel( |
| 42 | filePath: @"D:\ws\src\Foo.cs", |
| 43 | editorText: "", |
| 44 | selectionStart: 0, |
| 45 | selectionLength: 0); |
| 46 | |
| 47 | var payload = IntercomAttachDragFormats.EncodeTextPayload( |
| 48 | """{"kind":"problem","filePath":"D:\\ws\\src\\Foo.cs","line":3,"severity":"error","id":"CS1001","message":"; expected"}"""); |
| 49 | |
| 50 | var message = vm.ApplyAttachDropPayload(payload); |
| 51 | |
| 52 | Assert.Contains("Прикреплено", message, StringComparison.Ordinal); |
| 53 | Assert.Contains("⟦a:", vm.ChatInput, StringComparison.Ordinal); |
| 54 | } |
| 55 | |
| 56 | private static ChatPanelViewModel CreateChatPanel( |
| 57 | string filePath, |
| 58 | string editorText, |
| 59 | int selectionStart = 0, |
| 60 | int selectionLength = 0, |
| 61 | int caretOffset = 0) |
| 62 | { |
| 63 | var minimizer = new ContextMinimizer(new CSharpLanguageService()); |
| 64 | var aiProviderManager = new AiProviderManager(minimizer, _ => (null, "")); |
| 65 | var vm = new ChatPanelViewModel( |
| 66 | aiProviderManager, |
| 67 | () => "ollama", |
| 68 | () => null, |
| 69 | () => true, |
| 70 | () => false, |
| 71 | () => false, |
| 72 | () => filePath, |
| 73 | () => editorText, |
| 74 | () => @"D:\ws", |
| 75 | () => "", |
| 76 | () => "{}", |
| 77 | () => false, |
| 78 | () => null, |
| 79 | getSolutionPath: () => @"D:\ws\app.sln", |
| 80 | getEditorSelectionStart: () => selectionStart, |
| 81 | getEditorSelectionLength: () => selectionLength, |
| 82 | getEditorCaretOffset: () => caretOffset); |
| 83 | |
| 84 | vm.SetDiagnosticStripsAccessor(() => Array.Empty<EditorDiagnosticStrip>()); |
| 85 | return vm; |
| 86 | } |
| 87 | } |
| 88 | |