atiaomar1978-hub commented on code in PR #25204: URL: https://github.com/apache/camel/pull/25204#discussion_r3684414170
########## dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceViewerEditTest.java: ########## @@ -0,0 +1,407 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.tui; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import dev.tamboui.buffer.Buffer; +import dev.tamboui.layout.Rect; +import dev.tamboui.terminal.Frame; +import dev.tamboui.text.Span; +import dev.tamboui.tui.event.KeyCode; +import dev.tamboui.tui.event.KeyEvent; +import dev.tamboui.tui.event.KeyModifiers; +import dev.tamboui.tui.event.MouseButton; +import dev.tamboui.tui.event.MouseEvent; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for SourceViewer plain-text edit mode (CAMEL-24287). + */ +class SourceViewerEditTest { + + @TempDir + Path tempDir; + + private SourceViewer viewer; + private Path sourceFile; + + @BeforeEach + void setUp() throws Exception { + Theme.resetForTesting(); + viewer = new SourceViewer(); + sourceFile = tempDir.resolve("route.camel.yaml"); + Files.writeString(sourceFile, """ + - route: + from: + uri: timer:tick + steps: + - to: log:info + """, StandardCharsets.UTF_8); + } + + @Test + void loadFileMarksLocalWritableFileEditable() { + viewer.loadFile(sourceFile); + + assertThat(viewer.isVisible()).isTrue(); + assertThat(viewer.isEditable()).isTrue(); + assertThat(viewer.isEditMode()).isFalse(); + } + + @Test + void eEntersEditModeForLocalFile() { + viewer.loadFile(sourceFile); + + assertThat(viewer.handleKeyEvent(KeyEvent.ofChar('e', KeyModifiers.NONE))).isTrue(); + + assertThat(viewer.isEditMode()).isTrue(); + assertThat(viewer.isTextInputActive()).isTrue(); + } + + @Test + void escCancelsEditModeWithoutClosingViewer() { + viewer.loadFile(sourceFile); + viewer.handleKeyEvent(KeyEvent.ofChar('e', KeyModifiers.NONE)); + assertThat(viewer.isEditMode()).isTrue(); + + assertThat(viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.ESCAPE, KeyModifiers.NONE))).isTrue(); + + assertThat(viewer.isEditMode()).isFalse(); + assertThat(viewer.isVisible()).isTrue(); + assertThat(viewer.isEditable()).isTrue(); + } + + @Test + void typingInEditModeDoesNotCloseViewer() { + viewer.loadFile(sourceFile); + viewer.enterEditMode(); + + assertThat(viewer.handleKeyEvent(KeyEvent.ofChar('c', KeyModifiers.NONE))).isTrue(); + assertThat(viewer.handleKeyEvent(KeyEvent.ofChar('q', KeyModifiers.NONE))).isTrue(); + assertThat(viewer.handleKeyEvent(KeyEvent.ofChar('1', KeyModifiers.NONE))).isTrue(); + + assertThat(viewer.isEditMode()).isTrue(); + assertThat(viewer.isVisible()).isTrue(); + } + + @Test + void f5SavesEditedContentToDisk() throws Exception { + viewer.loadFile(sourceFile); + viewer.enterEditMode(); + + // Append a newline and a comment via editor keys + viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.END, KeyModifiers.NONE)); + // move to end of document + for (int i = 0; i < 20; i++) { + viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.DOWN, KeyModifiers.NONE)); + } + viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.END, KeyModifiers.NONE)); + viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.ENTER, KeyModifiers.NONE)); + for (char ch : "# edited".toCharArray()) { + viewer.handleKeyEvent(KeyEvent.ofChar(ch, KeyModifiers.NONE)); + } + + assertThat(viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.F5, KeyModifiers.NONE))).isTrue(); + + assertThat(viewer.isEditMode()).isFalse(); + assertThat(viewer.isVisible()).isTrue(); + String saved = Files.readString(sourceFile, StandardCharsets.UTF_8); + assertThat(saved).contains("# edited"); + assertThat(saved).contains("timer:tick"); + } + + @Test + void cancelDiscardsUnsavedEdits() throws Exception { + String original = Files.readString(sourceFile, StandardCharsets.UTF_8); + viewer.loadFile(sourceFile); + viewer.enterEditMode(); + + for (char ch : "CHANGED".toCharArray()) { + viewer.handleKeyEvent(KeyEvent.ofChar(ch, KeyModifiers.NONE)); + } + viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.ESCAPE, KeyModifiers.NONE)); + + assertThat(viewer.isEditMode()).isFalse(); + assertThat(Files.readString(sourceFile, StandardCharsets.UTF_8)).isEqualTo(original); + } + + @Test + void pasteInsertsIntoEditBuffer() throws Exception { + viewer.loadFile(sourceFile); + viewer.enterEditMode(); + viewer.handlePaste("\n# pasted-line\n"); + viewer.handleKeyEvent(KeyEvent.ofKey(KeyCode.F5, KeyModifiers.NONE)); + + assertThat(Files.readString(sourceFile, StandardCharsets.UTF_8)).contains("# pasted-line"); + } + + @Test + void remoteLoadSourceIsNotEditable() { + MonitorContext ctx = new MonitorContext( + new java.util.concurrent.atomic.AtomicReference<>(java.util.List.of()), Review Comment: Fixed in `40febda5f24` — `AtomicReference` and `List` now use imported simple names throughout the test file. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
