|
| 1 | +package org.opencds.cqf.cql.ls.server.manager; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.net.URI; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import org.cqframework.cql.cql2elm.CqlCompilerOptions; |
| 12 | +import org.cqframework.cql.cql2elm.LibraryBuilder.SignatureLevel; |
| 13 | +import org.eclipse.lsp4j.DidChangeWatchedFilesParams; |
| 14 | +import org.eclipse.lsp4j.FileChangeType; |
| 15 | +import org.eclipse.lsp4j.FileEvent; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.opencds.cqf.cql.ls.core.ContentService; |
| 19 | +import org.opencds.cqf.cql.ls.core.utility.Uris; |
| 20 | +import org.opencds.cqf.cql.ls.server.event.DidChangeWatchedFilesEvent; |
| 21 | +import org.opencds.cqf.cql.ls.server.service.TestContentService; |
| 22 | + |
| 23 | +class CompilerOptionsManagerTest { |
| 24 | + |
| 25 | + private CompilerOptionsManager manager; |
| 26 | + |
| 27 | + // A URI whose "head" (parent path) is /org/opencds/cqf/cql/ls/server/ |
| 28 | + // TestContentService.read will be called for the cql-options.json path, returning null |
| 29 | + // (no such classpath resource), so default options are used. |
| 30 | + private static final URI TEST_URI = Uris.parseOrNull("/org/opencds/cqf/cql/ls/server/One.cql"); |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() { |
| 34 | + ContentService cs = new TestContentService(); |
| 35 | + manager = new CompilerOptionsManager(cs); |
| 36 | + } |
| 37 | + |
| 38 | + // ----------------------------------------------------------------------- |
| 39 | + // getOptions — returns options enriched with required flags |
| 40 | + // ----------------------------------------------------------------------- |
| 41 | + |
| 42 | + @Test |
| 43 | + void getOptions_noOptionsFile_returnsNonNull() { |
| 44 | + CqlCompilerOptions options = manager.getOptions(TEST_URI); |
| 45 | + assertNotNull(options); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void getOptions_alwaysIncludesEnableLocators() { |
| 50 | + CqlCompilerOptions options = manager.getOptions(TEST_URI); |
| 51 | + assertTrue(options.getOptions().contains(CqlCompilerOptions.Options.EnableLocators)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void getOptions_alwaysIncludesEnableResultTypes() { |
| 56 | + CqlCompilerOptions options = manager.getOptions(TEST_URI); |
| 57 | + assertTrue(options.getOptions().contains(CqlCompilerOptions.Options.EnableResultTypes)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void getOptions_alwaysIncludesEnableAnnotations() { |
| 62 | + CqlCompilerOptions options = manager.getOptions(TEST_URI); |
| 63 | + assertTrue(options.getOptions().contains(CqlCompilerOptions.Options.EnableAnnotations)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void getOptions_signatureLevelIsAll() { |
| 68 | + CqlCompilerOptions options = manager.getOptions(TEST_URI); |
| 69 | + assertEquals(SignatureLevel.All, options.getSignatureLevel()); |
| 70 | + } |
| 71 | + |
| 72 | + // ----------------------------------------------------------------------- |
| 73 | + // caching — same instance returned on second call |
| 74 | + // ----------------------------------------------------------------------- |
| 75 | + |
| 76 | + @Test |
| 77 | + void getOptions_secondCall_returnsCachedInstance() { |
| 78 | + CqlCompilerOptions first = manager.getOptions(TEST_URI); |
| 79 | + CqlCompilerOptions second = manager.getOptions(TEST_URI); |
| 80 | + assertSame(first, second); |
| 81 | + } |
| 82 | + |
| 83 | + // ----------------------------------------------------------------------- |
| 84 | + // clearOptions — evicts cache so next call re-reads |
| 85 | + // ----------------------------------------------------------------------- |
| 86 | + |
| 87 | + @Test |
| 88 | + void clearOptions_evictsCache() { |
| 89 | + manager.getOptions(TEST_URI); // populate cache |
| 90 | + manager.clearOptions(TEST_URI); |
| 91 | + CqlCompilerOptions second = manager.getOptions(TEST_URI); |
| 92 | + // After clearing, a new instance is created and still satisfies the required-flags contract. |
| 93 | + assertTrue(second.getOptions().contains(CqlCompilerOptions.Options.EnableLocators)); |
| 94 | + } |
| 95 | + |
| 96 | + // ----------------------------------------------------------------------- |
| 97 | + // onMessageEvent — cql-options.json change clears cache |
| 98 | + // ----------------------------------------------------------------------- |
| 99 | + |
| 100 | + @Test |
| 101 | + void onMessageEvent_cqlOptionsChanged_clearsCache() { |
| 102 | + manager.getOptions(TEST_URI); // populate cache |
| 103 | + |
| 104 | + // Simulate a file-watch event for a cql-options.json under the same root. |
| 105 | + String optionsUri = Uris.getHead(TEST_URI).toString() + "/cql/cql-options.json"; |
| 106 | + FileEvent fileEvent = new FileEvent(optionsUri, FileChangeType.Changed); |
| 107 | + DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(List.of(fileEvent)); |
| 108 | + manager.onMessageEvent(new DidChangeWatchedFilesEvent(params)); |
| 109 | + |
| 110 | + CqlCompilerOptions second = manager.getOptions(TEST_URI); |
| 111 | + // After clearing via event, the options must still be enriched. |
| 112 | + assertNotNull(second); |
| 113 | + assertTrue(second.getOptions().contains(CqlCompilerOptions.Options.EnableLocators)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void onMessageEvent_unrelatedFile_doesNotClearCache() { |
| 118 | + CqlCompilerOptions first = manager.getOptions(TEST_URI); // populate cache |
| 119 | + |
| 120 | + FileEvent fileEvent = new FileEvent("file:///workspace/SomeOtherFile.json", FileChangeType.Changed); |
| 121 | + DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(Collections.singletonList(fileEvent)); |
| 122 | + manager.onMessageEvent(new DidChangeWatchedFilesEvent(params)); |
| 123 | + |
| 124 | + CqlCompilerOptions second = manager.getOptions(TEST_URI); |
| 125 | + assertSame(first, second); // cache intact → same instance |
| 126 | + } |
| 127 | +} |
0 commit comments