|
2 | 2 |
|
3 | 3 | import static com.odde.doughnut.controllers.AiControllerExtractNoteTestSupport.*; |
4 | 4 | import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.mockito.Mockito.verify; |
5 | 6 |
|
6 | 7 | import com.fasterxml.jackson.core.JsonProcessingException; |
7 | 8 | import com.odde.doughnut.entities.Note; |
8 | 9 | import com.odde.doughnut.entities.repositories.NoteRepository; |
9 | 10 | import com.odde.doughnut.exceptions.UnexpectedNoAccessRightException; |
10 | 11 | import com.odde.doughnut.services.ai.NoteExtractionResult; |
11 | 12 | import com.odde.doughnut.services.ai.NoteRefinementLayout; |
| 13 | +import com.odde.doughnut.services.ai.NoteRefinementLayoutItem; |
12 | 14 | import com.odde.doughnut.testability.OpenAiStructuredResponseMock; |
13 | 15 | import com.openai.client.OpenAIClient; |
| 16 | +import com.openai.models.responses.StructuredResponseCreateParams; |
14 | 17 | import java.util.List; |
| 18 | +import java.util.Optional; |
15 | 19 | import org.junit.jupiter.api.BeforeEach; |
16 | 20 | import org.junit.jupiter.api.Nested; |
17 | 21 | import org.junit.jupiter.api.Test; |
| 22 | +import org.mockito.ArgumentCaptor; |
18 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
19 | 24 | import org.springframework.test.context.bean.override.mockito.MockitoBean; |
20 | 25 |
|
@@ -64,5 +69,92 @@ void shouldReturnExtractionPreviewWithoutPersisting() |
64 | 69 | makeMe.entityPersister.refresh(testNote); |
65 | 70 | assertThat(testNote.getContent()).isEqualTo(originalContent); |
66 | 71 | } |
| 72 | + |
| 73 | + private NoteRefinementLayout sampleLayout() { |
| 74 | + return new NoteRefinementLayout( |
| 75 | + List.of( |
| 76 | + new NoteRefinementLayoutItem( |
| 77 | + "p1", |
| 78 | + "Main concept", |
| 79 | + false, |
| 80 | + List.of( |
| 81 | + new NoteRefinementLayoutItem( |
| 82 | + "p1-1", "key suggestion to extract", false, List.of()))), |
| 83 | + new NoteRefinementLayoutItem("p2", "Other point", false, List.of()))); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void shouldCallExtractNoteWithStructuredInstructions() |
| 88 | + throws UnexpectedNoAccessRightException, JsonProcessingException { |
| 89 | + Note testNote = newRootNoteWithExtractableContent(makeMe, currentUser.getUser()); |
| 90 | + openAiStructuredResponseMock.stubStructuredResponse( |
| 91 | + extractionResult( |
| 92 | + "Extracted Note", |
| 93 | + "Expanded content for the new note.", |
| 94 | + "Updated parent with summary.")); |
| 95 | + NoteRefinementLayout layout = sampleLayout(); |
| 96 | + |
| 97 | + controller.extractNotePreview( |
| 98 | + testNote, layoutSelectionRequest(layout, List.of("p1-1", "p2"))); |
| 99 | + |
| 100 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 101 | + ArgumentCaptor<StructuredResponseCreateParams<NoteExtractionResult>> paramsCaptor = |
| 102 | + ArgumentCaptor.forClass((Class) StructuredResponseCreateParams.class); |
| 103 | + verify(openAiStructuredResponseMock.responseService()).create(paramsCaptor.capture()); |
| 104 | + StructuredResponseCreateParams<NoteExtractionResult> params = paramsCaptor.getValue(); |
| 105 | + String instructions = params.rawParams().instructions().orElse(""); |
| 106 | + assertThat(params.rawParams().maxOutputTokens()).isEqualTo(Optional.of(3000L)); |
| 107 | + assertThat(instructions).contains("Full note layout:"); |
| 108 | + assertThat(instructions).contains("\"id\" : \"p1-1\""); |
| 109 | + assertThat(instructions).contains("Selected layout item ids to extract together"); |
| 110 | + assertThat(instructions).contains("[p1-1, p2]"); |
| 111 | + assertThat(instructions).contains("- p1-1: \"key suggestion to extract\""); |
| 112 | + assertThat(instructions).contains("- p2: \"Other point\""); |
| 113 | + assertThat(instructions) |
| 114 | + .contains( |
| 115 | + "Prefer replacing the removed content in the original note with a natural contextual wiki link to the new note"); |
| 116 | + assertThat(instructions) |
| 117 | + .contains( |
| 118 | + "Do not add YAML frontmatter or metadata properties, such as parent:, merely to backlink the new note to the original note"); |
| 119 | + assertThat(instructions) |
| 120 | + .contains("Never use a generic parent property as the default extraction relationship"); |
| 121 | + assertThat(instructions).contains("Wiki links are case-insensitive"); |
| 122 | + assertThat(instructions).contains("[[Canonical Note Title|visible text]]"); |
| 123 | + assertThat(instructions).contains("alreadyExtracted"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void shouldSanitizePathSeparatorsInExtractionPreview() |
| 128 | + throws UnexpectedNoAccessRightException, JsonProcessingException { |
| 129 | + Note testNote = newRootNoteWithExtractableContent(makeMe, currentUser.getUser()); |
| 130 | + openAiStructuredResponseMock.stubStructuredResponse( |
| 131 | + extractionResult( |
| 132 | + "foo/bar: baz", |
| 133 | + "See [[foo/bar: baz|link]] and [[MyNb:foo/bar|nb]].", |
| 134 | + "Back to [[foo/bar: baz]].")); |
| 135 | + NoteRefinementLayout layout = layoutWithItem("p1", "key suggestion to extract"); |
| 136 | + |
| 137 | + NoteExtractionResult response = |
| 138 | + controller.extractNotePreview(testNote, layoutSelectionRequest(layout, List.of("p1"))); |
| 139 | + |
| 140 | + assertThat(response.getNewNoteTitle()).isEqualTo("foo/bar: baz"); |
| 141 | + assertThat(response.getNewNoteContent()) |
| 142 | + .isEqualTo("See [[foo/bar: baz|link]] and [[MyNb:foo/bar|nb]]."); |
| 143 | + assertThat(response.getUpdatedOriginalNoteContent()).isEqualTo("Back to [[foo/bar: baz]]."); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void shouldTrimSurroundingWhitespaceFromExtractionPreviewTitle() |
| 148 | + throws UnexpectedNoAccessRightException, JsonProcessingException { |
| 149 | + Note testNote = newRootNoteWithExtractableContent(makeMe, currentUser.getUser()); |
| 150 | + openAiStructuredResponseMock.stubStructuredResponse( |
| 151 | + extractionResult("\u3000Extracted Note\u3000", "Expanded content.", "Updated parent.")); |
| 152 | + NoteRefinementLayout layout = layoutWithItem("p1", "key suggestion to extract"); |
| 153 | + |
| 154 | + NoteExtractionResult response = |
| 155 | + controller.extractNotePreview(testNote, layoutSelectionRequest(layout, List.of("p1"))); |
| 156 | + |
| 157 | + assertThat(response.getNewNoteTitle()).isEqualTo("Extracted Note"); |
| 158 | + } |
67 | 159 | } |
68 | 160 | } |
0 commit comments