From d566835bf9850f11497caf6d0b5ac946b4dd6d99 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 12:05:40 +0800 Subject: [PATCH 01/12] fix(provider): cancel sessions outside the registry monitor --- .../provider/ProviderExecutionSessions.java | 51 +++++++++++++------ .../ProviderExecutionTerminalScopeTest.java | 51 +++++++++++++++++++ 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/apps/backend/crydaq-service/src/main/java/com/opentest/service/grpc/provider/ProviderExecutionSessions.java b/apps/backend/crydaq-service/src/main/java/com/opentest/service/grpc/provider/ProviderExecutionSessions.java index b868374a..44bac5a7 100644 --- a/apps/backend/crydaq-service/src/main/java/com/opentest/service/grpc/provider/ProviderExecutionSessions.java +++ b/apps/backend/crydaq-service/src/main/java/com/opentest/service/grpc/provider/ProviderExecutionSessions.java @@ -162,29 +162,48 @@ public synchronized Long currentProjectId(String ownerSession) { } public CompletionStage cancelProject(long projectId) { - return cancelMatching(session -> session.projectId() == projectId); + return cancelSessions(sessions.values().stream() + .filter(session -> session.projectId() == projectId).toList()); } - public synchronized CompletionStage cancelUserSession(String sessionId) { - revokedSessions.add(sessionId); activeProjects.remove(sessionId); - return cancelMatching(session -> session.belongsToUserSession(sessionId)); + public CompletionStage cancelUserSession(String sessionId) { + List active; + synchronized (this) { + revokedSessions.add(sessionId); + activeProjects.remove(sessionId); + active = sessions.values().stream() + .filter(session -> session.belongsToUserSession(sessionId)).toList(); + } + return cancelSessions(active); } - public synchronized CompletionStage switchUserProject(String sessionId, long targetProjectId) { - activeProjects.put(java.util.Objects.requireNonNull(sessionId), targetProjectId); - return cancelMatching(session -> session.belongsToUserSession(sessionId) && session.projectId() != targetProjectId); + public CompletionStage switchUserProject(String sessionId, long targetProjectId) { + List active; + synchronized (this) { + activeProjects.put(Objects.requireNonNull(sessionId), targetProjectId); + active = sessions.values().stream().filter(session -> session.belongsToUserSession(sessionId) + && session.projectId() != targetProjectId).toList(); + } + return cancelSessions(active); } - public synchronized CompletionStage cancelAll() { - closed = true; - return cancelMatching(session -> true); + public CompletionStage cancelAll() { + List active; + synchronized (this) { + closed = true; + active = List.copyOf(sessions.values()); + } + return cancelSessions(active); } - /** Shutdown admission and claim every unconnected invocation atomically with its one-time attachment. */ - public synchronized CompletionStage cancelUnattached() { - closed = true; - var cancelled = sessions.values().stream().filter(ProviderExecutionSession::cancelIfUnattached).toList(); + /** Fence admission before each session atomically arbitrates cancellation against attachment. */ + public CompletionStage cancelUnattached() { + List active; + synchronized (this) { + closed = true; + active = List.copyOf(sessions.values()); + } + var cancelled = active.stream().filter(ProviderExecutionSession::cancelIfUnattached).toList(); return CompletableFuture.allOf(cancelled.stream().map(session -> session.released().toCompletableFuture()) .toArray(CompletableFuture[]::new)); } - private CompletionStage cancelMatching(java.util.function.Predicate predicate) { - var active = sessions.values().stream().filter(predicate).toList(); + private CompletionStage cancelSessions(List active) { active.forEach(ProviderExecutionSession::cancel); return CompletableFuture.allOf(active.stream().map(session -> session.released().toCompletableFuture()) .toArray(CompletableFuture[]::new)); diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/grpc/provider/ProviderExecutionTerminalScopeTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/grpc/provider/ProviderExecutionTerminalScopeTest.java index 61796a83..6192cc4b 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/grpc/provider/ProviderExecutionTerminalScopeTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/grpc/provider/ProviderExecutionTerminalScopeTest.java @@ -1,12 +1,18 @@ package com.opentest.service.grpc.provider; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.Map; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -66,6 +72,51 @@ class ProviderExecutionTerminalScopeTest { } } finally { release.complete(null); } } + @ParameterizedTest + @ValueSource(strings = {"all", "user", "project", "unattached"}) + void cancellationAndConcurrentReleaseCompleteWithoutBlockingEachOther(String scope) throws Exception { + var registry = new ProviderExecutionSessions(); + var released = new CompletableFuture(); + var execution = session(41, released); + registry.registerAndStart(execution); + var cancelling = new CountDownLatch(1); + var worker = Executors.newSingleThreadExecutor(Thread.ofPlatform().daemon(true).factory()); + var completion = worker.submit(() -> { + assertTrue(cancelling.await(5, TimeUnit.SECONDS)); + released.complete(null); + return null; + }); + doAnswer(call -> { + cancelling.countDown(); + completion.get(2, TimeUnit.SECONDS); + return null; + }).when(execution).cancel(); + when(execution.cancelIfUnattached()).thenAnswer(call -> { + execution.cancel(); + return true; + }); + try { + var stopped = switch (scope) { + case "all" -> registry.cancelAll(); + case "user" -> registry.cancelUserSession("owner"); + case "project" -> registry.switchUserProject("owner", 11); + case "unattached" -> registry.cancelUnattached(); + default -> throw new AssertionError(scope); + }; + stopped.toCompletableFuture().get(2, TimeUnit.SECONDS); + assertTrue(registry.active(20, 10, "test:measure", "owner").isEmpty()); + assertTrue(registry.terminalMatches(41, 20, 10, "test:measure", "owner")); + assertThrows(IllegalStateException.class, + () -> registry.reservePreparation(21, 10, "test:measure", "owner", Map.of())); + } finally { + cancelling.countDown(); + released.complete(null); + completion.get(2, TimeUnit.SECONDS); + worker.shutdownNow(); + registry.close(); + } + } + private static ProviderExecutionSession session(long request, CompletableFuture release) { var session = mock(ProviderExecutionSession.class); when(session.requestId()).thenReturn(request); when(session.projectId()).thenReturn(10L); when(session.dashboardId()).thenReturn(20L); From 7a859289048bc494ce96c2b6910975d9a6f1aafd Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 12:06:04 +0800 Subject: [PATCH 02/12] perf(tests): reduce fixture work and remove redundant backend cases --- ...thmOctaveIEC61260JNIBridgeServiceTest.java | 9 - .../model/dto/PostProcessStatusDtoTest.java | 8 - .../concurrent/RecordingWriteTrackerTest.java | 149 ------ .../MonitorMathMutationEntryIT.java | 474 +++++++++--------- .../MeasurementProjectImportEntryTest.java | 29 +- .../ProviderConfigurationEntryTest.java | 8 +- .../CalibrationGainMeasurementEntryTest.java | 134 ----- ...portedPostProcessingGraphicSqliteTest.java | 17 +- .../MathTypeTransitionGraphicSqliteTest.java | 76 +-- .../SoundPowerGraphicAxisPlannerTest.java | 135 ----- ...boardFrameworkConformanceBoundaryTest.java | 133 ++--- .../FileServiceStartupComponentTest.java | 33 +- ...encyResponseRmsCorrectionContractTest.java | 91 ---- ...nceSweepGainUnitConversionServiceTest.java | 9 - .../core/SequenceNodeSnapshotServiceTest.java | 122 ----- .../AutoSaveOrderedSchedulingSqliteTest.java | 2 + .../api/FullScaleBasisContractTest.java | 7 - 17 files changed, 428 insertions(+), 1008 deletions(-) delete mode 100644 apps/backend/crydaq-core/src/test/java/com/opentest/platform/file/concurrent/RecordingWriteTrackerTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/CalibrationGainMeasurementEntryTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/axis/SoundPowerGraphicAxisPlannerTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/export/SweepFrequencyResponseRmsCorrectionContractTest.java diff --git a/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/AlgorithmOctaveIEC61260JNIBridgeServiceTest.java b/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/AlgorithmOctaveIEC61260JNIBridgeServiceTest.java index 323b390c..505a3473 100644 --- a/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/AlgorithmOctaveIEC61260JNIBridgeServiceTest.java +++ b/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/AlgorithmOctaveIEC61260JNIBridgeServiceTest.java @@ -22,15 +22,6 @@ void processDataReturnsNullForNativeFailureResponse() { assertNull(response); } - @Test - void processDataReturnsNullWhenResultIsUnavailable() { - AlgorithmOctaveIEC61260JNIBridgeService service = serviceWithResponse(null); - - OctaveResponseDto response = service.processData( - "octave-task", new double[]{0.1D, 0.2D}, 48000D, 3, 20D, 20000D); - - assertNull(response); - } @Test void processDataAcceptsSuccessfulWrappedResponse() { diff --git a/apps/backend/crydaq-common/src/test/java/com/opentest/common/model/dto/PostProcessStatusDtoTest.java b/apps/backend/crydaq-common/src/test/java/com/opentest/common/model/dto/PostProcessStatusDtoTest.java index 82bbb2b8..bf7e236d 100644 --- a/apps/backend/crydaq-common/src/test/java/com/opentest/common/model/dto/PostProcessStatusDtoTest.java +++ b/apps/backend/crydaq-common/src/test/java/com/opentest/common/model/dto/PostProcessStatusDtoTest.java @@ -72,12 +72,4 @@ void shouldKeepThreeArgumentConstructorCompatibility() { assertNull(status.getErrorCode()); } - @Test - void shouldKeepNoArgsConstructorCompatibility() { - PostProcessStatusDto status = new PostProcessStatusDto(); - - assertFalse(status.isCompleted()); - assertFalse(status.isSuccess()); - assertNull(status.getMessage()); - } } diff --git a/apps/backend/crydaq-core/src/test/java/com/opentest/platform/file/concurrent/RecordingWriteTrackerTest.java b/apps/backend/crydaq-core/src/test/java/com/opentest/platform/file/concurrent/RecordingWriteTrackerTest.java deleted file mode 100644 index 59e8e35d..00000000 --- a/apps/backend/crydaq-core/src/test/java/com/opentest/platform/file/concurrent/RecordingWriteTrackerTest.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.opentest.platform.file.concurrent; - -import org.junit.jupiter.api.Test; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicLong; -import static org.junit.jupiter.api.Assertions.*; - -class RecordingWriteTrackerTest { - @Test - void settlementAlsoCoversWritesCompletedAfterTheirBatchWasQueued() { - AtomicLong clock = new AtomicLong(); - var tracker = new RecordingWriteTracker(clock::get); - var delayed = new ArrayDeque(); - for (int tick = 1; tick <= 800; tick++) { - clock.set(tick * 50_000_000L); - tracker.enqueue(50, .05).complete(50, 50, true); - if (tick > 20 && tick % 20 == 3) { - delayed.addLast(tracker.enqueue(1000, 1)); - } - if (tick > 20 && tick % 20 == 18) { - delayed.removeFirst().complete(1000, 1000, true); - } - if (tick >= 400 && tick % 10 == 0) { - assertFalse(tracker.snapshot().backlogged()); - assertEquals(2000D, tracker.snapshot().bytesPerSecond(), .01); - } - } - } - - @Test - void longerSourceBatchesExpandSettlementBeforeRebuildingTheWindow() { - AtomicLong clock = new AtomicLong(); - var tracker = new RecordingWriteTracker(clock::get); - for (int tick = 1; tick <= 240; tick++) { - clock.set(tick * 250_000_000L); - tracker.enqueue(250, .25).complete(250, 250, true); - if (tick > 16 && tick % 16 == 1) { - tracker.enqueue(4000, 4).complete(4000, 4000, true); - if (tick == 17) { - assertTrue(Double.isNaN(tracker.snapshot().bytesPerSecond())); - } - } - if (tick >= 100) { - assertEquals(2000D, tracker.snapshot().bytesPerSecond(), .01); - } - } - } - - @Test - void oversizedBatchesKeepCountersWithoutUsingTruncatedHistoryForCalibration() { - AtomicLong clock = new AtomicLong(300_000_000_000L); - var tracker = new RecordingWriteTracker(clock::get); - tracker.enqueue(300000, 300).complete(300000, 300000, true); - for (int tick = 1; tick <= 400; tick++) { - clock.addAndGet(250_000_000L); - tracker.enqueue(250, .25).complete(250, 250, true); - } - assertEquals(400000, tracker.snapshot().totalBytes()); - assertEquals(0, tracker.snapshot().queuedBytes()); - assertTrue(Double.isNaN(tracker.snapshot().bytesPerSecond())); - } - - @Test - void delayedOneSecondBatchesDoNotCalibrateAnUnfinishedWindow() { - AtomicLong clock = new AtomicLong(); - var tracker = new RecordingWriteTracker(clock::get); - for (int tick = 1; tick <= 2400; tick++) { - clock.set(tick * 50_000_000L); - tracker.enqueue(404625, .05).complete(404625, 404625, true); - if (tick > 20 && tick % 20 == 3) { - // FFT/WAV 持续产出,4 个缩略文件晚 150 ms 才提交前一秒的数据。 - tracker.enqueue(1_536_240, 1).complete(1_536_240, 1_536_240, true); - } - if (tick >= 400 && tick % 10 == 0) { - assertEquals(9_628_740D, tracker.snapshot().bytesPerSecond(), .01, - "已经用于估算的区间不能再缺少稍后提交的缩略文件字节,tick=" + tick); - } - } - } - - @Test - void freshPendingWritesOutsideSettledWindowDoNotInvalidateItsRate() { - AtomicLong clock = new AtomicLong(); - var tracker = new RecordingWriteTracker(clock::get); - for (int tick = 1; tick <= 160; tick++) { - clock.set(tick * 100_000_000L); - tracker.enqueue(100, .1).complete(100, 100, true); - } - var pending = tracker.enqueue(100, .1); - assertEquals(1000D, tracker.snapshot().bytesPerSecond(), .01); - assertEquals(100, tracker.snapshot().queuedBytes()); - pending.complete(100, 100, true); - } - - @Test - void countsConcurrentChannelsAgainstOneTimeIntervalAndExcludesHeaders() { - AtomicLong clock = new AtomicLong(); - RecordingWriteTracker tracker = new RecordingWriteTracker(clock::get); - for (int frame = 1; frame <= 48; frame++) { - clock.set(frame * 250_000_000L); - for (int channel = 0; channel < 2; channel++) { - tracker.enqueue(250, .25).complete(250 + (frame == 1 ? 8192 : 0), 250, true); - } - } - assertEquals(2000D, tracker.snapshot().bytesPerSecond(), 0.001); - assertEquals(24000 + 16384, tracker.snapshot().totalBytes()); - assertEquals(0, tracker.snapshot().queuedBytes()); - } - - @Test - void backlogCannotBecomeDrainRateAndOldTicketsRemainBounded() { - AtomicLong clock = new AtomicLong(); - RecordingWriteTracker tracker = new RecordingWriteTracker(clock::get); - var tickets = new ArrayList(); - for (int i = 1; i <= 300; i++) { - clock.set(i * 250_000_000L); - tickets.add(tracker.enqueue(250, .25)); - } - clock.set(100_000_000_000L); - assertTrue(tracker.snapshot().backlogged()); - tickets.forEach(ticket -> ticket.complete(250, 250, true)); - assertEquals(75000, tracker.snapshot().totalBytes()); - assertEquals(0, tracker.snapshot().queuedBytes()); - assertTrue(Double.isNaN(tracker.snapshot().bytesPerSecond())); - for (int i = 1; i <= 40; i++) { - clock.set(100_000_000_000L + i * 250_000_000L); - tracker.enqueue(250, .25).complete(250, 250, true); - } - assertEquals(1000D, tracker.snapshot().bytesPerSecond(), .001); - } - - @Test - void pauseDoesNotDiluteRateAndFailedTicketsCompleteOnce() { - AtomicLong clock = new AtomicLong(); - RecordingWriteTracker tracker = new RecordingWriteTracker(clock::get); - for (int frame = 1; frame <= 32; frame++) { - clock.set(frame * 250_000_000L); - tracker.enqueue(2, .25).complete(2, 2, true); - } - clock.set(9_000_000_000L); - assertEquals(8D, tracker.snapshot().bytesPerSecond(), .001); - var rejected = tracker.enqueue(200, .25); - rejected.complete(0, 0, false); - rejected.complete(200, 200, true); - assertEquals(64, tracker.snapshot().totalBytes()); - assertEquals(0, tracker.snapshot().queuedBytes()); - } -} diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/controller/MonitorMathMutationEntryIT.java b/apps/backend/crydaq-server/src/test/java/com/opentest/controller/MonitorMathMutationEntryIT.java index e9ddf1fd..8bfbaf14 100644 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/controller/MonitorMathMutationEntryIT.java +++ b/apps/backend/crydaq-server/src/test/java/com/opentest/controller/MonitorMathMutationEntryIT.java @@ -143,6 +143,7 @@ class MonitorMathMutationEntryIT { private PluginRuntime pluginRuntime; private final Map historicalFiles = new LinkedHashMap<>(); private Map historicalResult; + private final Map> fixtureColumns = new LinkedHashMap<>(); @BeforeEach void setup() throws Exception { @@ -153,148 +154,155 @@ void setup() throws Exception { new SpringContextHolder().setApplicationContext(application); var source = new DriverManagerDataSource("jdbc:sqlite:" + directory.resolve("monitor.db")); jdbc = new JdbcTemplate(source); - var configuration = new MybatisConfiguration(); - configuration.setMapUnderscoreToCamelCase(true); - configuration.setEnvironment(new Environment("monitor-mutation", new SpringManagedTransactionFactory(), source)); - for (var mapper : List.of(BoxChannelMapper.class, DashboardConfigMapper.class, DashboardResultMapper.class, - GraphicConfigMapper.class, GraphicResultCodeMapper.class)) configuration.addMapper(mapper); - var factory = new MybatisSqlSessionFactoryBuilder().build(configuration); - sql = new SqlSessionTemplate(factory); - for (var entity : List.of(BoxChannel.class, DashboardConfig.class, DashboardResult.class, GraphicConfig.class, GraphicResultCode.class)) { - var table = TableInfoHelper.getTableInfo(entity); - var columns = new ArrayList(); - columns.add("id INTEGER PRIMARY KEY"); - table.getFieldList().forEach(field -> columns.add('"' + field.getColumn() + '"' + " " - + (Number.class.isAssignableFrom(field.getPropertyType()) ? "NUMERIC" : "TEXT"))); - jdbc.execute("CREATE TABLE " + table.getTableName() + " (" + String.join(",", columns) + ")"); - } - List stored; - try (var input = getClass().getResourceAsStream("/fixtures/math-monitor/r10-rms-current-bindings.json")) { - stored = json.readValue(input, new TypeReference<>() { }); - } - var struct = Mappers.getMapper(BoxChannelStructMapper.class); - channels = spy(new BoxChannelService() { - @Override public List entityToDto(List records) { - return records.stream().map(struct::toDto).toList(); + var transactions = new DataSourceTransactionManager(source); + var preparation = transactions.getTransaction(null); + try { + var configuration = new MybatisConfiguration(); + configuration.setMapUnderscoreToCamelCase(true); + configuration.setEnvironment(new Environment("monitor-mutation", new SpringManagedTransactionFactory(), source)); + for (var mapper : List.of(BoxChannelMapper.class, DashboardConfigMapper.class, DashboardResultMapper.class, + GraphicConfigMapper.class, GraphicResultCodeMapper.class)) configuration.addMapper(mapper); + var factory = new MybatisSqlSessionFactoryBuilder().build(configuration); + sql = new SqlSessionTemplate(factory); + for (var entity : List.of(BoxChannel.class, DashboardConfig.class, DashboardResult.class, GraphicConfig.class, GraphicResultCode.class)) { + var table = TableInfoHelper.getTableInfo(entity); + var columns = new ArrayList(); + columns.add("id INTEGER PRIMARY KEY"); + table.getFieldList().forEach(field -> columns.add('"' + field.getColumn() + '"' + " " + + (Number.class.isAssignableFrom(field.getPropertyType()) ? "NUMERIC" : "TEXT"))); + jdbc.execute("CREATE TABLE " + table.getTableName() + " (" + String.join(",", columns) + ")"); } - }); - wireMapper(channels, sql.getMapper(BoxChannelMapper.class), factory, BoxChannel.class, BoxChannelMapper.class); - set(channels, "boxChannelStructMapper", struct); - set(channels, "virtualChannelConfigValidator", mock(VirtualChannelConfigValidator.class)); - var projects = mock(ProjectService.class); - var project = new Project(); project.setId(PROJECT); - when(projects.findById(PROJECT)).thenReturn(project); - set(channels, "projectService", projects); - facts = new LinkedHashMap<>(); - for (var binding : stored) for (var unit : binding.getUnitInfos()) for (String id : unit.getChannelIds()) { - var channel = new SimpleChannelDto(); channel.setVchId(id); channel.setCustomName(id); - channel.setChannelType(unit.getChannelType()); channel.setChannelSource(unit.getChannelSource()); - channel.setMathUnit(unit.getChannelType() == 5 ? unit.getResultUnit() : "FS"); - channel.setMathSampleSemantics(unit.getChannelType() == 5 - ? MathSampleSemantics.RAW_FS_CUSTOM : MathSampleSemantics.RAW_FS_STANDARD); - channel.setVp(2.8551D); facts.put(id, channel); - } - for (String id : List.of("Math17", "Math24")) { - var channel = new BoxChannel(); channel.setId(id.equals("Math24") ? MATH24 : 17L); - channel.setProjectId(PROJECT); channel.setChannelSource(1); channel.setChannelIo(0); - channel.setChannelType(1); channel.setVchId(id); channel.setCustomName(id); - channel.setMathFormula("'Ch1'"); channel.setMathUnit("FS"); channel.setMathVp(2.8551D); - channel.setMathSampleSemantics(MathSampleSemantics.RAW_FS_STANDARD); sql.getMapper(BoxChannelMapper.class).insert(channel); - } - when(channels.getChannelDtoMapByVchIds(anyLong(), anySet(), any(), eq(false))).thenAnswer(call -> { - Map result = new LinkedHashMap<>(); - facts.forEach((id, channel) -> result.put(id, BeanUtil.copyProperties(channel, ChannelDto.class))); - for (var channel : channels.listDto(new BoxChannelQuery())) { - result.put(channel.getVchId(), BeanUtil.copyProperties(channel, ChannelDto.class)); + List stored; + try (var input = getClass().getResourceAsStream("/fixtures/math-monitor/r10-rms-current-bindings.json")) { + stored = json.readValue(input, new TypeReference<>() { }); } - return result; - }); - var dashboard = new DashboardConfig(); dashboard.setId(DASHBOARD); dashboard.setProjectId(PROJECT); - dashboard.setProviderId(BuiltinDashboardProvider.MONITOR.getProviderId()); - dashboard.setHandlerConfig("{\"collectConfig\":{\"inputChannels\":[\"Math17\",\"Math24\"]}}"); - sql.getMapper(DashboardConfigMapper.class).insert(dashboard); - var graphic = new GraphicConfig(); graphic.setId(GRAPHIC); graphic.setProjectId(PROJECT); - graphic.setDashboardId(DASHBOARD); graphic.setGraphicType(10); - graphic.setChartConfig("{\"chartType\":\"horizontalBar\",\"code\":\"RMS Level\"}"); - sql.getMapper(GraphicConfigMapper.class).insert(graphic); - results = mock(DashboardResultService.class); - var result = new DashboardResultDto(); result.setId(RESULT); result.setDashboardId(DASHBOARD); - result.setProjectId(PROJECT); result.setPrimaryCode("RMS Level"); result.setAliasName("RMS Level"); - result.setDefaultGraphicType(10); result.setResultType(3); result.setProviderDefined(true); - result.setAlgorithmCode("monitor"); - result.setProviderPresentationFacts(Map.of("multiValueSelection", true)); - result.setCustomUnitY("dBV"); - result.setChannels(new ArrayList<>(facts.values())); - when(results.listDto(any(DashboardResultQuery.class))).thenAnswer(call -> { - result.getChannels().forEach(channel -> { - if (channel.getVchId().equals("Math24")) channel.setChannelType(channels.findById(MATH24).getChannelType()); + var struct = Mappers.getMapper(BoxChannelStructMapper.class); + channels = spy(new BoxChannelService() { + @Override public List entityToDto(List records) { + return records.stream().map(struct::toDto).toList(); + } }); - return new ArrayList<>(List.of(result)); - }); - when(results.getGroupDtoMapByProperty(anyList())).thenReturn(Map.of(RESULT, result)); - var bindings = dependencies(new GraphicResultCodeService()); - wireMapper(bindings, sql.getMapper(GraphicResultCodeMapper.class), factory, GraphicResultCode.class, GraphicResultCodeMapper.class); - set(bindings, "dashboardResultService", results); - set(bindings, "graphicConfigMapper", sql.getMapper(GraphicConfigMapper.class)); - set(bindings, "graphicResultCodeDtoMapper", Mappers.getMapper(GraphicResultCodeDtoMapper.class)); - for (var binding : stored) { - var row = BeanUtil.copyProperties(binding, GraphicResultCode.class); - row.setChannelIds(String.join(",", binding.getChannelIds())); row.setUnit(JSONUtil.toJsonStr(binding.getUnitInfos())); - sql.getMapper(GraphicResultCodeMapper.class).insert(row); - } - var frozen = new DashboardResult(); frozen.setId(100L); frozen.setProjectId(PROJECT); - frozen.setDashboardId(DASHBOARD); frozen.setRequestId(90L); frozen.setPrimaryCode("RMS Level"); - sql.getMapper(DashboardResultMapper.class).insert(frozen); - historicalResult = jdbc.queryForMap("SELECT * FROM dashboard_result WHERE id=100"); - var frozenBinding = BeanUtil.copyProperties(stored.getLast(), GraphicResultCode.class); - frozenBinding.setId(101L); frozenBinding.setGraphicConfigId(102L); frozenBinding.setDashboardResultId(100L); - frozenBinding.setChannelIds("Math23"); frozenBinding.setUnit(JSONUtil.toJsonStr(stored.getLast().getUnitInfos())); - sql.getMapper(GraphicResultCodeMapper.class).insert(frozenBinding); - for (String name : List.of("snapshot.json", "old-waveform.crybin")) { - try (var input = getClass().getResourceAsStream("/fixtures/history/legacy-math/" + name)) { - byte[] bytes = input.readAllBytes(); Path path = directory.resolve(name); - Files.write(path, bytes); historicalFiles.put(path, bytes); + wireMapper(channels, sql.getMapper(BoxChannelMapper.class), factory, BoxChannel.class, BoxChannelMapper.class); + set(channels, "boxChannelStructMapper", struct); + set(channels, "virtualChannelConfigValidator", mock(VirtualChannelConfigValidator.class)); + var projects = mock(ProjectService.class); + var project = new Project(); project.setId(PROJECT); + when(projects.findById(PROJECT)).thenReturn(project); + set(channels, "projectService", projects); + facts = new LinkedHashMap<>(); + for (var binding : stored) for (var unit : binding.getUnitInfos()) for (String id : unit.getChannelIds()) { + var channel = new SimpleChannelDto(); channel.setVchId(id); channel.setCustomName(id); + channel.setChannelType(unit.getChannelType()); channel.setChannelSource(unit.getChannelSource()); + channel.setMathUnit(unit.getChannelType() == 5 ? unit.getResultUnit() : "FS"); + channel.setMathSampleSemantics(unit.getChannelType() == 5 + ? MathSampleSemantics.RAW_FS_CUSTOM : MathSampleSemantics.RAW_FS_STANDARD); + channel.setVp(2.8551D); facts.put(id, channel); } + for (String id : List.of("Math17", "Math24")) { + var channel = new BoxChannel(); channel.setId(id.equals("Math24") ? MATH24 : 17L); + channel.setProjectId(PROJECT); channel.setChannelSource(1); channel.setChannelIo(0); + channel.setChannelType(1); channel.setVchId(id); channel.setCustomName(id); + channel.setMathFormula("'Ch1'"); channel.setMathUnit("FS"); channel.setMathVp(2.8551D); + channel.setMathSampleSemantics(MathSampleSemantics.RAW_FS_STANDARD); sql.getMapper(BoxChannelMapper.class).insert(channel); + } + when(channels.getChannelDtoMapByVchIds(anyLong(), anySet(), any(), eq(false))).thenAnswer(call -> { + Map result = new LinkedHashMap<>(); + facts.forEach((id, channel) -> result.put(id, BeanUtil.copyProperties(channel, ChannelDto.class))); + for (var channel : channels.listDto(new BoxChannelQuery())) { + result.put(channel.getVchId(), BeanUtil.copyProperties(channel, ChannelDto.class)); + } + return result; + }); + var dashboard = new DashboardConfig(); dashboard.setId(DASHBOARD); dashboard.setProjectId(PROJECT); + dashboard.setProviderId(BuiltinDashboardProvider.MONITOR.getProviderId()); + dashboard.setHandlerConfig("{\"collectConfig\":{\"inputChannels\":[\"Math17\",\"Math24\"]}}"); + sql.getMapper(DashboardConfigMapper.class).insert(dashboard); + var graphic = new GraphicConfig(); graphic.setId(GRAPHIC); graphic.setProjectId(PROJECT); + graphic.setDashboardId(DASHBOARD); graphic.setGraphicType(10); + graphic.setChartConfig("{\"chartType\":\"horizontalBar\",\"code\":\"RMS Level\"}"); + sql.getMapper(GraphicConfigMapper.class).insert(graphic); + results = mock(DashboardResultService.class); + var result = new DashboardResultDto(); result.setId(RESULT); result.setDashboardId(DASHBOARD); + result.setProjectId(PROJECT); result.setPrimaryCode("RMS Level"); result.setAliasName("RMS Level"); + result.setDefaultGraphicType(10); result.setResultType(3); result.setProviderDefined(true); + result.setAlgorithmCode("monitor"); + result.setProviderPresentationFacts(Map.of("multiValueSelection", true)); + result.setCustomUnitY("dBV"); + result.setChannels(new ArrayList<>(facts.values())); + when(results.listDto(any(DashboardResultQuery.class))).thenAnswer(call -> { + result.getChannels().forEach(channel -> { + if (channel.getVchId().equals("Math24")) channel.setChannelType(channels.findById(MATH24).getChannelType()); + }); + return new ArrayList<>(List.of(result)); + }); + when(results.getGroupDtoMapByProperty(anyList())).thenReturn(Map.of(RESULT, result)); + var bindings = dependencies(new GraphicResultCodeService()); + wireMapper(bindings, sql.getMapper(GraphicResultCodeMapper.class), factory, GraphicResultCode.class, GraphicResultCodeMapper.class); + set(bindings, "dashboardResultService", results); + set(bindings, "graphicConfigMapper", sql.getMapper(GraphicConfigMapper.class)); + set(bindings, "graphicResultCodeDtoMapper", Mappers.getMapper(GraphicResultCodeDtoMapper.class)); + for (var binding : stored) { + var row = BeanUtil.copyProperties(binding, GraphicResultCode.class); + row.setChannelIds(String.join(",", binding.getChannelIds())); row.setUnit(JSONUtil.toJsonStr(binding.getUnitInfos())); + sql.getMapper(GraphicResultCodeMapper.class).insert(row); + } + var frozen = new DashboardResult(); frozen.setId(100L); frozen.setProjectId(PROJECT); + frozen.setDashboardId(DASHBOARD); frozen.setRequestId(90L); frozen.setPrimaryCode("RMS Level"); + sql.getMapper(DashboardResultMapper.class).insert(frozen); + historicalResult = jdbc.queryForMap("SELECT * FROM dashboard_result WHERE id=100"); + var frozenBinding = BeanUtil.copyProperties(stored.getLast(), GraphicResultCode.class); + frozenBinding.setId(101L); frozenBinding.setGraphicConfigId(102L); frozenBinding.setDashboardResultId(100L); + frozenBinding.setChannelIds("Math23"); frozenBinding.setUnit(JSONUtil.toJsonStr(stored.getLast().getUnitInfos())); + sql.getMapper(GraphicResultCodeMapper.class).insert(frozenBinding); + for (String name : List.of("snapshot.json", "old-waveform.crybin")) { + try (var input = getClass().getResourceAsStream("/fixtures/history/legacy-math/" + name)) { + byte[] bytes = input.readAllBytes(); Path path = directory.resolve(name); + Files.write(path, bytes); historicalFiles.put(path, bytes); + } + } + dashboards = spy(dependencies(new DashboardConfigService())); + wireMapper(dashboards, sql.getMapper(DashboardConfigMapper.class), factory, DashboardConfig.class, DashboardConfigMapper.class); + doReturn(List.of(dashboard)).when(dashboards).list(any(DashboardConfigQuery.class)); + set(dashboards, "boxChannelService", channels); set(channels, "dashboardConfigService", dashboards); + graphics = spy(dependencies(new GraphicConfigService())); + wireMapper(graphics, sql.getMapper(GraphicConfigMapper.class), factory, GraphicConfig.class, GraphicConfigMapper.class); + doReturn("monitor").when(graphics).getAlgorithmCode(any()); + doReturn(List.of(graphic)).when(graphics).list(any(GraphicConfigQuery.class)); + set(graphics, "graphicResultCodeService", bindings); set(graphics, "dashboardResultService", results); + set(graphics, "dashboardConfigService", dashboards); set(graphics, "boxChannelService", channels); + set(graphics, "graphicConfigDtoMapper", Mappers.getMapper(GraphicConfigDtoMapper.class)); + set(graphics, "sweepRatioGraphicUnitService", null); + var context = mock(DefaultDashboardResultConfigContextBuilder.class); + when(context.buildFromAlgorithmCodes(any())).thenReturn(new DefaultDashboardResultConfigContext(Map.of(), Map.of())); + set(graphics, "configContextBuilder", context); + providerUnits = mock(ProviderGraphicUnitService.class); + when(providerUnits.resolveMember(eq(RESULT), any())).thenAnswer(call -> { + String id = call.getArgument(1); + String unit = id.equals("Math24") && channels.findById(MATH24).getChannelType() == 2 ? "dBSPL" : "dBV"; + var selected = new UnitCombination(null, unit, null, ResultUnitIdentity.STANDARD); + return new GraphicUnitResolution(selected, List.of(selected)); + }); + when(providerUnits.memberMetadata(any(), any())).thenAnswer(call -> { + var metadata = new UnitMemberMetadata(); metadata.setChannelSource(1); + metadata.setMathUnit("FS"); metadata.setSourceUnit("FS"); return metadata; + }); + set(graphics, "providerGraphicUnitService", providerUnits); + set(dashboards, "graphicConfigService", graphics); + var references = new VirtualChannelReferenceSyncService(); + set(references, "dashboardConfigService", dashboards); set(references, "boxChannelService", channels); + var math = new VirtualChannelConfigService(); + set(math, "boxChannelService", channels); set(math, "boxChannelStructMapper", struct); + set(math, "virtualChannelReferenceSyncService", references); + var proxy = new ProxyFactory(math); proxy.setProxyTargetClass(true); + proxy.addAdvice(new TransactionInterceptor(new DataSourceTransactionManager(source), new AnnotationTransactionAttributeSource())); + var controller = new BoxChannelController(); set(controller, "boxChannelService", channels); + set(controller, "virtualChannelConfigService", proxy.getProxy()); + http = MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new CryExceptionHandler()).build(); + var user = new LoginUserInfo(); user.setAuthenticated(true); session.setAttribute(LoginUserUtils.SESSION_USER_KEY, user); + transactions.commit(preparation); + } finally { + if (!preparation.isCompleted()) transactions.rollback(preparation); } - dashboards = spy(dependencies(new DashboardConfigService())); - wireMapper(dashboards, sql.getMapper(DashboardConfigMapper.class), factory, DashboardConfig.class, DashboardConfigMapper.class); - doReturn(List.of(dashboard)).when(dashboards).list(any(DashboardConfigQuery.class)); - set(dashboards, "boxChannelService", channels); set(channels, "dashboardConfigService", dashboards); - graphics = spy(dependencies(new GraphicConfigService())); - wireMapper(graphics, sql.getMapper(GraphicConfigMapper.class), factory, GraphicConfig.class, GraphicConfigMapper.class); - doReturn("monitor").when(graphics).getAlgorithmCode(any()); - doReturn(List.of(graphic)).when(graphics).list(any(GraphicConfigQuery.class)); - set(graphics, "graphicResultCodeService", bindings); set(graphics, "dashboardResultService", results); - set(graphics, "dashboardConfigService", dashboards); set(graphics, "boxChannelService", channels); - set(graphics, "graphicConfigDtoMapper", Mappers.getMapper(GraphicConfigDtoMapper.class)); - set(graphics, "sweepRatioGraphicUnitService", null); - var context = mock(DefaultDashboardResultConfigContextBuilder.class); - when(context.buildFromAlgorithmCodes(any())).thenReturn(new DefaultDashboardResultConfigContext(Map.of(), Map.of())); - set(graphics, "configContextBuilder", context); - providerUnits = mock(ProviderGraphicUnitService.class); - when(providerUnits.resolveMember(eq(RESULT), any())).thenAnswer(call -> { - String id = call.getArgument(1); - String unit = id.equals("Math24") && channels.findById(MATH24).getChannelType() == 2 ? "dBSPL" : "dBV"; - var selected = new UnitCombination(null, unit, null, ResultUnitIdentity.STANDARD); - return new GraphicUnitResolution(selected, List.of(selected)); - }); - when(providerUnits.memberMetadata(any(), any())).thenAnswer(call -> { - var metadata = new UnitMemberMetadata(); metadata.setChannelSource(1); - metadata.setMathUnit("FS"); metadata.setSourceUnit("FS"); return metadata; - }); - set(graphics, "providerGraphicUnitService", providerUnits); - set(dashboards, "graphicConfigService", graphics); - var references = new VirtualChannelReferenceSyncService(); - set(references, "dashboardConfigService", dashboards); set(references, "boxChannelService", channels); - var math = new VirtualChannelConfigService(); - set(math, "boxChannelService", channels); set(math, "boxChannelStructMapper", struct); - set(math, "virtualChannelReferenceSyncService", references); - var proxy = new ProxyFactory(math); proxy.setProxyTargetClass(true); - proxy.addAdvice(new TransactionInterceptor(new DataSourceTransactionManager(source), new AnnotationTransactionAttributeSource())); - var controller = new BoxChannelController(); set(controller, "boxChannelService", channels); - set(controller, "virtualChannelConfigService", proxy.getProxy()); - http = MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new CryExceptionHandler()).build(); - var user = new LoginUserInfo(); user.setAuthenticated(true); session.setAttribute(LoginUserUtils.SESSION_USER_KEY, user); } @AfterEach @@ -319,95 +327,104 @@ private Map loadMonitorFixture(String name) throws Exc try (var input = getClass().getResourceAsStream("/fixtures/math-monitor/" + name)) { fixture = json.readTree(input); } - jdbc.update("DELETE FROM box_channel"); - for (var row : fixture.path("channels")) insertFixtureRow("box_channel", row); - jdbc.update("DELETE FROM dashboard_config"); - insertFixtureRow("dashboard_config", fixture.path("dashboard")); - var dashboard = sql.getMapper(DashboardConfigMapper.class).selectById(DASHBOARD); - doReturn(List.of(dashboard)).when(dashboards).list(any(DashboardConfigQuery.class)); - var currentResults = new LinkedHashMap(); - for (var row : fixture.path("results")) { - insertFixtureRow("dashboard_result", row); - var result = BeanUtil.copyProperties(sql.getMapper(DashboardResultMapper.class).selectById(row.path("id").asLong()), DashboardResultDto.class); - result.setProviderDefined(true); - currentResults.put(result.getId(), result); - } - doAnswer(call -> { - var currentChannels = channels.listDto(new BoxChannelQuery()).stream() - .map(channel -> BeanUtil.copyProperties(channel, SimpleChannelDto.class)).toList(); - currentResults.values().forEach(result -> result.setChannels(currentChannels)); - return new ArrayList<>(currentResults.values()); - }).when(results).listDto(any(DashboardResultQuery.class)); - when(results.getGroupDtoMapByProperty(anyList())).thenReturn(currentResults); - jdbc.update("DELETE FROM graphic_config"); - jdbc.update("DELETE FROM graphic_result_code WHERE dashboard_result_id<>100"); - var cards = new ArrayList(); - var savedMetadata = new LinkedHashMap>(); - for (var card : fixture.path("cards")) { - insertFixtureRow("graphic_config", card.path("graphic")); - cards.add(sql.getMapper(GraphicConfigMapper.class).selectById(card.path("graphic").path("id").asLong())); - for (var row : card.path("bindings")) { - insertFixtureRow("graphic_result_code", row); - var units = json.readTree(row.path("unit").asText()).get(0); - var metadata = savedMetadata.computeIfAbsent(row.path("dashboard_result_id").asLong(), ignored -> new LinkedHashMap<>()); - var entries = units.path("memberUnitMetadata").fields(); - while (entries.hasNext()) { - var entry = entries.next(); - metadata.put(entry.getKey(), json.treeToValue(entry.getValue(), UnitMemberMetadata.class)); + var transactions = new DataSourceTransactionManager(jdbc.getDataSource()); + var preparation = transactions.getTransaction(null); + fixtureColumns.clear(); + try { + jdbc.update("DELETE FROM box_channel"); + for (var row : fixture.path("channels")) insertFixtureRow("box_channel", row); + jdbc.update("DELETE FROM dashboard_config"); + insertFixtureRow("dashboard_config", fixture.path("dashboard")); + var dashboard = sql.getMapper(DashboardConfigMapper.class).selectById(DASHBOARD); + doReturn(List.of(dashboard)).when(dashboards).list(any(DashboardConfigQuery.class)); + var currentResults = new LinkedHashMap(); + for (var row : fixture.path("results")) { + insertFixtureRow("dashboard_result", row); + var result = BeanUtil.copyProperties(sql.getMapper(DashboardResultMapper.class).selectById(row.path("id").asLong()), DashboardResultDto.class); + result.setProviderDefined(true); + currentResults.put(result.getId(), result); + } + doAnswer(call -> { + var currentChannels = channels.listDto(new BoxChannelQuery()).stream() + .map(channel -> BeanUtil.copyProperties(channel, SimpleChannelDto.class)).toList(); + currentResults.values().forEach(result -> result.setChannels(currentChannels)); + return new ArrayList<>(currentResults.values()); + }).when(results).listDto(any(DashboardResultQuery.class)); + when(results.getGroupDtoMapByProperty(anyList())).thenReturn(currentResults); + jdbc.update("DELETE FROM graphic_config"); + jdbc.update("DELETE FROM graphic_result_code WHERE dashboard_result_id<>100"); + var cards = new ArrayList(); + var savedMetadata = new LinkedHashMap>(); + for (var card : fixture.path("cards")) { + insertFixtureRow("graphic_config", card.path("graphic")); + cards.add(sql.getMapper(GraphicConfigMapper.class).selectById(card.path("graphic").path("id").asLong())); + for (var row : card.path("bindings")) { + insertFixtureRow("graphic_result_code", row); + var units = json.readTree(row.path("unit").asText()).get(0); + var metadata = savedMetadata.computeIfAbsent(row.path("dashboard_result_id").asLong(), ignored -> new LinkedHashMap<>()); + var entries = units.path("memberUnitMetadata").fields(); + while (entries.hasNext()) { + var entry = entries.next(); + metadata.put(entry.getKey(), json.treeToValue(entry.getValue(), UnitMemberMetadata.class)); + } } } + doAnswer(call -> cards.stream().map(card -> sql.getMapper(GraphicConfigMapper.class).selectById(card.getId())).toList()) + .when(graphics).list(any(GraphicConfigQuery.class)); + var axisPlanner = spy(new DefaultAxisContractPlanner()); + doAnswer(call -> { + StoredAxisContract stored = call.getArgument(0); + AxisMutation mutation = call.getArgument(2); + AxisContractPlan plan = (AxisContractPlan) call.callRealMethod(); + System.out.println("Monitor membership invocation " + mutation.getKind() + + " preserveAxes=" + mutation.isPreserveAxisPartitions() + + " coalesceBindings=" + mutation.allowsBindingCoalescing() + " stored=" + + stored.getResultBindings().stream().map(GraphicResultCodeDto::getId).toList() + " planned=" + + plan.getResultBindings().stream().map(binding -> binding.getId() + ":" + binding.getChannelIds() + ":" + binding.getAxisKey()).toList()); + return plan; + }).when(axisPlanner).plan(any(), anyList(), any()); + set(graphics, "axisContractPlanner", axisPlanner); + set(graphics, "legacyAxisReadNormalizer", new LegacyAxisReadNormalizer(axisPlanner)); + set(graphics, "overlayGraphicMembershipPlanner", new DefaultOverlayGraphicMembershipPlanner(axisPlanner)); + when(providerUnits.owns(anyLong())).thenReturn(true); + when(providerUnits.selected(any(), any())).thenAnswer(call -> { + GraphicResultCodeDto binding = call.getArgument(0); + var unit = binding.getUnitInfos().getFirst(); + return new UnitCombination(null, unit.getUnit(), null, + unit.getResultUnitIdentity() == null ? ResultUnitIdentity.STANDARD : unit.getResultUnitIdentity()); + }); + when(providerUnits.selected(any(), any(), any())).thenAnswer(call -> + providerUnits.selected(call.getArgument(0), call.getArgument(1))); + when(providerUnits.resolveMember(anyLong(), any())).thenAnswer(call -> { + long resultId = call.getArgument(0); + String member = call.getArgument(1); + var metadata = savedMetadata.get(resultId).get(member); + if (metadata != null) { + var combinations = metadata.getUnitSets().stream().map(unit -> new UnitCombination(null, unit, null, + metadata.getResultUnitIdentity() == null ? ResultUnitIdentity.STANDARD : metadata.getResultUnitIdentity())).toList(); + return new GraphicUnitResolution(combinations.getFirst(), combinations); + } + var channel = channels.listDto(new BoxChannelQuery()).stream().filter(item -> member.equals(item.getVchId())).findFirst().orElseThrow(); + String unit = channel.getChannelType() == 5 ? channel.getMathUnit() : resultId == 2118613893462614019L + ? channel.getChannelType() == 2 ? "Pa" : "Vpeak" + : channel.getChannelType() == 2 ? "dB SPL rms" : "dBV rms"; + var selected = new UnitCombination(null, unit, null, + channel.getChannelType() == 5 ? ResultUnitIdentity.CUSTOM : ResultUnitIdentity.STANDARD); + return new GraphicUnitResolution(selected, List.of(selected)); + }); + when(providerUnits.memberMetadata(any(), eq("Math24"))).thenAnswer(call -> { + var channel = channels.listDto(new BoxChannelQuery()).stream() + .filter(item -> "Math24".equals(item.getVchId())).findFirst().orElseThrow(); + var metadata = new UnitMemberMetadata(); metadata.setChannelSource(1); + metadata.setMathUnit(channel.getMathUnit()); metadata.setSourceUnit(channel.getMathUnit()); + return metadata; + }); + transactions.commit(preparation); + return currentResults; + } finally { + if (!preparation.isCompleted()) transactions.rollback(preparation); + fixtureColumns.clear(); } - doAnswer(call -> cards.stream().map(card -> sql.getMapper(GraphicConfigMapper.class).selectById(card.getId())).toList()) - .when(graphics).list(any(GraphicConfigQuery.class)); - var axisPlanner = spy(new DefaultAxisContractPlanner()); - doAnswer(call -> { - StoredAxisContract stored = call.getArgument(0); - AxisMutation mutation = call.getArgument(2); - AxisContractPlan plan = (AxisContractPlan) call.callRealMethod(); - System.out.println("Monitor membership invocation " + mutation.getKind() - + " preserveAxes=" + mutation.isPreserveAxisPartitions() - + " coalesceBindings=" + mutation.allowsBindingCoalescing() + " stored=" - + stored.getResultBindings().stream().map(GraphicResultCodeDto::getId).toList() + " planned=" - + plan.getResultBindings().stream().map(binding -> binding.getId() + ":" + binding.getChannelIds() + ":" + binding.getAxisKey()).toList()); - return plan; - }).when(axisPlanner).plan(any(), anyList(), any()); - set(graphics, "axisContractPlanner", axisPlanner); - set(graphics, "legacyAxisReadNormalizer", new LegacyAxisReadNormalizer(axisPlanner)); - set(graphics, "overlayGraphicMembershipPlanner", new DefaultOverlayGraphicMembershipPlanner(axisPlanner)); - when(providerUnits.owns(anyLong())).thenReturn(true); - when(providerUnits.selected(any(), any())).thenAnswer(call -> { - GraphicResultCodeDto binding = call.getArgument(0); - var unit = binding.getUnitInfos().getFirst(); - return new UnitCombination(null, unit.getUnit(), null, - unit.getResultUnitIdentity() == null ? ResultUnitIdentity.STANDARD : unit.getResultUnitIdentity()); - }); - when(providerUnits.selected(any(), any(), any())).thenAnswer(call -> - providerUnits.selected(call.getArgument(0), call.getArgument(1))); - when(providerUnits.resolveMember(anyLong(), any())).thenAnswer(call -> { - long resultId = call.getArgument(0); - String member = call.getArgument(1); - var metadata = savedMetadata.get(resultId).get(member); - if (metadata != null) { - var combinations = metadata.getUnitSets().stream().map(unit -> new UnitCombination(null, unit, null, - metadata.getResultUnitIdentity() == null ? ResultUnitIdentity.STANDARD : metadata.getResultUnitIdentity())).toList(); - return new GraphicUnitResolution(combinations.getFirst(), combinations); - } - var channel = channels.listDto(new BoxChannelQuery()).stream().filter(item -> member.equals(item.getVchId())).findFirst().orElseThrow(); - String unit = channel.getChannelType() == 5 ? channel.getMathUnit() : resultId == 2118613893462614019L - ? channel.getChannelType() == 2 ? "Pa" : "Vpeak" - : channel.getChannelType() == 2 ? "dB SPL rms" : "dBV rms"; - var selected = new UnitCombination(null, unit, null, - channel.getChannelType() == 5 ? ResultUnitIdentity.CUSTOM : ResultUnitIdentity.STANDARD); - return new GraphicUnitResolution(selected, List.of(selected)); - }); - when(providerUnits.memberMetadata(any(), eq("Math24"))).thenAnswer(call -> { - var channel = channels.listDto(new BoxChannelQuery()).stream() - .filter(item -> "Math24".equals(item.getVchId())).findFirst().orElseThrow(); - var metadata = new UnitMemberMetadata(); metadata.setChannelSource(1); - metadata.setMathUnit(channel.getMathUnit()); metadata.setSourceUnit(channel.getMathUnit()); - return metadata; - }); - return currentResults; } private void exerciseScopeAndFftSaves(boolean sharedVoltageBinding, @@ -544,10 +561,15 @@ private void assertScopeAndFftPreserved(List> before, List row = json.convertValue(value, new TypeReference<>() { }); - var columns = jdbc.queryForList("PRAGMA table_info(" + table + ")").stream().map(column -> column.get("name")).toList(); + Set columns = fixtureColumns.computeIfAbsent(table, name -> new LinkedHashSet<>( + jdbc.queryForList("PRAGMA table_info(" + name + ")").stream() + .map(column -> column.get("name").toString()).toList())); row.forEach((column, data) -> { - if (!columns.contains(column)) jdbc.execute("ALTER TABLE " + table + " ADD COLUMN " + column + " " - + (data instanceof Number ? "NUMERIC" : "TEXT")); + if (!columns.contains(column)) { + jdbc.execute("ALTER TABLE " + table + " ADD COLUMN " + column + " " + + (data instanceof Number ? "NUMERIC" : "TEXT")); + columns.add(column); + } }); jdbc.update("INSERT INTO " + table + " (" + String.join(",", row.keySet()) + ") VALUES (" + String.join(",", row.keySet().stream().map(ignored -> "?").toList()) + ")", row.values().toArray()); diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MeasurementProjectImportEntryTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MeasurementProjectImportEntryTest.java index 43542937..1de3cce4 100644 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MeasurementProjectImportEntryTest.java +++ b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MeasurementProjectImportEntryTest.java @@ -1,6 +1,7 @@ package com.opentest.plugin; import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.opentest.common.model.dto.ProjectExportDto; @@ -10,6 +11,9 @@ import com.opentest.service.project.*; import java.util.*; import org.mybatis.spring.SqlSessionTemplate; +import org.springframework.core.ResolvableType; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -77,17 +81,20 @@ static ImportFixture projectImport(ProviderConfigurationEntryTest entries, types.put(PostProcessPreResultService.class, PostProcessPreResultMapper.class); types.put(FlowExecuteResultService.class, FlowExecuteResultMapper.class); types.values().forEach(configuration::addMapper); - for (Class mapper : types.values()) { - Class entity = org.springframework.core.ResolvableType.forClass(mapper) - .as(com.baomidou.mybatisplus.core.mapper.BaseMapper.class).getGeneric(0).resolve(); - var table = TableInfoHelper.getTableInfo(entity); - var columns = new ArrayList(); - columns.add(table.getKeyColumn() + " INTEGER PRIMARY KEY"); - table.getFieldList().forEach(field -> columns.add('"' + field.getColumn() + '"' + " " - + (Number.class.isAssignableFrom(field.getPropertyType()) ? "NUMERIC" : "TEXT"))); - fixture.jdbc().execute("CREATE TABLE IF NOT EXISTS " + table.getTableName() - + " (" + String.join(",", columns) + ")"); - } + new TransactionTemplate(new DataSourceTransactionManager(fixture.jdbc().getDataSource())) + .executeWithoutResult(status -> { + for (Class mapper : types.values()) { + Class entity = ResolvableType.forClass(mapper) + .as(BaseMapper.class).getGeneric(0).resolve(); + var table = TableInfoHelper.getTableInfo(entity); + var columns = new ArrayList(); + columns.add(table.getKeyColumn() + " INTEGER PRIMARY KEY"); + table.getFieldList().forEach(field -> columns.add('"' + field.getColumn() + '"' + " " + + (Number.class.isAssignableFrom(field.getPropertyType()) ? "NUMERIC" : "TEXT"))); + fixture.jdbc().execute("CREATE TABLE IF NOT EXISTS " + table.getTableName() + + " (" + String.join(",", columns) + ")"); + } + }); var session = new SqlSessionTemplate(new MybatisSqlSessionFactoryBuilder().build(configuration)); Map, Object> services = new LinkedHashMap<>(); var converter = ReflectionTestUtils.getField(fixture.config(), "jacksonConverter"); diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ProviderConfigurationEntryTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ProviderConfigurationEntryTest.java index 38343a81..0e3b6da6 100644 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ProviderConfigurationEntryTest.java +++ b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ProviderConfigurationEntryTest.java @@ -33,6 +33,8 @@ import org.springframework.context.support.StaticApplicationContext; import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; +import org.springframework.transaction.support.TransactionTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; @@ -90,7 +92,11 @@ javax.sql.DataSource createDataSource(Path database) { Fixture fixture(Path database, boolean initialize) throws Exception { var previousRequestAttributes = RequestContextHolder.getRequestAttributes(); var source = createDataSource(database); - var jdbc = new JdbcTemplate(source); if (initialize) createSchema(jdbc); + var jdbc = new JdbcTemplate(source); + if (initialize) { + new TransactionTemplate(new DataSourceTransactionManager(source)) + .executeWithoutResult(status -> createSchema(jdbc)); + } var configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(true); configurePersistence(configuration); configuration.setEnvironment(new Environment("entry", new SpringManagedTransactionFactory(), source)); diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/CalibrationGainMeasurementEntryTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/CalibrationGainMeasurementEntryTest.java deleted file mode 100644 index 10243b62..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/CalibrationGainMeasurementEntryTest.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.opentest.service; - -import com.opentest.common.enums.ChannelIOEnum; -import com.opentest.common.enums.ChannelTypeEnum; -import com.opentest.common.model.dto.BoxChannelDto; -import com.opentest.common.model.dto.ChannelCalibrateDto; -import com.opentest.common.model.dto.ChannelDto; -import com.opentest.common.model.entity.BoxChannel; -import com.opentest.common.model.entity.Channel; -import com.opentest.common.model.entity.Device; -import com.opentest.service.jni.AudioJNIBridgeService; -import com.opentest.service.jni.AlgorithmBusinessService; -import com.opentest.common.model.dto.algorithm.MeterResponseDto; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.test.util.ReflectionTestUtils; -import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyDouble; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.anyList; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class CalibrationGainMeasurementEntryTest { - @Mock private AudioJNIBridgeService audioJNIBridgeService; - @Mock private DeviceService deviceService; - @Mock private BoxChannelService boxChannelService; - @Mock private ProjectChannelSensitivityService projectChannelSensitivityService; - @Spy @InjectMocks private ChannelService channelService; - - @Test - void calibrationThroughChannelServiceComputesBodySensitivityAndSavesItWithActualGain() { - var algorithm = mock(AlgorithmBusinessService.class); - SensorCalibrationService calibration = new SensorCalibrationService(); - ReflectionTestUtils.setField(calibration, "audioJNIBridgeService", audioJNIBridgeService); - ReflectionTestUtils.setField(calibration, "algorithmLibJNIBridgeService", algorithm); - ReflectionTestUtils.setField(calibration, "calculator", new SensorMeasurementCalculator()); - ReflectionTestUtils.setField(channelService, "sensorCalibrationService", calibration); - var binding = buildCalibrationBinding(ChannelTypeEnum.AUDIO.getValue()); - var box = buildBoxChannelDto(ChannelTypeEnum.AUDIO.getValue(), "OPENDAQ", "SonoDAQ"); - when(boxChannelService.findById(17L, false)).thenReturn(binding); - when(boxChannelService.findDtoById(17L)).thenReturn(box); - when(deviceService.findById(3L)).thenReturn(new Device()); - when(audioJNIBridgeService.captureAudioData(any(), anyList(), eq(700))) - .thenReturn(Map.of(7L, new double[]{0.1D, 0.2D, 0.3D, 0.4D})); - MeterResponseDto meter = new MeterResponseDto(); - meter.setRmsLevel(20D * Math.log10(0.25D)); - meter.setFrequency(1000D); - when(algorithm.calculateMeter(anyString(), eq(7L), any(double[].class), anyDouble(), anyInt(), anyDouble())) - .thenReturn(meter); - Channel physical = new Channel(); - physical.setId(7L); - doReturn(physical).when(channelService).findById(7L); - ChannelCalibrateDto request = new ChannelCalibrateDto(); - request.setBoxChannelId(17L); - request.setChannelId(7L); - request.setChannelType(ChannelTypeEnum.AUDIO.getValue()); - request.setDbspl(94D); - request.setFrequency(1000D); - request.setCalibrationInputGainDb(20D); - - var result = channelService.calibrateInputMic(request); - assertEquals(50D, result.getSensitivity()); - assertEquals(20D, result.getCalibrationInputGainDb()); - assertEquals(1000D, result.getMeasuredFrequency()); - ChannelDto application = new ChannelDto(); - application.setId(7L); - application.setBoxChannelId(17L); - application.setChannelType(ChannelTypeEnum.AUDIO.getValue()); - application.setSensitivity(result.getSensitivity()); - application.setCalibrationInputGainDb(result.getCalibrationInputGainDb()); - - assertEquals(true, channelService.saveCalibrationSensitivity(application)); - - verify(projectChannelSensitivityService).saveCalibration(eq(31L), eq(41L), eq(7L), - eq(ChannelTypeEnum.AUDIO.getValue()), eq(0), eq(50D), any(), eq(20D)); - verify(audioJNIBridgeService, never()).calibrateSensor(any(), any(), any(), any(), any()); - } - - private BoxChannel buildCalibrationBinding(Integer channelType) { - BoxChannel boxChannel = new BoxChannel(); - boxChannel.setId(17L); - boxChannel.setTenantId(31L); - boxChannel.setProjectId(41L); - boxChannel.setChannelId(7L); - boxChannel.setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - boxChannel.setChannelType(channelType); - boxChannel.setMeasurementMode(0); - return boxChannel; - } - - private BoxChannelDto buildBoxChannelDto(Integer channelType, String protocol, String deviceName) { - BoxChannelDto boxChannel = new BoxChannelDto(); - boxChannel.setId(17L); - boxChannel.setChannelId(7L); - boxChannel.setDeviceId(3L); - boxChannel.setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - boxChannel.setChannelType(channelType); - boxChannel.setProtocol(protocol); - boxChannel.setVp(2.0D); - boxChannel.setRange(0D); - boxChannel.setChannel(buildChannelDto(channelType, protocol, deviceName)); - return boxChannel; - } - - private ChannelDto buildChannelDto(Integer channelType, String protocol, String deviceName) { - ChannelDto channel = new ChannelDto(); - channel.setId(7L); - channel.setDeviceId(3L); - channel.setDeviceName(deviceName); - channel.setProtocol(protocol); - channel.setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - channel.setChannelType(channelType); - channel.setPanelName("MIC 1"); - channel.setJniChannelNumber(0); - channel.setSamplingRate(48000); - channel.setVp(2.0D); - channel.setRange(0D); - return channel; - } - -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java index 117ad49d..43203d79 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java @@ -57,7 +57,9 @@ import com.opentest.service.mapper.GraphicResultCodeDtoMapper; import com.opentest.service.project.ProjectOtPackageService; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -88,6 +90,7 @@ import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; import org.springframework.transaction.support.TransactionSynchronizationManager; +import org.springframework.transaction.support.TransactionTemplate; import org.sqlite.SQLiteDataSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -211,8 +214,14 @@ void setUp() throws Exception { }); chain(false); openSqlite(); - seedResults(); - seedMinimalLegacyGraphics(); + new TransactionTemplate(new DataSourceTransactionManager(dataSource)).executeWithoutResult(status -> { + seedResults(); + try { + seedMinimalLegacyGraphics(); + } catch (IOException exception) { + throw new UncheckedIOException(exception); + } + }); } @AfterEach @@ -1259,7 +1268,7 @@ private void seedResults() { } } - private void seedMinimalLegacyGraphics() throws Exception { + private void seedMinimalLegacyGraphics() throws IOException { try (InputStream input = getClass().getResourceAsStream("/fixtures/ot-import-legacy-post-graphics.json")) { assertNotNull(input); JSONArray fixtures = JSONUtil.parseArray(new String(input.readAllBytes(), StandardCharsets.UTF_8)); @@ -1364,6 +1373,7 @@ public Boolean baseServiceBatchUpdate(List updates) { private static void createSchema(SQLiteDataSource dataSource) throws Exception { try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { + connection.setAutoCommit(false); statement.execute("CREATE TABLE graphic_config (id BIGINT PRIMARY KEY, tenant_id BIGINT, name TEXT, algorithm_code TEXT, result_code TEXT, " + "chart_config TEXT, sort INTEGER, node_id BIGINT, handler_config TEXT, original TEXT, " + "current_version BIGINT, project_id BIGINT, dashboard_id BIGINT, custom_view_id BIGINT, " @@ -1392,6 +1402,7 @@ private static void createSchema(SQLiteDataSource dataSource) throws Exception { + "chart_config TEXT, relate_data_set_id BIGINT, parent_id BIGINT, derived_result_code TEXT, " + "reference_test_unit_config TEXT, " + "created_time TEXT, updated_time TEXT)"); + connection.commit(); } } diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/MathTypeTransitionGraphicSqliteTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/MathTypeTransitionGraphicSqliteTest.java index 64e4f35e..215230a0 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/MathTypeTransitionGraphicSqliteTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/MathTypeTransitionGraphicSqliteTest.java @@ -59,6 +59,7 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; +import org.springframework.transaction.support.DefaultTransactionDefinition; import java.nio.file.Files; import java.nio.file.Path; @@ -461,41 +462,48 @@ void seedLegacyFormalMembers(int type, String oldUnit) { configuration.setEnvironment(new Environment("math-transition", new SpringManagedTransactionFactory(), source)); configuration.addMapper(BoxChannelMapper.class); mapper = new SqlSessionTemplate(new MybatisSqlSessionFactoryBuilder().build(configuration)).getMapper(BoxChannelMapper.class); - var columns = new ArrayList(); columns.add("id INTEGER PRIMARY KEY"); - TableInfoHelper.getTableInfo(BoxChannel.class).getFieldList().stream() - .filter(field -> !List.of("math_vp", "math_sample_semantics").contains(field.getColumn())) - .forEach(field -> columns.add('"' + field.getColumn() + '"' + " " - + (Number.class.isAssignableFrom(field.getPropertyType()) ? "NUMERIC" : "TEXT"))); - jdbc.execute("CREATE TABLE box_channel (" + String.join(",", columns) + ")"); - var migration = Files.readString(Path.of("../crydaq-server/src/main/resources/db/migration/V2.5.0_20261105120000__math_fs_sample_semantics.sql")); - for (String sql : migration.split(";")) if (!sql.isBlank()) jdbc.execute(sql); - jdbc.update("INSERT INTO box_channel(id,project_id,channel_source,channel_io,channel_type,vch_id,custom_name,math_formula,math_unit,math_sample_semantics,math_vp,sensitivity,math_min,math_max) VALUES(8,1,1,0,2,'Math8','Math8 Audio','Ch1','FS','RAW_FS_STANDARD',10,100,-2,2)"); - jdbc.update("INSERT INTO box_channel(id,project_id,channel_source,channel_io,channel_type,vch_id,custom_name,math_formula,math_unit,math_sample_semantics) VALUES(5,1,1,0,5,'Math5','Other','Ch1','Pa','RAW_FS_CUSTOM')"); - jdbc.execute("CREATE TABLE binding(id INTEGER PRIMARY KEY, unit TEXT)"); - jdbc.execute("CREATE TABLE history(value TEXT)"); - jdbc.update("INSERT INTO history VALUES('frozen Audio -2 +2')"); - jdbc.update("INSERT INTO binding VALUES(501, ?)", "[{\"channelSource\":1,\"channelIds\":[\"Math8\"],\"unit\":\"dB\",\"resultUnit\":\"dB\",\"sourceUnitByChannel\":{\"Math8\":\"FS\"},\"unitSwitchSupported\":true,\"unitSets\":[\"dB\",\"FS\",\"Vrms\",\"Pa\"]}]"); - jdbc.update("INSERT INTO binding VALUES(502, ?)", "[{\"channelSource\":1,\"channelIds\":[\"Math5\"],\"sourceUnitByChannel\":{\"Math5\":\"Pa\"},\"unitSwitchSupported\":false,\"unitSets\":[]}]"); - if (overlay) { - jdbc.update("UPDATE box_channel SET channel_type=5,math_unit='Pa',math_sample_semantics='RAW_FS_CUSTOM',math_vp=NULL,sensitivity=NULL,math_min=8,math_max=9 WHERE id=8"); - jdbc.update("INSERT INTO box_channel(id,project_id,channel_source,channel_io,channel_type,vch_id,custom_name,math_formula,math_unit,math_sample_semantics) VALUES(12,1,1,0,5,'Math12','Other peer','Ch1','Pa','RAW_FS_CUSTOM')"); - jdbc.execute("ALTER TABLE binding ADD COLUMN axis_key TEXT"); - jdbc.execute("ALTER TABLE binding ADD COLUMN channel_ids TEXT"); - jdbc.execute("ALTER TABLE binding ADD COLUMN binding_order INTEGER"); - jdbc.update("DELETE FROM binding WHERE id=502"); - String output = "Frequency".equals(resultCode) ? "Hz" : "Pa"; - var unit = new UnitInfo(); unit.setChannelType(5); unit.setUnit(output); - if ("Frequency".equals(resultCode)) unit.setResultUnitIdentity(ResultUnitIdentity.STANDARD); - unit.setChannelIds(List.of("Math8", "Math5", "Math12")); - var members = new LinkedHashMap(); - for (String id : unit.getChannelIds()) { - var member = new UnitMemberMetadata(); member.setChannelSource(1); member.setMathUnit("Pa"); - member.setSourceUnit("Pa"); member.setResultUnit(output); member.setUnitSwitchSupported(false); - if ("Frequency".equals(resultCode)) member.setResultUnitIdentity(ResultUnitIdentity.STANDARD); - member.setUnitSets(new LinkedHashSet<>(List.of(output))); members.put(id, member); + var transactionManager = new DataSourceTransactionManager(source); + var preparation = transactionManager.getTransaction(new DefaultTransactionDefinition()); + try { + var columns = new ArrayList(); columns.add("id INTEGER PRIMARY KEY"); + TableInfoHelper.getTableInfo(BoxChannel.class).getFieldList().stream() + .filter(field -> !List.of("math_vp", "math_sample_semantics").contains(field.getColumn())) + .forEach(field -> columns.add('"' + field.getColumn() + '"' + " " + + (Number.class.isAssignableFrom(field.getPropertyType()) ? "NUMERIC" : "TEXT"))); + jdbc.execute("CREATE TABLE box_channel (" + String.join(",", columns) + ")"); + var migration = Files.readString(Path.of("../crydaq-server/src/main/resources/db/migration/V2.5.0_20261105120000__math_fs_sample_semantics.sql")); + for (String sql : migration.split(";")) if (!sql.isBlank()) jdbc.execute(sql); + jdbc.update("INSERT INTO box_channel(id,project_id,channel_source,channel_io,channel_type,vch_id,custom_name,math_formula,math_unit,math_sample_semantics,math_vp,sensitivity,math_min,math_max) VALUES(8,1,1,0,2,'Math8','Math8 Audio','Ch1','FS','RAW_FS_STANDARD',10,100,-2,2)"); + jdbc.update("INSERT INTO box_channel(id,project_id,channel_source,channel_io,channel_type,vch_id,custom_name,math_formula,math_unit,math_sample_semantics) VALUES(5,1,1,0,5,'Math5','Other','Ch1','Pa','RAW_FS_CUSTOM')"); + jdbc.execute("CREATE TABLE binding(id INTEGER PRIMARY KEY, unit TEXT)"); + jdbc.execute("CREATE TABLE history(value TEXT)"); + jdbc.update("INSERT INTO history VALUES('frozen Audio -2 +2')"); + jdbc.update("INSERT INTO binding VALUES(501, ?)", "[{\"channelSource\":1,\"channelIds\":[\"Math8\"],\"unit\":\"dB\",\"resultUnit\":\"dB\",\"sourceUnitByChannel\":{\"Math8\":\"FS\"},\"unitSwitchSupported\":true,\"unitSets\":[\"dB\",\"FS\",\"Vrms\",\"Pa\"]}]"); + jdbc.update("INSERT INTO binding VALUES(502, ?)", "[{\"channelSource\":1,\"channelIds\":[\"Math5\"],\"sourceUnitByChannel\":{\"Math5\":\"Pa\"},\"unitSwitchSupported\":false,\"unitSets\":[]}]"); + if (overlay) { + jdbc.update("UPDATE box_channel SET channel_type=5,math_unit='Pa',math_sample_semantics='RAW_FS_CUSTOM',math_vp=NULL,sensitivity=NULL,math_min=8,math_max=9 WHERE id=8"); + jdbc.update("INSERT INTO box_channel(id,project_id,channel_source,channel_io,channel_type,vch_id,custom_name,math_formula,math_unit,math_sample_semantics) VALUES(12,1,1,0,5,'Math12','Other peer','Ch1','Pa','RAW_FS_CUSTOM')"); + jdbc.execute("ALTER TABLE binding ADD COLUMN axis_key TEXT"); + jdbc.execute("ALTER TABLE binding ADD COLUMN channel_ids TEXT"); + jdbc.execute("ALTER TABLE binding ADD COLUMN binding_order INTEGER"); + jdbc.update("DELETE FROM binding WHERE id=502"); + String output = "Frequency".equals(resultCode) ? "Hz" : "Pa"; + var unit = new UnitInfo(); unit.setChannelType(5); unit.setUnit(output); + if ("Frequency".equals(resultCode)) unit.setResultUnitIdentity(ResultUnitIdentity.STANDARD); + unit.setChannelIds(List.of("Math8", "Math5", "Math12")); + var members = new LinkedHashMap(); + for (String id : unit.getChannelIds()) { + var member = new UnitMemberMetadata(); member.setChannelSource(1); member.setMathUnit("Pa"); + member.setSourceUnit("Pa"); member.setResultUnit(output); member.setUnitSwitchSupported(false); + if ("Frequency".equals(resultCode)) member.setResultUnitIdentity(ResultUnitIdentity.STANDARD); + member.setUnitSets(new LinkedHashSet<>(List.of(output))); members.put(id, member); + } + unit.setMemberUnitMetadata(members); + jdbc.update("UPDATE binding SET unit=?,axis_key='shared-pa',channel_ids='Math8,Math5,Math12',binding_order=0 WHERE id=501", JSONUtil.toJsonStr(List.of(unit))); } - unit.setMemberUnitMetadata(members); - jdbc.update("UPDATE binding SET unit=?,axis_key='shared-pa',channel_ids='Math8,Math5,Math12',binding_order=0 WHERE id=501", JSONUtil.toJsonStr(List.of(unit))); + transactionManager.commit(preparation); + } finally { + if (!preparation.isCompleted()) transactionManager.rollback(preparation); } var struct = Mappers.getMapper(BoxChannelStructMapper.class); var channels = mock(BoxChannelService.class); diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/axis/SoundPowerGraphicAxisPlannerTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/axis/SoundPowerGraphicAxisPlannerTest.java deleted file mode 100644 index 5c2e7fc0..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/axis/SoundPowerGraphicAxisPlannerTest.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.opentest.service.axis; - -import com.opentest.common.model.dto.GraphicResultCodeDto; -import com.opentest.common.model.dto.UnitInfo; -import com.opentest.common.model.dto.UnitMemberMetadata; -import com.opentest.common.model.entity.GraphicConfig; -import com.opentest.common.utils.ChartConfigStorageCodec; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.LinkedHashMap; -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class SoundPowerGraphicAxisPlannerTest { - private final AtomicInteger keys = new AtomicInteger(); - private final AxisContractPlanner axes = new DefaultAxisContractPlanner( - () -> "sound-power-axis-" + keys.incrementAndGet()); - private final LegacyAxisReadNormalizer normalizer = new LegacyAxisReadNormalizer(axes); - private final SoundPowerGraphicAxisPlanner planner = new SoundPowerGraphicAxisPlanner(axes, normalizer); - - @Test - void precisionK1TransitionsFromEmptyToPhysicalMembersAndBackWithCompleteAxes() { - GraphicConfig graphic = emptyGraphic(); - GraphicResultCodeDto carrier = binding(20L, "K1Z", "dB", List.of()); - GraphicResultCodeDto desired = binding(20L, "K1Z", "dB", List.of("Ch1", "Ch2")); - AxisContractPlan added = planner.plan(graphic, List.of(carrier), List.of(desired), members(desired)); - assertEquals(1, added.getYAxes().size()); - assertEquals("dB", added.getYAxes().get(0).getUnit()); - assertEquals(List.of("Ch1", "Ch2"), added.getResultBindings().get(0).getChannelIds()); - assertEquals(added.getYAxes().get(0).getAxisKey(), added.getResultBindings().get(0).getAxisKey()); - storeAndValidate(graphic, added); - - GraphicResultCodeDto one = binding(20L, "K1Z", "dB", List.of("Ch1")); - AxisContractPlan reduced = planner.plan(graphic, added.getResultBindings(), List.of(one), members(one)); - assertEquals(added.getYAxes().get(0).getAxisKey(), reduced.getYAxes().get(0).getAxisKey()); - storeAndValidate(graphic, reduced); - AxisContractPlan repeated = planner.plan(graphic, reduced.getResultBindings(), List.of(one), members(one)); - assertEquals(reduced.getYAxes(), repeated.getYAxes()); - - AxisContractPlan emptied = planner.plan(graphic, repeated.getResultBindings(), List.of(carrier), List.of()); - assertTrue(emptied.getYAxes().isEmpty()); - assertTrue(emptied.getResultBindings().get(0).getChannelIds().isEmpty()); - assertNull(emptied.getResultBindings().get(0).getAxisKey()); - storeAndValidate(graphic, emptied); - } - - @Test - void weightingChangesReplaceResultMembersAndAxesTogether() { - GraphicConfig graphic = emptyGraphic(); - GraphicResultCodeDto current = binding(20L, "LAeq", "dBA", List.of("Ch1")); - AxisContractPlan plan = planner.plan(graphic, - List.of(binding(20L, "LAeq", "dBA", List.of())), List.of(current), members(current)); - storeAndValidate(graphic, plan); - String[] weightings = {"C", "D", "Z", "A"}; - for (int index = 0; index < weightings.length; index++) { - String weighting = weightings[index]; - GraphicResultCodeDto target = binding(21L + index, "L" + weighting + "eq", - "dB" + weighting, List.of("Ch1")); - plan = planner.plan(graphic, plan.getResultBindings(), List.of(target), members(target)); - assertEquals("dB" + weighting, plan.getYAxes().get(0).getUnit()); - assertEquals(target.getDashboardResultId(), plan.getResultBindings().get(0).getDashboardResultId()); - storeAndValidate(graphic, plan); - } - } - - @Test - void rejectsPersistedMixedContractInsteadOfRepairingIt() { - GraphicResultCodeDto corrupt = binding(20L, "K1Z", "dB", List.of("K1Z")); - GraphicResultCodeDto target = binding(20L, "K1Z", "dB", List.of("Ch1")); - IllegalArgumentException error = assertThrows(IllegalArgumentException.class, - () -> planner.plan(emptyGraphic(), List.of(corrupt), List.of(target), members(target))); - assertEquals("Axis contract mixes legacy and stable axis markers", error.getMessage()); - assertEquals(0, keys.get()); - } - - private void storeAndValidate(GraphicConfig graphic, AxisContractPlan plan) { - var chart = ChartConfigStorageCodec.parse(graphic.getChartConfig()); - chart.setYAxes(plan.getYAxes()); - chart.clearLegacyYAxes(); - graphic.setChartConfig(ChartConfigStorageCodec.encode(chart)); - graphic.setOriginal(plan.getGraphDisplaySettings()); - assertFalse(graphic.getChartConfig().contains("\"yAxis\":")); - normalizer.normalizeForRead(graphic.getGraphicType(), graphic.getChartConfig(), - plan.getResultBindings(), graphic.getOriginal(), null, LegacyAxisReadContext.current()); - } - - private static GraphicConfig emptyGraphic() { - GraphicConfig g = new GraphicConfig(); - g.setId(30L); - g.setGraphicType(6); - g.setAlgorithmCode("precision_method"); - g.setChartConfig("{\"chartType\":\"line\",\"line\":{\"yAxes\":[],\"xAxis\":{\"unit\":\"Hz\"}}}"); - return g; - } - - private static GraphicResultCodeDto binding(long id, String code, String unit, List channels) { - GraphicResultCodeDto b = new GraphicResultCodeDto(); - b.setId(40L); - b.setDashboardResultId(id); - b.setGraphicConfigId(30L); - b.setResultCode(code); - b.setChannelIds(channels); - UnitInfo u = new UnitInfo(); - u.setChannelType(2); - u.setChannelIds(channels); - u.setUnit(unit); - u.setUnitSets(Set.of(unit)); - b.setUnitInfos(List.of(u)); - return b; - } - - private static List members(GraphicResultCodeDto binding) { - List result = new ArrayList<>(); - UnitInfo unit = binding.getUnitInfos().get(0); - unit.setMemberUnitMetadata(new LinkedHashMap<>()); - for (String channel : binding.getChannelIds()) { - UnitMemberMetadata metadata = new UnitMemberMetadata(); - metadata.setChannelSource(0); - metadata.setUnitSets(unit.getUnitSets()); - metadata.setUnitSwitchSupported(false); - unit.getMemberUnitMetadata().put(channel, metadata); - result.add(AxisMemberDescriptor.ordinary(binding, "result:" + binding.getDashboardResultId(), - channel, 2, unit.getUnit(), metadata)); - } - return result; - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/DashboardFrameworkConformanceBoundaryTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/DashboardFrameworkConformanceBoundaryTest.java index 49e73ebb..fdd1929a 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/DashboardFrameworkConformanceBoundaryTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/DashboardFrameworkConformanceBoundaryTest.java @@ -1,5 +1,6 @@ package com.opentest.service.dashboard.framework; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.asm.ClassReader; import org.springframework.asm.ClassVisitor; @@ -12,10 +13,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.Predicate; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -27,6 +31,31 @@ class DashboardFrameworkConformanceBoundaryTest { private static final Path REPOSITORY = Path.of("..").toAbsolutePath().normalize(); + private static final Pattern DASHBOARD_TABLE = Pattern.compile("(?i)\\bdashboard_config\\b"); + private static Map productionSources; + + @BeforeAll + static void indexProductionSources() throws IOException { + Map sources = new LinkedHashMap<>(); + for (Path root : sourceRoots("java")) { + try (Stream files = Files.walk(root)) { + for (Path file : files.filter(Files::isRegularFile) + .filter(path -> path.toString().endsWith(".java")).toList()) { + sources.put(file, Files.readString(file)); + } + } + } + productionSources = Map.copyOf(sources); + } + + private static List sourceRoots(String language) throws IOException { + try (Stream modules = Files.list(REPOSITORY)) { + return modules.filter(Files::isDirectory) + .map(module -> module.resolve("src/main/" + language)) + .filter(Files::isDirectory) + .toList(); + } + } @Test void completeProviderExistsOnlyInTestSourcesAndHostDoesNotKnowItsClass() throws Exception { @@ -77,12 +106,13 @@ void frameworkModulesAndSdkApiKeepTheirDeclaredDependencyDirection() throws Exce "com.opentest.service.", "org.springframework.")); - long commonProtoCount; - try (Stream paths = Files.walk(REPOSITORY)) { - commonProtoCount = paths - .filter(path -> path.toString().replace('\\', '/').contains("/src/main/proto/")) - .filter(path -> path.getFileName().toString().equals("common.proto")) - .count(); + long commonProtoCount = 0; + for (Path root : sourceRoots("proto")) { + try (Stream paths = Files.walk(root)) { + commonProtoCount += paths + .filter(path -> path.getFileName().toString().equals("common.proto")) + .count(); + } } assertEquals(1L, commonProtoCount, "common.proto 必须只有一个源码真相源"); assertTrue(Files.isRegularFile(REPOSITORY.resolve( @@ -112,12 +142,12 @@ void productionRoutesDoNotUseRetiredDraftAndPersistedTypeRemainsCompatible() thr "生产 HTTP/gRPC route 不得引用已移除的草案 Host:\n" + String.join("\n", routeReferences)); - List typeMigrations = findTextMatches( + List typeMigrations = findDashboardTypeMigrations( REPOSITORY.resolve("crydaq-server/src/main/resources/db/migration"), path -> path.toString().endsWith(".sql") && !path.getFileName().toString().equals("V1.0__init.sql"), Pattern.compile( - "(?is)(?=.*\\bdashboard_config\\b)(?:" + "(?is)(?:" + "\\bdashboard_type\\b|" + "(? forbidd if (!Files.isDirectory(source)) { return; } - try (Stream paths = Files.walk(source)) { - for (Path path : paths.filter(Files::isRegularFile) - .filter(item -> item.toString().endsWith(".java")).toList()) { - int lineNumber = 0; - for (String line : Files.readAllLines(path)) { - lineNumber++; - String trimmed = line.trim(); - if (!trimmed.startsWith("import ")) { - continue; - } - if (module.equals("crydaq-platform-support") - && trimmed.equals( - "import com.opentest.service.proto.CommonProto;")) { - // 迁移 wire contract 时为兼容已有调用者保留了生成类 FQCN。 - continue; - } - for (String prefix : forbidden) { - if (trimmed.contains(prefix)) { - violations.add(relative(path) + ":" + lineNumber + " " + trimmed); - } + for (var entry : productionSources.entrySet()) { + if (!entry.getKey().startsWith(source)) continue; + int lineNumber = 0; + for (String line : entry.getValue().lines().toList()) { + lineNumber++; + String trimmed = line.trim(); + if (!trimmed.startsWith("import ")) continue; + if (module.equals("crydaq-platform-support") + && trimmed.equals("import com.opentest.service.proto.CommonProto;")) { + // 迁移 wire contract 时为兼容已有调用者保留了生成类 FQCN。 + continue; + } + for (String prefix : forbidden) { + if (trimmed.contains(prefix)) { + violations.add(relative(entry.getKey()) + ":" + lineNumber + " " + trimmed); } } } @@ -240,29 +265,27 @@ private static boolean isSubtypeOf(String name, } private static List findJavaMatches(List roots, - java.util.function.Predicate filter, + Predicate filter, Pattern pattern) throws IOException { return findJavaMatches(roots, filter, pattern, false); } private static List findJavaMatches(List roots, - java.util.function.Predicate filter, + Predicate filter, Pattern pattern, boolean wholeFile) throws IOException { List matches = new ArrayList<>(); - for (Path root : roots) { - if (!Files.exists(root)) { - continue; - } - try (Stream paths = Files.walk(root)) { - for (Path path : paths.filter(filter) - .filter(item -> item.toString().endsWith(".java")).toList()) { - if (wholeFile) { - if (pattern.matcher(Files.readString(path)).find()) { - matches.add(relative(path)); - } - } else { - collectMatches(path, pattern, matches); + for (var entry : productionSources.entrySet()) { + Path path = entry.getKey(); + if (roots.stream().noneMatch(path::startsWith) || !filter.test(path)) continue; + if (wholeFile) { + if (pattern.matcher(entry.getValue()).find()) matches.add(relative(path)); + } else { + int lineNumber = 0; + for (String line : entry.getValue().lines().toList()) { + lineNumber++; + if (pattern.matcher(line).find()) { + matches.add(relative(path) + ":" + lineNumber + " " + line.trim()); } } } @@ -270,8 +293,8 @@ private static List findJavaMatches(List roots, return matches; } - private static List findTextMatches(Path root, - java.util.function.Predicate filter, + private static List findDashboardTypeMigrations(Path root, + Predicate filter, Pattern pattern) throws IOException { List matches = new ArrayList<>(); if (!Files.exists(root)) { @@ -279,7 +302,13 @@ private static List findTextMatches(Path root, } try (Stream paths = Files.walk(root)) { for (Path path : paths.filter(Files::isRegularFile).filter(filter).toList()) { - if (pattern.matcher(Files.readString(path)).find()) { + String text = Files.readString(path); + Matcher table = DASHBOARD_TABLE.matcher(text); + int lastTable = -1; + while (table.find()) lastTable = table.start(); + if (lastTable < 0) continue; + Matcher forbidden = pattern.matcher(text); + if (forbidden.find() && forbidden.start() <= lastTable) { matches.add(relative(path)); } } @@ -287,18 +316,6 @@ private static List findTextMatches(Path root, return matches; } - private static void collectMatches(Path path, - Pattern pattern, - List matches) throws IOException { - int lineNumber = 0; - for (String line : Files.readAllLines(path)) { - lineNumber++; - if (pattern.matcher(line).find()) { - matches.add(relative(path) + ":" + lineNumber + " " + line.trim()); - } - } - } - private static boolean isMainJava(Path path) { String normalized = path.toAbsolutePath().normalize().toString() .replace('\\', '/').toLowerCase(Locale.ROOT); diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java index 7e9a488e..510fc835 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java @@ -10,6 +10,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.springframework.beans.factory.ObjectProvider; @@ -23,6 +24,9 @@ import static org.mockito.Mockito.mockingDetails; class FileServiceStartupComponentTest { + private static List> startupComponent; + private static List> mockBoundaries; + static List> roots() { return List.of(FileService.class, ProviderGraphicUnitService.class, MeasurementEdits.class, MeasurementInputBindings.class); } @@ -46,9 +50,8 @@ static Set> component() throws Exception { return result; } - @ParameterizedTest(name = "complete startup component from {0}") - @MethodSource("entries") - void createsEveryRealBeanInThePreviouslyMissedComponent(Class first) throws Exception { + @BeforeAll + static void inspectStartupComponent() throws Exception { Set> component = component(); var graph = FileServiceDependencyGraphTest.graph(FileServiceDependencyGraphTest.components()); for (Class root : roots()) { @@ -56,18 +59,26 @@ void createsEveryRealBeanInThePreviouslyMissedComponent(Class first) throws E for (Class reachable : closure) if (FileServiceDependencyGraphTest.reachable(reachable, graph).contains(root)) assertTrue(component.contains(reachable), "New real cycle member must not be mocked: " + reachable.getName()); } + Set> boundaries = new LinkedHashSet<>(); + for (Class type : component) for (var dependency : FileServiceDependencyGraphTest.dependencies(type)) { + Class boundary = dependency.type(); + if (component.stream().noneMatch(boundary::isAssignableFrom)) boundaries.add(boundary); + } + boundaries.remove(ApplicationContext.class); boundaries.remove(ObjectProvider.class); + Files.write(Path.of("target/file-service-mocked-boundaries.txt"), boundaries.stream().map(Class::getName).sorted().toList()); + startupComponent = List.copyOf(component); + mockBoundaries = List.copyOf(boundaries); + } + + @ParameterizedTest(name = "complete startup component from {0}") + @MethodSource("entries") + void createsEveryRealBeanInThePreviouslyMissedComponent(Class first) throws Exception { + List> component = startupComponent; try (var context = new AnnotationConfigApplicationContext()) { context.setAllowCircularReferences(true); context.registerBean(first); for (Class type : component) if (type != first) context.registerBean(type); - Set> boundaries = new LinkedHashSet<>(); - for (Class type : component) for (var dependency : FileServiceDependencyGraphTest.dependencies(type)) { - Class boundary = dependency.type(); - if (component.stream().noneMatch(boundary::isAssignableFrom)) boundaries.add(boundary); - } - boundaries.remove(ApplicationContext.class); boundaries.remove(ObjectProvider.class); - Files.write(Path.of("target/file-service-mocked-boundaries.txt"), boundaries.stream().map(Class::getName).sorted().toList()); - for (Class boundary : boundaries) context.getBeanFactory().registerSingleton( + for (Class boundary : mockBoundaries) context.getBeanFactory().registerSingleton( Introspector.decapitalize(boundary.getSimpleName()), mock(boundary)); context.refresh(); var exports = (ObjectProvider) ReflectionTestUtils.getField(context.getBean(FileService.class), "savedProviderTabularExports"); diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/export/SweepFrequencyResponseRmsCorrectionContractTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/export/SweepFrequencyResponseRmsCorrectionContractTest.java deleted file mode 100644 index cc12f95a..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/export/SweepFrequencyResponseRmsCorrectionContractTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.opentest.service.export; - -import com.opentest.service.unit.SweepFrequencyResponseRmsCorrection; - -import com.opentest.common.enums.ChannelTypeEnum; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class SweepFrequencyResponseRmsCorrectionContractTest { - - private static final double EPSILON = 1E-12; - - @Test - void shouldRecognizeCurrentAndHistoricalSweepFrequencyResponseResults() { - assertTrue(applies("log_chirp", "RMS Level", "Frequency Response", "Vrms")); - assertTrue(applies("log_chirp", "RMS Level", "result.name.frequency_response", "dBV")); - assertTrue(applies("log_chirp", "RMS Level", "result.name.logchirp.rms_level", "dBV")); - assertTrue(applies("logchirp", "Frequency Response", null, "dBV")); - assertTrue(applies("harmonic_step_sweep", "Frequency Response", null, "dBSPL")); - } - - @Test - void shouldFailClosedOutsideTheExplicitCorrectionBoundary() { - assertFalse(applies("stepped_level_sweep", "Frequency Response", null, "Vrms")); - assertFalse(applies("harmonic_step_sweep", "RMS Level", "Frequency Response", "Vrms")); - assertFalse(applies("fft", "Frequency Response", null, "Vrms")); - assertFalse(applies("log_chirp", "THD Ratio", "Frequency Response", "Vrms")); - assertFalse(applies("log_chirp", "Frequency Response", null, "Vpeak")); - assertFalse(SweepFrequencyResponseRmsCorrection.applies( - "log_chirp", "Frequency Response", null, null, "dBFS", "Vrms")); - assertFalse(SweepFrequencyResponseRmsCorrection.applies( - "log_chirp", "Frequency Response", null, - ChannelTypeEnum.UNKNOWN.getValue(), "dBFS", "Vrms")); - assertFalse(SweepFrequencyResponseRmsCorrection.applies( - "log_chirp", "Frequency Response", null, 99, "dBFS", "Vrms")); - assertFalse(SweepFrequencyResponseRmsCorrection.applies( - "log_chirp", "Frequency Response", null, - ChannelTypeEnum.DIGITAL.getValue(), "dBFS", "Vrms")); - assertFalse(SweepFrequencyResponseRmsCorrection.applies( - "log_chirp", "Frequency Response", null, - ChannelTypeEnum.AUDIO.getValue(), "FS", "Vrms")); - } - - @Test - void shouldRecognizeStableResultCodesRegardlessOfDisplayName() { - for (String resultName : new String[]{"频率响应", "用户改名", "Overall Level", "", null}) { - for (String targetUnit : new String[]{"Vrms", "dBV", "dBSPL"}) { - assertTrue(applies("log_chirp", "RMS Level", resultName, targetUnit)); - } - } - for (String algorithmCode : new String[]{"meter", "monitor", "fft", "stepped_level_sweep", "", null}) { - assertFalse(applies(algorithmCode, "RMS Level", "Frequency Response", "Vrms")); - } - } - - @Test - void shouldCorrectLinearAndLogarithmicRmsTargetsInPlace() { - double[] vrms = {Math.sqrt(2D) * 0.1D, -Math.sqrt(2D) * 0.1D}; - double[] dbv = {0D, -10D}; - double[] dbspl = {94D}; - - assertArrayEquals( - new double[]{0.1D, -0.1D}, - SweepFrequencyResponseRmsCorrection.correctInPlace(vrms, "Vrms"), - EPSILON); - assertArrayEquals( - new double[]{-3.010299956639812D, -13.010299956639813D}, - SweepFrequencyResponseRmsCorrection.correctInPlace(dbv, "dBV"), - EPSILON); - assertArrayEquals( - new double[]{90.98970004336019D}, - SweepFrequencyResponseRmsCorrection.correctInPlace(dbspl, "dBSPL"), - EPSILON); - } - - private boolean applies(String algorithmCode, - String resultCode, - String resultName, - String targetUnit) { - return SweepFrequencyResponseRmsCorrection.applies( - algorithmCode, - resultCode, - resultName, - ChannelTypeEnum.AUDIO.getValue(), - "dBFS", - targetUnit); - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/sweep/SequenceSweepGainUnitConversionServiceTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/sweep/SequenceSweepGainUnitConversionServiceTest.java index fdd81c19..93289488 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/sweep/SequenceSweepGainUnitConversionServiceTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/sweep/SequenceSweepGainUnitConversionServiceTest.java @@ -81,15 +81,6 @@ void convertsDigitalInputAgainstVrmsOutputToFsPerVrms() { assertArrayEquals(new double[]{0.25}, y, 1e-9); } - @Test - void keepsDigitalInputAgainstVrmsOutputOnSameReferenceForLogChirp() { - ChannelDto input = channel(ChannelTypeEnum.DIGITAL, 1.0); - StimulusQuery stimulus = stimulus("Vrms", 0.2, 4.0); - - double[] y = service.convert(new double[]{0.0}, "dB", "FS/Vrms", input, stimulus); - - assertArrayEquals(new double[]{0.25}, y, 1e-9); - } @Test void mapsSameInputAndOutputUnitsToApxRatioChoices() { diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/core/SequenceNodeSnapshotServiceTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/core/SequenceNodeSnapshotServiceTest.java index 6af0b09b..ff9f1897 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/core/SequenceNodeSnapshotServiceTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/core/SequenceNodeSnapshotServiceTest.java @@ -2181,128 +2181,6 @@ void createNodeExecutionSnapshotReusesEmptyPrebuiltDisplayAssetsBeforeQueryingEx assertEquals("[]", snapshotInitResult.getGraphicConfigsJson()); } - @Test - void createNodeExecutionSnapshotRewritesPostProcessingPrebuiltUnitsFromCurrentViewWhenRuntimeSourceResultMissing() throws Exception { - Long postNodeId = 77L; - Long postSourceResultId = 601L; - Long postSnapshotResultId = 8501L; - - SequenceContext context = new SequenceContext(); - context.setRequestId(123L); - context.setBoxId(BOX_A); - context.reserveSeqExecutionOrder(postNodeId); - - DashboardResultDto prebuiltResult = new DashboardResultDto(); - prebuiltResult.setId(postSnapshotResultId); - prebuiltResult.setSourceId(postSourceResultId); - prebuiltResult.setRequestId(123L); - prebuiltResult.setNodeId(postNodeId); - prebuiltResult.setBoxId(BOX_A); - prebuiltResult.setHash("hash-post"); - prebuiltResult.setPrimaryCode("Result Set -> Specify Data Points"); - prebuiltResult.setAliasName("Result Set -> Specify Data Points"); - prebuiltResult.setResultType(ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - prebuiltResult.setCustomUnitY("dBSPL"); - prebuiltResult.setExecutedCount(1); - SimpleChannelDto prebuiltChannel = new SimpleChannelDto(); - prebuiltChannel.setVchId("Ch1"); - prebuiltChannel.setSourceVchId("Ch1"); - prebuiltChannel.setThreadChannelAlias("Ch1"); - prebuiltChannel.setCustomName("Ch1"); - prebuiltChannel.setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - prebuiltChannel.setChannelType(ChannelTypeEnum.VOLTAGE.getValue()); - prebuiltChannel.setChannelUnitY("dBSPL"); - prebuiltResult.setChannels(Collections.singletonList(prebuiltChannel)); - - GraphicResultCodeDto prebuiltResultCode = new GraphicResultCodeDto(); - prebuiltResultCode.setDashboardResultId(postSnapshotResultId); - prebuiltResultCode.setSourceDashboardResultId(postSourceResultId); - prebuiltResultCode.setResultCode("Result Set -> Specify Data Points"); - prebuiltResultCode.setResultName("Result Set -> Specify Data Points"); - prebuiltResultCode.setChannelIds(Collections.singletonList("Ch1")); - prebuiltResultCode.setUnitInfos(Collections.singletonList( - buildUnitInfo(ChannelTypeEnum.VOLTAGE.getValue(), Collections.singletonList("Ch1"), "dBSPL"))); - - GraphicConfigDto prebuiltGraphic = new GraphicConfigDto(); - prebuiltGraphic.setId(9901L); - prebuiltGraphic.setSourceGraphicConfigId(901L); - prebuiltGraphic.setNodeId(postNodeId); - prebuiltGraphic.setGraphicType(GraphicTypeEnum.FREQUENCY_DOMAIN_CURVE.getType()); - prebuiltGraphic.setName("Result Set -> Specify Data Points"); - prebuiltGraphic.setResultCode("Result Set -> Specify Data Points"); - prebuiltGraphic.setChannelIds("Ch1"); - prebuiltGraphic.setResultCodes(Collections.singletonList(prebuiltResultCode)); - LineChartConfig prebuiltChartConfig = new LineChartConfig(); - prebuiltChartConfig.setChartType("line"); - prebuiltChartConfig.setUnitX("Hz"); - prebuiltChartConfig.setUnitY("dBSPL"); - prebuiltGraphic.setChartConfig(prebuiltChartConfig); - - NodeDisplayAssetDto prebuiltAsset = new NodeDisplayAssetDto(); - prebuiltAsset.setDashboardResults(Collections.singletonList(prebuiltResult)); - prebuiltAsset.setGraphicConfigs(Collections.singletonList(prebuiltGraphic)); - prebuiltAsset.setSourceToSnapshotDashboardResultIdMap(Collections.singletonMap(postSourceResultId, postSnapshotResultId)); - prebuiltAsset.setSnapshotToSourceDashboardResultIdMap(Collections.singletonMap(postSnapshotResultId, postSourceResultId)); - - DashboardResultDto currentViewResult = buildDashboardResultDto(postSourceResultId, BOX_A, "Ch1"); - currentViewResult.setPrimaryCode("Result Set -> Specify Data Points"); - currentViewResult.setAliasName("Result Set -> Specify Data Points"); - currentViewResult.setResultType(ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - currentViewResult.setCustomUnitY("dBV"); - currentViewResult.getChannels().get(0).setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - currentViewResult.getChannels().get(0).setChannelType(ChannelTypeEnum.VOLTAGE.getValue()); - currentViewResult.getChannels().get(0).setChannelUnitY("dBV"); - - GraphicResultCodeDto currentViewResultCode = new GraphicResultCodeDto(); - currentViewResultCode.setDashboardResultId(postSourceResultId); - currentViewResultCode.setResultCode("Result Set -> Specify Data Points"); - currentViewResultCode.setResultName("Result Set -> Specify Data Points"); - currentViewResultCode.setChannelIds(Collections.singletonList("Ch1")); - currentViewResultCode.setUnitInfos(Collections.singletonList( - buildUnitInfo(ChannelTypeEnum.VOLTAGE.getValue(), Collections.singletonList("Ch1"), "dBV"))); - - GraphicConfigDto currentViewGraphic = new GraphicConfigDto(); - currentViewGraphic.setId(901L); - currentViewGraphic.setNodeId(postNodeId); - currentViewGraphic.setGraphicType(GraphicTypeEnum.FREQUENCY_DOMAIN_CURVE.getType()); - currentViewGraphic.setName("Result Set -> Specify Data Points"); - currentViewGraphic.setResultCode("Result Set -> Specify Data Points"); - currentViewGraphic.setChannelIds("Ch1"); - currentViewGraphic.setResultCodes(Collections.singletonList(currentViewResultCode)); - LineChartConfig currentViewChartConfig = new LineChartConfig(); - currentViewChartConfig.setChartType("line"); - currentViewChartConfig.setUnitX("Hz"); - currentViewChartConfig.setUnitY("dBV"); - currentViewGraphic.setChartConfig(currentViewChartConfig); - - PostProcessingNodeConfigDto nodeConfig = new PostProcessingNodeConfigDto(); - nodeConfig.ensurePrimaryCalculation().ensureSourceA().setNodeId(55L); - nodeConfig.ensurePrimaryCalculation().ensureSourceA().setResultSetId(501L); - nodeConfig.ensurePrimaryCalculation().ensureSourceA().setResultSetName("Frequency Response"); - nodeConfig.setMethod("specifyDataPoints"); - nodeConfig.setInputChannels(BOX_A, Collections.singletonList(buildInputChannel("Ch1"))); - - FlowNodeConfigDto currentNode = new FlowNodeConfigDto(); - currentNode.setId(postNodeId); - currentNode.setNodeConfig(nodeConfig); - currentNode.setSourceDashboardResultDtos(Collections.singletonList(currentViewResult)); - currentNode.setSourceGraphicConfigs(Collections.singletonList(currentViewGraphic)); - currentNode.putPrebuiltNodeDisplayAsset(BOX_A, prebuiltAsset); - - SequenceNodeSnapshotService.NodeExecutionSnapshotInitResult snapshotInitResult = - sequenceNodeSnapshotService.createNodeExecutionSnapshot(currentNode, context, 0, 88L, BOX_A); - - JsonNode graphicJson = new ObjectMapper().readTree(snapshotInitResult.getGraphicConfigsJson()).get(0); - JsonNode dashboardResultJson = new ObjectMapper().readTree(snapshotInitResult.getDashboardResultsJson()).get(0); - - assertEquals("dBV", snapshotInitResult.getDashboardResults().get(0).getCustomUnitY()); - assertEquals("dBV", snapshotInitResult.getDashboardResults().get(0).getChannels().get(0).getChannelUnitY()); - assertEquals("dBV", snapshotInitResult.getGraphicConfigs().get(0).getResultCodes().get(0).getUnitInfos().get(0).getUnit()); - assertEquals("dBV", dashboardResultJson.get("customUnitY").asText()); - assertEquals("dBV", dashboardResultJson.get("channels").get(0).get("channelUnitY").asText()); - assertEquals("dBV", graphicJson.get("resultCodes").get(0).get("unitInfos").get(0).get("unit").asText()); - assertEquals("dBV", graphicJson.get("chartConfig").get("line").get("yAxis").get("unit").asText()); - } @Test void createNodeExecutionSnapshotBackfillsExistingReferenceAnalysisDisplayAssetColorsFromRuntimeContext() throws Exception { diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java index 9ec0a817..988bd4f5 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java @@ -76,11 +76,13 @@ void openIsolatedDatabase() throws Exception { UnpooledDataSource dataSource = new UnpooledDataSource( "org.sqlite.JDBC", "jdbc:sqlite:" + temporaryDirectory.resolve("queue.sqlite"), null, null); try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { + connection.setAutoCommit(false); statement.execute("CREATE TABLE flow_execute_result (id BIGINT PRIMARY KEY, tenant_id BIGINT, created_time TEXT)"); executeMigration(statement, "V1.13.6__20260523_autosave_export_job.sql"); executeMigration(statement, "V1.13.8__20260525_autosave_file_group_context.sql"); executeMigration(statement, "V2.0.4_20260714201211__add_autosave_export_job_error_code.sql"); executeMigration(statement, "V2.5.0_20261103140000__autosave_commit_receipts_and_source_order.sql"); + connection.commit(); } MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(true); diff --git a/apps/backend/opentest-dashboard-api/src/test/java/com/opentest/dashboard/api/FullScaleBasisContractTest.java b/apps/backend/opentest-dashboard-api/src/test/java/com/opentest/dashboard/api/FullScaleBasisContractTest.java index 1383ae19..d6260d4e 100644 --- a/apps/backend/opentest-dashboard-api/src/test/java/com/opentest/dashboard/api/FullScaleBasisContractTest.java +++ b/apps/backend/opentest-dashboard-api/src/test/java/com/opentest/dashboard/api/FullScaleBasisContractTest.java @@ -39,13 +39,6 @@ class FullScaleBasisContractTest { assertEquals(FullScaleBasis.AES17_SINE_RMS, curve.withDashboardResultId(2L).fullScaleBasis()); } - @Test void conversionRequestCarriesIndependentSourceAndTargetBasis() { - var channel = new ChannelMetadata("input", SignalType.AUDIO, 48000, "FS", null, 2D, null); - var request = new UnitConversionRequest("RMS Level", new double[]{1}, "FS", "FS", channel, null, - FullScaleBasis.SAMPLE_PEAK_REFERENCE, FullScaleBasis.AES17_SINE_RMS); - assertEquals(FullScaleBasis.SAMPLE_PEAK_REFERENCE, request.sourceFullScaleBasis()); - assertEquals(FullScaleBasis.AES17_SINE_RMS, request.targetFullScaleBasis()); - } private static CompletedResultCurve curve(FullScaleBasis basis) { return new CompletedResultCurve("input", null, "RMS Level", "key", new double[0], new double[]{1}, From 32638b52bac848318de8f5c7a8c6d724af97c6dd Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 12:09:45 +0800 Subject: [PATCH 03/12] perf(frontend): reuse the detached dialog test module --- .../__tests__/sequence-detached-dialog-host.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/frontend/src/modules/sequence/detached-hosts/__tests__/sequence-detached-dialog-host.test.ts b/apps/frontend/src/modules/sequence/detached-hosts/__tests__/sequence-detached-dialog-host.test.ts index d97a3513..eef60c01 100644 --- a/apps/frontend/src/modules/sequence/detached-hosts/__tests__/sequence-detached-dialog-host.test.ts +++ b/apps/frontend/src/modules/sequence/detached-hosts/__tests__/sequence-detached-dialog-host.test.ts @@ -319,7 +319,6 @@ const mountHost = (viewState: DetachedViewState, { stubs = {}, directives = {}, describe('SequenceDetachedDialogHost', () => { beforeEach(async () => { - vi.resetModules() vi.useRealTimers() showOpenDialogMock.mockReset().mockResolvedValue([]) sendSequenceDialogEventMock.mockClear() From 668341994e9a81b6760cf3f801dcf01b800fd48b Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 12:30:17 +0800 Subject: [PATCH 04/12] test(recording): compare reanalysis paths across platforms --- .../MeasureReanalysisOutputPathContractTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputPathContractTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputPathContractTest.java index 2532b115..c76f9ec3 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputPathContractTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputPathContractTest.java @@ -107,9 +107,9 @@ void savedChildOutputRoundTripsThroughHistoricalMapperWithOneStorageRoot( assertEquals(1, rendered.getShardsNo()); assertEquals("FS", rendered.getFileUnitY()); Path expected = root.resolve(relative); - assertEquals(expected.toString(), rendered.getFilePath()); - assertEquals(expected.toString().substring(0, expected.toString().lastIndexOf('_')), - rendered.getBasePathPrefix()); + assertEquals(expected, Path.of(rendered.getFilePath())); + assertEquals(Path.of(expected.toString().substring(0, expected.toString().lastIndexOf('_'))), + Path.of(rendered.getBasePathPrefix())); assertTrue(Files.isRegularFile(Path.of(rendered.getFilePath()))); try (var input = Files.newInputStream(Path.of(rendered.getFilePath()))) { var read = CrybinReader.read(input, saved.getFileName()); From 7d85eb1b417c86039b26b37986c741b13127ef4f Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 13:10:11 +0800 Subject: [PATCH 05/12] perf(ci): run backend tests in two isolated JVMs --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fecc52b0..a9707c5f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,4 +67,4 @@ jobs: java-version: '21' cache: maven - name: Build and test backend - run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' -f apps/backend/pom.xml verify + run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-DforkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify From a6492a282c4cbbbfc132b901ebad049bdd59b83d Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 13:25:17 +0800 Subject: [PATCH 06/12] fix(tests): isolate native library caches by fork --- apps/backend/pom.xml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/apps/backend/pom.xml b/apps/backend/pom.xml index 46527dd8..e9825219 100644 --- a/apps/backend/pom.xml +++ b/apps/backend/pom.xml @@ -69,6 +69,24 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + ${project.build.directory}/test-native/${surefire.forkNumber} + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + ${project.build.directory}/test-native/${surefire.forkNumber} + + + org.apache.maven.plugins maven-compiler-plugin From e4e259c24d6bd0739e971e53d5fedab0fdd545a1 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 13:27:42 +0800 Subject: [PATCH 07/12] fix(ci): keep recording integration tests serial --- .github/workflows/ci.yml | 2 +- apps/backend/pom.xml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9707c5f..054e6734 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,4 +67,4 @@ jobs: java-version: '21' cache: maven - name: Build and test backend - run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-DforkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify + run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-Dbackend.test.forkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify diff --git a/apps/backend/pom.xml b/apps/backend/pom.xml index e9825219..a6218f49 100644 --- a/apps/backend/pom.xml +++ b/apps/backend/pom.xml @@ -40,6 +40,7 @@ + 1 2.5.0 21 UTF-8 @@ -73,6 +74,7 @@ org.apache.maven.plugins maven-surefire-plugin + ${backend.test.forkCount} ${project.build.directory}/test-native/${surefire.forkNumber} From 76f220e520ea2dcef9d386007dff1f630a5875b6 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 14:15:04 +0800 Subject: [PATCH 08/12] test(ci): report individual backend test durations --- .github/workflows/ci.yml | 10 ++ tools/ci/report-backend-test-times.py | 135 ++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 tools/ci/report-backend-test-times.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 054e6734..167bb127 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,3 +68,13 @@ jobs: cache: maven - name: Build and test backend run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-Dbackend.test.forkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify + - name: Report backend case timings + if: ${{ always() }} + run: python tools/ci/report-backend-test-times.py --output backend-test-times.json + - name: Upload backend case timings + if: ${{ always() }} + uses: actions/upload-artifact@v7 + with: + name: backend-test-times + path: backend-test-times.json + if-no-files-found: error diff --git a/tools/ci/report-backend-test-times.py b/tools/ci/report-backend-test-times.py new file mode 100644 index 00000000..7d0634ae --- /dev/null +++ b/tools/ci/report-backend-test-times.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +"""Export Surefire and Failsafe case timings without report environment data.""" + +import argparse +from collections import Counter +from decimal import Decimal, InvalidOperation +import json +from pathlib import Path +import xml.etree.ElementTree as ET + + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument("--backend-root", type=Path, default=Path("apps/backend")) +parser.add_argument("--output", type=Path, default=Path("backend-test-times.json")) +args = parser.parse_args() + +reports = sorted( + path + for phase in ("surefire", "failsafe") + for path in args.backend_root.glob(f"**/target/{phase}-reports/TEST-*.xml") +) +if not reports: + parser.error(f"no Surefire or Failsafe TEST-*.xml reports under {args.backend_root}") + +cases = [] +suites = [] +threshold_seconds = Decimal("0.5") +case_seconds = Decimal(0) +suite_seconds = Decimal(0) +try: + for report in reports: + relative_report = report.relative_to(args.backend_root) + module = report.parent.parent.parent.relative_to(args.backend_root).as_posix() + phase = report.parent.name.removesuffix("-reports") + suite = ET.parse(report).getroot() + if suite.tag != "testsuite": + raise ValueError(f"expected testsuite root, got {suite.tag}") + suite_name = suite.attrib["name"] + if not suite_name: + raise ValueError("empty suite name") + duration = Decimal(suite.attrib["time"]) + if not duration.is_finite() or duration < 0: + raise ValueError("suite time must be finite and nonnegative") + totals = {key: int(suite.attrib[key]) for key in ("tests", "failures", "errors", "skipped")} + if any(value < 0 for value in totals.values()): + raise ValueError("suite counts must be nonnegative") + suite_cases = [] + attributed_seconds = Decimal(0) + statuses = Counter() + for case in suite.findall("testcase"): + class_name = case.attrib["classname"] + case_name = case.attrib["name"] + if not class_name or not case_name: + raise ValueError("empty testcase class or name") + seconds = Decimal(case.attrib["time"]) + if not seconds.is_finite() or seconds < 0: + raise ValueError(f"invalid testcase time for {class_name}#{case_name}") + outcomes = [status for status in ("failure", "error", "skipped") if case.find(status) is not None] + if len(outcomes) > 1: + raise ValueError(f"multiple outcomes for {class_name}#{case_name}") + status = outcomes[0] if outcomes else "passed" + statuses[status] += 1 + attributed_seconds += seconds + suite_cases.append({ + "module": module, + "phase": phase, + "report": relative_report.as_posix(), + "class": class_name, + "case": case_name, + "status": status, + "seconds": float(seconds), + "over_threshold": seconds > threshold_seconds, + }) + observed = { + "tests": len(suite_cases), + "failures": statuses["failure"], + "errors": statuses["error"], + "skipped": statuses["skipped"], + } + if totals != observed: + raise ValueError(f"suite counts {totals} do not match testcases {observed}") + suites.append({ + "module": module, + "phase": phase, + "report": relative_report.as_posix(), + "class": suite_name, + **totals, + "seconds": float(duration), + "case_seconds": float(attributed_seconds), + "unattributed_seconds": float(duration - attributed_seconds), + }) + cases.extend(suite_cases) + case_seconds += attributed_seconds + suite_seconds += duration +except (OSError, ET.ParseError, KeyError, ValueError, InvalidOperation) as error: + parser.error(f"cannot read {report}: {error}") + +cases.sort(key=lambda case: (-case["seconds"], case["report"], case["class"], case["case"])) +status_counts = Counter(case["status"] for case in cases) +summary = { + "report_count": len(reports), + "suite_count": len(suites), + "case_count": len(cases), + "status_counts": {status: status_counts[status] for status in ("passed", "failure", "error", "skipped")}, + "over_threshold_count": sum(case["over_threshold"] for case in cases), + "case_seconds": float(case_seconds), + "suite_seconds": float(suite_seconds), + "unattributed_seconds": float(suite_seconds - case_seconds), +} +result = { + "threshold_seconds": float(threshold_seconds), + "timing_notes": [ + "Case times are the XML testcase times and can include framework setup or teardown attributed to a case.", + "Unattributed time is suite time minus summed case time. It can be negative with overlapping cases or rounding.", + "Summed suite times are not build wall time and do not include all Maven or JVM startup overhead.", + "The 500 ms threshold is reported only and does not change test execution or fail this command.", + ], + "summary": summary, + "suites": suites, + "cases": cases, +} +try: + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(result, indent=2, ensure_ascii=True, allow_nan=False) + "\n", encoding="utf-8") +except (OSError, ValueError) as error: + parser.error(f"cannot write {args.output}: {error}") + +print(f"Backend timing: {len(reports)} reports, {len(cases)} cases, {summary['over_threshold_count']} over 500 ms.") +print(f"Suite total {suite_seconds:.3f}s; case total {case_seconds:.3f}s; unattributed {suite_seconds - case_seconds:.3f}s.") +for note in result["timing_notes"]: + print(note) +print("Slowest 20 cases:") +for case in cases[:20]: + print(f" {case['seconds'] * 1000:10.3f} ms {case['status']:7} {case['module']} [{case['phase']}] {case['class']}#{case['case']}") +print(f"All case timings written to {args.output}") From a1be8a264b3577db8991f0dbdec83afe1d0c43e8 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 14:17:59 +0800 Subject: [PATCH 09/12] perf(tests): batch migration fixture writes --- .../ReferenceTestMigrationLogStartupTest.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java index 4dd1ee35..d12c8f89 100644 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java +++ b/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java @@ -18,6 +18,7 @@ import org.springframework.context.support.StaticApplicationContext; import org.springframework.test.util.ReflectionTestUtils; +import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -30,7 +31,13 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; /** 真实升级队列的日志等级、一次性处理和事务边界,全部使用本地临时 SQLite。 */ @ResourceLock("ReferenceTestSelectionUpgradeGuard.logger") @@ -47,6 +54,7 @@ class ReferenceTestMigrationLogStartupTest { void manyOrdinaryGraphsAndUnfrozenHistoryProduceOneInfoSummaryAndNoWarnings() throws Exception { try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { try (Connection connection = pool.getConnection()) { + connection.setAutoCommit(false); schema211(connection); for (int id = 1; id <= 120; id++) { graph(connection, id, "FFT Spectrum", "Vrms"); @@ -57,6 +65,8 @@ void manyOrdinaryGraphsAndUnfrozenHistoryProduceOneInfoSummaryAndNoWarnings() th insert(connection, "INSERT INTO graphic_config_snapshot VALUES (?, ?)", id, "{\"unit\":\"historical-frozen-unit\"}"); } + connection.commit(); + connection.setAutoCommit(true); } startup(pool, resources()); String summary = logs.onlySummary(Level.INFO); @@ -81,6 +91,7 @@ void manyOrdinaryGraphsAndUnfrozenHistoryProduceOneInfoSummaryAndNoWarnings() th void onlyRelatedUnresolvedSelectionsRaiseOneWarningWithSeparateUnknownCount() throws Exception { try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { try (Connection connection = pool.getConnection()) { + connection.setAutoCommit(false); schema211(connection); for (int id = 1; id <= 25; id++) { graph(connection, id, "FFT Spectrum", "Vrms"); @@ -88,6 +99,8 @@ void onlyRelatedUnresolvedSelectionsRaiseOneWarningWithSeparateUnknownCount() th graph(connection, 26, "Cross Spectrum", "unrecognized-old-unit"); graph(connection, 27, "Cross Spectrum", "another-unrecognized-unit"); execute(connection, "INSERT INTO graphic_config_snapshot VALUES (1, '{\"unit\":\"V²\"}')"); + connection.commit(); + connection.setAutoCommit(true); } startup(pool, resources()); String summary = logs.onlySummary(Level.WARN); @@ -115,12 +128,15 @@ void alreadyProcessedEvidenceIsNeverReclassifiedAndRepeatedStartupHasNoSummary() String oldSelection = "{\"Cross Spectrum\":[{\"channelType\":0,\"unit\":\"m/s\"}]}"; try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { try (Connection connection = pool.getConnection()) { + connection.setAutoCommit(false); schema211(connection); graph(connection, 1, "Cross Spectrum", "unrecognized-old-unit"); graph(connection, 2, "FFT Spectrum", "Vrms"); execute(connection, resourceSql(EVIDENCE)); insert(connection, "INSERT INTO reference_test_selection_evidence VALUES ('graphic_config', 1, 'EXPLICIT_OLD_VALUE', ?, ?, 1, 'UNRESOLVED')", oldSelection, frozenContext); + connection.commit(); + connection.setAutoCommit(true); } Path migrationResources = resources(); startup(pool, migrationResources); @@ -155,12 +171,15 @@ void alreadyProcessedEvidenceIsNeverReclassifiedAndRepeatedStartupHasNoSummary() void failedRestoreRollsBackSelectionsAndProcessingWithoutSuccessSummary() throws Exception { try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { try (Connection connection = pool.getConnection()) { + connection.setAutoCommit(false); schema211(connection); graph(connection, 1, "Cross Spectrum", "(m/s)²"); graph(connection, 2, "Cross Spectrum", "(m/s)²"); execute(connection, resourceSql(EVIDENCE)); execute(connection, "CREATE TRIGGER fail_second BEFORE UPDATE OF processed ON reference_test_selection_evidence " + "WHEN NEW.source_id=2 BEGIN SELECT RAISE(ABORT, 'restore fixture failure'); END"); + connection.commit(); + connection.setAutoCommit(true); } Path migrationResources = resources(); assertThrows(IllegalStateException.class, () -> startup(pool, migrationResources)); @@ -228,7 +247,7 @@ private String resourceSql(String name) throws Exception { private void startup(DruidDataSource pool, Path resources) { assertTimeoutPreemptively(Duration.ofSeconds(10), () -> { ClassLoader previous = Thread.currentThread().getContextClassLoader(); - try (URLClassLoader loader = new URLClassLoader(new java.net.URL[]{resources.toUri().toURL()}, previous); + try (URLClassLoader loader = new URLClassLoader(new URL[]{resources.toUri().toURL()}, previous); StaticApplicationContext context = new StaticApplicationContext()) { Thread.currentThread().setContextClassLoader(loader); context.registerSingleton("preserveSelections", V2_2_0_20260826162241__preserve_reference_test_selections.class); From ccdae68f750ef918a9397a7760f54cc8556b10a7 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 14:41:50 +0800 Subject: [PATCH 10/12] fix(ci): preserve Unicode in test timing output --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 167bb127..b0dd0ba6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,7 +70,7 @@ jobs: run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-Dbackend.test.forkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify - name: Report backend case timings if: ${{ always() }} - run: python tools/ci/report-backend-test-times.py --output backend-test-times.json + run: python -X utf8 tools/ci/report-backend-test-times.py --output backend-test-times.json - name: Upload backend case timings if: ${{ always() }} uses: actions/upload-artifact@v7 From 4919631fe709b6335ae32cd8b58e00de56635ee9 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 14:48:21 +0800 Subject: [PATCH 11/12] fix(ci): exclude stale test timing reports --- .github/workflows/ci.yml | 6 ++++-- tools/ci/report-backend-test-times.py | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b0dd0ba6..974ad46e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,10 +67,12 @@ jobs: java-version: '21' cache: maven - name: Build and test backend - run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-Dbackend.test.forkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify + run: | + python -c "import time; print(time.time())" > backend-test-started.txt + mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-Dbackend.test.forkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify - name: Report backend case timings if: ${{ always() }} - run: python -X utf8 tools/ci/report-backend-test-times.py --output backend-test-times.json + run: python -X utf8 tools/ci/report-backend-test-times.py --since (Get-Content backend-test-started.txt) --output backend-test-times.json - name: Upload backend case timings if: ${{ always() }} uses: actions/upload-artifact@v7 diff --git a/tools/ci/report-backend-test-times.py b/tools/ci/report-backend-test-times.py index 7d0634ae..f91c8f12 100644 --- a/tools/ci/report-backend-test-times.py +++ b/tools/ci/report-backend-test-times.py @@ -5,6 +5,7 @@ from collections import Counter from decimal import Decimal, InvalidOperation import json +import math from pathlib import Path import xml.etree.ElementTree as ET @@ -12,12 +13,16 @@ parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--backend-root", type=Path, default=Path("apps/backend")) parser.add_argument("--output", type=Path, default=Path("backend-test-times.json")) +parser.add_argument("--since", type=float, help="Only include reports modified since this Unix timestamp.") args = parser.parse_args() +if args.since is not None and (not math.isfinite(args.since) or args.since < 0): + parser.error("--since must be a finite nonnegative Unix timestamp") reports = sorted( path for phase in ("surefire", "failsafe") for path in args.backend_root.glob(f"**/target/{phase}-reports/TEST-*.xml") + if args.since is None or path.stat().st_mtime >= args.since ) if not reports: parser.error(f"no Surefire or Failsafe TEST-*.xml reports under {args.backend_root}") @@ -109,7 +114,9 @@ } result = { "threshold_seconds": float(threshold_seconds), + "reports_since": args.since, "timing_notes": [ + "Without --since, all existing matching XML files are included, potentially from older runs.", "Case times are the XML testcase times and can include framework setup or teardown attributed to a case.", "Unattributed time is suite time minus summed case time. It can be negative with overlapping cases or rounding.", "Summed suite times are not build wall time and do not include all Maven or JVM startup overhead.", From 5eceaf59b109cc73157d1aef41297cf70e7722ea Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 15:36:31 +0800 Subject: [PATCH 12/12] perf(tests): remove slow low-value backend suites --- .github/workflows/ci.yml | 14 +- .../configurer/CloudAuthHttpContractTest.java | 438 ----- .../ReferenceTestMigrationLogStartupTest.java | 331 ---- .../CompleteStorageSnapshotEntryTest.java | 79 - .../ContinuousMeasurementEntryTest.java | 71 - .../MonitorInputSlotLifecycleEntryIT.java | 553 ------ ...portedPostProcessingGraphicSqliteTest.java | 1636 ----------------- .../FileServiceDependencyGraphTest.java | 9 +- .../FileServiceStartupComponentTest.java | 91 - .../DevicePluginManagementTest.java | 791 -------- .../AutoSaveOrderedSchedulingSqliteTest.java | 613 ------ .../MeasureReanalysisExecutorTest.java | 990 ---------- apps/backend/pom.xml | 20 - .../plugin-pipeline-file-output.md | 2 +- .../plugin-pipeline-file-output_cn.md | 2 +- tools/ci/report-backend-test-times.py | 142 -- 16 files changed, 11 insertions(+), 5771 deletions(-) delete mode 100644 apps/backend/crydaq-server/src/test/java/com/opentest/configurer/CloudAuthHttpContractTest.java delete mode 100644 apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java delete mode 100644 apps/backend/crydaq-server/src/test/java/com/opentest/plugin/CompleteStorageSnapshotEntryTest.java delete mode 100644 apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ContinuousMeasurementEntryTest.java delete mode 100644 apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MonitorInputSlotLifecycleEntryIT.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/deviceplugin/DevicePluginManagementTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java delete mode 100644 apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutorTest.java delete mode 100644 tools/ci/report-backend-test-times.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 974ad46e..fecc52b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,16 +67,4 @@ jobs: java-version: '21' cache: maven - name: Build and test backend - run: | - python -c "import time; print(time.time())" > backend-test-started.txt - mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' '-Dbackend.test.forkCount=2' '-DreuseForks=true' -f apps/backend/pom.xml verify - - name: Report backend case timings - if: ${{ always() }} - run: python -X utf8 tools/ci/report-backend-test-times.py --since (Get-Content backend-test-started.txt) --output backend-test-times.json - - name: Upload backend case timings - if: ${{ always() }} - uses: actions/upload-artifact@v7 - with: - name: backend-test-times - path: backend-test-times.json - if-no-files-found: error + run: mvn -B --no-transfer-progress --fail-at-end '-Dskip.crowdin.download=true' -f apps/backend/pom.xml verify diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/CloudAuthHttpContractTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/CloudAuthHttpContractTest.java deleted file mode 100644 index f4ad9eaa..00000000 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/CloudAuthHttpContractTest.java +++ /dev/null @@ -1,438 +0,0 @@ -package com.opentest.configurer; - -import com.opentest.common.cloud.CloudApiClient; -import com.opentest.common.cloud.CloudAuthException; -import com.opentest.common.context.ContextHolder; -import com.opentest.common.enums.ContextTypeEnum; -import com.opentest.common.model.LoginUserInfo; -import com.opentest.common.utils.LoginUserUtils; -import com.opentest.common.utils.SpringContextHolder; -import com.opentest.controller.CloudAuthController; -import com.opentest.controller.CloudLogoutController; -import com.opentest.service.auth.AuthV2Service; -import com.opentest.service.auth.OfflineWorkspaceSessionService; -import com.opentest.service.auth.cloud.CloudLoginRecord; -import com.opentest.service.auth.cloud.CloudLoginService; -import com.opentest.service.auth.cloud.CloudLoginStore; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.servlet.Filter; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.MediaType; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.mock.web.MockServletContext; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.context.SecurityContextImpl; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.authority.AuthorityUtils; -import org.springframework.security.web.context.HttpSessionSecurityContextRepository; -import org.springframework.transaction.support.TransactionSynchronizationManager; -import org.springframework.session.Session; -import org.springframework.session.SessionRepository; -import org.springframework.session.jdbc.JdbcIndexedSessionRepository; -import org.springframework.session.web.http.SessionRepositoryFilter; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.transaction.support.TransactionTemplate; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; - -import java.nio.file.Path; -import java.time.Duration; -import java.time.Instant; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -/** Real HTTP security chain, cloud lifecycle, and SQLite Sessions; remote wire failures are controlled. */ -class CloudAuthHttpContractTest { - @TempDir Path directory; - private static final ObjectMapper JSON = new ObjectMapper(); - - @Test - void loginCommitsRealSessionAndNeverReturnsCloudCredentials() throws Exception { - try (Fixture f = new Fixture(directory.resolve("login.db"))) { - String id = f.login(); - assertNotNull(f.repository.findById(id)); - assertEquals(id, f.cloud.current().getLocalSessionId()); - assertEquals(Duration.ofSeconds(-1), f.repository.findById(id).getMaxInactiveInterval()); - f.mvc.perform(get("/v1/auth/user").header("JSESSIONID", id)) - .andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value(true)) - .andExpect(jsonPath("$.userId").value("user-1")); - f.mvc.perform(post("/protected").header("JSESSIONID", id)).andExpect(status().isOk()); - String second = f.login(id); - assertEquals(id, second); - assertEquals(1, f.jdbc.queryForObject("SELECT COUNT(*) FROM SPRING_SESSION", Integer.class)); - } - } - - @Test - void repeatedPasswordLoginUpdatesStoredProfileWithoutChangingSession() throws Exception { - try (Fixture f = new Fixture(directory.resolve("profile-refresh.db"))) { - String id = f.login(); - assertNull(f.cloud.current().getUser().getFirstName()); - f.remote.reuseFirstName = "Ada"; - f.remote.reuseLastName = "Lovelace"; - assertEquals(id, f.login(id)); - assertEquals("Ada", f.cloud.current().getUser().getFirstName()); - assertEquals("Lovelace", f.cloud.current().getUser().getLastName()); - LoginUserInfo sessionUser = f.repository.findById(id).getAttribute(LoginUserUtils.SESSION_USER_KEY); - assertEquals("Ada", sessionUser.getFirstName()); - assertEquals("Lovelace", sessionUser.getLastName()); - assertEquals(1, f.jdbc.queryForObject("SELECT COUNT(*) FROM SPRING_SESSION", Integer.class)); - assertEquals(2, f.remote.logins.get()); - } - } - - @Test - void repeatedPasswordLoginRejectsProfileForAnotherUser() throws Exception { - try (Fixture f = new Fixture(directory.resolve("profile-mismatch.db"))) { - String id = f.login(); - f.remote.reuseFirstName = "Wrong"; - f.remote.reuseLastName = "User"; - f.remote.reuseProfileUserId = "other-user"; - f.mvc.perform(post("/v2/auth/login").header("JSESSIONID", id).contentType(MediaType.APPLICATION_JSON) - .content("{\"username\":\"user\",\"password\":\"password\"}")) - .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(1201014)); - assertNull(f.cloud.current().getUser().getFirstName()); - LoginUserInfo sessionUser = f.repository.findById(id).getAttribute(LoginUserUtils.SESSION_USER_KEY); - assertNull(sessionUser.getFirstName()); - assertEquals("user-1", sessionUser.getUserId()); - } - } - - @Test - void failedCredentialSaveRollsBackTheRealSessionInsert() throws Exception { - try (Fixture f = new Fixture(directory.resolve("save-fail.db"))) { - f.jdbc.execute("CREATE TRIGGER fail_login BEFORE INSERT ON OPENTEST_CLOUD_LOGIN BEGIN SELECT RAISE(ABORT, 'fixture'); END"); - f.mvc.perform(post("/v2/auth/login").contentType(MediaType.APPLICATION_JSON) - .content("{\"username\":\"user\",\"password\":\"password\"}")) - .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(1201004)) - .andExpect(jsonPath("$.isLogin").value(false)).andExpect(jsonPath("$.data.JSESSIONID").doesNotExist()); - assertEquals(0, f.jdbc.queryForObject("SELECT COUNT(*) FROM SPRING_SESSION", Integer.class)); - assertNull(f.cloud.current()); - } - } - - @Test - void failedRepeatPasswordKeepsOriginalAuthentication() throws Exception { - try (Fixture f = new Fixture(directory.resolve("password.db"))) { - String id = f.login(); - f.remote.loginFailure = new CloudAuthException(1201001, "云账号认证失败"); - f.mvc.perform(post("/v2/auth/login").header("JSESSIONID", id).contentType(MediaType.APPLICATION_JSON) - .content("{\"username\":\"user\",\"password\":\"wrong\"}")) - .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(1201001)) - .andExpect(jsonPath("$.isLogin").value(true)); - f.mvc.perform(post("/protected").header("JSESSIONID", id)).andExpect(status().isOk()); - assertEquals(id, f.cloud.current().getLocalSessionId()); - } - } - - @Test - void licenseFailureAfterLoginStillReturnsDurableIdentity() throws Exception { - try (Fixture f = new Fixture(directory.resolve("certificate.db"))) { - doThrow(new IllegalStateException("JNI unavailable")) - .when(f.licenses).downloadCertificateByResolvedUser("user-1"); - String id = f.login(); - f.mvc.perform(post("/protected").header("JSESSIONID", id)).andExpect(status().isOk()); - } - } - - @Test - void malformedLoginDoesNotEchoPasswordOrOpenOtherAuthPaths() throws Exception { - try (Fixture f = new Fixture(directory.resolve("invalid.db"))) { - for (String body : new String[]{"{\"username\":\"user\",\"password\":\"secret\",\"userId\":\"other\"}", - "{\"password\":\"never-echo-this", "never-echo-this"}) { - MvcResult result = f.mvc.perform(post("/v2/auth/login").contentType(MediaType.APPLICATION_JSON).content(body)) - .andExpect(status().isBadRequest()).andExpect(jsonPath("$.code").value(1201017)) - .andExpect(header().string("Cache-Control", "no-store")).andReturn(); - assertFalse(result.getResponse().getContentAsString().contains("secret")); - assertFalse(result.getResponse().getContentAsString().contains("never-echo")); - if (result.getResolvedException() != null) { - assertFalse(result.getResolvedException().getMessage().contains("never-echo")); - assertNull(result.getResolvedException().getCause()); - } - } - f.mvc.perform(get("/v2/auth/login")).andExpect(status().isUnauthorized()); - f.mvc.perform(post("/v1/auth/casdoor/login")).andExpect(status().isUnauthorized()); - f.mvc.perform(get("/oauth2/authorization/casdoor")).andExpect(status().isUnauthorized()); - assertEquals(0, f.remote.logins.get()); - } - } - - @Test - void strictCertificateFailurePreventsCloudRevocationAndRetainsSession() throws Exception { - try (Fixture f = new Fixture(directory.resolve("offline-fail.db"))) { - String id = f.login(); - doThrow(new CloudAuthException(1201013, "离线证书切换失败")).when(f.licenses).switchToOfflineCertificateStrict(); - f.mvc.perform(post("/logout").header("JSESSIONID", id)) - .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(1201013)) - .andExpect(jsonPath("$.isLogin").value(true)); - assertEquals(0, f.remote.logouts.get()); - assertNotNull(f.repository.findById(id)); - assertNotNull(f.cloud.current()); - verifyNoInteractions(f.tasks); - } - } - - @ParameterizedTest - @ValueSource(booleans = {false, true}) - void cloudFailureCanBeManuallyRetriedAndOnlyOriginalSessionIsDeleted(boolean query) throws Exception { - try (Fixture f = new Fixture(directory.resolve("logout-" + query + ".db"))) { - String id = f.login(); - Session unrelated = f.repository.createSession(); - unrelated.setMaxInactiveInterval(Duration.ofDays(1)); - f.repository.save(unrelated); - var inFlight = f.repository.findById(id); - f.remote.logoutFailure = CloudAuthException.unavailable(); - var first = query ? post("/logout").queryParam("JSESSIONID", id) : post("/logout").header("JSESSIONID", id); - f.mvc.perform(first).andExpect(status().isOk()).andExpect(jsonPath("$.code").value(1201000)) - .andExpect(jsonPath("$.isLogin").value(true)); - assertNotNull(f.repository.findById(id)); - f.remote.logoutFailure = null; - var retry = query ? post("/logout").queryParam("JSESSIONID", id) : post("/logout").header("JSESSIONID", id); - f.mvc.perform(retry).andExpect(status().isOk()).andExpect(jsonPath("$.code").value(0)) - .andExpect(jsonPath("$.isLogin").value(false)); - assertNull(f.cloud.current()); - assertNull(f.repository.findById(id)); - assertNotNull(f.repository.findById(unrelated.getId())); - inFlight.setLastAccessedTime(Instant.now()); - f.repository.save(inFlight); - assertNull(f.repository.findById(id)); - f.mvc.perform(post("/protected").header("JSESSIONID", id)).andExpect(status().isUnauthorized()); - assertEquals(2, f.remote.logouts.get()); - } - } - - @Test - void localCleanupFailureKeepsOriginalCredentialsForCompletedCloudRetry() throws Exception { - try (Fixture f = new Fixture(directory.resolve("cleanup-fail.db"))) { - String id = f.login(); - doThrow(new IllegalStateException("task shutdown unavailable")).when(f.tasks).stop(id); - f.mvc.perform(post("/logout").header("JSESSIONID", id)) - .andExpect(jsonPath("$.code").value(1201012)).andExpect(jsonPath("$.isLogin").value(true)); - assertNotNull(f.cloud.current()); - assertNotNull(f.repository.findById(id)); - reset(f.tasks); - f.mvc.perform(post("/logout").header("JSESSIONID", id)) - .andExpect(jsonPath("$.code").value(0)).andExpect(jsonPath("$.isLogin").value(false)); - assertEquals(2, f.remote.logouts.get()); - } - } - - @Test - void redundantWrapperDeletionCannotTurnCommittedLogoutIntoFailure() throws Exception { - try (Fixture f = new Fixture(directory.resolve("redundant-delete.db"))) { - String id = f.login(); - doAnswer(invocation -> { - if (!TransactionSynchronizationManager.isActualTransactionActive()) { - throw new IllegalStateException("database unavailable after successful commit"); - } - return invocation.callRealMethod(); - }).when(f.repository).deleteById(id); - f.mvc.perform(post("/logout").header("JSESSIONID", id)) - .andExpect(jsonPath("$.code").value(0)).andExpect(jsonPath("$.isLogin").value(false)); - assertNull(f.cloud.current()); - assertNull(f.repository.findById(id)); - } - } - - @Test - void oldSameUserSessionCannotBorrowLaterCloudLoginOverHttpOrGrpc() throws Exception { - try (Fixture f = new Fixture(directory.resolve("old-session.db"))) { - Session old = f.repository.createSession(); - old.setMaxInactiveInterval(Duration.ofDays(1)); - old.setAttribute(LoginUserUtils.SESSION_USER_KEY, LoginUserInfo.builder().userId("user-1") - .authenticated(true).authType("oauth2").build()); - old.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, - new SecurityContextImpl(UsernamePasswordAuthenticationToken.authenticated("user-1", null, AuthorityUtils.NO_AUTHORITIES))); - f.repository.save(old); - String current = f.login(); - f.mvc.perform(post("/protected").header("JSESSIONID", old.getId())).andExpect(status().isUnauthorized()); - f.mvc.perform(post("/v1/auth/cert/download").header("JSESSIONID", old.getId())) - .andExpect(jsonPath("$.code").value(1201006)); - try { - ContextHolder.setContext(ContextTypeEnum.JSESSIONID.getCode(), old.getId()); - assertNull(LoginUserUtils.getCurrentUserInfo()); - ContextHolder.setContext(ContextTypeEnum.JSESSIONID.getCode(), current); - assertEquals("user-1", LoginUserUtils.getCurrentUserInfo().getUserId()); - } finally { ContextHolder.clearAll(); } - assertNotNull(f.repository.findById(old.getId())); - } - } - - @Test - void offlineWorkspaceDoesNotAuthorizeCloudBinding() throws Exception { - try (Fixture f = new Fixture(directory.resolve("offline-bind.db"))) { - Session offline = f.repository.createSession(); - offline.setAttribute(OfflineWorkspaceSessionService.SESSION_KEY, OfflineWorkspaceSessionService.OWNER); - f.repository.save(offline); - f.mvc.perform(post("/v2/auth/bind-unbind").header("JSESSIONID", offline.getId()) - .contentType(MediaType.APPLICATION_JSON).content("{\"authCode\":\"code\",\"status\":\"BIND\"}")) - .andExpect(status().isUnauthorized()).andExpect(jsonPath("$.code").value(22)) - .andExpect(jsonPath("$.isLogin").value(false)); - verifyNoInteractions(f.licenses); - } - } - - @Test - void reopeningDatabaseKeepsPermanentLoginWithoutCloudContact() throws Exception { - Path database = directory.resolve("restart.db"); - String id; - try (Fixture f = new Fixture(database)) { id = f.login(); } - try (Fixture restored = new Fixture(database)) { - restored.remote.loginFailure = CloudAuthException.unavailable(); - restored.mvc.perform(post("/protected").header("JSESSIONID", id)).andExpect(status().isOk()); - assertEquals(id, restored.cloud.current().getLocalSessionId()); - assertEquals(0, restored.remote.logins.get()); - } - } - - @Test - void obsoleteGenerationCannotUseNewLoginAndInvalidCertificateSessionCannotFallBackToDevice() throws Exception { - try (Fixture f = new Fixture(directory.resolve("stale.db"))) { - String id = f.login(); - Session stale = f.repository.findById(id); - stale.setAttribute(CloudLoginService.SESSION_GENERATION, "another-generation"); - f.repository.save(stale); - f.mvc.perform(post("/protected").header("JSESSIONID", id)).andExpect(status().isUnauthorized()); - f.mvc.perform(get("/v1/auth/user").header("JSESSIONID", id)) - .andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value(false)); - f.mvc.perform(post("/v1/auth/cert/download").header("JSESSIONID", "missing-session")) - .andExpect(jsonPath("$.code").value(1201006)); - assertNotNull(f.repository.findById(id)); - } - } - - private static final class Fixture implements AutoCloseable { - final JdbcTemplate jdbc; - final SessionRepository repository; - final CloudLoginService cloud; - final Remote remote = new Remote(); - final AuthV2Service licenses = mock(AuthV2Service.class); - final CloudLogoutTasks tasks = mock(CloudLogoutTasks.class); - final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - final MockMvc mvc; - - @SuppressWarnings("unchecked") - Fixture(Path path) { - var source = new DriverManagerDataSource("jdbc:sqlite:" + path + "?foreign_keys=on&busy_timeout=10000"); - jdbc = new JdbcTemplate(source); - createTables(jdbc); - var manager = new DataSourceTransactionManager(source); - var jdbcSessions = new JdbcIndexedSessionRepository(jdbc, new TransactionTemplate(manager)); - new SessionPersistenceConfigurer().explicitSessionDeletion().customize(jdbcSessions); - repository = (SessionRepository) (SessionRepository) spy(jdbcSessions); - var store = new CloudLoginStore(source); - store.initialize(); - cloud = new CloudLoginService(store, remote, manager); - context.setServletContext(new MockServletContext()); - context.addBeanFactoryPostProcessor(factory -> { - factory.registerSingleton("cloudLoginService", cloud); - factory.registerSingleton("sessionRepository", repository); - factory.registerSingleton("authV2Service", licenses); - factory.registerSingleton("cloudLogoutTasks", tasks); - factory.registerSingleton("offlineWorkspaceSessionService", mock(OfflineWorkspaceSessionService.class)); - }); - context.register(Web.class, StateEndpoint.class, CloudAuthController.class, CloudLogoutController.class, - CloudLocalSessions.class, SecurityConfig.class, WorkspaceRequestAuthorization.class, CloudAuthWebConfigurer.class); - new SpringContextHolder().setApplicationContext(context); - context.refresh(); - var sessionFilter = new SessionRepositoryFilter<>(repository); - sessionFilter.setHttpSessionIdResolver(new HeaderHttpSessionIdResolver("JSESSIONID")); - mvc = MockMvcBuilders.webAppContextSetup(context) - .addFilters(sessionFilter, context.getBean("springSecurityFilterChain", Filter.class)).build(); - } - - String login() throws Exception { return login(null); } - String login(String currentId) throws Exception { - var request = post("/v2/auth/login").contentType(MediaType.APPLICATION_JSON) - .content("{\"username\":\"user\",\"password\":\"password\"}"); - if (currentId != null) request.header("JSESSIONID", currentId); - String body = mvc.perform(request).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(0)).andExpect(jsonPath("$.isLogin").value(true)) - .andExpect(header().string("Cache-Control", "no-store")).andReturn().getResponse().getContentAsString(); - for (String secret : new String[]{"accessToken", "refreshToken", "logoutToken", "cloudSessionId"}) assertFalse(body.contains(secret)); - return JSON.readTree(body).path("data").path("JSESSIONID").asText(); - } - public void close() { - context.close(); - new SpringContextHolder().setApplicationContext(null); - SecurityContextHolder.clearContext(); - } - } - - static void createTables(JdbcTemplate jdbc) { - jdbc.execute("CREATE TABLE IF NOT EXISTS SPRING_SESSION (PRIMARY_ID CHAR(36) PRIMARY KEY, SESSION_ID CHAR(36) UNIQUE NOT NULL, CREATION_TIME INTEGER NOT NULL, LAST_ACCESS_TIME INTEGER NOT NULL, MAX_INACTIVE_INTERVAL INTEGER NOT NULL, EXPIRY_TIME INTEGER NOT NULL, PRINCIPAL_NAME VARCHAR(100))"); - jdbc.execute("CREATE TABLE IF NOT EXISTS SPRING_SESSION_ATTRIBUTES (SESSION_PRIMARY_ID CHAR(36) NOT NULL, ATTRIBUTE_NAME VARCHAR(200) NOT NULL, ATTRIBUTE_BYTES BLOB NOT NULL, PRIMARY KEY (SESSION_PRIMARY_ID,ATTRIBUTE_NAME), FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE)"); - } - - private static final class Remote extends CloudApiClient { - final AtomicInteger logins = new AtomicInteger(); - final AtomicInteger logouts = new AtomicInteger(); - RuntimeException loginFailure; - RuntimeException logoutFailure; - String reuseFirstName; - String reuseLastName; - String reuseProfileUserId = "user-1"; - Remote() { super("https://manager.example"); } - @Override public JsonNode post(String path, Object body, String access) { - if (path.endsWith("/auth/login")) { - logins.incrementAndGet(); - if (loginFailure != null) throw loginFailure; - if (access != null && reuseFirstName != null) - return JSON.valueToTree(Map.of("cloudSessionId", "cloud-1", "userId", "user-1", - "user", Map.of("userId", reuseProfileUserId, "firstName", reuseFirstName, "lastName", reuseLastName))); - if (access != null) return JSON.valueToTree(Map.of("cloudSessionId", "cloud-1", "userId", "user-1")); - return JSON.valueToTree(Map.of("cloudSessionId", "cloud-1", "user", Map.of("userId", "user-1"), - "accessToken", "test_access", "logoutToken", "test_logout", "tokenType", "Bearer", "expiresIn", -1)); - } - if (path.endsWith("/auth/logout")) { - logouts.incrementAndGet(); - if (logoutFailure != null) throw logoutFailure; - return JSON.valueToTree(Map.of("cloudSessionId", "cloud-1", "cleanupStatus", "COMPLETED")); - } - throw new AssertionError("Unexpected cloud route: " + path); - } - } - - @Configuration(proxyBeanMethods = false) - @EnableWebMvc - static class Web { } - @RestController - static class StateEndpoint { - @GetMapping("/v1/auth/user") - Map user() { - LoginUserInfo user = LoginUserUtils.getCurrentUserInfo(); - return user == null ? Map.of("authenticated", false) : Map.of("authenticated", true, "userId", user.getUserId()); - } - @PostMapping("/protected") Map protectedCall() { return Map.of("ok", true); } - } -} diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java deleted file mode 100644 index d12c8f89..00000000 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/configurer/ReferenceTestMigrationLogStartupTest.java +++ /dev/null @@ -1,331 +0,0 @@ -package com.opentest.configurer; - -import com.alibaba.druid.pool.DruidDataSource; -import com.opentest.dbmigration.ReferenceTestSelectionUpgradeGuard; -import com.opentest.dbmigration.V2_2_0_20260826162241__preserve_reference_test_selections; -import com.opentest.dbmigration.V2_2_0_20260826165959__protect_reference_test_selections_before_backfill; -import com.opentest.dbmigration.V2_2_0_20260826170000__backfill_cross_spectrum_unit_config; -import com.opentest.dbmigration.V2_2_0_20260908170000__restore_reference_test_selections; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.Logger; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.config.Property; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.junit.jupiter.api.parallel.ResourceLock; -import org.springframework.context.support.StaticApplicationContext; -import org.springframework.test.util.ReflectionTestUtils; - -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Statement; -import java.time.Duration; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** 真实升级队列的日志等级、一次性处理和事务边界,全部使用本地临时 SQLite。 */ -@ResourceLock("ReferenceTestSelectionUpgradeGuard.logger") -class ReferenceTestMigrationLogStartupTest { - private static final String ADD = "V2.2.0_20260826162242__add_cross_spectrum_unit_config.sql"; - private static final String DROP = "V2.2.0_20260903143000__replace_cross_spectrum_with_reference_test_unit_config.sql"; - private static final String EVIDENCE = "V2.2.0_20260908153000__preserve_reference_test_selection_evidence.sql"; - private static final String SUMMARY = "Reference/Test 升级处理完成:"; - - @TempDir - Path temp; - - @Test - void manyOrdinaryGraphsAndUnfrozenHistoryProduceOneInfoSummaryAndNoWarnings() throws Exception { - try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { - try (Connection connection = pool.getConnection()) { - connection.setAutoCommit(false); - schema211(connection); - for (int id = 1; id <= 120; id++) { - graph(connection, id, "FFT Spectrum", "Vrms"); - } - // 空绑定和缺失冻结身份的历史不能被当作普通图。 - execute(connection, "INSERT INTO graphic_config VALUES (121, '{\"unit\":\"unbound\"}')"); - for (int id = 1; id <= 3; id++) { - insert(connection, "INSERT INTO graphic_config_snapshot VALUES (?, ?)", id, - "{\"unit\":\"historical-frozen-unit\"}"); - } - connection.commit(); - connection.setAutoCommit(true); - } - startup(pool, resources()); - String summary = logs.onlySummary(Level.INFO); - assertTrue(summary.contains("total=124"), summary); - assertTrue(summary.contains("NOT_APPLICABLE=120"), summary); - assertTrue(summary.contains("UNDETERMINED=4"), summary); - assertEquals(0, logs.count(Level.WARN)); - assertEquals(1, logs.count(Level.INFO)); - assertFalse(summary.contains("id="), summary); - try (Connection connection = pool.getConnection()) { - assertEquals("120", scalar(connection, "SELECT COUNT(*) FROM reference_test_selection_evidence WHERE recovery_status='NOT_APPLICABLE' AND processed=1")); - assertEquals("4", scalar(connection, "SELECT COUNT(*) FROM reference_test_selection_evidence WHERE recovery_status='UNDETERMINED' AND processed=1")); - assertEquals("0", scalar(connection, "SELECT COUNT(*) FROM graphic_config WHERE reference_test_unit_config IS NOT NULL")); - assertEquals("0", scalar(connection, "SELECT COUNT(*) FROM graphic_config_snapshot WHERE reference_test_unit_config IS NOT NULL")); - assertEquals("3", scalar(connection, "SELECT COUNT(*) FROM graphic_config_snapshot WHERE chart_config='{\"unit\":\"historical-frozen-unit\"}'")); - assertEquals("120", scalar(connection, "SELECT COUNT(*) FROM graphic_result_code WHERE unit LIKE '%Vrms%'")); - } - } - } - - @Test - void onlyRelatedUnresolvedSelectionsRaiseOneWarningWithSeparateUnknownCount() throws Exception { - try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { - try (Connection connection = pool.getConnection()) { - connection.setAutoCommit(false); - schema211(connection); - for (int id = 1; id <= 25; id++) { - graph(connection, id, "FFT Spectrum", "Vrms"); - } - graph(connection, 26, "Cross Spectrum", "unrecognized-old-unit"); - graph(connection, 27, "Cross Spectrum", "another-unrecognized-unit"); - execute(connection, "INSERT INTO graphic_config_snapshot VALUES (1, '{\"unit\":\"V²\"}')"); - connection.commit(); - connection.setAutoCommit(true); - } - startup(pool, resources()); - String summary = logs.onlySummary(Level.WARN); - assertTrue(summary.contains("total=28"), summary); - assertTrue(summary.contains("RELATED_UNRESOLVED=2"), summary); - assertTrue(summary.contains("NOT_APPLICABLE=25"), summary); - assertTrue(summary.contains("UNDETERMINED=1"), summary); - assertEquals(1, logs.count(Level.WARN)); - assertEquals(0, logs.count(Level.INFO)); - assertFalse(summary.contains("id="), summary); - assertTrue(logs.events.stream().anyMatch(event -> event.getLevel() == Level.DEBUG - && event.getMessage().getFormattedMessage().contains("id=26"))); - try (Connection connection = pool.getConnection()) { - assertEquals("2", scalar(connection, "SELECT COUNT(*) FROM reference_test_selection_evidence WHERE recovery_status='RELATED_UNRESOLVED' AND processed=1")); - assertEquals("UNDETERMINED", scalar(connection, "SELECT recovery_status FROM reference_test_selection_evidence WHERE source_table='graphic_config_snapshot'")); - assertEquals("0", scalar(connection, "SELECT COUNT(*) FROM graphic_config WHERE reference_test_unit_config IS NOT NULL")); - assertTrue(scalar(connection, "SELECT unit FROM graphic_result_code WHERE id=26").contains("unrecognized-old-unit")); - } - } - } - - @Test - void alreadyProcessedEvidenceIsNeverReclassifiedAndRepeatedStartupHasNoSummary() throws Exception { - String frozenContext = "{\"old-version-evidence\":true}"; - String oldSelection = "{\"Cross Spectrum\":[{\"channelType\":0,\"unit\":\"m/s\"}]}"; - try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { - try (Connection connection = pool.getConnection()) { - connection.setAutoCommit(false); - schema211(connection); - graph(connection, 1, "Cross Spectrum", "unrecognized-old-unit"); - graph(connection, 2, "FFT Spectrum", "Vrms"); - execute(connection, resourceSql(EVIDENCE)); - insert(connection, "INSERT INTO reference_test_selection_evidence VALUES ('graphic_config', 1, 'EXPLICIT_OLD_VALUE', ?, ?, 1, 'UNRESOLVED')", - oldSelection, frozenContext); - connection.commit(); - connection.setAutoCommit(true); - } - Path migrationResources = resources(); - startup(pool, migrationResources); - String summary = logs.onlySummary(Level.INFO); - assertTrue(summary.contains("total=1"), summary); - assertTrue(summary.contains("NOT_APPLICABLE=1"), summary); - assertEquals(0, logs.count(Level.WARN)); - try (Connection connection = pool.getConnection()) { - assertEquals("UNRESOLVED", scalar(connection, "SELECT recovery_status FROM reference_test_selection_evidence WHERE source_id=1")); - assertEquals(frozenContext, scalar(connection, "SELECT source_context FROM reference_test_selection_evidence WHERE source_id=1")); - assertEquals(oldSelection, scalar(connection, "SELECT legacy_config FROM reference_test_selection_evidence WHERE source_id=1")); - assertNull(scalar(connection, "SELECT reference_test_unit_config FROM graphic_config WHERE id=1")); - // 用户升级后保存的值和新增图不应触发第二次取证或恢复。 - insert(connection, "UPDATE graphic_config SET reference_test_unit_config=? WHERE id=2", "{\"user-edit\":[]}"); - execute(connection, "INSERT INTO graphic_config VALUES (3, '{}', NULL)"); - } - logs.events.clear(); - startup(pool, migrationResources); - assertTrue(logs.summaries().isEmpty()); - assertEquals(0, logs.count(Level.WARN)); - try (Connection connection = pool.getConnection()) { - assertEquals("2", scalar(connection, "SELECT COUNT(*) FROM reference_test_selection_evidence")); - assertEquals("UNRESOLVED", scalar(connection, "SELECT recovery_status FROM reference_test_selection_evidence WHERE source_id=1")); - assertEquals(frozenContext, scalar(connection, "SELECT source_context FROM reference_test_selection_evidence WHERE source_id=1")); - assertEquals(oldSelection, scalar(connection, "SELECT legacy_config FROM reference_test_selection_evidence WHERE source_id=1")); - assertEquals("{\"user-edit\":[]}", scalar(connection, "SELECT reference_test_unit_config FROM graphic_config WHERE id=2")); - } - } - } - - @Test - void failedRestoreRollsBackSelectionsAndProcessingWithoutSuccessSummary() throws Exception { - try (DruidDataSource pool = database(); CapturedLogs logs = new CapturedLogs()) { - try (Connection connection = pool.getConnection()) { - connection.setAutoCommit(false); - schema211(connection); - graph(connection, 1, "Cross Spectrum", "(m/s)²"); - graph(connection, 2, "Cross Spectrum", "(m/s)²"); - execute(connection, resourceSql(EVIDENCE)); - execute(connection, "CREATE TRIGGER fail_second BEFORE UPDATE OF processed ON reference_test_selection_evidence " - + "WHEN NEW.source_id=2 BEGIN SELECT RAISE(ABORT, 'restore fixture failure'); END"); - connection.commit(); - connection.setAutoCommit(true); - } - Path migrationResources = resources(); - assertThrows(IllegalStateException.class, () -> startup(pool, migrationResources)); - assertTrue(logs.summaries().isEmpty(), "回滚不得发出升级完成汇总"); - assertEquals(0, logs.count(Level.WARN)); - try (Connection connection = pool.getConnection()) { - assertEquals("0", scalar(connection, "SELECT COUNT(*) FROM graphic_config WHERE reference_test_unit_config IS NOT NULL")); - assertEquals("2", scalar(connection, "SELECT COUNT(*) FROM reference_test_selection_evidence WHERE processed=0 AND recovery_status IS NULL")); - execute(connection, "DROP TRIGGER fail_second"); - } - logs.events.clear(); - startup(pool, migrationResources); - String summary = logs.onlySummary(Level.INFO); - assertTrue(summary.contains("total=2"), summary); - assertTrue(summary.contains("RECOVERED_FROM_PROJECTION=2"), summary); - try (Connection connection = pool.getConnection()) { - assertEquals("2", scalar(connection, "SELECT COUNT(*) FROM graphic_config WHERE reference_test_unit_config IS NOT NULL")); - assertEquals("2", scalar(connection, "SELECT COUNT(*) FROM reference_test_selection_evidence WHERE processed=1")); - } - } - } - - private DruidDataSource database() throws Exception { - DruidDataSource pool = new DruidDataSource(); - pool.setUrl("jdbc:sqlite:" + temp.resolve("data.db")); - pool.setDriverClassName("org.sqlite.JDBC"); - pool.setMaxActive(1); - pool.setInitialSize(1); - pool.setMaxWait(1500); - pool.init(); - return pool; - } - - private void schema211(Connection connection) throws Exception { - execute(connection, "CREATE TABLE graphic_config (id INTEGER PRIMARY KEY, chart_config TEXT)"); - execute(connection, "CREATE TABLE graphic_config_snapshot (id INTEGER PRIMARY KEY, chart_config TEXT)"); - execute(connection, "CREATE TABLE dashboard_config (id INTEGER PRIMARY KEY, handler_config TEXT)"); - execute(connection, "CREATE TABLE dashboard_result (id INTEGER PRIMARY KEY, dashboard_id INTEGER, primary_code TEXT, algorithm_code TEXT)"); - execute(connection, "CREATE TABLE graphic_result_code (id INTEGER PRIMARY KEY, graphic_config_id INTEGER, dashboard_result_id INTEGER, channel_ids TEXT, axis_type INTEGER, axis_key TEXT, binding_order INTEGER, unit TEXT)"); - } - - private void graph(Connection connection, int id, String code, String unit) throws Exception { - insert(connection, "INSERT INTO graphic_config VALUES (?, '{}')", id); - insert(connection, "INSERT INTO dashboard_result VALUES (?, NULL, ?, 'fft')", id, code); - insert(connection, "INSERT INTO graphic_result_code VALUES (?, ?, ?, 'Ch1', 0, 'axis', 0, ?)", - id, id, id, "[{\"channelType\":0,\"unit\":\"" + unit + "\",\"channelIds\":[\"Ch1\"]}]"); - } - - private Path resources() throws Exception { - Path root = Files.createTempDirectory(temp, "migrations"); - Path folder = Files.createDirectory(root.resolve("selection-log-test")); - for (String script : List.of(ADD, DROP, EVIDENCE)) { - Files.writeString(folder.resolve(script), resourceSql(script)); - } - return root; - } - - private String resourceSql(String name) throws Exception { - try (var input = getClass().getResourceAsStream("/db/migration/" + name)) { - assertNotNull(input, name); - return new String(input.readAllBytes(), StandardCharsets.UTF_8); - } - } - - private void startup(DruidDataSource pool, Path resources) { - assertTimeoutPreemptively(Duration.ofSeconds(10), () -> { - ClassLoader previous = Thread.currentThread().getContextClassLoader(); - try (URLClassLoader loader = new URLClassLoader(new URL[]{resources.toUri().toURL()}, previous); - StaticApplicationContext context = new StaticApplicationContext()) { - Thread.currentThread().setContextClassLoader(loader); - context.registerSingleton("preserveSelections", V2_2_0_20260826162241__preserve_reference_test_selections.class); - context.registerSingleton("protectSelections", V2_2_0_20260826165959__protect_reference_test_selections_before_backfill.class); - context.registerSingleton("legacyBackfill", V2_2_0_20260826170000__backfill_cross_spectrum_unit_config.class); - context.registerSingleton("restoreSelections", V2_2_0_20260908170000__restore_reference_test_selections.class); - DbMigrateStartupRunner runner = new DbMigrateStartupRunner(); - ReflectionTestUtils.setField(runner, "dataSource", pool); - ReflectionTestUtils.setField(runner, "migrationsPath", "selection-log-test"); - ReflectionTestUtils.setField(runner, "schemaTable", "schema_history"); - ReflectionTestUtils.setField(runner, "baseline", false); - runner.setApplicationContext(context); - runner.run(); - } finally { - Thread.currentThread().setContextClassLoader(previous); - } - }); - } - - private void execute(Connection connection, String sql) throws Exception { - try (Statement statement = connection.createStatement()) { - statement.execute(sql); - } - } - - private void insert(Connection connection, String sql, Object... values) throws Exception { - try (PreparedStatement statement = connection.prepareStatement(sql)) { - for (int i = 0; i < values.length; i++) { - statement.setObject(i + 1, values[i]); - } - statement.executeUpdate(); - } - } - - private String scalar(Connection connection, String sql) throws Exception { - try (Statement statement = connection.createStatement(); ResultSet rows = statement.executeQuery(sql)) { - assertTrue(rows.next()); - return rows.getString(1); - } - } - - private static final class CapturedLogs extends AbstractAppender implements AutoCloseable { - private final Logger logger = (Logger) LogManager.getLogger(ReferenceTestSelectionUpgradeGuard.class); - private final Level previousLevel = logger.getLevel(); - private final List events = new CopyOnWriteArrayList<>(); - - private CapturedLogs() { - super("reference-test-migration-log-test", null, null, false, Property.EMPTY_ARRAY); - start(); - logger.addAppender(this); - logger.setLevel(Level.DEBUG); - } - - @Override - public void append(LogEvent event) { - events.add(event.toImmutable()); - } - - private long count(Level level) { - return events.stream().filter(event -> event.getLevel() == level).count(); - } - - private List summaries() { - return events.stream().filter(event -> event.getMessage().getFormattedMessage().startsWith(SUMMARY)).toList(); - } - - private String onlySummary(Level level) { - List summaries = summaries(); - assertEquals(1, summaries.size(), () -> events.toString()); - assertEquals(level, summaries.get(0).getLevel()); - return summaries.get(0).getMessage().getFormattedMessage(); - } - - @Override - public void close() { - logger.removeAppender(this); - logger.setLevel(previousLevel); - stop(); - } - } -} diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/CompleteStorageSnapshotEntryTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/CompleteStorageSnapshotEntryTest.java deleted file mode 100644 index a9eea9c5..00000000 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/CompleteStorageSnapshotEntryTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.opentest.plugin; - -import com.opentest.common.enums.ResultStorageModeEnum; -import com.opentest.common.model.dto.FlowExecuteSnapshotDataDto; -import com.opentest.service.component.ProjectDashboardComponent; -import com.opentest.service.component.dashboard.DashboardExecuteStrategyFactory; -import com.opentest.service.grpc.core.sync.SyncMeasurementHandlerFactory; -import com.opentest.service.grpc.plan.DashboardExecutionStrategyResolver; -import com.opentest.service.grpc.provider.ProviderExecutionSession; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.springframework.test.util.ReflectionTestUtils; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; - -class CompleteStorageSnapshotEntryTest { - @TempDir Path temporary; - - @Test void formalAndRepeatSnapshotsUseTheSameCompletePlanInsteadOfResolvingTheThresholdAgain() throws Exception { - var entries = new Entries(); - try { - MinimalMeasurementExecutionEntryTest.withFixture(temporary, entries, fixture -> { - var beans = fixture.configurationFixture().context(); - var component = beans.getBean("component", ProjectDashboardComponent.class); - var strategies = (DashboardExecuteStrategyFactory) ReflectionTestUtils.getField(component, "dashboardExecuteStrategyFactory"); - var strategy = strategies.getExecutionStrategy(fixture.dashboard()); - var threshold = mock(DashboardExecutionStrategyResolver.class); - when(threshold.resolveStorageMode(any())).thenReturn(ResultStorageModeEnum.WAV_ONLY); - ReflectionTestUtils.setField(strategy, "dashboardExecutionStrategyResolver", threshold); - var factory = beans.getBean("handlerFactory", SyncMeasurementHandlerFactory.class); - ReflectionTestUtils.setField(factory, "measurementStorageMode", threshold); - fixture.dashboard().getDashboardItemConfig().getCollectConfig().getChannels() - .forEach(channel -> channel.setBoxChannelId(1L)); - var response = fixture.http().perform(post("/v1/dashboard/exec/20").session(fixture.owner()) - .contentType("application/json").content("{}")) .andReturn().getResponse(); - assertEquals(0, fixture.json().readTree(response.getContentAsString()).path("code").asInt(-1), response.getContentAsString()); - ProviderExecutionSession first = fixture.handlers().getFirst().measurementSession(); - ProviderExecutionSession next = null; - try { - assertEquals(ResultStorageModeEnum.LEGACY, first.handler().measurementResultStorageMode()); - assertEquals(ResultStorageModeEnum.LEGACY, FlowExecuteSnapshotDataDto.parseJson( - fixture.records().selectById(first.requestId()).getSnapshotData()).getResultStorageMode()); - next = component.prepareRepeatMeasurement(first.requestId() + 1, first, ignored -> {}); - assertEquals(ResultStorageModeEnum.LEGACY, next.handler().measurementResultStorageMode()); - assertEquals(ResultStorageModeEnum.LEGACY, FlowExecuteSnapshotDataDto.parseJson( - fixture.records().selectById(next.requestId()).getSnapshotData()).getResultStorageMode()); - verify(threshold, never()).resolveStorageMode(any()); - } finally { - if (next != null) { next.cancel(); next.released().toCompletableFuture().get(5, TimeUnit.SECONDS); } - first.cancel(); first.released().toCompletableFuture().get(5, TimeUnit.SECONDS); - } - }); - } finally { if (entries.pluginLoader != null) entries.pluginLoader.close(); } - } - - private static final class Entries extends MinimalMeasurementProviderEntryTest.Entries { - @Override Path authorSource() { - try { - String source = Files.readString(super.authorSource()); - String original = "Set.of(ExecutionInputPurpose.CAPTURE_STREAM)), new Provider());"; - if (!source.contains(original)) throw new IllegalStateException("Independent author fixture changed"); - source = source.replace(original, "Set.of(ExecutionInputPurpose.CAPTURE_STREAM), " - + "(configuration, purpose) -> new MeasurementExecutionAdmission.Declaration(Set.of(), Set.of(), true)), new Provider());"); - Path selected = Files.createDirectories(temporary.resolve("complete-author-source")).resolve("RmsPlugin.java"); - Files.writeString(selected, source); - return selected; - } catch (IOException failure) { throw new IllegalStateException(failure); } - } - } -} diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ContinuousMeasurementEntryTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ContinuousMeasurementEntryTest.java deleted file mode 100644 index 34de3fab..00000000 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/ContinuousMeasurementEntryTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.opentest.plugin; - -import com.opentest.service.proto.CommonProto; -import com.opentest.common.config.InstalledPluginContributions; -import java.io.IOException; -import java.net.URL; -import java.util.List; -import io.grpc.stub.StreamObserver; -import java.nio.file.Path; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** Continuous display uses the production start/stop entry without making formal measurement history. */ -class ContinuousMeasurementEntryTest { - @TempDir Path temporary; - - @Test - void continuousDisplayStartsAndStopsWithoutCreatingFormalResults() throws Exception { - var entries = new MinimalMeasurementProviderEntryTest.Entries(); - MinimalMeasurementExecutionEntryTest.withFixture(temporary, entries, fixture -> { - try (var contributions = InstalledPluginContributions.bind(new InstalledPluginContributions.Source() { - public boolean ready() { return true; } - public List services(Class type) { return fixture.configurationFixture().runtime().services(type); } - public List resources(String name) throws IOException { return fixture.configurationFixture().runtime().resources(name); } - })) { - var started = new CountDownLatch(1); - var stopped = new CountDownLatch(1); - var messages = new CopyOnWriteArrayList(); - var stream = fixture.grpc().dashboardDataStream(new StreamObserver<>() { - public void onNext(CommonProto.Response value) { - messages.add(value); - if (!value.hasTaskOperationMsg()) return; - var operation = value.getTaskOperationMsg().getOperationType(); - if (operation == CommonProto.TaskOperationType.TOTAL_TASK_START) started.countDown(); - if (operation == CommonProto.TaskOperationType.TOTAL_TASK_STOP) stopped.countDown(); - } - public void onError(Throwable error) { throw new AssertionError(error); } - public void onCompleted() { } - }); - try { - stream.onNext(CommonProto.Request.newBuilder().setOperation("start").setJSessionId(fixture.owner().getId()) - .setStartMode(CommonProto.DashboardStartMode.REALTIME_MONITOR) - .addSyncDashboards(CommonProto.SyncDashboardStartItem.newBuilder().setDashboardId(20)).build()); - assertTrue(started.await(5, TimeUnit.SECONDS), messages.toString()); - assertTrue(messages.stream().anyMatch(value -> value.hasTaskOperationMsg() - && value.getTaskOperationMsg().getOperationType() == CommonProto.TaskOperationType.TOTAL_TASK_START - && value.getTaskOperationMsg().getSuccess()), messages.toString()); - long captureDeadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); - while (!Boolean.FALSE.equals(fixture.handlers().getLast().getFinished()) && System.nanoTime() < captureDeadline) - Thread.sleep(10); - assertEquals(Boolean.FALSE, fixture.handlers().getLast().getFinished(), "Session start precedes the first capture frame"); - stream.onNext(CommonProto.Request.newBuilder().setOperation("stop").setJSessionId(fixture.owner().getId()).build()); - assertTrue(stopped.await(10, TimeUnit.SECONDS), messages.toString()); - assertTrue(messages.stream().anyMatch(value -> value.hasTaskOperationMsg() - && value.getTaskOperationMsg().getOperationType() == CommonProto.TaskOperationType.TOTAL_TASK_STOP - && value.getTaskOperationMsg().getSuccess()), messages.toString()); - assertEquals(0, fixture.jdbc().queryForObject("SELECT COUNT(*) FROM flow_execute_result", Integer.class)); - assertEquals(0, fixture.jdbc().queryForObject("SELECT COUNT(*) FROM dashboard_result WHERE request_id IS NOT NULL", Integer.class)); - assertEquals(0, fixture.jdbc().queryForObject("SELECT COUNT(*) FROM graphic_config_snapshot", Integer.class)); - assertEquals(0, fixture.jdbc().queryForObject("SELECT COUNT(*) FROM file", Integer.class)); - } finally { stream.onCompleted(); } - } - }); - } -} diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MonitorInputSlotLifecycleEntryIT.java b/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MonitorInputSlotLifecycleEntryIT.java deleted file mode 100644 index 67b8e389..00000000 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/plugin/MonitorInputSlotLifecycleEntryIT.java +++ /dev/null @@ -1,553 +0,0 @@ -package com.opentest.plugin; - -import com.baomidou.mybatisplus.core.MybatisConfiguration; -import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; -import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; -import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; -import net.sf.jsqlparser.expression.Expression; -import net.sf.jsqlparser.expression.LongValue; -import com.opentest.common.config.DashboardConfigurationJson; -import com.opentest.common.utils.LocaleUtil; -import com.opentest.common.enums.ChannelIOEnum; -import com.opentest.common.enums.GraphicTypeEnum; -import cn.hutool.core.bean.BeanUtil; -import com.opentest.common.model.dto.BoxChannelDto; -import com.opentest.common.model.dto.MathChannelSaveRequestDto; -import com.opentest.common.model.dto.UnitInfo; -import com.opentest.common.model.dto.RenderDataDto; -import com.fasterxml.jackson.core.type.TypeReference; -import com.opentest.common.model.dto.ProjectDto; -import com.opentest.common.model.dto.ProjectPackageManifestDto; -import com.opentest.service.project.ProjectOtPackageService; -import com.opentest.common.model.dto.dashboard.DashboardItemConfigDto; -import com.opentest.common.model.entity.Project; -import com.opentest.manager.PluginPackageManager; -import com.opentest.runtime.PluginRuntime; -import com.opentest.service.BoxChannelService; -import com.opentest.service.DashboardConfigService; -import com.opentest.service.ProjectService; -import com.opentest.service.ProjectChannelViewAssembler; -import com.opentest.service.virtualchannel.VirtualChannelConfigService; -import com.opentest.service.virtualchannel.VirtualChannelConfigValidator; -import com.opentest.service.virtualchannel.VirtualChannelReferenceSyncService; -import com.opentest.service.axis.DefaultAxisContractPlanner; -import com.opentest.service.membership.DefaultOverlayGraphicMembershipPlanner; -import com.opentest.service.jni.LicenseJNIBridgeService; -import com.opentest.service.mapper.BoxChannelStructMapper; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.junit.jupiter.api.io.TempDir; -import org.mapstruct.factory.Mappers; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.aop.framework.ProxyFactory; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; -import org.springframework.transaction.interceptor.TransactionInterceptor; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** Actual project slot commands, packaged Monitor definitions and SQLite current graphics. */ -class MonitorInputSlotLifecycleEntryIT { - @TempDir Path temporary; - - @Test void newlyCreatedEmptySlotJoinsExistingMonitorWithoutBackfillingOldSlots() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - // This pre-existing empty slot was not selected in the imported/older Monitor. - fixture.jdbc().update("INSERT INTO box_channel(id, project_id, tenant_id, vch_id, vch_id_number, channel_io, channel_source) VALUES(1,10,1,'Ch1',1,0,0)"); - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(ChannelIOEnum.INPUT.getValue()); - command.setQuickCount(2); command.setChannelColors(List.of("#123456")); - - channels.optNum(command); - - var saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch2"), saved.getCollectConfig().getInputChannels()); - assertNull(saved.getCollectConfig().getChannels().getFirst().getChannelType()); - assertNull(saved.getCollectConfig().getChannels().getFirst().getSamplingRate()); - assertEquals(List.of("Ch2"), saved.getListenAudioConfig().getChannels().stream().map(channel -> channel.getChannelId()).toList()); - assertTrue(fixture.bindings().selectList(null).stream().anyMatch(binding -> "Ch2".equals(binding.getChannelIds()))); - channels.optNum(command); - channels.modifyPatch(entries.json.readTree("{\"id\":1,\"customName\":\"Renamed old input\"}")); - saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch2"), saved.getCollectConfig().getInputChannels()); - String edited = entries.databaseState(fixture); - fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(edited, entries.databaseState(fixture), "Reading must not rewrite current graph membership"); - }); - } - - @Test void recorderSubsetSurvivesSaveRenameGrowthAndDeletionWithoutBecomingAllInputs() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(0); - command.setQuickCount(1); command.setChannelColors(List.of("#123456")); - channels.optNum(command); - var request = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertNull(request.getDashboardItemConfig().getCollectConfig().getRecorderChannels()); - request.getDashboardItemConfig().getCollectConfig().setRecorderChannels(List.of("Ch1")); - fixture.config().modify(request); - command.setQuickCount(2); - channels.optNum(command); - long first = channels.getProjectInputChannelMap(10L).get("Ch1").getId(); - channels.modifyPatch(entries.json.readTree("{\"id\":" + first + ",\"customName\":\"Recorder input\"}")); - var saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch1", "Ch2"), saved.getCollectConfig().getInputChannels()); - assertEquals(List.of("Ch1"), saved.getCollectConfig().getRecorderChannels()); - channels.deletePhysicalChannelSlot(first); - saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch2"), saved.getCollectConfig().getInputChannels()); - assertEquals(List.of(), saved.getCollectConfig().getRecorderChannels()); - request = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - request.getDashboardItemConfig().getCollectConfig().setRecorderChannels(List.of("outside-monitor")); - var invalid = request; - assertThrows(RuntimeException.class, () -> fixture.config().modify(invalid)); - saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of(), saved.getCollectConfig().getRecorderChannels()); - }); - } - - @ParameterizedTest - @ValueSource(booleans = {false, true}) - void mathDeletionPrunesRecorderMembersWhilePreservingDynamicAllAndHistory(boolean dynamicAll) throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(0); - command.setQuickCount(1); command.setChannelColors(List.of("#123456")); - channels.optNum(command); - fixture.jdbc().update("INSERT INTO channel(id,tenant_id,channel_io,channel_type,sampling_rate) VALUES(101,1,0,1,48000)"); - long slotId = channels.getProjectInputChannelMap(10L).get("Ch1").getId(); - channels.modifyPatch(entries.json.readTree("{\"id\":" + slotId + ",\"channelId\":101}")); - for (int index = 1; index <= 2; index++) { - fixture.jdbc().update("INSERT INTO box_channel(id,project_id,tenant_id,vch_id,vch_id_number,channel_io,channel_source,channel_type,custom_name,math_formula,math_unit,sampling_rate) VALUES(?,10,1,?,?,0,1,1,?,?,'Vpeak',48000)", - 200 + index, "Math" + index, index, "M" + index, "'Ch1' * " + index); - fixture.config().addProjectInputChannels(10L, List.of("Math" + index)); - } - var request = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - request.getDashboardItemConfig().getCollectConfig().setRecorderChannels(dynamicAll ? null : List.of("Math1", "Math2")); - fixture.config().modify(request); - var references = new VirtualChannelReferenceSyncService(); - ReflectionTestUtils.setField(references, "boxChannelService", channels); - ReflectionTestUtils.setField(references, "dashboardConfigService", fixture.config()); - var math = new VirtualChannelConfigService(); - ReflectionTestUtils.setField(math, "boxChannelService", channels); - ReflectionTestUtils.setField(math, "boxChannelStructMapper", Mappers.getMapper(BoxChannelStructMapper.class)); - ReflectionTestUtils.setField(math, "virtualChannelReferenceSyncService", references); - var proxy = new ProxyFactory(math); - proxy.setProxyTargetClass(true); - proxy.addAdvice(new TransactionInterceptor(new DataSourceTransactionManager(fixture.jdbc().getDataSource()), - new AnnotationTransactionAttributeSource())); - var commands = (VirtualChannelConfigService) proxy.getProxy(); - var valueGraphicsBefore = fixture.graphics().selectList(null).stream() - .filter(graphic -> GraphicTypeEnum.MULTI_VALUE.getType().equals(graphic.getGraphicType())).toList(); - assertTrue(!valueGraphicsBefore.isEmpty(), "Fixture must contain the shared Value cards"); - var sharedCard = valueGraphicsBefore.getFirst(); - var mathOnlyCard = BeanUtil.copyProperties(sharedCard, sharedCard.getClass()); - mathOnlyCard.setId(90001L); - mathOnlyCard.setName("Mathematical output only"); - mathOnlyCard.setOriginal(null); - fixture.graphics().insert(mathOnlyCard); - var mathOnlyBinding = fixture.bindings().selectList(null).stream() - .filter(binding -> sharedCard.getId().equals(binding.getGraphicConfigId()) - && binding.getChannelIds() != null && binding.getChannelIds().contains("Math2")) - .findFirst().orElseThrow(); - mathOnlyBinding.setId(90002L); - mathOnlyBinding.setGraphicConfigId(mathOnlyCard.getId()); - mathOnlyBinding.setChannelIds("Math2"); - fixture.bindings().insert(mathOnlyBinding); - var physicalBindingsBefore = fixture.bindings().selectList(null).stream() - .filter(binding -> "Ch1".equals(binding.getChannelIds())).toList(); - commands.deleteMathChannel(10L, "Math2"); - assertNull(fixture.graphics().selectById(mathOnlyCard.getId()), - "A wholly empty duplicate card must still be removed"); - assertNull(fixture.bindings().selectById(mathOnlyBinding.getId())); - var saved = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(List.of("Ch1", "Math1"), saved.getDashboardItemConfig().getCollectConfig().getInputChannels()); - assertEquals(dynamicAll ? null : List.of("Math1"), saved.getDashboardItemConfig().getCollectConfig().getRecorderChannels()); - commands.deleteMathChannel(10L, "Math1"); - var bindingsAfterDeletion = fixture.bindings().selectList(null); - assertEquals(valueGraphicsBefore.stream().map(graphic -> graphic.getId()).sorted().toList(), - fixture.graphics().selectList(null).stream() - .filter(graphic -> GraphicTypeEnum.MULTI_VALUE.getType().equals(graphic.getGraphicType())) - .map(graphic -> graphic.getId()).sorted().toList(), - "Deleting the mathematical unit group must retain Value cards for Ch1"); - for (var originalCard : valueGraphicsBefore) { - var retainedCard = fixture.graphics().selectById(originalCard.getId()); - assertEquals(originalCard.getChartConfig(), retainedCard.getChartConfig()); - assertEquals(originalCard.getName(), retainedCard.getName()); - assertTrue(fixture.bindings().selectList(null).stream().anyMatch(binding -> - originalCard.getId().equals(binding.getGraphicConfigId()) - && "Ch1".equals(binding.getChannelIds()))); - } - saved = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(List.of("Ch1"), saved.getDashboardItemConfig().getCollectConfig().getInputChannels()); - assertEquals(dynamicAll ? null : List.of(), saved.getDashboardItemConfig().getCollectConfig().getRecorderChannels()); - saved.getDashboardItemConfig().getCollectConfig().setRecorderChannels(List.of("Ch1")); - fixture.config().modify(saved); - assertEquals(List.of("Ch1"), fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID) - .getDashboardItemConfig().getCollectConfig().getRecorderChannels()); - fixture.jdbc().update("INSERT INTO box_channel(id,project_id,tenant_id,vch_id,vch_id_number,channel_io,channel_source,channel_type,custom_name,math_formula,math_unit,sampling_rate) VALUES(203,10,1,'Math2',2,0,1,1,'M2',?,'Vpeak',48000)", "'Ch1' * 2"); - fixture.config().addProjectInputChannels(10L, List.of("Math2")); - ReflectionTestUtils.setField(channels, "virtualChannelConfigValidator", mock(VirtualChannelConfigValidator.class)); - var formulaChange = new MathChannelSaveRequestDto(); - formulaChange.setId(203L); formulaChange.setProjectId(10L); - formulaChange.setCustomName("M2"); formulaChange.setMathFormula("'Ch1' * 3"); - formulaChange.setChannelType(1); formulaChange.setMathUnit("Vpeak"); - commands.saveMathChannel(formulaChange); - assertEquals("'Ch1' * 3", channels.findById(203L, false).getMathFormula()); - for (var originalCard : valueGraphicsBefore) { - assertEquals(originalCard.getChartConfig(), fixture.graphics().selectById(originalCard.getId()).getChartConfig()); - assertTrue(bindingsAfterDeletion.stream() - .filter(binding -> originalCard.getId().equals(binding.getGraphicConfigId())) - .allMatch(binding -> binding.getChannelIds() != null && !binding.getChannelIds().isBlank()), - "Deleting a member must remove its empty partition from a populated Value card"); - } - for (var originalBinding : physicalBindingsBefore) { - var retained = fixture.bindings().selectById(originalBinding.getId()); - assertEquals(originalBinding.getChannelIds(), retained.getChannelIds()); - assertEquals(entries.json.readValue(originalBinding.getUnit(), new TypeReference>() { }), - entries.json.readValue(retained.getUnit(), new TypeReference>() { })); - } - }); - } - - @Test void newPhysicalInputsJoinEachValueCardOnceAcrossExistingSplitBindings() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(0); - command.setQuickCount(2); command.setChannelColors(List.of("#123456")); - channels.optNum(command); - for (int index = 1; index <= 4; index++) { - fixture.jdbc().update("INSERT INTO channel(id,tenant_id,channel_io,channel_type,sampling_rate) VALUES(?,1,0,1,48000)", 100 + index); - if (index <= 2) { - long slot = channels.getProjectInputChannelMap(10L).get("Ch" + index).getId(); - channels.modifyPatch(entries.json.readTree("{\"id\":" + slot + ",\"channelId\":" + (100 + index) + "}")); - } - } - for (int index = 1; index <= 2; index++) { - fixture.jdbc().update("INSERT INTO box_channel(id,project_id,tenant_id,vch_id,vch_id_number,channel_io,channel_source,channel_type,custom_name,math_formula,math_unit,sampling_rate) VALUES(?,10,1,?,?,0,1,1,?,?,'Vpeak',48000)", - 200 + index, "Math" + index, index, "M" + index, "'Ch1' * " + index); - fixture.config().addProjectInputChannels(10L, List.of("Math" + index)); - } - var valueCards = new ArrayList<>(fixture.graphics().selectList(null).stream() - .filter(graphic -> GraphicTypeEnum.MULTI_VALUE.getType().equals(graphic.getGraphicType())).toList()); - assertEquals(5, valueCards.size()); - long splitId = 80000L; - var neighbor = BeanUtil.copyProperties(valueCards.getFirst(), valueCards.getFirst().getClass()); - neighbor.setId(90001L); - fixture.graphics().insert(neighbor); - for (var binding : fixture.bindings().selectList(null)) { - if (valueCards.getFirst().getId().equals(binding.getGraphicConfigId())) { - var copied = BeanUtil.copyProperties(binding, binding.getClass()); - copied.setId(++splitId); - copied.setGraphicConfigId(neighbor.getId()); - fixture.bindings().insert(copied); - } - } - valueCards.add(neighbor); - var oldBindings = fixture.bindings().selectList(null).stream() - .filter(binding -> valueCards.stream().anyMatch(card -> card.getId().equals(binding.getGraphicConfigId()))) - .toList(); - for (var card : valueCards) { - var physical = oldBindings.stream().filter(binding -> card.getId().equals(binding.getGraphicConfigId()) - && List.of("Ch1", "Ch2").contains(binding.getChannelIds())).toList(); - assertEquals(2, physical.size(), "Fixture must start with separate physical GRC rows in each Value card: " + oldBindings); - card.setName("Retained " + card.getId()); - fixture.graphics().updateById(card); - } - for (int index = 3; index <= 4; index++) { - var added = new BoxChannelDto(); - added.setProjectId(10L); added.setTenantId(1L); added.setChannelIo(0); - added.setChannelSource(0); added.setVchId("Ch" + index); added.setVchIdNumber(index); - added.setChannelId(100L + index); - added.setChannelType(1); added.setSamplingRate(48000); - channels.add(added); - } - var current = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(List.of("Ch1", "Ch2", "Math1", "Math2", "Ch3", "Ch4"), - current.getDashboardItemConfig().getCollectConfig().getInputChannels()); - assertEquals(6, current.getGraphicConfigs().stream() - .filter(graphic -> GraphicTypeEnum.MULTI_VALUE.getType().equals(graphic.getGraphicType())).count()); - for (var card : valueCards) { - var retained = fixture.graphics().selectById(card.getId()); - assertEquals(card.getName(), retained.getName()); - assertEquals(card.getSort(), retained.getSort()); - var members = fixture.bindings().selectList(null).stream() - .filter(binding -> card.getId().equals(binding.getGraphicConfigId())).toList(); - for (String channel : List.of("Ch1", "Ch2", "Ch3", "Ch4", "Math1", "Math2")) { - assertEquals(1L, members.stream().flatMap(binding -> Arrays.stream(binding.getChannelIds().split(","))) - .filter(channel::equals).count(), - card.getName() + " must contain " + channel + " once"); - } - var read = current.getGraphicConfigs().stream().filter(graphic -> card.getId().equals(graphic.getId())).findFirst().orElseThrow(); - var keys = read.getRenderDatas().stream().map(RenderDataDto::getUniqueKey).toList(); - assertEquals(6, keys.size()); - assertEquals(keys.size(), new HashSet<>(keys).size(), "Projected runtime identities must be unique"); - assertEquals(List.of("Ch1", "Ch2", "Math1", "Math2", "Ch3", "Ch4"), - read.getRenderDatas().stream().map(RenderDataDto::getChannelId).toList()); - } - for (var old : oldBindings) { - var retained = fixture.bindings().selectById(old.getId()); - assertTrue(Arrays.asList(retained.getChannelIds().split(",")).containsAll(Arrays.asList(old.getChannelIds().split(",")))); - var oldUnits = entries.json.readValue(old.getUnit(), new TypeReference>() { }); - var retainedUnits = entries.json.readValue(retained.getUnit(), new TypeReference>() { }); - for (var oldUnit : oldUnits) { - var retainedUnit = retainedUnits.stream().filter(unit -> unit.getChannelIds().containsAll(oldUnit.getChannelIds())).findFirst().orElseThrow(); - assertEquals(oldUnit.getUnit(), retainedUnit.getUnit()); - assertEquals(oldUnit.getChannelType(), retainedUnit.getChannelType()); - assertEquals(oldUnit.getChannelSource(), retainedUnit.getChannelSource()); - if (oldUnit.getMemberUnitMetadata() != null) oldUnit.getMemberUnitMetadata().forEach((id, metadata) -> - assertEquals(metadata, retainedUnit.getMemberUnitMetadata().get(id))); - } - assertEquals(old.getBindingOrder(), retained.getBindingOrder()); - } - String beforeRepeat = entries.databaseState(fixture); - command.setQuickCount(4); - channels.optNum(command); - fixture.config().addProjectInputChannels(10L, List.of("Ch3", "Ch4")); - fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(beforeRepeat, entries.databaseState(fixture)); - }); - } - - @Test void loadingExistingMonitorNeverRebuildsItsSelectionFromCurrentBindings() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - fixture.jdbc().update("INSERT INTO box_channel(id, project_id, tenant_id, vch_id, vch_id_number, channel_io, channel_source, channel_id) VALUES(1,10,1,'Ch1',1,0,0,999)"); - String before = entries.databaseState(fixture); - var read = fixture.config().getConfigByProject(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(List.of(), read.getDashboardItemConfig().getCollectConfig().getInputChannels()); - assertEquals(before, entries.databaseState(fixture)); - }); - } - - @Test void bindingAnOldSlotAddsListeningAndResultMembersAndUnbindingRetainsThem() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - fixture.jdbc().update("INSERT INTO box_channel(id, project_id, tenant_id, vch_id, vch_id_number, channel_io, channel_source) VALUES(1,10,1,'Ch1',1,0,0)"); - fixture.jdbc().update("INSERT INTO channel(id, tenant_id, channel_io, channel_type, sampling_rate) VALUES(101,1,0,1,48000)"); - channels.modifyPatch(entries.json.readTree("{\"id\":1,\"channelId\":101}")); - var saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch1"), saved.getCollectConfig().getInputChannels()); - assertEquals(List.of("Ch1"), saved.getListenAudioConfig().getChannels().stream().map(channel -> channel.getChannelId()).toList()); - var bindings = fixture.bindings().selectList(null); - assertTrue(bindings.stream().anyMatch(binding -> "Ch1".equals(binding.getChannelIds()))); - var units = bindings.stream().map(binding -> binding.getId() + ":" + binding.getUnit()).sorted().toList(); - channels.modifyPatch(entries.json.readTree("{\"id\":1,\"channelId\":null}")); - saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch1"), saved.getCollectConfig().getInputChannels()); - assertEquals(units, fixture.bindings().selectList(null).stream().map(binding -> binding.getId() + ":" + binding.getUnit()).sorted().toList()); - }); - } - - @Test void deletingSelectedSlotDoesNotAdmitOlderUnselectedSlots() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - fixture.jdbc().update("INSERT INTO box_channel(id, project_id, tenant_id, vch_id, vch_id_number, channel_io, channel_source) VALUES(1,10,1,'Ch1',1,0,0)"); - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(ChannelIOEnum.INPUT.getValue()); - command.setQuickCount(2); command.setChannelColors(List.of("#123456")); - channels.optNum(command); - channels.deletePhysicalChannelSlot(channels.getProjectInputChannelMap(10L).get("Ch2").getId()); - var saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of(), saved.getCollectConfig().getInputChannels()); - assertTrue(fixture.bindings().selectList(null).stream().allMatch(binding -> binding.getChannelIds() == null || binding.getChannelIds().isEmpty())); - }); - } - - @Test void singleSlotCreationKeepsUnitsUndeterminedUntilPhysicalBinding() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - var request = new BoxChannelDto(); - request.setTenantId(1L); request.setProjectId(10L); request.setChannelIo(0); - request.setChannelSource(0); request.setVchId("Ch1"); request.setVchIdNumber(1); - long slotId = Long.parseLong(channels.add(request)); - var saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of("Ch1"), saved.getCollectConfig().getInputChannels()); - assertNull(saved.getCollectConfig().getChannels().getFirst().getSamplingRate()); - boolean pending = false; - for (var binding : fixture.bindings().selectList(null)) { - var unit = entries.json.readTree(binding.getUnit()).path(0); - pending |= unit.path("unit").isMissingNode() || unit.path("unit").isNull(); - } - assertTrue(pending, "At least one physical result must retain undetermined units"); - fixture.jdbc().update("INSERT INTO channel(id, tenant_id, channel_io, channel_type, sampling_rate) VALUES(101,1,0,1,48000)"); - channels.modifyPatch(entries.json.readTree("{\"id\":" + slotId + ",\"channelId\":101}")); - var restoredUnits = fixture.bindings().selectList(null).stream().map(binding -> binding.getUnit()).toList(); - assertTrue(restoredUnits.stream().anyMatch(unit -> unit.contains("\"unit\":\"Vpeak\"")), restoredUnits.toString()); - }); - } - - @Test void importingAnExistingMonitorDoesNotAdmitOldSlotsButLaterCreationDoes() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - fixture.jdbc().update("INSERT INTO box_channel(id, project_id, tenant_id, vch_id, vch_id_number, channel_io, channel_source) VALUES(1,10,1,'Ch1',1,0,0)"); - var exported = MeasurementProjectImportEntryTest.emptyExport(); - exported.setExportVersion("2.3.0"); - var project = new ProjectDto(); - project.setId(10L); project.setTenantId(1L); project.setProjectName("Monitor source"); - exported.setProject(project); - exported.setBoxChannels(channels.list()); - var sourceDashboard = fixture.config().findById(dashboard, false); - var sourceConfiguration = DashboardConfigurationJson.read(sourceDashboard.getHandlerConfig(), DashboardItemConfigDto.class); - sourceConfiguration.getCollectConfig().setRecorderChannels(List.of()); - sourceDashboard.setHandlerConfig(DashboardConfigurationJson.write(sourceConfiguration)); - exported.setDashboardConfigs(List.of(sourceDashboard)); - exported.setDashboardResults(fixture.results().selectList(null)); - exported.setGraphicConfigs(fixture.graphics().selectList(null)); - exported.setGraphicResultCodes(fixture.bindings().selectList(null)); - Path file = temporary.resolve("Existing-monitor.ot"); - new ProjectOtPackageService().write(file, exported, new ProjectPackageManifestDto(), List.of()); - var projects = (ProjectService) ReflectionTestUtils.getField(channels, "projectService"); - ReflectionTestUtils.setField(projects, "importProviders", - ReflectionTestUtils.getField(fixture.graphicService(), "currentParameterProviders")); - long imported = Long.parseLong(projects.importProjectFromFile(file.toString(), false, false)); - var monitor = fixture.config().getConfigByProject(imported, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(List.of(), monitor.getDashboardItemConfig().getCollectConfig().getInputChannels()); - var command = new BoxChannelDto(); - command.setProjectId(imported); command.setChannelIo(0); - command.setQuickCount(2); command.setChannelColors(List.of("#123456")); - channels.optNum(command); - monitor = fixture.config().getConfigByProject(imported, DashboardConfigService.MONITOR_PROVIDER_ID); - assertEquals(List.of("Ch2"), monitor.getDashboardItemConfig().getCollectConfig().getInputChannels()); - assertEquals(List.of(), monitor.getDashboardItemConfig().getCollectConfig().getRecorderChannels()); - var source = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of(), source.getCollectConfig().getInputChannels()); - }); - } - - @Test void aBoundPhysicalMemberWithMissingUnitsRemainsInvalid() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(0); - command.setQuickCount(1); command.setChannelColors(List.of("#123456")); - channels.optNum(command); - fixture.jdbc().update("INSERT INTO channel(id, tenant_id, channel_io, channel_type, sampling_rate) VALUES(101,1,0,1,48000)"); - long slot = channels.getProjectInputChannelMap(10L).get("Ch1").getId(); - channels.modifyPatch(entries.json.readTree("{\"id\":" + slot + ",\"channelId\":101}")); - var binding = fixture.bindings().selectList(null).stream() - .filter(row -> row.getUnit().contains("Vpeak")).findFirst().orElseThrow(); - fixture.jdbc().update("UPDATE graphic_result_code SET unit=? WHERE id=?", - "[{\"channelType\":1,\"channelIds\":[\"Ch1\"],\"memberUnitMetadata\":{\"Ch1\":{\"channelSource\":0}}}]", binding.getId()); - assertThrows(IllegalArgumentException.class, - () -> fixture.graphicService().findDtoById(binding.getGraphicConfigId())); - }); - } - - @Test void impedanceInputsOutputsAndOldMathSlotsNeverBecomeNewMonitorInputs() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - // Saved Sequence owns a paired input; it stays excluded even while unbound. - fixture.jdbc().update("INSERT INTO dashboard_config(id,project_id,tenant_id,provider_id,handler_config) VALUES(2,10,1,?,?)", - "com.opentest.framework:sequence", "{\"impedanceConfig\":{\"inputChannels\":[\"Ch1\"]},\"sequenceConfig\":{\"boxList\":[]}}"); - fixture.jdbc().update("INSERT INTO box_channel(id,project_id,tenant_id,vch_id,vch_id_number,channel_io,channel_source) VALUES(1,10,1,'Math1',1,0,1)"); - var input = new BoxChannelDto(); - input.setTenantId(1L); input.setProjectId(10L); input.setChannelIo(0); - input.setChannelSource(0); input.setVchId("Ch1"); input.setVchIdNumber(1); - channels.add(input); - var output = new BoxChannelDto(); - output.setTenantId(1L); output.setProjectId(10L); output.setChannelIo(1); - output.setChannelSource(0); output.setVchId("Ch2"); output.setVchIdNumber(2); - channels.add(output); - fixture.jdbc().update("INSERT INTO channel(id,tenant_id,channel_io,channel_type,sampling_rate) VALUES(101,1,0,1,48000)"); - long paired = channels.getProjectInputChannelMap(10L).get("Ch1").getId(); - channels.modifyPatch(entries.json.readTree("{\"id\":" + paired + ",\"channelId\":101}")); - var saved = DashboardConfigurationJson.read(fixture.config().findById(dashboard, false).getHandlerConfig(), DashboardItemConfigDto.class); - assertEquals(List.of(), saved.getCollectConfig().getInputChannels()); - assertTrue(fixture.bindings().selectList(null).stream().allMatch(binding -> binding.getChannelIds() == null || binding.getChannelIds().isEmpty())); - }); - } - - @Test void aLateGraphicFailureRollsBackSlotAndAllMonitorWrites() throws Exception { - withFixture((entries, fixture, channels, dashboard) -> { - String before = entries.databaseState(fixture); - fixture.jdbc().execute("CREATE TRIGGER reject_graphic_update BEFORE UPDATE ON graphic_config BEGIN SELECT RAISE(ABORT, 'late graphic failure'); END"); - var command = new BoxChannelDto(); - command.setProjectId(10L); command.setChannelIo(0); - command.setQuickCount(1); command.setChannelColors(List.of("#123456")); - assertThrows(RuntimeException.class, () -> channels.optNum(command)); - assertEquals(0L, fixture.jdbc().queryForObject("SELECT count(*) FROM box_channel", Long.class)); - assertEquals(before, entries.databaseState(fixture)); - }); - } - - private void withFixture(Scenario scenario) throws Exception { - var plugins = new PluginConfiguration(); - var entries = new ProviderConfigurationEntryTest() { - @Override void configurePersistence(MybatisConfiguration configuration) { - var interceptor = new MybatisPlusInterceptor(); - interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { - @Override public Expression getTenantId() { return new LongValue(1L); } - })); - configuration.addInterceptor(interceptor); - } - @Override PluginRuntime runtime() throws Exception { - Path root = temporary.toRealPath().resolve("plugins"); - ReflectionTestUtils.setField(plugins, "configuredRoot", root.toString()); - var runtime = plugins.pluginRuntime(new PluginPackageManager(root)); - assertTrue(runtime.ready(), runtime.snapshot(false).toString()); - return runtime; - } - }; - entries.temporary = temporary; - entries.suppliedChannels = new BoxChannelService(); - try (var fixture = entries.fixture()) { - fixture.context().getStaticMessageSource().setUseCodeAsDefaultMessage(true); - fixture.context().addMessage("algorithm.name.monitor", LocaleUtil.getLocale(), "Monitor"); - fixture.jdbc().update("DELETE FROM dashboard_config"); - var imports = MeasurementProjectImportEntryTest.projectImport(entries, fixture); - var channels = (BoxChannelService) ReflectionTestUtils.getField(imports.projects(), "boxChannelService"); - ReflectionTestUtils.setField(channels, "boxChannelStructMapper", Mappers.getMapper(BoxChannelStructMapper.class)); - var license = mock(LicenseJNIBridgeService.class); - when(license.checkMaxChannelNumFeature()).thenReturn(16); - ReflectionTestUtils.setField(channels, "licenseJNIBridgeService", license); - var project = new Project(); - project.setId(10L); project.setTenantId(1L); - fixture.jdbc().update("INSERT INTO project(id, tenant_id, collect_config, is_del) VALUES(10,1,'{}',0)"); - fixture.context().addMessage("channel.type.voltage", LocaleUtil.getLocale(), "Voltage"); - var projects = mock(ProjectService.class); - when(projects.findById(10L)).thenReturn(project); - when(projects.findDtoById(10L, false)).thenReturn(new ProjectDto()); - ReflectionTestUtils.setField(fixture.config(), "projectService", projects); - ReflectionTestUtils.setField(fixture.config(), "providerConfigurations", - ReflectionTestUtils.getField(fixture.graphicService(), "currentParameterProviders")); - ReflectionTestUtils.setField(fixture.graphicService(), "overlayGraphicMembershipPlanner", - new DefaultOverlayGraphicMembershipPlanner(new DefaultAxisContractPlanner())); - Long dashboard = fixture.config().firstCreateHostBaseline(10L, DashboardConfigService.MONITOR_PROVIDER_ID); - ReflectionTestUtils.setField(channels, "projectChannelViewAssembler", new ProjectChannelViewAssembler()); - var channelProxy = new ProxyFactory(channels); - channelProxy.setProxyTargetClass(true); - channelProxy.addAdvice(new TransactionInterceptor(new DataSourceTransactionManager(fixture.jdbc().getDataSource()), - new AnnotationTransactionAttributeSource())); - var frozenResult = fixture.results().selectList(null).getFirst(); - frozenResult.setId(9L); frozenResult.setRequestId(99L); - fixture.results().insert(frozenResult); - fixture.jdbc().update("INSERT INTO flow_execute_result(id,tenant_id,project_id,dashboard_id,snapshot_data) VALUES(99,1,10,?,?)", - dashboard, "{\"collectConfig\":{\"inputChannels\":[\"FrozenInput\"]}}"); - fixture.jdbc().update("INSERT INTO file(id,tenant_id,business_id,request_id) VALUES(9,1,10,99)"); - String frozen = historicalState(entries, fixture); - scenario.run(entries, fixture, (BoxChannelService) channelProxy.getProxy(), dashboard); - assertEquals(frozen, historicalState(entries, fixture), "Current slot operations must preserve frozen configurations, results and files"); - } finally { - plugins.unbindContributions(); - } - } - private String historicalState(ProviderConfigurationEntryTest entries, - ProviderConfigurationEntryTest.Fixture fixture) throws Exception { - return entries.json.writeValueAsString(List.of( - fixture.jdbc().queryForList("SELECT * FROM dashboard_result WHERE request_id=99 ORDER BY id"), - fixture.jdbc().queryForList("SELECT * FROM flow_execute_result WHERE id=99"), - fixture.jdbc().queryForList("SELECT * FROM file WHERE request_id=99 ORDER BY id"))); - } - - @FunctionalInterface - private interface Scenario { - void run(ProviderConfigurationEntryTest entries, ProviderConfigurationEntryTest.Fixture fixture, - BoxChannelService channels, Long dashboard) throws Exception; - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java deleted file mode 100644 index 43203d79..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/ImportedPostProcessingGraphicSqliteTest.java +++ /dev/null @@ -1,1636 +0,0 @@ -package com.opentest.service; - -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.convert.ConverterRegistry; -import cn.hutool.json.JSONArray; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; -import com.baomidou.mybatisplus.core.MybatisConfiguration; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.extension.repository.CrudRepository; -import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; -import com.opentest.common.constants.CryDaqConstant; -import com.opentest.common.converter.BaseVariableParametersConverter; -import com.opentest.common.enums.ChannelIOEnum; -import com.opentest.common.enums.ChannelTypeEnum; -import com.opentest.common.enums.BuiltinDashboardProvider; -import com.opentest.common.enums.ResultTypeEnum; -import com.opentest.common.exception.CryBusinessException; -import com.opentest.common.model.dto.FlowNodeConfigDto; -import com.opentest.common.model.dto.GraphicResultCodeDto; -import com.opentest.common.model.dto.DashboardResultDto; -import com.opentest.common.model.dto.SimpleChannelDto; -import com.opentest.common.model.dto.UnitInfo; -import com.opentest.common.model.dto.dashboard.BoxChannelConfigItem; -import com.opentest.common.model.dto.dashboard.DashboardItemConfigDto; -import com.opentest.common.model.dto.dashboard.SequenceConfigDto; -import com.opentest.common.model.dto.dashboard.ThreadChannelMappingDto; -import com.opentest.common.model.dto.node.NodeConfigCodec; -import com.opentest.common.model.dto.parameters.BaseVariableParameters; -import com.opentest.common.model.entity.DashboardConfig; -import com.opentest.common.model.entity.DashboardResult; -import com.opentest.common.model.entity.FlowNodeConfig; -import com.opentest.common.model.entity.GraphicConfig; -import com.opentest.common.model.entity.GraphicResultCode; -import com.opentest.common.model.query.DashboardResultQuery; -import com.opentest.common.utils.JacksonConverter; -import com.opentest.common.utils.SpringContextHolder; -import com.opentest.dal.mapper.sqlite.DashboardResultMapper; -import com.opentest.dal.mapper.sqlite.GraphicConfigMapper; -import com.opentest.dal.mapper.sqlite.GraphicResultCodeMapper; -import com.opentest.service.auth.AuthV2Service; -import com.opentest.service.axis.DashboardResultYAxisPresentationResolver; -import com.opentest.service.axis.DefaultAxisContractPlanner; -import com.opentest.service.axis.ImportedGraphicAxisContractConverger; -import com.opentest.service.axis.ImportedGraphicAxisContractFormalizer; -import com.opentest.service.axis.LegacyAxisReadNormalizer; -import com.opentest.service.axis.RenderDataAxisBindingService; -import com.opentest.service.component.CommonComponent; -import com.opentest.service.helper.DefaultDashboardResultConfigContextBuilder; -import com.opentest.service.jni.LicenseJNIBridgeService; -import com.opentest.service.liteflow.core.SequenceResultUnitOverrideResolver; -import com.opentest.service.liteflow.node.postprocessing.PostProcessingNodeConfigDto; -import com.opentest.service.liteflow.node.postprocessing.PostProcessingReferenceViewResolver; -import com.opentest.service.liteflow.node.signalanalysis.SignalAnalysisNodeConfigDto; -import com.opentest.service.mapper.DashboardResultDtoMapper; -import com.opentest.service.mapper.GraphicConfigDtoMapper; -import com.opentest.service.mapper.GraphicResultCodeDtoMapper; -import com.opentest.service.project.ProjectOtPackageService; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.sql.Connection; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.apache.ibatis.session.SqlSessionFactory; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mapstruct.factory.Mappers; -import org.mybatis.spring.SqlSessionTemplate; -import org.springframework.aop.framework.ProxyFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.MessageSource; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; -import org.springframework.transaction.interceptor.TransactionInterceptor; -import org.springframework.transaction.support.TransactionSynchronizationManager; -import org.springframework.transaction.support.TransactionTemplate; -import org.sqlite.SQLiteDataSource; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assumptions.assumeTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyList; -import static org.mockito.ArgumentMatchers.anyMap; -import static org.mockito.ArgumentMatchers.anySet; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.isNull; -import static org.mockito.Mockito.clearInvocations; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -/** 原始旧GRC分组经过真实SQLite读写与Spring事务;不执行OT文件导入。 */ -class ImportedPostProcessingGraphicSqliteTest { - private static final long PROJECT_ID = 777L; - private static final long DASHBOARD_ID = 11L; - private static final long SOURCE = 100L; - private static final long RANGE = 200L; - private static final long ADD = 300L; - private static final long SUBTRACT = 400L; - - private final Map records = new LinkedHashMap<>(); - private final Map configs = new LinkedHashMap<>(); - private DashboardResultService results; - private GraphicConfigService graphics; - private DefaultDashboardResultConfigContextBuilder defaultConfigs; - private DashboardConfigService dashboards; - private FlowNodeConfigService nodes; - private Object previousApplicationContext; - private Object previousVariableParametersConverter; - - @TempDir - Path tempDir; - private SQLiteDataSource dataSource; - private JdbcTemplate jdbc; - private DashboardResultMapper resultMapper; - private GraphicConfigMapper graphicMapper; - private GraphicResultCodeMapper bindingMapper; - private FailingBindingWriter bindingWriter; - private GraphicConfigService transactionalGraphics; - private Set capturedLegacyGraphicIds = Set.of(); - private final Map replayNodes = new LinkedHashMap<>(); - private Map>> packageBeforeUnitResolution; - private Path packageReplayOverride; - private final Map seededGraphics = new LinkedHashMap<>(); - private final Map seededBindings = new LinkedHashMap<>(); - @BeforeEach - void setUp() throws Exception { - // 与服务器CustomInitializationListener一致,formalizer深拷贝真实普通结果参数时需要此转换器。 - Map customConverters = (Map) ReflectionTestUtils.getField( - ConverterRegistry.getInstance(), "customConverterMap"); - previousVariableParametersConverter = customConverters == null ? null - : customConverters.get(BaseVariableParameters.class); - ConverterRegistry.getInstance().putCustom(BaseVariableParameters.class, new BaseVariableParametersConverter()); - previousApplicationContext = ReflectionTestUtils.getField(SpringContextHolder.class, "context"); - results = spy(new DashboardResultService()); - org.springframework.test.util.ReflectionTestUtils.setField(results, "resultPluginAccess", - com.opentest.service.testsupport.ResultAccessFixture.installed(java.util.Set.of(), java.util.Set.of())); - graphics = spy(new FailingGraphicWriter()); - dashboards = mock(DashboardConfigService.class); - nodes = mock(FlowNodeConfigService.class); - defaultConfigs = mock(DefaultDashboardResultConfigContextBuilder.class); - JacksonConverter converter = new JacksonConverter(); - ReflectionTestUtils.setField(converter, "objectMapper", new ObjectMapper()); - ReflectionTestUtils.setField(results, "jacksonConverter", converter); - ReflectionTestUtils.setField(results, "dashboardResultDtoMapper", Mappers.getMapper(DashboardResultDtoMapper.class)); - ReflectionTestUtils.setField(results, "dashboardConfigService", dashboards); - ReflectionTestUtils.setField(results, "flowNodeConfigService", nodes); - ReflectionTestUtils.setField(results, "postProcessingReferenceViewResolver", new PostProcessingReferenceViewResolver()); - ReflectionTestUtils.setField(results, "configContextBuilder", defaultConfigs); - ReflectionTestUtils.setField(results, "graphicConfigService", graphics); - ReflectionTestUtils.setField(results, "commonComponent", mock(CommonComponent.class)); - ReflectionTestUtils.setField(results, "licenseJNIBridgeService", mock(LicenseJNIBridgeService.class)); - ReflectionTestUtils.setField(results, "authV2Service", mock(AuthV2Service.class)); - ReflectionTestUtils.setField(graphics, "dashboardResultService", results); - ReflectionTestUtils.setField(graphics, "flowNodeConfigService", nodes); - ReflectionTestUtils.setField(graphics, "sequenceResultUnitOverrideResolver", mock(SequenceResultUnitOverrideResolver.class)); - ReflectionTestUtils.setField(graphics, "configContextBuilder", defaultConfigs); - ReflectionTestUtils.setField(graphics, "commonComponent", mock(CommonComponent.class)); - ApplicationContext context = mock(ApplicationContext.class); - MessageSource messages = mock(MessageSource.class); - when(context.getBean(MessageSource.class)).thenReturn(messages); - when(messages.getMessage(any(String.class), any(), any(String.class), any(java.util.Locale.class))) - .thenAnswer(call -> call.getArgument(2)); - ReflectionTestUtils.setField(SpringContextHolder.class, "context", context); - - DashboardConfig dashboard = dashboard(); - when(dashboards.findById(DASHBOARD_ID)).thenReturn(dashboard); - when(dashboards.listByIds(any())).thenReturn(List.of(dashboard)); - // 后处理节点没有直接输入通道;只有普通上游结果能从通用读取拿到通道。 - Map> sourceChannels = Map.of( - DashboardConfigService.buildNodeScopedSimpleChannelKey(ChannelIOEnum.INPUT.getChannelIo(), nodeId(SOURCE)), physicalChannels()); - when(dashboards.getSimpleChannelList(eq(dashboard), isNull(), anySet(), isNull())) - .thenReturn(sourceChannels); - when(dashboards.getSimpleChannelList(eq(dashboard), isNull(), anySet(), isNull(), anyList(), anyMap())) - .thenReturn(sourceChannels); - when(nodes.listByIds(any())).thenAnswer(call -> { - Collection ids = call.getArgument(0); - return ids.stream().map(this::nodeEntity).filter(Objects::nonNull).toList(); - }); - when(nodes.findById(any(Long.class), eq(false))).thenAnswer(call -> nodeEntity(call.getArgument(0))); - when(nodes.findDtoById(any(Long.class), eq(false))).thenAnswer(call -> { - Long id = call.getArgument(0); - FlowNodeConfig entity = nodeEntity(id); - if (entity == null) { - return null; - } - FlowNodeConfigDto dto = BeanUtil.copyProperties(entity, FlowNodeConfigDto.class); - dto.setNodeConfig(NodeConfigCodec.deserialize(entity.getComponentId(), entity.getJsonConfig())); - return dto; - }); - chain(false); - openSqlite(); - new TransactionTemplate(new DataSourceTransactionManager(dataSource)).executeWithoutResult(status -> { - seedResults(); - try { - seedMinimalLegacyGraphics(); - } catch (IOException exception) { - throw new UncheckedIOException(exception); - } - }); - } - - @AfterEach - @SuppressWarnings("unchecked") - void restoreApplicationContext() { - Map customConverters = (Map) ReflectionTestUtils.getField( - ConverterRegistry.getInstance(), "customConverterMap"); - if (previousVariableParametersConverter == null) { - customConverters.remove(BaseVariableParameters.class); - } else { - customConverters.put(BaseVariableParameters.class, previousVariableParametersConverter); - } - ReflectionTestUtils.setField(SpringContextHolder.class, "context", previousApplicationContext); - } - - - @Test - void legacyCombinedGroupsBecomeExactOutputsWithoutChangingOtherRecords() { - List> originalSource = rows("dashboard_result", "id = " + SOURCE); - List> originalHistory = rows("dashboard_result", "request_id IS NOT NULL"); - List> originalOtherBinding = rows("graphic_result_code", "dashboard_result_id = " + SOURCE); - assertOriginalLegacyGroups(); - - assertEquals(3, transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - - assertSavedBindings(Set.of(RANGE, ADD, SUBTRACT), "dBSPL"); - for (long id : List.of(RANGE, ADD, SUBTRACT)) { - assertEquals("dBSPL", resultMapper.selectById(id).getCustomUnitY()); - } - assertEquals(originalSource, rows("dashboard_result", "id = " + SOURCE)); - assertEquals(originalHistory, rows("dashboard_result", "request_id IS NOT NULL")); - assertEquals(originalOtherBinding, rows("graphic_result_code", "dashboard_result_id = " + SOURCE)); - assertEquals(0, transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - assertTrue(bindingWriter.transactionActive); - } - - @Test - void failureAfterRealBindingWriteRollsBackResultAndBindingChanges() { - Map>> before = databaseState(); - bindingWriter.failAfterWrite = true; - - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - - assertTrue(bindingWriter.transactionActive); - assertTrue(bindingWriter.realWrites > 0, "必须先经过真实GRC写入才能证明回滚"); - assertEquals(before, databaseState()); - } - - @Test - void failureAfterRealGraphicWriteRollsBackAllThreeTables() { - DashboardResult source = resultMapper.selectById(SOURCE); - source.setCustomUnitY("Pa"); - assertEquals(1, resultMapper.updateById(source)); - Map>> before = databaseState(); - FailingGraphicWriter writer = (FailingGraphicWriter) graphics; - writer.failAfterWrite = true; - - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - - assertTrue(writer.transactionActive); - assertTrue(writer.realWrites > 0, "必须先经过真实图谱写入才能证明三表回滚"); - assertTrue(bindingWriter.realWrites > 0); - assertEquals(before, databaseState()); - } - - @Test - void manyLegacyGraphicsUseBatchBindingAndGraphicReads() { - GraphicConfig templateGraphic = seededGraphics.values().iterator().next(); - GraphicResultCode templateBinding = seededBindings.values().iterator().next(); - for (long id = 10000; id < 10064; id++) { - GraphicConfig graphic = BeanUtil.copyProperties(templateGraphic, GraphicConfig.class); - graphic.setId(id); - GraphicResultCode binding = BeanUtil.copyProperties(templateBinding, GraphicResultCode.class); - binding.setId(id + 100000); - binding.setGraphicConfigId(id); - seedGraphicAndBinding(graphic, binding); - } - clearInvocations(bindingWriter, graphics); - - assertEquals(3, transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - - verify(bindingWriter, times(1)).listByDashboardResultIds(any()); - verify(bindingWriter, never()).findByDashboardResultId(any(Long.class)); - verify(graphics, times(1)).listByIds(any()); - assertSavedBindings(Set.of(RANGE, ADD, SUBTRACT), "dBSPL"); - } - - @ParameterizedTest - @ValueSource(strings = {"v2.2.0", "v2.1.0", ""}) - void unrelatedVersionsKeepRejectingLegacyMergedGroups(String version) { - assertRejectedWithoutWrites(version); - } - - @Test - void unversionedEntryKeepsStrictGroupContract() { - Map>> before = databaseState(); - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID)); - assertEquals(before, databaseState()); - } - - @ParameterizedTest - @ValueSource(strings = {"empty-axis-key", "root-y-axes", "line-empty-y-axes", "line-null-y-axes"}) - void explicitStableMarkersCannotBeTreatedAsUnmarkedLegacy(String marker) { - GraphicResultCode binding = seededBindings.values().iterator().next(); - if ("empty-axis-key".equals(marker)) { - binding.setAxisKey(""); - assertEquals(1, bindingMapper.updateById(binding)); - } else { - GraphicConfig graphic = graphicMapper.selectById(binding.getGraphicConfigId()); - JSONObject rawChart = JSONUtil.parseObj(graphic.getChartConfig()); - if ("root-y-axes".equals(marker)) { - rawChart.set("yAxes", new JSONArray()); - } else if (!"line-null-y-axes".equals(marker)) { - rawChart.getJSONObject("line").set("yAxes", new JSONArray()); - } - String storedChart = rawChart.toString(); - if ("line-null-y-axes".equals(marker)) { - // 直接嵌入null,避免JSON writer忽略null字段而把损坏marker变成缺省。 - assertTrue(storedChart.contains("\"line\":{")); - storedChart = storedChart.replace("\"line\":{", "\"line\":{\"yAxes\":null,"); - } - graphic.setChartConfig(storedChart); - assertEquals(1, graphicMapper.updateById(graphic)); - } - assertRejectedWithoutWrites("v2.1.1-Beta"); - } - - @Test - void knownChannelTypeConflictIsRejected() { - GraphicResultCode binding = seededBindings.values().iterator().next(); - JSONArray groups = JSONUtil.parseArray(binding.getUnit()); - groups.getJSONObject(0).set("channelType", ChannelTypeEnum.VOLTAGE.getValue()); - binding.setUnit(groups.toString()); - assertEquals(1, bindingMapper.updateById(binding)); - assertRejectedWithoutWrites("v2.1.1-Beta"); - } - - @Test - void explicitlySelectedSubsetIsNotExpandedToEveryResultOutput() { - for (GraphicResultCode binding : seededBindings.values()) { - if (Set.of(RANGE, ADD, SUBTRACT).contains(binding.getDashboardResultId())) { - binding.setChannelIds("Ch2,Ch4"); - assertEquals(1, bindingMapper.updateById(binding)); - } - } - - assertEquals(3, transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - - assertSavedBindings(Set.of(RANGE, ADD, SUBTRACT), "dBSPL"); - } - - @Test - void selectionOfMissingOutputIsRejected() { - GraphicResultCode binding = seededBindings.values().iterator().next(); - binding.setChannelIds("Ch1,Ch9"); - assertEquals(1, bindingMapper.updateById(binding)); - assertRejectedWithoutWrites("v2.1.1-Beta"); - } - - @Test - void resolvedUnitUpdatesExistingDisplayUnitAndPreservesEveryOtherDisplayField() { - DashboardResult source = resultMapper.selectById(SOURCE); - source.setCustomUnitY("Pa"); - assertEquals(1, resultMapper.updateById(source)); - - assertEquals(3, transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - - assertSavedBindings(Set.of(RANGE, ADD, SUBTRACT), "Pa"); - } - - private void assertRejectedWithoutWrites(String version) { - Map>> before = databaseState(); - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, version)); - assertEquals(0, bindingWriter.realWrites); - assertEquals(0, ((FailingGraphicWriter) graphics).realWrites); - assertEquals(before, databaseState()); - } - - /** 本地诊断入口:元数据只供配置和结果事实重放,既不恢复文件也不执行硬件读取。 */ - @Test - void importedGraphicHelpersAndFormalizerPrecedePostProcessingReconcile() throws Exception { - GraphicResultCode typedBinding = seededBindings.values().stream() - .filter(binding -> Objects.equals(binding.getDashboardResultId(), RANGE)) - .findFirst().orElseThrow(); - Map imported = recreateGraphicsThroughImportHelpers(PROJECT_ID, - List.of(seededGraphics.get(typedBinding.getGraphicConfigId())), List.of(typedBinding)); - Long importedId = imported.get(typedBinding.getGraphicConfigId()); - Map evidence = new LinkedHashMap<>(); - evidence.put("beforeFormalization", importedChannelEvidence(Set.of(RANGE))); - formalizeImportedGraphics(List.of(importedId)); - assertFormalized(importedId); - evidence.put("afterFormalization", rows("graphic_result_code", "graphic_config_id=" + importedId)); - evidence.put("afterFormalizationChannels", importedChannelEvidence(Set.of(RANGE))); - writeImportEvidence("minimal-formalizer", evidence); - GraphicConfig beforeGraphic = graphicMapper.selectById(importedId); - GraphicResultCode beforeBinding = bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", importedId)).getFirst(); - List> history = rows("dashboard_result", "request_id IS NOT NULL"); - - transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta", capturedLegacyGraphicIds); - - assertFormalizedRangeContract(importedId, beforeBinding, "dBSPL"); - assertEquals(JSONUtil.parseObj(beforeGraphic.getChartConfig()), - JSONUtil.parseObj(graphicMapper.selectById(importedId).getChartConfig()), "同单位同步保持既有轴布局"); - assertEquals(history, rows("dashboard_result", "request_id IS NOT NULL")); - } - - @ParameterizedTest - @ValueSource(strings = {"v2.2.0", "v2.1.0", ""}) - void formalizedAutoOutputStillRequires211ImportAuthorization(String version) { - createFormalizedRangeGraphic(version); - assertTrue(capturedLegacyGraphicIds.isEmpty()); - Map>> before = databaseState(); - - CryBusinessException failure = assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, version, capturedLegacyGraphicIds)); - - assertTrue(failure.getMessage().contains("缺少精确输出分组")); - assertEquals(before, databaseState()); - } - - @ParameterizedTest - @ValueSource(strings = {"binding", "graphic"}) - void formalizedAutoOutputWritesRollBackAtomically(String failureTarget) { - createFormalizedRangeGraphic(); - DashboardResult source = resultMapper.selectById(SOURCE); - source.setCustomUnitY("Pa"); - assertEquals(1, resultMapper.updateById(source)); - Map>> before = databaseState(); - bindingWriter.realWrites = 0; - FailingGraphicWriter writer = (FailingGraphicWriter) graphics; - writer.realWrites = 0; - bindingWriter.failAfterWrite = "binding".equals(failureTarget); - writer.failAfterWrite = "graphic".equals(failureTarget); - - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta", capturedLegacyGraphicIds)); - - assertTrue(bindingWriter.transactionActive); - assertTrue(bindingWriter.realWrites > 0, "正式合同必须实际执行GRC替换后才能验证回滚"); - if ("graphic".equals(failureTarget)) { - assertTrue(writer.transactionActive); - assertTrue(writer.realWrites > 0); - } - assertEquals(before, databaseState()); - } - - private Long createFormalizedRangeGraphic() { - return createFormalizedRangeGraphic("v2.1.1-Beta"); - } - - private Long createFormalizedRangeGraphic(String version) { - GraphicResultCode binding = seededBindings.values().stream() - .filter(row -> Objects.equals(row.getDashboardResultId(), RANGE)).findFirst().orElseThrow(); - Long graphicId = recreateGraphicsThroughImportHelpers(PROJECT_ID, - List.of(seededGraphics.get(binding.getGraphicConfigId())), List.of(binding)) - .get(binding.getGraphicConfigId()); - capturedLegacyGraphicIds = graphics.findImportedLegacyGraphicIds(version, - List.of(seededGraphics.get(binding.getGraphicConfigId())), List.of(binding), - Map.of(binding.getGraphicConfigId(), graphicId)); - formalizeImportedGraphics(List.of(graphicId)); - assertFormalized(graphicId); - return graphicId; - } - - @Test - void previouslyFormalizedImportWithoutOriginalCaptureIsNotRepaired() { - createFormalizedRangeGraphic(); - Map>> before = databaseState(); - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta")); - assertEquals(before, databaseState()); - } - - @ParameterizedTest - @ValueSource(strings = {"axis-key", "empty-axis-key", "line-y-axes", "root-y-axes", "mixed-bindings"}) - void originalLegacyCaptureRejectsFormalAndMixedMarkers(String marker) { - GraphicResultCode original = seededBindings.values().iterator().next(); - GraphicConfig graphic = BeanUtil.copyProperties(seededGraphics.get(original.getGraphicConfigId()), GraphicConfig.class); - GraphicResultCode binding = BeanUtil.copyProperties(original, GraphicResultCode.class); - List bindings = new ArrayList<>(List.of(binding)); - if ("axis-key".equals(marker) || "empty-axis-key".equals(marker)) { - binding.setAxisKey("empty-axis-key".equals(marker) ? "" : "existing-formal-axis"); - } else if ("mixed-bindings".equals(marker)) { - GraphicResultCode formal = BeanUtil.copyProperties(binding, GraphicResultCode.class); - formal.setId(60000L); - formal.setAxisKey("existing-formal-axis"); - bindings.add(formal); - } else { - JSONObject chart = JSONUtil.parseObj(graphic.getChartConfig()); - ("root-y-axes".equals(marker) ? chart : chart.getJSONObject("line")).set("yAxes", new JSONArray()); - graphic.setChartConfig(chart.toString()); - } - clearInvocations(graphics, bindingWriter); - assertEquals(Set.of(), graphics.findImportedLegacyGraphicIds("v2.1.1-Beta", - List.of(graphic), bindings, Map.of(graphic.getId(), 70000L))); - verify(graphics, never()).listByIds(any()); - verify(bindingWriter, never()).listByDashboardResultIds(any()); - } - - @Test - void sameAxisAutoOutputsWithDifferentSourceUnitsAreRejectedWithoutWrites() throws Exception { - createFormalizedRangeGraphic(); - List sourceUnits = new ArrayList<>(); - for (int index = 1; index <= 4; index++) { - UnitInfo unit = new UnitInfo(); - unit.setChannelIds(List.of("Ch" + index)); - unit.setChannelType(ChannelTypeEnum.AUDIO.getValue()); - unit.setChannelSource(0); - unit.setUnit(index == 4 ? "Pa" : "dBSPL"); - sourceUnits.add(unit); - } - DashboardResult source = resultMapper.selectById(SOURCE); - source.setCustomUnitY(JSONUtil.toJsonStr(sourceUnits)); - assertEquals(1, resultMapper.updateById(source)); - DashboardResultDto sourceDto = results.findDtoByIdWithoutDefaultConfig(SOURCE); - assertEquals("Pa", sourceDto.getCustomUnitY(sourceDto.getChannels().get(3)), "源Ch4必须真实解析为Pa"); - DashboardResultQuery query = new DashboardResultQuery(); - query.setProjectId(PROJECT_ID); - query.setResultType(ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - DashboardResultService.ImportedPostProcessingContext context = results.prepareImportedPostProcessingChannels( - results.listDtoWithoutDefaultConfig(query)); - Class cacheType = java.util.Arrays.stream(GraphicConfigService.class.getDeclaredClasses()) - .filter(type -> "SequencePostProcessingUnitResolveCache".equals(type.getSimpleName())).findFirst().orElseThrow(); - java.lang.reflect.Constructor constructor = cacheType.getDeclaredConstructor(); - constructor.setAccessible(true); - Object cache = constructor.newInstance(); - ReflectionTestUtils.invokeMethod(cache, "putSourceResults", context.sourceResults()); - context.nodes().forEach((id, node) -> ReflectionTestUtils.invokeMethod(cache, "putPostProcessingNode", id, node)); - List outputPlan = ReflectionTestUtils.invokeMethod(graphics, "resolvePostProcessingFixedOutputUnitGroups", - context.sourceResults().get(RANGE), Set.of(ChannelTypeEnum.AUDIO.getValue()), cache); - assertNotNull(outputPlan); - assertEquals(List.of("dBSPL", "dBSPL", "dBSPL", "Pa"), outputPlan.stream().map(UnitInfo::getUnit).toList(), - "必须先确认精确输出单位计划存在冲突,再验证同轴拒绝"); - Map>> before = databaseState(); - - assertThrows(CryBusinessException.class, - () -> transactionalGraphics.reconcileImportedCurrentPostProcessingUnits( - PROJECT_ID, "v2.1.1-Beta", capturedLegacyGraphicIds)); - - assertEquals(before, databaseState()); - } - - @Test - void formalizedAutoOutputsPreserveSeparateLeftAndRightAxes() { - GraphicResultCode template = seededBindings.values().stream() - .filter(row -> Objects.equals(row.getDashboardResultId(), RANGE)).findFirst().orElseThrow(); - GraphicConfig graphic = BeanUtil.copyProperties(seededGraphics.get(template.getGraphicConfigId()), GraphicConfig.class); - List rows = new ArrayList<>(); - for (int side = 0; side < 2; side++) { - GraphicResultCode binding = BeanUtil.copyProperties(template, GraphicResultCode.class); - binding.setId(template.getId() + side); - binding.setAxisType(side); - List selected = side == 0 ? List.of("Ch1", "Ch2") : List.of("Ch3", "Ch4"); - binding.setChannelIds(String.join(",", selected)); - UnitInfo unit = JSONUtil.toList(template.getUnit(), UnitInfo.class).getFirst(); - unit.setChannelIds(selected); - binding.setUnit(JSONUtil.toJsonStr(List.of(unit))); - rows.add(binding); - } - Long importedId = recreateGraphicsThroughImportHelpers(PROJECT_ID, List.of(graphic), rows).get(graphic.getId()); - formalizeImportedGraphics(List.of(importedId)); - assertFormalized(importedId); - List before = bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", importedId)); - assertEquals(2, before.stream().map(GraphicResultCode::getAxisKey).distinct().count()); - String chart = graphicMapper.selectById(importedId).getChartConfig(); - JSONArray axes = JSONUtil.parseObj(chart).getJSONObject("line").getJSONArray("yAxes"); - assertNotNull(axes); - assertEquals(Set.of("LEFT", "RIGHT"), axes.stream().map(value -> JSONUtil.parseObj(value).getStr("side")) - .collect(java.util.stream.Collectors.toSet())); - - transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta", capturedLegacyGraphicIds); - - assertEquals(JSONUtil.parseObj(chart), JSONUtil.parseObj(graphicMapper.selectById(importedId).getChartConfig())); - for (GraphicResultCode original : before) { - GraphicResultCode saved = bindingMapper.selectById(original.getId()); - assertNotNull(saved); - assertEquals(original.getAxisKey(), saved.getAxisKey()); - assertEquals(original.getAxisType(), saved.getAxisType()); - assertEquals(original.getChannelIds(), saved.getChannelIds()); - } - assertEquals(before.size(), bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", importedId)).size()); - } - - @Test - void manyFormalizedGraphicsReusePreparedSourcesAndBatchReads() { - GraphicResultCode template = seededBindings.values().stream() - .filter(binding -> Objects.equals(binding.getDashboardResultId(), RANGE)).findFirst().orElseThrow(); - List exportedGraphics = new ArrayList<>(); - List exportedBindings = new ArrayList<>(); - for (long id = 10000; id < 10064; id++) { - GraphicConfig graphic = BeanUtil.copyProperties(seededGraphics.get(template.getGraphicConfigId()), GraphicConfig.class); - graphic.setId(id); - GraphicResultCode binding = BeanUtil.copyProperties(template, GraphicResultCode.class); - binding.setId(id + 100000); - binding.setGraphicConfigId(id); - exportedGraphics.add(graphic); - exportedBindings.add(binding); - } - Map imported = recreateGraphicsThroughImportHelpers(PROJECT_ID, exportedGraphics, exportedBindings); - formalizeImportedGraphics(imported.values()); - imported.values().forEach(this::assertFormalized); - clearInvocations(bindingWriter, graphics, results, nodes); - - assertEquals(3, transactionalGraphics.reconcileImportedCurrentPostProcessingUnits( - PROJECT_ID, "v2.1.1-Beta", capturedLegacyGraphicIds)); - - verify(bindingWriter, times(1)).listByDashboardResultIds(any()); - verify(bindingWriter, never()).findByDashboardResultId(any(Long.class)); - verify(graphics, times(1)).listByIds(any()); - verify(nodes, never()).findDtoById(any(Long.class), eq(false)); - verify(results, never()).findDtoByIdWithoutDefaultConfig(any(Long.class)); - verify(results, never()).findPostProcessingSourceDtoByIdWithoutDefaultConfig(any(Long.class)); - assertEquals(64L, graphicMapper.selectCount(new QueryWrapper())); - assertEquals(64L, bindingMapper.selectCount(new QueryWrapper())); - } - - @Test - void existingFormalManualRangeSurvivesImportedUnitSync() { - Long importedId = createFormalizedRangeGraphic(); - GraphicConfig before = graphicMapper.selectById(importedId); - GraphicResultCode binding = bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", importedId)).getFirst(); - String oldAxisKey = binding.getAxisKey(); - JSONObject original = JSONUtil.parseObj(before.getOriginal()); - JSONObject manual = original.getJSONObject("yAxesByKey").getJSONObject(oldAxisKey); - manual.set("manualMin", -5); - manual.set("manualMax", 5); - manual.set("autoScale", false); - manual.set("useSystemSettings", false); - before.setOriginal(original.toString()); - assertEquals(1, graphicMapper.updateById(before)); - JSONObject manualBefore = JSONUtil.parseObj(graphicMapper.selectById(importedId).getOriginal()) - .getJSONObject("yAxesByKey").getJSONObject(oldAxisKey); - assertEquals(-5D, manualBefore.getDouble("manualMin")); - assertEquals(5D, manualBefore.getDouble("manualMax")); - assertEquals(false, manualBefore.getBool("autoScale")); - assertEquals(false, manualBefore.getBool("useSystemSettings")); - DashboardResult source = resultMapper.selectById(SOURCE); - source.setCustomUnitY("Pa"); - assertEquals(1, resultMapper.updateById(source)); - - transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(PROJECT_ID, "v2.1.1-Beta", capturedLegacyGraphicIds); - - GraphicResultCode savedBinding = bindingMapper.selectById(binding.getId()); - assertEquals(oldAxisKey, savedBinding.getAxisKey()); - GraphicConfig saved = graphicMapper.selectById(importedId); - JSONObject manualAfter = JSONUtil.parseObj(saved.getOriginal()).getJSONObject("yAxesByKey").getJSONObject(oldAxisKey); - assertEquals(withoutUnitFields(manualBefore), withoutUnitFields(manualAfter)); - assertEquals(-5D, manualAfter.getDouble("manualMin")); - assertEquals(5D, manualAfter.getDouble("manualMax")); - assertEquals(false, manualAfter.getBool("autoScale")); - assertEquals(false, manualAfter.getBool("useSystemSettings")); - assertEquals("Pa", JSONUtil.parseObj(saved.getHandlerConfig()).getStr("unitY")); - } - - private void assertFormalizedRangeContract(Long graphicId, GraphicResultCode original, String expectedUnit) { - assertFormalized(graphicId); - List bindings = bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", graphicId)); - assertEquals(1, bindings.size(), "四个auto输出保留同一正式轴绑定"); - GraphicResultCode binding = bindings.getFirst(); - assertEquals(original.getId(), binding.getId()); - assertEquals(original.getAxisKey(), binding.getAxisKey()); - assertEquals(original.getAxisType(), binding.getAxisType()); - assertEquals(original.getBindingOrder(), binding.getBindingOrder()); - assertEquals(original.getChannelIds(), binding.getChannelIds()); - List units = JSONUtil.toList(binding.getUnit(), UnitInfo.class); - assertEquals(1, units.size(), "正式绑定仍只含一个完整UnitInfo"); - UnitInfo unit = units.getFirst(); - assertEquals(List.of("Ch1", "Ch2", "Ch3", "Ch4"), unit.getChannelIds()); - assertEquals(expectedUnit, unit.getUnit()); - assertEquals(ChannelTypeEnum.AUDIO.getValue(), unit.getChannelType()); - assertEquals(Set.of(expectedUnit), unit.getUnitSets()); - assertEquals(false, unit.getUnitSwitchSupported()); - assertEquals(Set.copyOf(unit.getChannelIds()), unit.getMemberUnitMetadata().keySet()); - for (String channel : unit.getChannelIds()) { - assertEquals(expectedUnit, unit.getSourceUnitByChannel().get(channel)); - assertEquals(expectedUnit, unit.getMemberUnitMetadata().get(channel).getSourceUnit()); - assertEquals(Set.of(expectedUnit), unit.getMemberUnitMetadata().get(channel).getUnitSets()); - assertEquals(false, unit.getMemberUnitMetadata().get(channel).getUnitSwitchSupported()); - } - } - - /** 多原包诊断入口:读取真实OT,保持包版本,经图谱/GRC重ID与正式化后协调。 */ - @Test - void replayLocalPackageThroughImportGraphics() throws Exception { - replayLocalPackageThroughImportGraphics((String) null); - } - - @ParameterizedTest(name = "{0}") - @ValueSource(strings = {"NewProject_1.ot", "NewProject_2.ot", "Q74C.ot"}) - void replayAllThreeLocalPackagesThroughImportGraphics(String packageName) throws Exception { - String directory = System.getProperty("opentest.importPostProcessingPackageDirectory"); - assumeTrue(directory != null && !directory.isBlank()); - packageReplayOverride = "Q74C.ot".equals(packageName) - ? Path.of(System.getProperty("opentest.importPostProcessingQ74Package", - ".scratch/ot-import-graphic-unit-20260909/source.ot")) - : Path.of(directory).resolve(packageName); - assertTrue(Files.isRegularFile(packageReplayOverride), "指定三包回放必须三包均存在"); - replayLocalPackageThroughImportGraphics((String) null); - } - - @ParameterizedTest - @ValueSource(strings = {"missing", "different-box", "foreign-project", "ambiguous"}) - void twoBoxPackageRejectsMissingOrAmbiguousCurrentSource(String mutation) { - String packagePath = packageReplayOverride == null ? System.getProperty("opentest.importPostProcessingPackage") - : packageReplayOverride.toString(); - assumeTrue(packagePath != null && packagePath.endsWith("NewProject_2.ot")); - assertThrows(RuntimeException.class, () -> replayLocalPackageThroughImportGraphics(mutation)); - assertNotNull(packageBeforeUnitResolution, "先完成fixture导入持久化,不能以装配异常代替拒绝"); - assertEquals(packageBeforeUnitResolution, databaseState()); - } - - private void replayLocalPackageThroughImportGraphics(String mutation) throws Exception { - String packagePath = packageReplayOverride == null ? System.getProperty("opentest.importPostProcessingPackage") - : packageReplayOverride.toString(); - assumeTrue(packagePath != null && !packagePath.isBlank()); - var exported = new ProjectOtPackageService().readMetadata(Path.of(packagePath)).getProjectExportDto(); - JSONObject metadata = JSONUtil.parseObj(JSONUtil.toJsonStr(exported)); - long projectId = seedMetadata(metadata); - if (mutation != null) { - DashboardResult sibling = records.get(2097529890021191683L); - assertNotNull(sibling); - if ("missing".equals(mutation)) { - records.remove(sibling.getId()); - assertEquals(1, resultMapper.deleteById(sibling.getId())); - } else if ("ambiguous".equals(mutation)) { - DashboardResult duplicate = BeanUtil.copyProperties(sibling, DashboardResult.class); - duplicate.setId(7000000000000000001L); - records.put(duplicate.getId(), duplicate); - assertEquals(1, resultMapper.insert(duplicate)); - } else { - if ("different-box".equals(mutation)) { - sibling.setBoxId("2"); - } else { - sibling.setProjectId(projectId + 1); - } - assertEquals(1, resultMapper.updateById(sibling)); - } - } - Map imported = recreateGraphicsThroughImportHelpers(projectId, - new ArrayList<>(seededGraphics.values()), new ArrayList<>(seededBindings.values())); - capturedLegacyGraphicIds = graphics.findImportedLegacyGraphicIds(exported.getExportVersion(), - seededGraphics.values(), seededBindings.values(), imported); - packageBeforeUnitResolution = databaseState(); - DashboardResultQuery postQuery = new DashboardResultQuery(); - postQuery.setProjectId(projectId); - postQuery.setResultType(ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - DashboardResultService.ImportedPostProcessingContext prepared = results.prepareImportedPostProcessingChannels( - results.listDtoWithoutDefaultConfig(postQuery)); - for (DashboardResultDto result : prepared.sourceResults().values()) { - if (!Objects.equals(result.getResultType(), ResultTypeEnum.POST_ANALYSIS_DATA.getType()) - || result.getBoxId() == null || "0".equals(result.getBoxId())) { - continue; - } - FlowNodeConfigDto node = prepared.nodes().get(result.getNodeId()); - if (node == null || !(node.getNodeConfig() instanceof PostProcessingNodeConfigDto config)) { - continue; - } - for (int index = 0; index < config.getCalculations().size(); index++) { - var calculation = config.getCalculations().get(index); - for (var operand : PostProcessingNodeConfigDto.SourceOperand.values()) { - var source = operand == PostProcessingNodeConfigDto.SourceOperand.SOURCE_A - ? calculation.getSourceA() : calculation.getSourceB(); - if (source == null || !"previousNode".equals(source.getNormalizedSourceType())) { - continue; - } - String sourceKey = PostProcessingNodeConfigDto.calculationSourceKey(index, operand); - var reference = config.getSourceReferencesByBox().get(result.getBoxId()).get(sourceKey); - assertNotNull(reference, "生产prepare必须自己补齐同箱引用"); - DashboardResult actualSource = records.get(reference.getDashboardResultId()); - assertNotNull(actualSource); - assertEquals(result.getBoxId(), actualSource.getBoxId()); - assertEquals(result.getProjectId(), actualSource.getProjectId()); - assertEquals(result.getDashboardId(), actualSource.getDashboardId()); - assertEquals(source.getNodeId(), actualSource.getNodeId()); - } - } - } - writeImportEvidence("package-prepared-sources", Map.of("results", prepared.sourceResults().values().stream().map(result -> { - JSONObject facts = new JSONObject(); - facts.set("id", result.getId()); - facts.set("nodeId", result.getNodeId()); - facts.set("boxId", result.getBoxId()); - facts.set("primaryCode", result.getPrimaryCode()); - facts.set("customUnitY", result.getCustomUnitY()); - facts.set("channels", result.getChannels()); - return facts; - }).toList())); - formalizeImportedGraphics(imported.values()); - writeImportEvidence("package-formalized-input", Map.of( - "exportVersion", exported.getExportVersion(), - "capturedLegacyGraphicIds", capturedLegacyGraphicIds, - "graphics", rows("graphic_config", "1=1"), - "bindings", rows("graphic_result_code", "1=1"))); - - packageBeforeUnitResolution = databaseState(); - transactionalGraphics.reconcileImportedCurrentPostProcessingUnits( - projectId, exported.getExportVersion(), capturedLegacyGraphicIds); - - if (packagePath.endsWith("NewProject_2.ot")) { - assertEquals("dBFS", resultMapper.selectById(2097529803022938115L).getCustomUnitY()); - assertEquals("dBFS", resultMapper.selectById(2097529890021191683L).getCustomUnitY()); - assertEquals("dBFS", resultMapper.selectById(2097534000325865474L).getCustomUnitY()); - assertEquals("dBFS", resultMapper.selectById(2097533988644728834L).getCustomUnitY()); - } - assertEquals(exported.getGraphicConfigs().size(), graphicMapper.selectCount(new QueryWrapper())); - } - - /** 完整图谱前序重放:真实创建helper分配新图谱/GRC ID,再执行生产converger。 */ - @Test - void replayLocalMetadataThroughImportGraphicHelpersAndFormalizer() throws Exception { - String metadataPath = System.getProperty("opentest.importPostProcessingMetadata"); - assumeTrue(metadataPath != null && !metadataPath.isBlank()); - JSONObject metadata = JSONUtil.parseObj(Files.readString(Path.of(metadataPath))); - long projectId = seedMetadata(metadata); - Map imported = recreateGraphicsThroughImportHelpers(projectId, - new ArrayList<>(seededGraphics.values()), new ArrayList<>(seededBindings.values())); - Set postResults = replayExpectedSourceUnits().keySet(); - Map evidence = new LinkedHashMap<>(); - evidence.put("beforeFormalization", importedChannelEvidence(postResults)); - formalizeImportedGraphics(imported.values()); - // 九条post绑定必须实际正式化,包括两条原UNKNOWN,禁止吞异常绕过待测入口。 - for (GraphicResultCode binding : seededBindings.values()) { - if (postResults.contains(binding.getDashboardResultId())) { - assertFormalized(imported.get(binding.getGraphicConfigId())); - } - } - evidence.put("afterFormalization", rows("graphic_result_code", "dashboard_result_id IN (" + sqlIds(postResults) + ")")); - evidence.put("afterFormalizationChannels", importedChannelEvidence(postResults)); - writeImportEvidence("metadata-formalizer", evidence); - Set affectedGraphics = seededBindings.values().stream() - .filter(binding -> postResults.contains(binding.getDashboardResultId())) - .map(binding -> imported.get(binding.getGraphicConfigId())).collect(java.util.stream.Collectors.toSet()); - String otherGraphicPredicate = "id NOT IN (" + sqlIds(affectedGraphics) + ")"; - String otherBindingPredicate = "dashboard_result_id NOT IN (" + sqlIds(postResults) + ")"; - List> otherGraphics = rows("graphic_config", otherGraphicPredicate); - List> otherBindings = rows("graphic_result_code", otherBindingPredicate); - List> ordinaryResults = rows("dashboard_result", "result_type <> 5"); - Map beforePostGraphics = new LinkedHashMap<>(); - affectedGraphics.forEach(id -> beforePostGraphics.put(id, graphicMapper.selectById(id))); - - transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(projectId, metadata.getStr("exportVersion"), capturedLegacyGraphicIds); - - Map> savedSelections = new LinkedHashMap<>(); - for (GraphicResultCode binding : bindingMapper.selectList(new QueryWrapper() - .in("dashboard_result_id", postResults))) { - savedSelections.computeIfAbsent(binding.getDashboardResultId(), ignored -> new ArrayList<>()) - .addAll(List.of(binding.getChannelIds().split(","))); - String expectedUnit = replayExpectedSourceUnits().get(binding.getDashboardResultId()).unit(); - for (UnitInfo unit : JSONUtil.toList(binding.getUnit(), UnitInfo.class)) { - assertEquals(expectedUnit, unit.getUnit()); - assertEquals(ChannelTypeEnum.AUDIO.getValue(), unit.getChannelType()); - } - GraphicConfig savedGraphic = graphicMapper.selectById(binding.getGraphicConfigId()); - assertEquals(expectedUnit, JSONUtil.parseObj(savedGraphic.getHandlerConfig()).getStr("unitY"), - "handler单位镜像必须跟随源权威: " + savedGraphic.getName()); - JSONObject line = JSONUtil.parseObj(savedGraphic.getChartConfig()).getJSONObject("line"); - JSONArray axes = line.getJSONArray("yAxes"); - assertNotNull(axes); - axes.forEach(axis -> assertEquals(expectedUnit, JSONUtil.parseObj(axis).getStr("unit"))); - GraphicConfig beforeGraphic = beforePostGraphics.get(savedGraphic.getId()); - JSONArray originalAxes = JSONUtil.parseObj(beforeGraphic.getChartConfig()).getJSONObject("line").getJSONArray("yAxes"); - JSONObject beforeDisplay = JSONUtil.parseObj(beforeGraphic.getOriginal()); - JSONObject afterDisplay = JSONUtil.parseObj(savedGraphic.getOriginal()); - if ("平衡度".equals(savedGraphic.getName())) { - JSONObject displaysByKey = afterDisplay.getJSONObject("yAxesByKey"); - assertNotNull(displaysByKey); - writeImportEvidence("balance-display-stages", Map.of( - "beforeUnitSync", beforeDisplay, - "afterUnitSync", afterDisplay)); - // 原包使用系统设置;start/end是旧缓存,导入不能替用户激活手工范围。 - JSONObject cachedAxis = afterDisplay.getJSONObject("yAxis"); - assertEquals(-5D, cachedAxis.getDouble("start")); - assertEquals(5D, cachedAxis.getDouble("end")); - assertEquals(false, cachedAxis.getBool("autoScale")); - assertEquals(true, cachedAxis.getBool("useSystemSettings")); - assertEquals(withoutUnitFields(beforeDisplay.getJSONObject("yAxis")), withoutUnitFields(cachedAxis)); - axisKeySides(originalAxes).keySet().forEach(key -> assertEquals( - beforeDisplay.getJSONObject("yAxesByKey").getJSONObject(key).getBool("useSystemSettings"), - displaysByKey.getJSONObject(key).getBool("useSystemSettings"))); - } - FlowNodeConfig node = replayNodes.get(records.get(binding.getDashboardResultId()).getNodeId()); - PostProcessingNodeConfigDto config = (PostProcessingNodeConfigDto) NodeConfigCodec.deserialize( - node.getComponentId(), node.getJsonConfig()); - if (config.getCalculations().stream().allMatch(config::isPassThroughOutput)) { - assertEquals(axisKeySides(originalAxes), axisKeySides(axes), "auto图全部轴键/方向保持"); - assertEquals(withoutUnitFields(beforeDisplay), withoutUnitFields(afterDisplay), - "auto图手工范围和样式不得改变: " + savedGraphic.getName()); - assertEquals(withoutUnitFields(JSONUtil.parseObj(beforeGraphic.getChartConfig())), - withoutUnitFields(JSONUtil.parseObj(savedGraphic.getChartConfig())), "auto图轴布局不得改变"); - } else { - Map originalSides = axisKeySides(originalAxes); - Map savedSides = axisKeySides(axes); - assertEquals(4, savedSides.size(), "四个显式alias依照既有合同拆成四个独立轴"); - assertTrue(savedSides.keySet().containsAll(originalSides.keySet()), "UNKNOWN恢复后保留旧轴键"); - originalSides.forEach((key, side) -> { - assertEquals(side, savedSides.get(key), "旧轴方向保持"); - assertEquals(withoutUnitFields(beforeDisplay.getJSONObject("yAxesByKey").getJSONObject(key)), - withoutUnitFields(afterDisplay.getJSONObject("yAxesByKey").getJSONObject(key)), - "alias分轴保留旧轴手工范围和样式"); - }); - List aliasBindings = bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", savedGraphic.getId())); - assertEquals(4, aliasBindings.size()); - assertEquals(4, aliasBindings.stream().map(GraphicResultCode::getAxisKey).distinct().count()); - assertTrue(aliasBindings.stream().allMatch(row -> !row.getChannelIds().contains(","))); - } - } - assertEquals(postResults, savedSelections.keySet()); - savedSelections.values().forEach(selection -> { - assertEquals(4, selection.size()); - assertEquals(Set.of("Ch1", "Ch2", "Ch3", "Ch4"), Set.copyOf(selection)); - }); - assertEquals(otherGraphics, rows("graphic_config", otherGraphicPredicate)); - assertEquals(otherBindings, rows("graphic_result_code", otherBindingPredicate)); - assertEquals(ordinaryResults, rows("dashboard_result", "result_type <> 5")); - assertEquals(94L, graphicMapper.selectCount(new QueryWrapper())); - } - - private Map axisKeySides(JSONArray axes) { - return axes.stream().map(JSONUtil::parseObj).collect(java.util.stream.Collectors.toMap( - axis -> axis.getStr("axisKey"), axis -> axis.getStr("side"))); - } - - private Object withoutUnitFields(Object value) { - if (value instanceof JSONObject object) { - Map result = new LinkedHashMap<>(); - object.forEach((key, child) -> { - if (!"unit".equals(key) && !"unitY".equals(key)) { - result.put(key, withoutUnitFields(child)); - } - }); - return result; - } - if (value instanceof JSONArray array) { - return array.stream().map(this::withoutUnitFields).toList(); - } - return value; - } - - private Map recreateGraphicsThroughImportHelpers(long projectId, - List exportedGraphics, List exportedBindings) { - jdbc.update("DELETE FROM graphic_result_code"); - jdbc.update("DELETE FROM graphic_config"); - ProjectService importer = new ProjectService(); - ReflectionTestUtils.setField(importer, "graphicConfigService", graphics); - ReflectionTestUtils.setField(importer, "graphicResultCodeService", bindingWriter); - Map nodeIds = new LinkedHashMap<>(); - Map dashboardIds = new LinkedHashMap<>(); - Map resultIds = new LinkedHashMap<>(); - Map customViewIds = new LinkedHashMap<>(); - records.values().forEach(result -> { - resultIds.put(result.getId(), result.getId()); - nodeIds.put(result.getNodeId(), result.getNodeId()); - dashboardIds.put(result.getDashboardId(), result.getDashboardId()); - }); - exportedGraphics.forEach(graphic -> { - nodeIds.put(graphic.getNodeId(), graphic.getNodeId()); - customViewIds.put(graphic.getCustomViewId(), graphic.getCustomViewId()); - }); - Map graphicIds = ReflectionTestUtils.invokeMethod(importer, - "createGraphicConfigsFromExport", exportedGraphics, projectId, nodeIds, dashboardIds, customViewIds); - assertNotNull(graphicIds); - assertTrue(graphicIds.entrySet().stream().allMatch(entry -> !entry.getKey().equals(entry.getValue()))); - ReflectionTestUtils.invokeMethod(importer, "createGraphicResultCodesFromExport", - exportedBindings, graphicIds, resultIds, projectId, nodeIds); - capturedLegacyGraphicIds = graphics.findImportedLegacyGraphicIds( - "v2.1.1-Beta", exportedGraphics, exportedBindings, graphicIds); - return graphicIds; - } - - private void formalizeImportedGraphics(Collection graphicIds) { - ImportedGraphicAxisContractFormalizer formalizer = new ImportedGraphicAxisContractFormalizer( - graphicMapper, bindingWriter, new LegacyAxisReadNormalizer(new DefaultAxisContractPlanner()), - new DashboardResultYAxisPresentationResolver(mock(CommonComponent.class))); - ProxyFactory proxy = new ProxyFactory(formalizer); - proxy.setProxyTargetClass(true); - proxy.addAdvice(new TransactionInterceptor(new DataSourceTransactionManager(dataSource), - new AnnotationTransactionAttributeSource())); - new ImportedGraphicAxisContractConverger((ImportedGraphicAxisContractFormalizer) proxy.getProxy()) - .converge(graphicIds); - } - - private void assertFormalized(Long graphicId) { - assertTrue(graphicMapper.selectById(graphicId).getChartConfig().contains("yAxes")); - List bindings = bindingMapper.selectList(new QueryWrapper() - .eq("graphic_config_id", graphicId)); - assertFalse(bindings.isEmpty()); - assertTrue(bindings.stream().allMatch(binding -> binding.getAxisKey() != null - && !binding.getAxisKey().isBlank()), "converger必须实际成功正式化"); - } - - private Map importedChannelEvidence(Set resultIds) { - Map evidence = new LinkedHashMap<>(); - Map grouped = results.getGroupDtoMapByProperty(resultIds); - evidence.put("groupDtoChannels", grouped.entrySet().stream().collect(java.util.stream.Collectors.toMap( - Map.Entry::getKey, entry -> entry.getValue().getChannels() == null ? List.of() : entry.getValue().getChannels()))); - DashboardResultQuery query = new DashboardResultQuery(); - query.setProjectId(records.values().iterator().next().getProjectId()); - query.setResultType(ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - Map prepared = results.prepareImportedPostProcessingChannels( - results.listDtoWithoutDefaultConfig(query)).sourceResults(); - evidence.put("preparedChannels", resultIds.stream().collect(java.util.stream.Collectors.toMap( - id -> id, id -> prepared.get(id).getChannels()))); - return evidence; - } - - private void writeImportEvidence(String name, Map evidence) throws Exception { - String outputDirectory = System.getProperty("opentest.importPostProcessingEvidenceDir"); - if (outputDirectory != null && !outputDirectory.isBlank()) { - Path directory = Files.createDirectories(Path.of(outputDirectory)); - String prefix = packageReplayOverride == null ? "" : packageReplayOverride.getFileName() + "-"; - Files.writeString(directory.resolve(prefix + name + ".json"), JSONUtil.toJsonPrettyStr(evidence)); - } - } - - @Test - void replayLocalMetadataWhenExplicitlyProvided() throws Exception { - String metadataPath = System.getProperty("opentest.importPostProcessingMetadata"); - assumeTrue(metadataPath != null && !metadataPath.isBlank()); - JSONObject metadata = JSONUtil.parseObj(Files.readString(Path.of(metadataPath))); - long projectId = seedMetadata(metadata); - Set postResultIds = records.values().stream() - .filter(record -> Objects.equals(record.getResultType(), ResultTypeEnum.POST_ANALYSIS_DATA.getType())) - .map(DashboardResult::getId).collect(java.util.stream.Collectors.toSet()); - Map expectedSources = replayExpectedSourceUnits(); - assertEquals(expectedSources.keySet(), seededBindings.values().stream() - .filter(binding -> postResultIds.contains(binding.getDashboardResultId())) - .map(GraphicResultCode::getDashboardResultId).collect(java.util.stream.Collectors.toSet())); - for (ReplaySourceUnit expected : expectedSources.values()) { - // 独立oracle取原包普通源明确值,不从待修post结果或旧图谱镜像反推。 - assertEquals(expected.unit(), records.get(expected.sourceResultId()).getCustomUnitY()); - } - Set affectedGraphicIds = seededBindings.values().stream() - .filter(binding -> postResultIds.contains(binding.getDashboardResultId())) - .map(GraphicResultCode::getGraphicConfigId).collect(java.util.stream.Collectors.toSet()); - assertEquals(357, records.size()); - assertEquals(94, seededGraphics.size()); - assertEquals(97, seededBindings.size()); - assertEquals(9, affectedGraphicIds.size()); - String otherBindingsPredicate = "dashboard_result_id NOT IN (" + sqlIds(postResultIds) + ")"; - String otherGraphicsPredicate = "id NOT IN (" + sqlIds(affectedGraphicIds) + ")"; - String ordinaryResultsPredicate = "result_type <> " + ResultTypeEnum.POST_ANALYSIS_DATA.getType(); - List> otherBindings = rows("graphic_result_code", otherBindingsPredicate); - List> otherGraphics = rows("graphic_config", otherGraphicsPredicate); - List> ordinaryResults = rows("dashboard_result", ordinaryResultsPredicate); - assertEquals(88, otherBindings.size()); - assertEquals(85, otherGraphics.size()); - assertEquals(341, ordinaryResults.size()); - - transactionalGraphics.reconcileImportedCurrentPostProcessingUnits(projectId, metadata.getStr("exportVersion")); - - DashboardResultQuery currentQuery = new DashboardResultQuery(); - currentQuery.setProjectId(projectId); - currentQuery.setResultType(ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - List current = results.listDtoWithoutDefaultConfig(currentQuery); - Map currentById = results.prepareImportedPostProcessingChannels(current).sourceResults(); - int exactGroups = 0; - int postBindings = 0; - int unknownTypeBindings = 0; - for (GraphicResultCode original : seededBindings.values()) { - if (!postResultIds.contains(original.getDashboardResultId())) { - continue; - } - postBindings++; - String expectedUnit = expectedSources.get(original.getDashboardResultId()).unit(); - boolean oldUnknown = JSONUtil.toList(original.getUnit(), UnitInfo.class).stream() - .anyMatch(group -> Objects.equals(group.getChannelType(), ChannelTypeEnum.UNKNOWN.getValue())); - if (oldUnknown) { - unknownTypeBindings++; - } - GraphicResultCode saved = bindingMapper.selectById(original.getId()); - assertEquals(original.getChannelIds(), saved.getChannelIds()); - assertEquals(original.getGraphicConfigId(), saved.getGraphicConfigId()); - assertEquals(original.getDashboardResultId(), saved.getDashboardResultId()); - List groups = JSONUtil.toList(saved.getUnit(), UnitInfo.class); - List selected = List.of(original.getChannelIds().split(",")); - assertEquals(4, selected.size()); - assertEquals(selected.size(), groups.size()); - DashboardResultDto result = currentById.get(saved.getDashboardResultId()); - assertNotNull(result); - for (int index = 0; index < selected.size(); index++) { - String channelId = selected.get(index); - UnitInfo group = groups.get(index); - assertTrue(group.getChannelIds().contains(channelId)); - assertEquals(1, group.getChannelIds().stream().filter(id -> id.matches("Ch[0-9]+")).count()); - SimpleChannelDto output = result.getChannels().stream() - .filter(channel -> channelId.equals(channel.getVchId())).findFirst().orElseThrow(); - assertEquals(output.getChannelType(), group.getChannelType()); - assertEquals(expectedUnit, result.getCustomUnitY(output)); - assertEquals(expectedUnit, group.getUnit()); - assertEquals(Set.of(group.getUnit()), group.getUnitSets()); - assertEquals(false, group.getUnitSwitchSupported()); - if (oldUnknown) { - assertEquals(ChannelTypeEnum.AUDIO.getValue(), group.getChannelType()); - } - exactGroups++; - } - GraphicConfig graphic = graphicMapper.selectById(saved.getGraphicConfigId()); - GraphicConfig originalGraphic = seededGraphics.get(graphic.getId()); - JSONObject expectedDisplay = JSONUtil.parseObj(originalGraphic.getOriginal()); - JSONObject leftAxis = expectedDisplay.getJSONObject("yAxis"); - if (leftAxis != null && leftAxis.containsKey("unit")) { - leftAxis.set("unit", expectedUnit); - } - assertEquals(expectedDisplay, JSONUtil.parseObj(graphic.getOriginal())); - assertEquals(originalGraphic.getName(), graphic.getName()); - assertEquals(originalGraphic.getNodeId(), graphic.getNodeId()); - } - assertEquals(9, postBindings); - assertEquals(36, exactGroups); - assertEquals(2, unknownTypeBindings); - assertEquals(otherBindings, rows("graphic_result_code", otherBindingsPredicate)); - assertEquals(otherGraphics, rows("graphic_config", otherGraphicsPredicate)); - assertEquals(ordinaryResults, rows("dashboard_result", ordinaryResultsPredicate)); - assertEquals(97L, bindingMapper.selectCount(new QueryWrapper())); - assertEquals(94L, graphicMapper.selectCount(new QueryWrapper())); - } - - /** 来自该v2.1.1原包的普通源customUnitY;FS/dBFS不是post旧dBSPL镜像。 */ - private Map replayExpectedSourceUnits() { - return Map.ofEntries( - Map.entry(2089920475537010689L, new ReplaySourceUnit(2089920474547154945L, "FS")), - Map.entry(2089919965547393025L, new ReplaySourceUnit(2089917040397840387L, "%")), - Map.entry(2089919768184418305L, new ReplaySourceUnit(2089917040381063173L, "%")), - Map.entry(2089918495691304961L, new ReplaySourceUnit(2089917040364285953L, "dBFS")), - Map.entry(2089917786967175169L, new ReplaySourceUnit(2089917040364285953L, "dBFS")), - Map.entry(2089915997932941313L, new ReplaySourceUnit(2089915997572231169L, "FS")), - Map.entry(2089912747590610946L, new ReplaySourceUnit(2089912746663669761L, "FS")), - Map.entry(2089912151902973953L, new ReplaySourceUnit(2089912150879563777L, "FS")), - Map.entry(2089911882595102722L, new ReplaySourceUnit(2089910523053404161L, "FS"))); - } - - private record ReplaySourceUnit(long sourceResultId, String unit) { - } - - private String sqlIds(Collection ids) { - assertFalse(ids.isEmpty()); - return ids.stream().map(String::valueOf).collect(java.util.stream.Collectors.joining(",")); - } - - private void assertOriginalLegacyGroups() { - for (GraphicResultCode binding : seededBindings.values()) { - if (!Set.of(RANGE, ADD, SUBTRACT).contains(binding.getDashboardResultId())) { - continue; - } - assertEquals(binding.getUnit(), bindingMapper.selectById(binding.getId()).getUnit()); - List groups = JSONUtil.toList(binding.getUnit(), UnitInfo.class); - assertEquals(1, groups.size()); - assertEquals(List.of("Ch1", "Ch2", "Ch3", "Ch4"), groups.getFirst().getChannelIds()); - } - } - - private void assertSavedBindings(Set resultIds, String expectedUnit) { - for (GraphicResultCode original : seededBindings.values()) { - if (!resultIds.contains(original.getDashboardResultId())) { - continue; - } - GraphicResultCode saved = bindingMapper.selectById(original.getId()); - assertNotNull(saved); - assertEquals(original.getChannelIds(), saved.getChannelIds()); - assertEquals(original.getGraphicConfigId(), saved.getGraphicConfigId()); - assertEquals(original.getDashboardResultId(), saved.getDashboardResultId()); - List units = JSONUtil.toList(saved.getUnit(), UnitInfo.class); - List selectedIds = List.of(original.getChannelIds().split(",")); - assertEquals(selectedIds.size(), units.size()); - for (int index = 0; index < selectedIds.size(); index++) { - UnitInfo group = units.get(index); - String channelId = selectedIds.get(index); - assertTrue(group.getChannelIds().contains(channelId)); - assertEquals(1, group.getChannelIds().stream().filter(id -> id.matches("Ch[0-9]+")).count()); - assertEquals(ChannelTypeEnum.AUDIO.getValue(), group.getChannelType()); - assertEquals(expectedUnit, group.getUnit()); - assertEquals(Set.of(expectedUnit), group.getUnitSets()); - assertEquals(false, group.getUnitSwitchSupported()); - } - GraphicConfig graphic = graphicMapper.selectById(saved.getGraphicConfigId()); - GraphicConfig before = seededGraphics.get(graphic.getId()); - assertEquals(before.getName(), graphic.getName()); - assertEquals(before.getNodeId(), graphic.getNodeId()); - JSONObject expectedDisplay = JSONUtil.parseObj(before.getOriginal()); - JSONObject leftAxis = expectedDisplay.getJSONObject("yAxis"); - if (leftAxis != null && leftAxis.containsKey("unit")) { - leftAxis.set("unit", expectedUnit); - } - assertEquals(expectedDisplay, JSONUtil.parseObj(graphic.getOriginal())); - assertEquals("Hz", graphic.getUnitX()); - assertTrue(graphic.getChartConfig().contains(expectedUnit)); - } - } - - private void openSqlite() throws Exception { - dataSource = new SQLiteDataSource(); - dataSource.setUrl("jdbc:sqlite:" + tempDir.resolve("import-post-processing.db")); - createSchema(dataSource); - jdbc = new JdbcTemplate(dataSource); - MybatisConfiguration configuration = new MybatisConfiguration(); - configuration.setMapUnderscoreToCamelCase(true); - configuration.addMapper(DashboardResultMapper.class); - configuration.addMapper(GraphicConfigMapper.class); - configuration.addMapper(GraphicResultCodeMapper.class); - MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); - factory.setDataSource(dataSource); - factory.setConfiguration(configuration); - SqlSessionFactory sessionFactory = factory.getObject(); - assertNotNull(sessionFactory); - SqlSessionTemplate template = new SqlSessionTemplate(sessionFactory); - resultMapper = template.getMapper(DashboardResultMapper.class); - graphicMapper = template.getMapper(GraphicConfigMapper.class); - bindingMapper = template.getMapper(GraphicResultCodeMapper.class); - setMapper(results, resultMapper); - setMapper(graphics, graphicMapper); - bindingWriter = spy(new FailingBindingWriter()); - setMapper(bindingWriter, bindingMapper); - ReflectionTestUtils.setField(bindingWriter, "dashboardResultService", results); - ReflectionTestUtils.setField(bindingWriter, "graphicResultCodeDtoMapper", Mappers.getMapper(GraphicResultCodeDtoMapper.class)); - ReflectionTestUtils.setField(graphics, "graphicResultCodeService", bindingWriter); - ReflectionTestUtils.setField(graphics, "graphicConfigDtoMapper", Mappers.getMapper(GraphicConfigDtoMapper.class)); - ReflectionTestUtils.setField(graphics, "dashboardConfigService", dashboards); - DefaultAxisContractPlanner planner = new DefaultAxisContractPlanner(); - ReflectionTestUtils.setField(graphics, "axisContractPlanner", planner); - ReflectionTestUtils.setField(graphics, "legacyAxisReadNormalizer", new LegacyAxisReadNormalizer(planner)); - ReflectionTestUtils.setField(graphics, "renderDataAxisBindingService", new RenderDataAxisBindingService()); - ReflectionTestUtils.setField(graphics, "yAxisPresentationResolver", - new DashboardResultYAxisPresentationResolver(mock(CommonComponent.class))); - ProxyFactory proxyFactory = new ProxyFactory(graphics); - proxyFactory.setProxyTargetClass(true); - proxyFactory.addAdvice(new TransactionInterceptor(new DataSourceTransactionManager(dataSource), - new AnnotationTransactionAttributeSource())); - transactionalGraphics = (GraphicConfigService) proxyFactory.getProxy(); - } - - private void seedResults() { - for (DashboardResult record : records.values()) { - assertEquals(1, resultMapper.insert(record)); - } - } - - private void seedMinimalLegacyGraphics() throws IOException { - try (InputStream input = getClass().getResourceAsStream("/fixtures/ot-import-legacy-post-graphics.json")) { - assertNotNull(input); - JSONArray fixtures = JSONUtil.parseArray(new String(input.readAllBytes(), StandardCharsets.UTF_8)); - long[] ids = {RANGE, ADD, SUBTRACT}; - for (int index = 0; index < ids.length; index++) { - JSONObject fixture = fixtures.getJSONObject(index); - GraphicConfig graphic = fixture.getJSONObject("graphic").toBean(GraphicConfig.class); - graphic.setProjectId(PROJECT_ID); - graphic.setDashboardId(DASHBOARD_ID); - graphic.setNodeId(nodeId(ids[index])); - GraphicResultCode binding = fixture.getJSONObject("binding").toBean(GraphicResultCode.class); - binding.setProjectId(PROJECT_ID); - binding.setDashboardResultId(ids[index]); - seedGraphicAndBinding(graphic, binding); - } - GraphicConfig unrelated = BeanUtil.copyProperties(seededGraphics.values().iterator().next(), GraphicConfig.class); - unrelated.setId(9000L); - unrelated.setNodeId(nodeId(SOURCE)); - GraphicResultCode otherBinding = BeanUtil.copyProperties(seededBindings.values().iterator().next(), GraphicResultCode.class); - otherBinding.setId(9001L); - otherBinding.setGraphicConfigId(unrelated.getId()); - otherBinding.setDashboardResultId(SOURCE); - seedGraphicAndBinding(unrelated, otherBinding); - DashboardResult history = BeanUtil.copyProperties(records.get(RANGE), DashboardResult.class); - history.setId(9002L); - history.setRequestId(9003L); - history.setCustomUnitY("historical-unit"); - assertEquals(1, resultMapper.insert(history)); - } - } - - private void seedGraphicAndBinding(GraphicConfig graphic, GraphicResultCode binding) { - // 原始存储JSON直接入库,禁止经batchSave/图谱编码器提前修整旧分组。 - assertEquals(1, graphicMapper.insert(graphic)); - assertEquals(1, bindingMapper.insert(binding)); - seededGraphics.put(graphic.getId(), BeanUtil.copyProperties(graphic, GraphicConfig.class)); - seededBindings.put(binding.getId(), BeanUtil.copyProperties(binding, GraphicResultCode.class)); - } - - private List> rows(String table, String predicate) { - return jdbc.queryForList("SELECT * FROM " + table + " WHERE " + predicate + " ORDER BY id"); - } - - private Map>> databaseState() { - Map>> state = new LinkedHashMap<>(); - for (String table : List.of("dashboard_result", "graphic_result_code", "graphic_config")) { - state.put(table, rows(table, "1=1")); - } - return state; - } - - private static void setMapper(Object service, Object mapper) throws Exception { - for (Class owner : List.of(BaseService.class, CrudRepository.class)) { - java.lang.reflect.Field field = owner.getDeclaredField("baseMapper"); - field.setAccessible(true); - field.set(service, mapper); - } - } - - public static class FailingGraphicWriter extends GraphicConfigService { - private boolean failAfterWrite; - private boolean transactionActive; - private int realWrites; - - @Override - public Boolean baseServiceBatchUpdate(List updates) { - transactionActive = TransactionSynchronizationManager.isActualTransactionActive(); - Boolean written = super.baseServiceBatchUpdate(updates); - if (Boolean.TRUE.equals(written)) { - realWrites += updates.size(); - } - return failAfterWrite ? false : written; - } - } - - private static class FailingBindingWriter extends GraphicResultCodeService { - private boolean failAfterWrite; - private boolean transactionActive; - private int realWrites; - - @Override - public Boolean batchSave(Long graphicId, List bindings, boolean preserveExistingIds) { - transactionActive = TransactionSynchronizationManager.isActualTransactionActive(); - Boolean written = super.batchSave(graphicId, bindings, preserveExistingIds); - if (Boolean.TRUE.equals(written)) { - realWrites += bindings.size(); - } - return failAfterWrite ? false : written; - } - - @Override - public Boolean baseServiceBatchUpdate(List updates) { - transactionActive = TransactionSynchronizationManager.isActualTransactionActive(); - Boolean written = super.baseServiceBatchUpdate(updates); - if (Boolean.TRUE.equals(written)) { - realWrites += updates.size(); - } - return failAfterWrite ? false : written; - } - } - - private static void createSchema(SQLiteDataSource dataSource) throws Exception { - try (Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { - connection.setAutoCommit(false); - statement.execute("CREATE TABLE graphic_config (id BIGINT PRIMARY KEY, tenant_id BIGINT, name TEXT, algorithm_code TEXT, result_code TEXT, " - + "chart_config TEXT, sort INTEGER, node_id BIGINT, handler_config TEXT, original TEXT, " - + "current_version BIGINT, project_id BIGINT, dashboard_id BIGINT, custom_view_id BIGINT, " - + "graphic_type INTEGER, scene_type INTEGER, dashboard_provider_id TEXT, primary_result INTEGER, " - + "derived_result INTEGER, derived_options INTEGER, unit_x TEXT, unit_y TEXT, " - + "reference_test_unit_config TEXT, " - + "created_time TEXT, updated_time TEXT)"); - statement.execute("CREATE TABLE dashboard_result (id BIGINT PRIMARY KEY, project_id BIGINT, dashboard_id BIGINT, request_id BIGINT, " - + "source_id BIGINT, pre_dashboard_result_id BIGINT, primary_code TEXT, alias_name TEXT, " - + "variable_parameters_config TEXT, post_process_config TEXT, hash TEXT, " - + "custom_unit_x TEXT, custom_unit_y TEXT, algorithm_code TEXT, result_type INTEGER, " - + "result_display_mode INTEGER, concatenate_multi_channel_data INTEGER, " - + "default_graphic_type INTEGER, box_id TEXT, default_i18n_name_key TEXT, " - + "node_id BIGINT, executed_count INTEGER, tenant_id BIGINT, " - + "created_time TEXT, updated_time TEXT)"); - statement.execute("CREATE TABLE graphic_result_code (" - + "id BIGINT PRIMARY KEY, tenant_id BIGINT, graphic_config_id BIGINT, dashboard_result_id BIGINT, " - + "channel_ids TEXT, project_id BIGINT, axis_type INTEGER, axis_key TEXT, binding_order INTEGER, " - + "auxiliary_axis_keys TEXT, unit TEXT, created_time TEXT, updated_time TEXT)"); - statement.execute("CREATE TABLE graphic_config_snapshot (" - + "id BIGINT PRIMARY KEY, tenant_id BIGINT, graphic_config_id BIGINT, custom_view_id BIGINT, " - + "name TEXT, request_id BIGINT, box_id TEXT, sort INTEGER, node_id BIGINT, " - + "primary_result INTEGER, derived_result INTEGER, derived_options INTEGER, channel_ids TEXT, " - + "unit_x TEXT, unit_y TEXT, handler_config TEXT, original TEXT, current_version BIGINT, " - + "project_id BIGINT, graphic_type INTEGER, result_code TEXT, algorithm_code TEXT, " - + "chart_config TEXT, relate_data_set_id BIGINT, parent_id BIGINT, derived_result_code TEXT, " - + "reference_test_unit_config TEXT, " - + "created_time TEXT, updated_time TEXT)"); - connection.commit(); - } - } - - private long seedMetadata(JSONObject metadata) { - for (String table : List.of("graphic_result_code", "graphic_config", "dashboard_result")) { - jdbc.update("DELETE FROM " + table); - } - records.clear(); - configs.clear(); - replayNodes.clear(); - seededGraphics.clear(); - seededBindings.clear(); - for (Object value : metadata.getJSONArray("dashboardResults")) { - DashboardResult result = withoutAudit(value).toBean(DashboardResult.class); - records.put(result.getId(), result); - } - for (Object value : metadata.getJSONArray("flowNodeConfigs")) { - FlowNodeConfig node = withoutAudit(value).toBean(FlowNodeConfig.class); - replayNodes.put(node.getId(), node); - } - Map replayDashboards = new LinkedHashMap<>(); - for (Object value : metadata.getJSONArray("dashboardConfigs")) { - DashboardConfig dashboard = withoutAudit(value).toBean(DashboardConfig.class); - replayDashboards.put(dashboard.getId(), dashboard); - } - when(dashboards.findById(any(Long.class))).thenAnswer(call -> replayDashboards.get(call.getArgument(0))); - when(dashboards.listByIds(any())).thenAnswer(call -> { - Collection ids = call.getArgument(0); - return ids.stream().map(replayDashboards::get).filter(Objects::nonNull).toList(); - }); - Map physicalByVch = new LinkedHashMap<>(); - for (Object value : metadata.getJSONArray("boxChannels")) { - JSONObject channel = JSONUtil.parseObj(value); - if (Objects.equals(channel.getInt("channelIo"), ChannelIOEnum.INPUT.getChannelIo())) { - physicalByVch.put(channel.getStr("vchId"), channel); - } - } - org.mockito.stubbing.Answer>> channelReader = call -> { - DashboardConfig dashboard = call.getArgument(0); - Set requestedNodes = call.getArgument(2); - return replayPhysicalChannels(dashboard, requestedNodes, physicalByVch); - }; - when(dashboards.getSimpleChannelList(any(DashboardConfig.class), isNull(), anySet(), isNull())) - .thenAnswer(channelReader); - when(dashboards.getSimpleChannelList(any(DashboardConfig.class), isNull(), anySet(), isNull(), anyList(), anyMap())) - .thenAnswer(channelReader); - seedResults(); - for (Object value : metadata.getJSONArray("graphicConfigs")) { - GraphicConfig graphic = withoutAudit(value).toBean(GraphicConfig.class); - assertEquals(1, graphicMapper.insert(graphic)); - seededGraphics.put(graphic.getId(), graphic); - } - for (Object value : metadata.getJSONArray("graphicResultCodes")) { - GraphicResultCode binding = withoutAudit(value).toBean(GraphicResultCode.class); - assertTrue(seededGraphics.containsKey(binding.getGraphicConfigId())); - assertEquals(1, bindingMapper.insert(binding)); - seededBindings.put(binding.getId(), binding); - } - return metadata.getJSONObject("project").getLong("id"); - } - - private JSONObject withoutAudit(Object value) { - JSONObject copy = JSONUtil.parseObj(JSONUtil.toJsonStr(value)); - copy.remove("createdTime"); - copy.remove("updatedTime"); - return copy; - } - - private Map> replayPhysicalChannels( - DashboardConfig dashboard, Set nodeIds, Map physicalByVch) { - Map> channels = new LinkedHashMap<>(); - DashboardItemConfigDto item = JSONUtil.toBean(dashboard.getHandlerConfig(), DashboardItemConfigDto.class); - SequenceConfigDto sequence = item == null ? null : item.getSequenceConfig(); - if (sequence == null || sequence.getBoxList() == null) { - return channels; - } - for (Long nodeId : nodeIds) { - FlowNodeConfig node = replayNodes.get(nodeId); - if (node == null || PostProcessingNodeConfigDto.COMPONENT_ID.equals(node.getComponentId())) { - continue; - } - List nodeChannels = new ArrayList<>(); - for (int boxIndex = 0; boxIndex < sequence.getBoxList().size(); boxIndex++) { - BoxChannelConfigItem box = sequence.getBoxList().get(boxIndex); - for (ThreadChannelMappingDto mapping : box.getInputThreadChannelMappings()) { - JSONObject physical = physicalByVch.get(mapping.getVchId()); - if (physical == null) { - continue; - } - SimpleChannelDto channel = new SimpleChannelDto(); - channel.setVchId(mapping.getVchId()); - channel.setSourceVchId(mapping.getVchId()); - channel.setThreadChannelAliasId(mapping.getThreadChannelAliasId()); - channel.setThreadChannelAlias(mapping.getThreadChannelAlias()); - channel.setChannelType(physical.getInt("channelType")); - channel.setChannelSource(physical.getInt("channelSource")); - channel.setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - channel.setSamplingRate(physical.getInt("samplingRate")); - channel.setNodeId(nodeId); - channel.setBoxName(String.valueOf(boxIndex)); - nodeChannels.add(channel); - } - } - channels.put(DashboardConfigService.buildNodeScopedSimpleChannelKey( - ChannelIOEnum.INPUT.getChannelIo(), nodeId), nodeChannels); - } - return channels; - } - - - private void chain(boolean reverse) { - records.clear(); - configs.clear(); - PostProcessingNodeConfigDto range = new PostProcessingNodeConfigDto(); - range.setMethod("specifyDataPoints"); - range.getConfigs().getSpecifyDataPoints().setMode("range"); - source(range.ensurePrimaryCalculation().ensureSourceA(), SOURCE, null); - range.ensurePrimaryCalculation().setOutputChannelAliasId("auto"); - configs.put(nodeId(RANGE), range); - configs.put(nodeId(ADD), arithmetic(ADD, RANGE, false)); - configs.put(nodeId(SUBTRACT), arithmetic(SUBTRACT, ADD, true)); - for (long id : reverse ? List.of(SUBTRACT, ADD, RANGE, SOURCE) : List.of(SOURCE, RANGE, ADD, SUBTRACT)) { - DashboardResult entity = new DashboardResult(); - entity.setId(id); - entity.setProjectId(PROJECT_ID); - entity.setDashboardId(DASHBOARD_ID); - entity.setNodeId(nodeId(id)); - entity.setBoxId("0"); - entity.setPrimaryCode(id == SOURCE ? "RMS Level" : "Imported result " + id); - entity.setAlgorithmCode(id == SOURCE ? "FFT" : PostProcessingNodeConfigDto.COMPONENT_ID); - entity.setResultType(id == SOURCE ? ResultTypeEnum.FREQUENCY.getType() : ResultTypeEnum.POST_ANALYSIS_DATA.getType()); - entity.setCustomUnitX("Hz"); - entity.setCustomUnitY(id == SOURCE ? "dBSPL" : "stale-unit"); - records.put(id, entity); - } - } - - private PostProcessingNodeConfigDto arithmetic(long id, long upstream, boolean subtract) { - PostProcessingNodeConfigDto config = new PostProcessingNodeConfigDto(); - config.setNewDataSetName("Imported result " + id); - config.setOperation(subtract ? "subtract" : "add"); - for (int channel = 1; channel <= 4; channel++) { - PostProcessingNodeConfigDto.CalculationConfig calculation = new PostProcessingNodeConfigDto.CalculationConfig(); - source(calculation.ensureSourceA(), upstream, "input-ch" + channel); - if (subtract) { - source(calculation.ensureSourceB(), upstream, "input-ch" + (channel % 4 + 1)); - } else { - calculation.ensureSourceB().setSourceType("constant"); - calculation.ensureSourceB().setConstantValue((double) channel); - } - calculation.setOutputChannelAliasId("input-ch" + channel); - config.getCalculations().add(calculation); - } - return config; - } - - private void source(PostProcessingNodeConfigDto.ResultSourceConfig source, long resultId, String alias) { - source.setSourceType("previousNode"); - source.setResultSetId(resultId); - source.setNodeId(nodeId(resultId)); - source.setSelectionScope(alias == null ? "resultSet" : "channel"); - source.setThreadChannelAliasId(alias); - } - - private FlowNodeConfig nodeEntity(Long id) { - if (replayNodes.containsKey(id)) { - return replayNodes.get(id); - } - if (!Objects.equals(id, nodeId(SOURCE)) && !configs.containsKey(id)) { - return null; - } - FlowNodeConfig node = new FlowNodeConfig(); - node.setId(id); - node.setGraphId(21L); - node.setProjectId(PROJECT_ID); - if (Objects.equals(id, nodeId(SOURCE))) { - SignalAnalysisNodeConfigDto sourceConfig = new SignalAnalysisNodeConfigDto(); - sourceConfig.setAlgorithmId("FFT"); - node.setComponentId(CryDaqConstant.COMPONENT_ID_SIGNAL_ANALYSIS); - node.setJsonConfig(NodeConfigCodec.serialize(sourceConfig)); - } else { - node.setComponentId(PostProcessingNodeConfigDto.COMPONENT_ID); - node.setJsonConfig(NodeConfigCodec.serialize(configs.get(id))); - } - return node; - } - - private long nodeId(long resultId) { - return resultId + 1000; - } - - private DashboardConfig dashboard() { - List mappings = new ArrayList<>(); - for (int channel = 1; channel <= 4; channel++) { - ThreadChannelMappingDto mapping = new ThreadChannelMappingDto(); - mapping.setVchId("Ch" + channel); - mapping.setThreadChannelAliasId("input-ch" + channel); - mapping.setThreadChannelAlias("Input " + channel); - mappings.add(mapping); - } - BoxChannelConfigItem box = new BoxChannelConfigItem(); - box.setInputThreadChannelMappings(mappings); - SequenceConfigDto sequence = new SequenceConfigDto(); - sequence.setBoxList(List.of(box)); - DashboardItemConfigDto item = new DashboardItemConfigDto(); - item.setSequenceConfig(sequence); - DashboardConfig dashboard = new DashboardConfig(); - dashboard.setId(DASHBOARD_ID); - dashboard.setProjectId(PROJECT_ID); - dashboard.setProviderId(BuiltinDashboardProvider.SEQUENCE.getProviderId()); - dashboard.setHandlerConfig(JSONUtil.toJsonStr(item)); - return dashboard; - } - - private List physicalChannels() { - List channels = new ArrayList<>(); - for (int index = 1; index <= 4; index++) { - SimpleChannelDto channel = new SimpleChannelDto(); - channel.setVchId("Ch" + index); - channel.setThreadChannelAliasId("input-ch" + index); - channel.setThreadChannelAlias("Input " + index); - channel.setChannelIo(ChannelIOEnum.INPUT.getChannelIo()); - channel.setChannelType(ChannelTypeEnum.AUDIO.getValue()); - channel.setBoxName("0"); - channel.setNodeId(nodeId(SOURCE)); - channel.setChannelUnitY("dBSPL"); - channels.add(channel); - } - return channels; - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceDependencyGraphTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceDependencyGraphTest.java index a4c60ea0..5948b535 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceDependencyGraphTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceDependencyGraphTest.java @@ -2,11 +2,14 @@ import com.opentest.service.FileService; import jakarta.annotation.Resource; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayDeque; @@ -103,7 +106,11 @@ static Set> reachable(Class start, Map, Set>> grap report.append("ROOT ").append(root.getName()).append(" closure=").append(closure.size()).append(" component=").append(component.size()).append('\n'); for (Class member : component) report.append(" ").append(member.getName()).append('\n'); } - var baseline = FileServiceStartupComponentTest.component(); + Set> baseline = new LinkedHashSet<>(); + try (var input = FileServiceDependencyGraphTest.class.getResourceAsStream("/spring/file-service-startup-component.txt"); + var lines = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) { + for (String name : lines.lines().toList()) baseline.add(Class.forName(name)); + } report.append("CONSTRUCTOR CYCLE EDGES\n"); for (Class owner : baseline) for (Dependency dependency : dependencies(owner)) if (!dependency.lazy() && dependency.point().startsWith("constructor ") && baseline.contains(dependency.type()) && reachable(dependency.type(), graph).contains(owner)) diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java deleted file mode 100644 index 510fc835..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/dashboard/framework/FileServiceStartupComponentTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.opentest.service.dashboard.framework; - -import com.opentest.service.FileService; -import java.beans.Introspector; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.test.util.ReflectionTestUtils; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockingDetails; - -class FileServiceStartupComponentTest { - private static List> startupComponent; - private static List> mockBoundaries; - - static List> roots() { - return List.of(FileService.class, ProviderGraphicUnitService.class, MeasurementEdits.class, MeasurementInputBindings.class); - } - - static List> entries() throws Exception { - Set> result = new LinkedHashSet<>(roots()); - Set actualStartup = Set.of("AuthService", "AuthV2Service", "DeviceAuthorizationService", "DeviceService", - "AudioJNIBridgeService", "ChannelService", "ProjectService", "FlowNodeConfigService", "BoxChannelService", "DashboardConfigService"); - for (Class type : component()) { - if (actualStartup.contains(type.getSimpleName())) result.add(type); - } - return List.copyOf(result); - } - - static Set> component() throws Exception { - Set> result = new LinkedHashSet<>(); - try (var input = FileServiceStartupComponentTest.class.getResourceAsStream("/spring/file-service-startup-component.txt"); - var lines = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) { - for (String name : lines.lines().toList()) result.add(Class.forName(name)); - } - return result; - } - - @BeforeAll - static void inspectStartupComponent() throws Exception { - Set> component = component(); - var graph = FileServiceDependencyGraphTest.graph(FileServiceDependencyGraphTest.components()); - for (Class root : roots()) { - var closure = FileServiceDependencyGraphTest.reachable(root, graph); - for (Class reachable : closure) if (FileServiceDependencyGraphTest.reachable(reachable, graph).contains(root)) - assertTrue(component.contains(reachable), "New real cycle member must not be mocked: " + reachable.getName()); - } - Set> boundaries = new LinkedHashSet<>(); - for (Class type : component) for (var dependency : FileServiceDependencyGraphTest.dependencies(type)) { - Class boundary = dependency.type(); - if (component.stream().noneMatch(boundary::isAssignableFrom)) boundaries.add(boundary); - } - boundaries.remove(ApplicationContext.class); boundaries.remove(ObjectProvider.class); - Files.write(Path.of("target/file-service-mocked-boundaries.txt"), boundaries.stream().map(Class::getName).sorted().toList()); - startupComponent = List.copyOf(component); - mockBoundaries = List.copyOf(boundaries); - } - - @ParameterizedTest(name = "complete startup component from {0}") - @MethodSource("entries") - void createsEveryRealBeanInThePreviouslyMissedComponent(Class first) throws Exception { - List> component = startupComponent; - try (var context = new AnnotationConfigApplicationContext()) { - context.setAllowCircularReferences(true); - context.registerBean(first); - for (Class type : component) if (type != first) context.registerBean(type); - for (Class boundary : mockBoundaries) context.getBeanFactory().registerSingleton( - Introspector.decapitalize(boundary.getSimpleName()), mock(boundary)); - context.refresh(); - var exports = (ObjectProvider) ReflectionTestUtils.getField(context.getBean(FileService.class), "savedProviderTabularExports"); - assertSame(context.getBean(SavedProviderTabularExports.class), exports.getObject()); - assertFalse(mockingDetails(exports.getObject()).isMock()); - assertFalse(mockingDetails(context.getBean(SavedProviderExecutionReader.class)).isMock()); - for (Class type : component) assertFalse(mockingDetails(context.getBean(type)).isMock(), type.getName()); - } - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/deviceplugin/DevicePluginManagementTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/deviceplugin/DevicePluginManagementTest.java deleted file mode 100644 index 81e1c78f..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/deviceplugin/DevicePluginManagementTest.java +++ /dev/null @@ -1,791 +0,0 @@ -package com.opentest.service.deviceplugin; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.opentest.service.deviceplugin.DevicePluginModel.Source; -import com.opentest.service.deviceplugin.DevicePluginModel.Phase; -import com.opentest.service.deviceplugin.DevicePluginModel.State; -import com.opentest.service.deviceplugin.DevicePluginModel.Installed; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.lang.reflect.Field; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.attribute.FileTime; -import java.time.Duration; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.zip.ZipEntry; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; -import java.util.zip.ZipOutputStream; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.mockito.Mockito; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DevicePluginManagementTest { - private static final String PLUGIN_DIRECTORY = System.getProperty("os.name", "").startsWith("Windows") ? "plugins" : "Plugs"; - @TempDir Path temp; - final List managers = new ArrayList<>(); - @org.junit.jupiter.api.BeforeEach void canonicalizeTempRoot() throws IOException { temp = temp.toRealPath(); } - @AfterEach void stop() { managers.forEach(DevicePluginManagement::stopObserving); } - private DevicePluginManagement open(FakeHost host) throws Exception { return open("state", host); } - private DevicePluginManagement open(String name, FakeHost host) throws Exception { - return open(name, host, Duration.ofMinutes(2)); - } - private DevicePluginManagement open(String name, FakeHost host, Duration limit) throws Exception { - Files.createDirectories(temp.resolve(name)); Files.createDirectories(temp.resolve("distribution")); - Path nativeRoot = name.equals("state") ? temp.resolve("distribution") : Files.createDirectories(temp.resolve(name).resolve("native")); - DevicePluginManagement manager = new DevicePluginManagement(temp.resolve(name).toRealPath(), nativeRoot.toRealPath(), host, limit); - managers.add(manager); manager.startManagedSession("test-instance", () -> 0); return manager; - } - private ObjectNode envelope(DevicePluginManagement manager, String action) { - return DevicePluginJson.object().put("action", action).put("requestId", UUID.randomUUID().toString()) - .put("expectedGeneration", manager.snapshot(true).path("generation").asLong()); - } - private ObjectNode install(DevicePluginManagement manager, String id, String version, String bytes) throws Exception { - ObjectNode preview = manager.preview(new ByteArrayInputStream(zip(id, version, bytes))); - return manager.command(envelope(manager, "install").put("previewId", preview.path("previewId").asText()).put("trusted", true).put("enabled", true)); - } - private JsonNode row(DevicePluginManagement manager, String id) { - for (JsonNode item : manager.snapshot(true).path("packages")) if (item.path("packageId").asText().equals(id)) return item; - throw new AssertionError("Missing package " + id); - } - private Path active(String id) { return temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices").resolve(id).resolve("driver.dll"); } - - @Test void runtimeUsesNativeDirectoryAndKeepsItsManagementStateAcrossRestart() throws Exception { - Path nativeRoot = Files.createDirectories(temp.resolve("native/windows-amd64")); - Path devices = nativeRoot.resolve("" + PLUGIN_DIRECTORY + "/devices"); - Path stateRoot = Files.createDirectories(devices.resolve(".opentest")); - Path unrelated = devices.resolve("manual-not-registered.txt"); Files.writeString(unrelated, "keep"); - writePackage(devices.resolve("unregistered"), "unregistered", "1", "manual"); - FakeHost host = new FakeHost(); - var manager = new DevicePluginManagement(stateRoot, nativeRoot, host, Duration.ofSeconds(10)); - managers.add(manager); manager.startManagedSession("canonical-directory", () -> 0); - assertEquals(nativeRoot, host.root); - install(manager, "fixture", "1", "A"); - assertEquals(devices.resolve("fixture/driver.dll").toString(), row(manager, "fixture").path("entryPath").asText()); - assertEquals("A", Files.readString(devices.resolve("fixture/driver.dll"))); - assertTrue(Files.isRegularFile(stateRoot.resolve("device-state.json"))); - assertEquals("keep", Files.readString(unrelated)); - assertEquals("manual", Files.readString(devices.resolve("unregistered/driver.dll"))); - assertFalse(Files.exists(stateRoot.resolve("active"))); - manager.previewPackage(devices.resolve("unregistered")); - manager.close(); - var reopened = new DevicePluginManagement(stateRoot, nativeRoot, new FakeHost(), Duration.ofSeconds(10)); - managers.add(reopened); reopened.startManagedSession("canonical-directory-restart", () -> 0); - assertEquals("active", row(reopened, "fixture").path("status").asText()); - assertEquals("A", Files.readString(devices.resolve("fixture/driver.dll"))); - assertEquals("manual", Files.readString(devices.resolve("unregistered/driver.dll"))); - } - - @Test void audioMetadataIsStrictAndRegistryKeepsOtherKinds() throws Exception { - Path devices = Files.createDirectories(temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices")); - ObjectNode registry = DevicePluginJson.object().put("schemaVersion", 1); - ObjectNode foreign = registry.putArray("plugins").addObject().put("packageId", "future.camera").put("pluginKind", "camera-device") - .put("entry", "future.camera/camera.dll").put("enabled", true).put("opaque", "keep"); - DevicePluginJson.write(devices.resolve("plugins.json"), registry); - var manager = open(new FakeHost()); - var preview = manager.preview(new ByteArrayInputStream(zip("fixture", null, "A"))); - assertEquals("audio-device", preview.path("pluginKind").asText()); - assertEquals(manager.snapshot(true).path("generation"), preview.path("generation")); - manager.command(envelope(manager, "install").put("previewId", preview.path("previewId").asText()).put("trusted", true).put("enabled", true)); - manager.close(); open(new FakeHost()); - JsonNode saved = DevicePluginJson.JSON.readTree(Files.readAllBytes(temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices/plugins.json"))); - assertTrue(saved.path("plugins").findValuesAsText("pluginKind").contains("audio-device")); - assertEquals(foreign, saved.path("plugins").get(0)); - assertEquals("A", Files.readString(active("fixture"))); - Path invalid = Files.createDirectories(temp.resolve("unsupported")); - var legacy = manifest("unsupported", null); legacy.remove("pluginKind"); legacy.put("pluginType", "device"); - DevicePluginJson.write(invalid.resolve("plugin-package.json"), legacy); Files.writeString(invalid.resolve("driver.dll"), "B"); - assertThrows(DevicePluginFailure.class, () -> new DevicePackageStore(temp.resolve("strict"), temp.resolve("strict/active")).retain(invalid, Source.USER)); - legacy.put("pluginKind", "audio-device"); DevicePluginJson.write(invalid.resolve("plugin-package.json"), legacy); - assertThrows(DevicePluginFailure.class, () -> new DevicePackageStore(temp.resolve("strict"), temp.resolve("strict/active")).retain(invalid, Source.USER)); - } - - @Test void foreignIdentityCollisionsCannotOverwriteFilesDuringPreviewCommandOrRestart() throws Exception { - var manager = open(new FakeHost()); - var preview = manager.preview(new ByteArrayInputStream(zip("fixture", null, "A"))); - Path activeRoot = temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices"); - var foreign = foreignRegistry(activeRoot); - assertThrows(DevicePluginFailure.class, () -> manager.preview(new ByteArrayInputStream(zip("fixture", null, "A")))); - assertThrows(DevicePluginFailure.class, () -> manager.command(envelope(manager, "install") - .put("previewId", preview.path("previewId").asText()).put("trusted", true).put("enabled", true))); - assertEquals("FOREIGN", Files.readString(activeRoot.resolve("fixture/driver.dll"))); - assertEquals(foreign, DevicePluginJson.JSON.readTree(Files.readAllBytes(activeRoot.resolve("plugins.json")))); - var queued = open("queued", new FakeHost()); install(queued, "fixture", null, "A"); queued.close(); - Path queuedRoot = temp.resolve("queued/native/" + PLUGIN_DIRECTORY + "/devices"); var queuedForeign = foreignRegistry(queuedRoot); - assertThrows(DevicePluginFailure.class, () -> open("queued", new FakeHost())); - assertEquals("FOREIGN", Files.readString(queuedRoot.resolve("fixture/driver.dll"))); - assertEquals(queuedForeign, DevicePluginJson.JSON.readTree(Files.readAllBytes(queuedRoot.resolve("plugins.json")))); - } - private ObjectNode foreignRegistry(Path root) throws IOException { - Files.createDirectories(root.resolve("fixture")); Files.writeString(root.resolve("fixture/driver.dll"), "FOREIGN"); - var registry = DevicePluginJson.object().put("schemaVersion", 1); - registry.putArray("plugins").addObject().put("packageId", "FiXtUrE").put("pluginKind", "camera-device") - .put("entry", "FiXtUrE/driver.dll").put("enabled", true); - DevicePluginJson.write(root.resolve("plugins.json"), registry); return registry; - } - @Test void foreignKindCannotBeRemovedByAnAudioOperation() throws Exception { - Path material = temp.resolve("guarded/active"); Path devices = material.resolve("" + PLUGIN_DIRECTORY + "/devices"); - var registry = foreignRegistry(devices); - var store = new DevicePackageStore(temp.resolve("guarded"), material); - assertThrows(DevicePluginFailure.class, () -> store.remove("fixture")); - assertEquals("FOREIGN", Files.readString(devices.resolve("fixture/driver.dll"))); - assertEquals(registry, DevicePluginJson.JSON.readTree(Files.readAllBytes(devices.resolve("plugins.json")))); - } - - @Test void newInstallBecomesActiveWithoutRestart() throws Exception { - FakeHost host = new FakeHost(); - DevicePluginManagement manager = open(host); - ObjectNode receipt = install(manager, "fixture", "1", "A"); - assertTrue(Files.exists(active("fixture")), "New installation must publish its native files immediately"); - manager.refreshRuntime(); - assertEquals(0, manager.snapshot(true).path("pendingCount").asInt()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - assertEquals("active", row(manager, "fixture").path("status").asText()); - assertEquals(0, row(manager, "fixture").path("deviceCount").asInt()); - assertEquals("succeeded", manager.operation(receipt.path("operationId").asText()).path("status").asText()); - assertEquals(List.of("fixture"), host.loadCalls); - assertEquals(true, host.decisions.get("fixture")); - } - - @Test void uninstallImmediatelyUnloadsAndRemovesThePackageWithoutRestart() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "fixture", "1", "A"); - var receipt = manager.command(envelope(manager, "uninstall").put("packageId", "fixture")); - assertEquals("succeeded", receipt.path("status").asText()); - assertEquals("native-fixture", host.unloadedId); - assertEquals(0, manager.snapshot(true).path("installedCount").asInt()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - assertFalse(Files.exists(active("fixture"))); - manager.close(); - assertEquals(0, open(new FakeHost()).snapshot(true).path("installedCount").asInt()); - } - - @Test void reinstallAfterSuccessfulUninstallBecomesActiveInTheSameSession() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "fixture", "1", "A"); - for (int round = 0; round < 2; round++) { - assertEquals("succeeded", manager.command(envelope(manager, "uninstall").put("packageId", "fixture")).path("status").asText()); - var receipt = install(manager, "fixture", "1", "A"); - manager.refreshRuntime(); - assertEquals("succeeded", manager.operation(receipt.path("operationId").asText()).path("status").asText()); - assertEquals("active", row(manager, "fixture").path("status").asText()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - assertTrue(Files.exists(active("fixture"))); - } - assertEquals(List.of("fixture", "fixture", "fixture"), host.loadCalls); - } - - @Test void failedOrTimedOutReinstallDoesNotBreakHealthyPluginManagement() throws Exception { - for (String content : List.of("F", "S")) { - FakeHost host = new FakeHost(); var manager = open("reinstall-" + content, host, Duration.ofMillis(20)); - install(manager, "good", "1", "G"); install(manager, "fixture", "1", "A"); - manager.command(envelope(manager, "uninstall").put("packageId", "fixture")); - host.failContent = "F"; host.initializingContent = "S"; - var receipt = install(manager, "fixture", "1", content); - Thread.sleep(30); manager.refreshRuntime(); - assertEquals("failed", manager.operation(receipt.path("operationId").asText()).path("status").asText()); - assertEquals("active", row(manager, "good").path("status").asText()); - assertEquals("succeeded", manager.command(envelope(manager, "enabled").put("packageId", "good").put("enabled", false)).path("status").asText()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - } - } - - @Test void disablingAndEnablingUseNativeControlsWithoutReplacingFilesOrRestarting() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "fixture", "1", "A"); - FileTime marker = FileTime.fromMillis(12345000); - Files.setLastModifiedTime(active("fixture"), marker); - assertEquals("succeeded", manager.command(envelope(manager, "enabled").put("packageId", "fixture").put("enabled", false)).path("status").asText()); - assertEquals("disabled", row(manager, "fixture").path("status").asText()); - assertEquals("succeeded", manager.command(envelope(manager, "enabled").put("packageId", "fixture").put("enabled", true)).path("status").asText()); - assertEquals("active", row(manager, "fixture").path("status").asText()); - assertEquals(List.of("native-fixture:false", "native-fixture:true"), host.enabledCalls); - assertEquals(marker, Files.getLastModifiedTime(active("fixture"))); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - } - - @Test void interruptedAndPreviouslyRestoredUninstallsKeepTheOriginalRemovalIntent() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "fixture", "1", "A"); host.failUnload = true; - var receipt = manager.command(envelope(manager, "uninstall").put("packageId", "fixture")); manager.close(); - Path file = temp.resolve("state/device-state.json"); - ObjectNode state = (ObjectNode) DevicePluginJson.JSON.readTree(Files.readAllBytes(file)); - ObjectNode change = (ObjectNode) state.path("pending").path("fixture").deepCopy(); - ((ObjectNode) state.path("pending")).remove("fixture"); - ((ObjectNode) state.path("attempt").path("items")).putObject("fixture").put("phase", "PREPARED").set("change", change); - ((ObjectNode) state.path("operations").path(receipt.path("operationId").asText())).putNull("error"); - DevicePluginJson.write(file, state); - var recovered = open(new FakeHost()); - assertEquals(0, recovered.snapshot(true).path("installedCount").asInt()); - assertEquals("succeeded", recovered.operation(receipt.path("operationId").asText()).path("status").asText()); recovered.close(); - - ObjectNode installed = (ObjectNode) change.path("before").deepCopy(); - ((ObjectNode) state.path("installed")).set("fixture", installed); - ObjectNode operation = (ObjectNode) state.path("operations").path(receipt.path("operationId").asText()); - operation.put("status", "failed").putObject("error").put("code", "DEVICE_STARTUP_INTERRUPTED").put("message", "interrupted") - .putObject("details").putObject("recovery").put("status", "restored").put("digest", installed.path("content").path("digest").asText()).put("enabled", true); - ObjectNode restoration = ((ObjectNode) state.path("attempt").path("items")).putObject("fixture").put("phase", "SETTLED").putObject("change"); - restoration.put("operationId", UUID.randomUUID().toString()).put("action", "restore").set("before", installed); restoration.set("target", installed.deepCopy()); - DevicePluginJson.write(file, state); - assertEquals(0, open(new FakeHost()).snapshot(true).path("installedCount").asInt()); - } - - @Test void asynchronousEnableFailureRetainsEnableIntentInsteadOfRestoringDisabledState() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "fixture", "1", "A"); - manager.command(envelope(manager, "enabled").put("packageId", "fixture").put("enabled", false)); - host.initializingContent = "A"; - var receipt = manager.command(envelope(manager, "enabled").put("packageId", "fixture").put("enabled", true)); - assertEquals("pending", receipt.path("status").asText()); - host.failContent = "A"; manager.refreshRuntime(); - assertEquals("enable", row(manager, "fixture").path("pendingType").asText()); - assertTrue(manager.snapshot(true).path("requiresRestart").asBoolean()); - assertEquals("pending", manager.operation(receipt.path("operationId").asText()).path("status").asText()); - var persisted = DevicePluginJson.JSON.readTree(Files.readAllBytes(temp.resolve("state/device-state.json"))); - assertFalse(persisted.path("restores").has("fixture")); - assertTrue(persisted.path("pending").path("fixture").path("target").path("enabled").asBoolean()); - } - - @Test void anUnchangedRuntimePackageIsReusedButTamperedOrExtraFilesAreReplaced() throws Exception { - Path source = Files.createDirectories(temp.resolve("source")); writePackage(source, "fixture", "1", "A"); - Path root = temp.resolve("store"), material = root.resolve("active"); - var store = new DevicePackageStore(root, material); - var ref = store.retain(source, Source.USER); - var installed = new Installed(ref, true); - store.materialize(installed); - Path dll = store.entry(ref); FileTime marker = FileTime.fromMillis(12345000); - Files.setLastModifiedTime(dll, marker); store.materialize(installed); - assertEquals(marker, Files.getLastModifiedTime(dll)); - Files.writeString(dll, "tampered"); store.materialize(installed); assertEquals("A", Files.readString(dll)); - Path extra = dll.resolveSibling("extra.dll"); Files.writeString(extra, "extra"); store.materialize(installed); - assertFalse(Files.exists(extra)); - } - - @Test void newInstallWaitsForReadinessWithoutRestartAndDoesNotReplayLoad() throws Exception { - FakeHost host = new FakeHost(); host.initializingContent = "S"; - var manager = open(host); - var preview = manager.preview(new ByteArrayInputStream(zip("fixture", "1", "S"))); - var command = envelope(manager, "install").put("previewId", preview.path("previewId").asText()).put("trusted", true).put("enabled", true); - var receipt = manager.command(command); - assertEquals("pending", receipt.path("status").asText()); - assertEquals("starting", row(manager, "fixture").path("status").asText()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - assertEquals(receipt, manager.command(command)); - assertFalse(host.decisions.containsKey("fixture")); - host.initializingContent = null; manager.refreshRuntime(); - assertEquals("active", row(manager, "fixture").path("status").asText()); - assertEquals("succeeded", manager.command(command).path("status").asText()); - assertEquals(List.of("fixture"), host.loadCalls); - } - - @Test void newInstallHasItsOwnInitializationDeadlineAfterBackendHasBeenRunning() throws Exception { - FakeHost host = new FakeHost(); host.initializingContent = "S"; - var manager = open("state", host, Duration.ofMillis(250)); - Thread.sleep(350); - install(manager, "fixture", "1", "S"); - assertEquals("starting", row(manager, "fixture").path("status").asText()); - Thread.sleep(350); manager.refreshRuntime(); - assertEquals("failed", row(manager, "fixture").path("status").asText()); - assertEquals("DEVICE_INITIALIZATION_TIMEOUT", row(manager, "fixture").path("lastError").path("code").asText()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - } - - @Test void disabledNewInstallIsRegisteredWithoutLoading() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - var preview = manager.preview(new ByteArrayInputStream(zip("fixture", "1", "A"))); - var receipt = manager.command(envelope(manager, "install").put("previewId", preview.path("previewId").asText()).put("trusted", true).put("enabled", false)); - assertEquals("succeeded", receipt.path("status").asText()); - assertEquals("disabled", row(manager, "fixture").path("status").asText()); - assertTrue(Files.exists(active("fixture"))); - assertTrue(host.loadCalls.isEmpty()); - assertFalse(manager.snapshot(true).path("requiresRestart").asBoolean()); - } - - @Test void materializationTimeDoesNotConsumeTheNewPluginsInitializationDeadline() throws Exception { - FakeHost host = new FakeHost(); host.initializingContent = "S"; - var manager = open("state", host, Duration.ofMillis(250)); - var store = Mockito.spy(new DevicePackageStore(temp.resolve("state"), temp.resolve("distribution"))); - Mockito.doAnswer(invocation -> { - Thread.sleep(350); - return invocation.callRealMethod(); - }).when(store).materialize(Mockito.any(Installed.class)); - Field packages = DevicePluginManagement.class.getDeclaredField("packages"); packages.setAccessible(true); packages.set(manager, store); - install(manager, "fixture", "1", "S"); - assertEquals("starting", row(manager, "fixture").path("status").asText()); - assertFalse(host.decisions.containsKey("fixture")); - } - - @Test void loadFailureDoesNotSettleAbsentPackageOrBreakHealthyPluginAndCannotReplayAfterCleanup() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "good", "1", "A"); host.failLoadId = "bad"; - var receipt = install(manager, "bad", "1", "B"); - assertEquals("failed", receipt.path("status").asText()); - assertEquals("DEVICE_LOAD_FAILED", receipt.path("error").path("code").asText()); - assertFalse(host.decisions.containsKey("bad")); - manager.refreshRuntime(); - assertEquals("active", row(manager, "good").path("status").asText()); - assertEquals("ready", manager.snapshot(true).path("status").asText()); - manager.command(envelope(manager, "cleanup").put("packageId", "bad")); - install(manager, "bad", "1", "C"); - assertEquals("pending-install", row(manager, "bad").path("status").asText()); - assertEquals(List.of("good", "bad"), host.loadCalls); - } - - @Test void liveInstallOnlySucceedsAfterAdmissionAndAdmissionRetryDoesNotReload() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); host.rejectAdmission = true; - var receipt = install(manager, "fixture", "1", "A"); - assertEquals("pending", receipt.path("status").asText()); - assertFalse(row(manager, "fixture").path("admitted").asBoolean()); - assertEquals(0, manager.snapshot(true).path("installedCount").asInt()); - host.rejectAdmission = false; manager.refreshRuntime(); - assertEquals("active", row(manager, "fixture").path("status").asText()); - assertEquals("succeeded", manager.operation(receipt.path("operationId").asText()).path("status").asText()); - assertEquals(List.of("fixture"), host.loadCalls); - } - - @Test void loadResponseLossRejectsAndCleansUpTheWorkerWithoutReplayingLoad() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); - install(manager, "good", "1", "A"); - host.failAfterRegister = "bad"; host.unloadCompleted = new CountDownLatch(1); - var receipt = install(manager, "bad", "1", "B"); - assertEquals("failed", receipt.path("status").asText()); - manager.refreshRuntime(); - assertTrue(host.unloadCompleted.await(2, TimeUnit.SECONDS)); - assertEquals("native-bad", host.unloadedId); - assertEquals(false, host.decisions.get("bad")); - assertEquals("active", row(manager, "good").path("status").asText()); - assertEquals(List.of("good", "bad"), host.loadCalls); - } - - @Test void publicationCommitFailureDoesNotObserveOrAdmitAnUndispatchedCandidate() throws Exception { - FakeHost host = new FakeHost(); var manager = open("state", host, Duration.ZERO); - install(manager, "good", "1", "A"); - DeviceStateStore store = Mockito.spy(new DeviceStateStore(temp.resolve("state"))); - Mockito.doAnswer(invocation -> { - State next = invocation.getArgument(0); - var work = next.attempt().items().get("bad"); - if (work != null && work.phase() == Phase.FILES_APPLIED) throw new IOException("simulated state commit failure"); - return invocation.callRealMethod(); - }).when(store).write(Mockito.any(State.class)); - Field states = DevicePluginManagement.class.getDeclaredField("states"); states.setAccessible(true); states.set(manager, store); - assertThrows(IOException.class, () -> install(manager, "bad", "1", "B")); - assertTrue(Files.exists(active("bad"))); - assertEquals(List.of("good"), host.loadCalls); - manager.refreshRuntime(); - assertEquals("ready", manager.snapshot(true).path("status").asText()); - assertEquals("active", row(manager, "good").path("status").asText()); - assertFalse(host.decisions.containsKey("bad")); - manager.close(); - var recovered = open(new FakeHost()); - assertEquals("DEVICE_STARTUP_INTERRUPTED", row(recovered, "bad").path("lastError").path("code").asText()); - } - - @Test void interruptedLiveLoadRetainsRecoverableWorkWithoutClaimingSuccess() throws Exception { - FakeHost host = new FakeHost(); var manager = open(host); host.abortLoad = true; - assertThrows(AssertionError.class, () -> install(manager, "fixture", "1", "A")); - assertEquals(0, manager.snapshot(true).path("installedCount").asInt()); - manager.close(); - var recovered = open(new FakeHost()); - assertEquals("failed", row(recovered, "fixture").path("status").asText()); - assertEquals("DEVICE_STARTUP_INTERRUPTED", row(recovered, "fixture").path("lastError").path("code").asText()); - assertEquals(0, recovered.snapshot(true).path("installedCount").asInt()); - } - - @Test void replacementOnlyStagesAndUndoSurvivesRestart() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); - install(manager, "fixture", "1", "A"); - ObjectNode receipt = install(manager, "fixture", "2", "B"); - assertEquals("A", Files.readString(active("fixture"))); - assertEquals(1, manager.snapshot(true).path("installedCount").asInt()); - assertEquals("replace", row(manager, "fixture").path("pendingType").asText()); - manager.command(envelope(manager, "undo").put("packageId", "fixture")); - assertEquals("cancelled", manager.operation(receipt.path("operationId").asText()).path("status").asText()); - manager.close(); - DevicePluginManagement reopened = open(new FakeHost()); - assertEquals(1, reopened.snapshot(true).path("installedCount").asInt()); - assertEquals("A", Files.readString(active("fixture"))); - } - - @Test void zeroDevicesIsInstalledAndRequestsAreIdempotentBeforeGenerationCheck() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); - ObjectNode preview = manager.preview(new ByteArrayInputStream(zip("fixture", null, "A"))); - ObjectNode command = envelope(manager, "install").put("previewId", preview.path("previewId").asText()).put("trusted", true).put("enabled", true); - ObjectNode receipt = manager.command(command); - assertEquals(receipt, manager.command(command)); - assertThrows(DevicePluginFailure.class, () -> manager.command(command.deepCopy().put("enabled", false))); - manager.close(); - DevicePluginManagement reopened = open(new FakeHost()); - assertEquals(1, reopened.snapshot(true).path("installedCount").asInt()); - assertEquals("active", row(reopened, "fixture").path("status").asText()); - assertEquals(0, row(reopened, "fixture").path("deviceCount").asInt()); - assertEquals("succeeded", reopened.request(command.path("requestId").asText()).path("status").asText()); - } - - @Test void sameVersionReplacementFailureKeepsIdentityAndRestoresExactBytesNextRestart() throws Exception { - DevicePluginManagement initial = open(new FakeHost()); install(initial, "fixture", "1.0", "A"); initial.close(); - DevicePluginManagement installed = open(new FakeHost()); String digest = row(installed, "fixture").path("installedDigest").asText(); - ObjectNode replacement = install(installed, "fixture", "1.0", "B"); - assertEquals("A", Files.readString(active("fixture"))); installed.close(); - FakeHost failing = new FakeHost(); failing.failContent = "B"; - DevicePluginManagement failed = open(failing); - assertEquals("B", Files.readString(active("fixture"))); - assertEquals(digest, row(failed, "fixture").path("installedDigest").asText()); - assertEquals("restore-next-start", row(failed, "fixture").path("status").asText()); - assertEquals(false, failing.decisions.get("fixture")); - assertThrows(DevicePluginFailure.class, () -> install(failed, "fixture", "0.5", "C")); - failed.close(); - DevicePluginManagement restored = open(new FakeHost()); - assertEquals("A", Files.readString(active("fixture"))); - assertEquals(digest, row(restored, "fixture").path("installedDigest").asText()); - assertFalse(restored.snapshot(true).path("requiresRestart").asBoolean()); - ObjectNode recoveredReceipt = restored.request(replacement.path("requestId").asText()); - assertEquals("failed", recoveredReceipt.path("status").asText()); - assertEquals("restored", recoveredReceipt.path("error").path("details").path("recovery").path("status").asText()); - assertEquals(digest, recoveredReceipt.path("error").path("details").path("recovery").path("digest").asText()); - assertTrue(recoveredReceipt.path("error").path("details").path("recovery").path("enabled").asBoolean()); - install(restored, "fixture", "0.5", "C"); restored.close(); - DevicePluginManagement downgraded = open(new FakeHost()); - assertEquals("C", Files.readString(active("fixture"))); - assertNotEquals(digest, row(downgraded, "fixture").path("installedDigest").asText()); - } - - @Test void firstFailureRetainsCandidateAndRetryOnlyAppliesOnRestart() throws Exception { - FakeHost bad = new FakeHost(); bad.failContent = "B"; - DevicePluginManagement failed = open(bad); - install(failed, "fixture", "1", "B"); - assertEquals(0, failed.snapshot(true).path("installedCount").asInt()); - assertTrue(row(failed, "fixture").path("hasFailedCandidate").asBoolean()); - failed.command(envelope(failed, "retry").put("packageId", "fixture")); - assertEquals(0, failed.snapshot(true).path("installedCount").asInt()); failed.close(); - DevicePluginManagement recovered = open(new FakeHost()); - assertEquals(1, recovered.snapshot(true).path("installedCount").asInt()); - assertFalse(row(recovered, "fixture").path("hasFailedCandidate").asBoolean()); - } - - @Test void cleanupFailureCandidateDoesNotRemoveHealthyInstallation() throws Exception { - FakeHost host = new FakeHost(); host.failContent = "B"; - DevicePluginManagement failed = open(host); - install(failed, "good", "1", "A"); install(failed, "bad", "1", "B"); - assertEquals(1, failed.snapshot(true).path("installedCount").asInt()); - assertEquals(true, host.decisions.get("good")); assertEquals(false, host.decisions.get("bad")); - String discardedDigest = row(failed, "bad").path("digest").asText(); - failed.command(envelope(failed, "cleanup").put("packageId", "bad")); - assertTrue(Files.exists(active("bad"))); - Path abandoned = temp.resolve("state/materialization/old-interrupted"); Files.createDirectories(abandoned); Files.writeString(abandoned.resolve("driver.dll"), "stale"); - assertEquals(0, failed.snapshot(true).path("failedCount").asInt()); - assertEquals("active", row(failed, "good").path("status").asText()); - failed.close(); - DevicePluginManagement next = open(new FakeHost()); - assertEquals(1, next.snapshot(true).path("installedCount").asInt()); - assertFalse(Files.exists(active("bad"))); - assertFalse(Files.exists(temp.resolve("state/content").resolve(discardedDigest))); - assertFalse(Files.exists(abandoned)); - } - - @Test void interruptedReplacementRestoresBeforeAndInterruptedFirstInstallIsNotInstalled() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); install(manager, "existing", "1", "A"); manager.close(); - FakeHost baseHost = new FakeHost(); - DevicePluginManagement base = open(baseHost); install(base, "existing", "1", "B"); - baseHost.abortLoad = true; - assertThrows(AssertionError.class, () -> install(base, "new", "1", "N")); base.close(); - FakeHost interruptedHost = new FakeHost(); - DevicePluginManagement interrupted = new DevicePluginManagement(temp.resolve("state").toRealPath(), temp.resolve("distribution").toRealPath(), interruptedHost, Duration.ofMinutes(2)); - managers.add(interrupted); - assertThrows(IllegalStateException.class, () -> interrupted.startManagedSession("test-instance", () -> { throw new IllegalStateException("simulated process exit after file publication"); })); - interrupted.close(); - DevicePluginManagement recovered = open(new FakeHost()); - assertEquals("A", Files.readString(active("existing"))); - assertEquals(1, recovered.snapshot(true).path("installedCount").asInt()); - assertTrue(row(recovered, "new").path("hasFailedCandidate").asBoolean()); - } - - @Test void legacyRegistrationIsImportedOnceAndDisabledReplacementStaysDisabled() throws Exception { - Path devices = temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices"); Files.createDirectories(devices.resolve("fixture")); - writePackage(devices.resolve("fixture"), "fixture", "1", "A"); - ObjectNode registry = DevicePluginJson.object().put("schemaVersion", 1); - registry.putArray("plugins").addObject().put("packageId", "fixture").put("pluginKind", "audio-device").put("entry", "fixture/driver.dll").put("enabled", false).put("bundled", true); - DevicePluginJson.write(devices.resolve("plugins.json"), registry); - DevicePluginManagement manager = open(new FakeHost()); - assertEquals("BUNDLED", row(manager, "fixture").path("source").asText()); - install(manager, "fixture", "0", "B"); manager.close(); - DevicePluginManagement replaced = open(new FakeHost()); - assertFalse(row(replaced, "fixture").path("enabled").asBoolean()); - assertEquals("disabled", row(replaced, "fixture").path("status").asText()); - replaced.command(envelope(replaced, "uninstall").put("packageId", "fixture")); replaced.close(); - DevicePluginManagement removed = open(new FakeHost()); - assertEquals(0, removed.snapshot(true).path("installedCount").asInt()); - assertFalse(Files.exists(devices.resolve("fixture")), "Uninstall removes the package from the actual native directory"); - removed.close(); assertEquals(0, open(new FakeHost()).snapshot(true).path("installedCount").asInt()); - } - - @Test void observationsRefreshWithoutChangingSettledInstallationAndSlowOptionalPackageDoesNotBlock() throws Exception { - FakeHost host = new FakeHost(); host.initializingContent = "S"; - DevicePluginManagement active = open(host); - install(active, "fixture", "1", "A"); install(active, "slow", "1", "S"); - assertEquals(1, active.snapshot(true).path("installedCount").asInt()); - assertEquals("starting", row(active, "slow").path("status").asText()); - host.deviceCount = 3; active.refreshRuntime(); - assertEquals(3, row(active, "fixture").path("deviceCount").asInt()); - host.failContent = "A"; active.refreshRuntime(); - assertEquals("failed", row(active, "fixture").path("status").asText()); - assertEquals(1, active.snapshot(true).path("installedCount").asInt()); - assertFalse(active.snapshot(true).path("requiresRestart").asBoolean()); - host.initializingContent = null; active.refreshRuntime(); - assertEquals(2, active.snapshot(true).path("installedCount").asInt()); - } - - @Test void infrastructureFailureIsUnknownAndCannotMasqueradeAsActiveOrUninstalled() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); install(manager, "fixture", "1", "A"); manager.close(); - FakeHost host = new FakeHost(); DevicePluginManagement active = open(host); - host.health = 4; - assertThrows(DevicePluginFailure.class, active::refreshRuntime); - assertEquals("unavailable", active.snapshot(true).path("status").asText()); - assertEquals("unavailable", row(active, "fixture").path("status").asText()); - assertEquals("unknown", row(active, "fixture").path("runtimeState").asText()); - assertFalse(row(active, "fixture").path("admitted").asBoolean()); - assertEquals(1, active.snapshot(true).path("installedCount").asInt()); - host.health = 2; active.refreshRuntime(); - assertEquals("active", row(active, "fixture").path("status").asText()); - } - - @Test void rejectedWorkerCleanupRunsAfterHealthyAdmissionAndDoesNotBlockStartup() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); - install(manager, "bad", "1", "B"); install(manager, "good", "1", "A"); manager.close(); - FakeHost host = new FakeHost(); host.failContent = "B"; - host.cleanupEntered = new CountDownLatch(1); host.cleanupRelease = new CountDownLatch(1); - DevicePluginManagement next = open(host); - try { - assertTrue(host.cleanupEntered.await(2, TimeUnit.SECONDS)); - assertEquals(true, host.decisions.get("good")); - assertEquals(false, host.decisions.get("bad")); - assertEquals("active", row(next, "good").path("status").asText()); - assertEquals("pending", row(next, "bad").path("cleanup").path("status").asText()); - } finally { host.cleanupRelease.countDown(); } - } - - @Test void corruptStateDoesNotSilentlyImportAgain() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); manager.close(); - Files.writeString(temp.resolve("state/device-state.json"), "{broken"); - assertThrows(IOException.class, () -> open(new FakeHost())); - assertEquals("{broken", Files.readString(temp.resolve("state/device-state.json"))); - } - - @Test void registryCapacityRejectsThe257thTargetWithoutChangingCommittedState() throws Exception { - prepareDisabledCatalog(254); - DevicePluginManagement manager = open(new FakeHost()); - install(manager, "new-one", "1", "N"); install(manager, "new-two", "1", "N"); - String before = Files.readString(temp.resolve("state/device-state.json")); - DevicePluginFailure failure = assertThrows(DevicePluginFailure.class, () -> install(manager, "new-three", "1", "N")); - assertEquals("DEVICE_PACKAGE_LIMIT", failure.code()); - assertEquals(before, Files.readString(temp.resolve("state/device-state.json"))); - assertEquals(0, manager.snapshot(true).path("pendingCount").asInt()); - assertEquals(256, manager.snapshot(true).path("installedCount").asInt()); - } - - @Test void initializingFirstInstallsReserveCapacityUntilTheyHaveSettled() throws Exception { - prepareDisabledCatalog(254); - FakeHost host = new FakeHost(); host.initializingContent = "S"; - DevicePluginManagement starting = open(host); - install(starting, "starting-one", "1", "S"); install(starting, "starting-two", "1", "S"); - assertEquals(254, starting.snapshot(true).path("installedCount").asInt()); - assertEquals(0, starting.snapshot(true).path("pendingCount").asInt()); - assertEquals("starting", row(starting, "starting-one").path("status").asText()); - String before = Files.readString(temp.resolve("state/device-state.json")); - DevicePluginFailure error = assertThrows(DevicePluginFailure.class, () -> install(starting, "third", "1", "N")); - assertEquals("DEVICE_PACKAGE_LIMIT", error.code()); - assertEquals(before, Files.readString(temp.resolve("state/device-state.json"))); - host.initializingContent = null; host.failContent = "S"; starting.refreshRuntime(); - install(starting, "third", "1", "N"); - assertEquals(0, starting.snapshot(true).path("pendingCount").asInt()); - assertEquals("active", row(starting, "third").path("status").asText()); - } - - @Test void syntacticallyValidButImpossibleStartupStateIsRejectedWithoutWriting() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); install(manager, "fixture", "1", "A"); manager.close(); - DevicePluginManagement initial = open(new FakeHost()); install(initial, "fixture", "1", "B"); initial.close(); - DevicePluginManagement interrupted = new DevicePluginManagement(temp.resolve("state").toRealPath(), temp.resolve("distribution").toRealPath(), new FakeHost(), Duration.ofMinutes(2)); - managers.add(interrupted); - assertThrows(IllegalStateException.class, () -> interrupted.startManagedSession("test", () -> { throw new IllegalStateException("exit"); })); - interrupted.close(); - Path file = temp.resolve("state/device-state.json"); - ObjectNode persisted = (ObjectNode) DevicePluginJson.JSON.readTree(Files.readAllBytes(file)); - ObjectNode change = (ObjectNode) persisted.path("attempt").path("items").path("fixture").path("change"); - change.putNull("before").putNull("target"); DevicePluginJson.write(file, persisted); - String corrupt = Files.readString(file); - assertThrows(IOException.class, () -> open(new FakeHost())); - assertEquals(corrupt, Files.readString(file)); - assertTrue(persisted.path("installed").has("fixture")); - } - - @Test void caseCollidingIdentitiesAreRejectedAcrossPendingInstalledAndStoredState() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); - install(manager, "foo", "1", "A"); - DevicePluginFailure pending = assertThrows(DevicePluginFailure.class, () -> install(manager, "Foo", "1", "B")); - assertEquals("DEVICE_PACKAGE_ID_COLLISION", pending.code()); - manager.close(); DevicePluginManagement installed = open(new FakeHost()); - assertThrows(DevicePluginFailure.class, () -> install(installed, "FOO", "1", "B")); - assertEquals("A", Files.readString(active("foo"))); - installed.close(); - Path file = temp.resolve("state/device-state.json"); - ObjectNode state = (ObjectNode) DevicePluginJson.JSON.readTree(Files.readAllBytes(file)); - ((ObjectNode) state.path("installed")).set("Foo", state.path("installed").path("foo").deepCopy()); - DevicePluginJson.write(file, state); - assertThrows(IOException.class, () -> open(new FakeHost())); - } - - @Test void windowsEntryCaseVariantCanBeInstalledDisabledWithoutLoading() throws Exception { - FakeHost host = new FakeHost(); - DevicePluginManagement manager = open(host); - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ZipOutputStream zip = new ZipOutputStream(bytes)) { - zip.putNextEntry(new ZipEntry("plugin-package.json")); - zip.write(DevicePluginJson.JSON.writeValueAsBytes(manifest("fixture", "1").put("entry", "DRIVER.dll"))); - zip.closeEntry(); - zip.putNextEntry(new ZipEntry("driver.dll")); zip.write("A".getBytes(StandardCharsets.UTF_8)); zip.closeEntry(); - } - ObjectNode preview = manager.preview(new ByteArrayInputStream(bytes.toByteArray())); - assertTrue(preview.path("valid").asBoolean()); - assertEquals("DRIVER.dll", preview.path("package").path("entry").asText()); - ObjectNode receipt = manager.command(envelope(manager, "install").put("previewId", preview.path("previewId").asText()) - .put("trusted", true).put("enabled", false)); - assertEquals("succeeded", receipt.path("status").asText()); - assertEquals(1, manager.snapshot(true).path("installedCount").asInt()); - assertTrue(Files.exists(active("fixture"))); - assertTrue(host.loadCalls.isEmpty()); - } - - @Test void invalidArchiveCannotWriteOutsidePreviewOrChangeRegistry() throws Exception { - DevicePluginManagement manager = open(new FakeHost()); - byte[] registry = Files.readAllBytes(temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices/plugins.json")); - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ZipOutputStream zip = new ZipOutputStream(bytes)) { zip.putNextEntry(new ZipEntry("../escape")); zip.write(1); zip.closeEntry(); } - assertThrows(DevicePluginFailure.class, () -> manager.preview(new ByteArrayInputStream(bytes.toByteArray()))); - assertEquals(new String(registry, StandardCharsets.UTF_8), Files.readString(temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices/plugins.json"))); - assertFalse(Files.exists(temp.resolve("state/escape"))); - } - - private void prepareDisabledCatalog(int count) throws IOException { - Path devices = temp.resolve("distribution/" + PLUGIN_DIRECTORY + "/devices"); Files.createDirectories(devices); - ObjectNode registry = DevicePluginJson.object().put("schemaVersion", 1); - var entries = registry.putArray("plugins"); - for (int index = 0; index < count; index++) { - String id = "fixture-" + index; Files.createDirectories(devices.resolve(id)); - writePackage(devices.resolve(id), id, "1", "A"); - entries.addObject().put("packageId", id).put("pluginKind", "audio-device").put("entry", id + "/driver.dll").put("enabled", false); - } - DevicePluginJson.write(devices.resolve("plugins.json"), registry); - } - - private static byte[] zip(String id, String version, String content) throws IOException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ZipOutputStream zip = new ZipOutputStream(bytes)) { - zip.putNextEntry(new ZipEntry("plugin-package.json")); zip.write(DevicePluginJson.JSON.writeValueAsBytes(manifest(id, version))); zip.closeEntry(); - zip.putNextEntry(new ZipEntry("driver.dll")); zip.write(content.getBytes(StandardCharsets.UTF_8)); zip.closeEntry(); - } - return bytes.toByteArray(); - } - private static ObjectNode manifest(String id, String version) { - ObjectNode manifest = DevicePluginJson.object().put("schemaVersion", 1).put("pluginKind", "audio-device").put("packageId", id).put("entry", "driver.dll"); - if (version != null) manifest.put("version", version); - manifest.putArray("files").add("driver.dll"); return manifest; - } - private static void writePackage(Path path, String id, String version, String content) throws IOException { - DevicePluginJson.write(path.resolve("plugin-package.json"), manifest(id, version)); Files.writeString(path.resolve("driver.dll"), content); - } - static final class FakeHost implements ManagedDeviceHost { - Path root; String failContent; String initializingContent; int deviceCount; int health = 2; - CountDownLatch cleanupEntered; CountDownLatch cleanupRelease; - final Map decisions = new HashMap<>(); - final Map currentDecisions = new HashMap<>(); - final List loadCalls = new ArrayList<>(); - final Set loaded = ConcurrentHashMap.newKeySet(); - String failLoadId; - String failAfterRegister; - CountDownLatch unloadCompleted; - volatile String unloadedId; - boolean bootstrapped; - boolean rejectAdmission; - boolean abortLoad; - boolean failUnload; - final List enabledCalls = new ArrayList<>(); - final Set inactive = ConcurrentHashMap.newKeySet(); - public ObjectNode setEnabled(String id, boolean enabled) { - enabledCalls.add(id + ":" + enabled); - String packageId = id.replaceFirst("^native-", ""); - if (enabled) inactive.remove(packageId); else inactive.add(packageId); - return DevicePluginJson.object(); - } - @Override public ObjectNode loadInstalled(String id) { - loadCalls.add(id); - if (abortLoad) throw new AssertionError("simulated process exit during load"); - if (id.equals(failLoadId)) throw new IllegalStateException("simulated load RPC failure"); - try { - boolean registered = false; - for (JsonNode entry : DevicePluginJson.JSON.readTree(Files.readAllBytes(root.resolve("" + PLUGIN_DIRECTORY + "/devices/plugins.json"))).path("plugins")) - if (entry.path("packageId").asText().equals(id) && entry.path("enabled").asBoolean() - && Files.isRegularFile(root.resolve("" + PLUGIN_DIRECTORY + "/devices").resolve(entry.path("entry").asText()))) registered = true; - if (!registered) throw new AssertionError("Load before materialization and registration"); - } catch (IOException error) { throw new IllegalStateException(error); } - loaded.add(id); - if (id.equals(failAfterRegister)) throw new IllegalStateException("simulated lost load response"); - return DevicePluginJson.object().put("loaded", id); - } - @Override public String prepare(String instanceId, String attemptId, Path materialRoot, Path stateRoot) { root = materialRoot; return "session"; } - @Override public ObjectNode snapshot() { - try { - ObjectNode snapshot = DevicePluginJson.object().put("processId", 42).put("owner", 1).put("health", health); - var plugins = snapshot.putArray("plugins"); snapshot.putArray("diagnostics"); - for (JsonNode entry : DevicePluginJson.JSON.readTree(Files.readAllBytes(root.resolve("" + PLUGIN_DIRECTORY + "/devices/plugins.json"))).path("plugins")) { - if (!entry.path("pluginKind").asText().equals("audio-device")) continue; - String id = entry.path("packageId").asText(); - if (!entry.path("enabled").asBoolean() && !loaded.contains(id)) continue; - if (!bootstrapped) loaded.add(id); - if (!loaded.contains(id)) continue; - Path path = root.resolve("" + PLUGIN_DIRECTORY + "/devices").resolve(entry.path("entry").asText()); String content = Files.readString(path); - int state = content.equals(failContent) ? 3 : content.equals(initializingContent) ? 4 : inactive.contains(id) ? 2 : 1; - plugins.addObject().put("pluginId", "native-" + entry.path("packageId").asText()).put("dllPath", path.toString()).put("state", state).put("deviceCount", deviceCount); - } - bootstrapped = true; - return snapshot; - } catch (IOException error) { throw new IllegalStateException(error); } - } - @Override public void settle(String token, String attempt, String id, boolean allowed) { - if (!loaded.contains(id)) throw new AssertionError("Admission for absent package"); - if (rejectAdmission) throw new IllegalStateException("simulated admission RPC failure"); - Boolean previous = currentDecisions.putIfAbsent(id, allowed); - if (previous != null && previous != allowed) throw new AssertionError("Admission changed"); - decisions.put(id, allowed); - } - @Override public ObjectNode unload(String id) { - if (failUnload) throw new IllegalStateException("simulated unload failure"); - if (cleanupEntered != null) { - if (!Boolean.TRUE.equals(decisions.get("good"))) throw new AssertionError("Cleanup preceded healthy admission"); - cleanupEntered.countDown(); - try { if (!cleanupRelease.await(5, TimeUnit.SECONDS)) throw new AssertionError("Cleanup not released"); } - catch (InterruptedException error) { Thread.currentThread().interrupt(); throw new IllegalStateException(error); } - } - String packageId = id.replaceFirst("^native-", ""); - loaded.remove(packageId); currentDecisions.remove(packageId); inactive.remove(packageId); - unloadedId = id; - if (unloadCompleted != null) unloadCompleted.countDown(); - return DevicePluginJson.object().put("unloaded", id); - } - @Override public void close(String token) { } - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java deleted file mode 100644 index 988bd4f5..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/liteflow/node/autosave/AutoSaveOrderedSchedulingSqliteTest.java +++ /dev/null @@ -1,613 +0,0 @@ -package com.opentest.service.liteflow.node.autosave; - -import com.baomidou.mybatisplus.core.MybatisConfiguration; -import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder; -import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; -import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; -import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; -import cn.hutool.json.JSONUtil; -import com.opentest.common.enums.AutoSaveExportJobStatus; -import com.opentest.common.enums.FlowExecStatusEnum; -import com.opentest.common.model.entity.AutoSaveExportJob; -import com.opentest.common.model.dto.FlowExecuteSnapshotDataDto; -import com.opentest.dal.mapper.sqlite.AutoSaveExportJobMapper; -import com.opentest.service.FlowExecuteResultService; -import com.opentest.service.liteflow.debug.runtime.DebugWorkspaceSnapshotService; -import com.opentest.service.liteflow.debug.runtime.SequenceContextRestoreService; -import com.opentest.service.liteflow.debug.runtime.snapshot.WorkspaceContextSnapshotDto; -import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import net.sf.jsqlparser.expression.Expression; -import net.sf.jsqlparser.expression.LongValue; - -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.sql.Connection; -import java.sql.Statement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.time.Duration; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** 使用真实迁移、SQLite 和 Mapper,验证任务领取边界与 Worker 实际文件追加次序。 */ -class AutoSaveOrderedSchedulingSqliteTest { - - private static final LocalDateTime NOW = LocalDateTime.of(2026, 10, 9, 12, 0); - - @TempDir - Path temporaryDirectory; - - private SqlSession session; - private AutoSaveExportJobMapper mapper; - private AutoSaveExportJobService service; - - @BeforeEach - void openIsolatedDatabase() throws Exception { - UnpooledDataSource dataSource = new UnpooledDataSource( - "org.sqlite.JDBC", "jdbc:sqlite:" + temporaryDirectory.resolve("queue.sqlite"), null, null); - try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { - connection.setAutoCommit(false); - statement.execute("CREATE TABLE flow_execute_result (id BIGINT PRIMARY KEY, tenant_id BIGINT, created_time TEXT)"); - executeMigration(statement, "V1.13.6__20260523_autosave_export_job.sql"); - executeMigration(statement, "V1.13.8__20260525_autosave_file_group_context.sql"); - executeMigration(statement, "V2.0.4_20260714201211__add_autosave_export_job_error_code.sql"); - executeMigration(statement, "V2.5.0_20261103140000__autosave_commit_receipts_and_source_order.sql"); - connection.commit(); - } - MybatisConfiguration configuration = new MybatisConfiguration(); - configuration.setMapUnderscoreToCamelCase(true); - configuration.setEnvironment(new Environment("sqlite", new JdbcTransactionFactory(), dataSource)); - configuration.addMapper(AutoSaveExportJobMapper.class); - session = new MybatisSqlSessionFactoryBuilder().build(configuration).openSession(true); - mapper = session.getMapper(AutoSaveExportJobMapper.class); - service = new AutoSaveExportJobService(mapper); - } - - @AfterEach - void closeDatabase() { - if (session != null) { - session.close(); - } - } - - @Test - void sourceRecordOrderWinsOverEnqueueOrderAndSurvivesSourceDeletion() throws Exception { - execute("INSERT INTO flow_execute_result VALUES (100, 1, '2026-10-09 10:00:00')," - + " (200, 1, '2026-10-09 10:01:00')"); - AutoSaveExportJob laterRecord = job(1, "result", 200); - AutoSaveExportJob earlierRecord = job(2, "result", 100); - service.freezeSourceOrder(Arrays.asList(laterRecord, earlierRecord)); - service.saveAll(Arrays.asList(laterRecord, earlierRecord)); - execute("DELETE FROM flow_execute_result"); - - assertEquals(Collections.singletonList(2L), readyIds()); - assertEquals(100L, mapper.selectById(2L).getSourceRecordId()); - assertEquals(LocalDateTime.of(2026, 10, 9, 10, 0), mapper.selectById(2L).getSourceRecordCreatedTime()); - } - - @Test - void futureRetryHeadAndExportingHeadBlockOnlyTheirNormalizedTarget() { - AutoSaveExportJob waiting = job(1, "D:\\DATA\\result.xlsx", 100); - waiting.setStatus(AutoSaveExportJobStatus.RETRY_WAITING.name()); - waiting.setNextRetryTime(NOW.plusDays(1)); - insert(waiting, job(2, "d:/data/result.xlsx", 200), job(3, "other.xlsx", 300)); - - assertEquals(Collections.singletonList(3L), readyIds()); - assertFalse(service.claimForExport(mapper.selectById(2L), "worker", "later", NOW)); - executeUnchecked("UPDATE autosave_export_job SET status='EXPORTING', next_retry_time=NULL WHERE id=1"); - assertEquals(Collections.singletonList(3L), readyIds()); - assertFalse(service.claimForExport(mapper.selectById(2L), "worker", "later", NOW)); - } - - @Test - void tenantsRemainIndependentAndPartialSuccessDoesNotOccupyTheHead() { - AutoSaveExportJob firstTenant = job(1, "result", 100); - firstTenant.setStatus("PARTIALLY_EXPORTED"); - AutoSaveExportJob secondTenant = job(3, "result", 100); - secondTenant.setTenantId(2L); - insert(firstTenant, job(2, "result", 200), secondTenant); - - assertEquals(Arrays.asList(2L, 3L), readyIds()); - assertTrue(service.claimForExport(mapper.selectById(2L), "one", "attempt-one", NOW)); - assertTrue(service.claimForExport(mapper.selectById(3L), "two", "attempt-two", NOW)); - } - - @Test - void configuredTenantInterceptorParsesAndExecutesHeadSelectionAndAtomicClaim() { - insert(job(1, "result", 100)); - MybatisConfiguration tenantConfiguration = new MybatisConfiguration(); - tenantConfiguration.setMapUnderscoreToCamelCase(true); - tenantConfiguration.setEnvironment(session.getConfiguration().getEnvironment()); - MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); - interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { - @Override - public Expression getTenantId() { - return new LongValue(1L); - } - })); - tenantConfiguration.addInterceptor(interceptor); - tenantConfiguration.addMapper(AutoSaveExportJobMapper.class); - try (SqlSession tenantSession = new MybatisSqlSessionFactoryBuilder().build(tenantConfiguration).openSession(true)) { - AutoSaveExportJobService tenantService = new AutoSaveExportJobService( - tenantSession.getMapper(AutoSaveExportJobMapper.class)); - AutoSaveExportJob head = tenantService.listReadyJobs(NOW, 50).get(0); - assertTrue(tenantService.claimForExport(head, "tenant-worker", "tenant-attempt", NOW)); - tenantService.markExported(head, AutoSaveExecutionResult.success(Collections.emptyList()), NOW); - } - session.clearCache(); - assertEquals("EXPORTED", mapper.selectById(1L).getStatus()); - } - - @Test - void oldNullTenantJobsUseTheSameScopeWithoutBlockingOtherTenants() { - AutoSaveExportJob oldFirst = job(1, "result", 100); - AutoSaveExportJob oldSecond = job(2, "result", 200); - oldFirst.setTenantId(null); - oldSecond.setTenantId(null); - insert(oldFirst, oldSecond, job(3, "result", 300)); - - assertEquals(Arrays.asList(1L, 3L), readyIds()); - assertTrue(service.claimForExport(mapper.selectById(1L), "old-tenant", "old-attempt", NOW)); - assertFalse(service.claimForExport(mapper.selectById(2L), "old-tenant", "later", NOW)); - assertTrue(service.claimForExport(mapper.selectById(3L), "tenant-one", "independent", NOW)); - } - - @Test - void claimRechecksTheHeadAfterASelectionRaceAndChecksTenantIdentity() { - AutoSaveExportJob later = job(2, "result", 200); - insert(later); - AutoSaveExportJob selected = service.listReadyJobs(NOW, 50).get(0); - insert(job(1, "result", 100)); - - assertFalse(service.claimForExport(selected, "worker", "attempt", NOW)); - AutoSaveExportJob forgedTenant = mapper.selectById(1L); - forgedTenant.setTenantId(99L); - assertFalse(service.claimForExport(forgedTenant, "worker", "attempt", NOW)); - assertEquals("PENDING", mapper.selectById(1L).getStatus()); - } - - @Test - void obsoleteWorkerCannotOverwriteTheNewAttemptAfterLockRecovery() { - insert(job(1, "result", 100)); - AutoSaveExportJob previousAttempt = service.listReadyJobs(NOW, 50).get(0); - assertTrue(service.claimForExport(previousAttempt, "old-worker", "old-attempt", NOW.minusMinutes(20))); - assertEquals(1, service.recoverStaleExportingJobs(Duration.ofMinutes(10), NOW)); - AutoSaveExportJob newAttempt = service.listReadyJobs(NOW, 50).get(0); - assertTrue(service.claimForExport(newAttempt, "new-worker", "new-attempt", NOW)); - - assertThrows(AutoSaveJobPersistenceException.class, () -> service.markExported( - previousAttempt, AutoSaveExecutionResult.success(Collections.emptyList()), NOW)); - assertThrows(AutoSaveJobPersistenceException.class, () -> service.markWaitingSource( - previousAttempt, "old wait", NOW)); - assertThrows(AutoSaveJobPersistenceException.class, () -> service.markRetryWaiting( - previousAttempt, "old error", null, NOW)); - - AutoSaveExportJob persisted = mapper.selectById(1L); - assertEquals("EXPORTING", persisted.getStatus()); - assertEquals("new-worker", persisted.getLockedBy()); - assertEquals("new-attempt", persisted.getAttemptId()); - assertEquals(0, persisted.getRetryCount()); - service.markExported(newAttempt, AutoSaveExecutionResult.success(Collections.emptyList()), NOW); - persisted = mapper.selectById(1L); - assertEquals("EXPORTED", persisted.getStatus()); - assertNull(persisted.getLockedBy()); - assertNull(persisted.getAttemptId()); - assertNull(persisted.getNextRetryTime()); - } - - @Test - void oldJobsWithoutAnyCreationTimeUseTaskIdForStableOrder() { - AutoSaveExportJob later = job(20, "result", 100); - AutoSaveExportJob earlier = job(10, "result", 200); - for (AutoSaveExportJob job : Arrays.asList(later, earlier)) { - job.setCreatedTime(null); - job.setSourceRecordCreatedTime(null); - job.setSourceRecordId(null); - } - insert(later, earlier); - - assertEquals(Collections.singletonList(10L), readyIds()); - assertTrue(service.claimForExport(earlier, "worker", "first", NOW)); - service.markExported(earlier, AutoSaveExecutionResult.success(Collections.emptyList()), NOW); - assertEquals(Collections.singletonList(20L), readyIds()); - } - - @Test - void sameRoundAdvancesTheTargetAndWritesRealXlsxRowsInFrozenRecordThenTaskOrder() throws Exception { - AutoSaveExportJob laterRecord = job(1, "result", 200); - AutoSaveExportJob firstInEarlierRecord = job(2, "result", 100); - AutoSaveExportJob secondInEarlierRecord = job(3, "result", 100); - firstInEarlierRecord.setCreatedTime(NOW.minusMinutes(2)); - secondInEarlierRecord.setCreatedTime(NOW.minusMinutes(1)); - insert(laterRecord, secondInEarlierRecord, firstInEarlierRecord); - Path output = temporaryDirectory.resolve("result.xlsx"); - AutoSaveExecutionService execution = mock(AutoSaveExecutionService.class); - when(execution.executeResolvedFileGroup(any(), any())).thenAnswer(invocation -> { - AutoSaveExportJob job = invocation.getArgument(0); - appendWorkbookRow(output, job.getId()); - return AutoSaveExecutionResult.success(Collections.singletonList(output)); - }); - - assertEquals(3, worker(execution, readyRebuildService(), 50).processOnce()); - - try (InputStream input = Files.newInputStream(output); XSSFWorkbook workbook = new XSSFWorkbook(input)) { - Sheet sheet = workbook.getSheetAt(0); - List rowIds = new ArrayList<>(); - sheet.forEach(row -> rowIds.add((long) row.getCell(0).getNumericCellValue())); - assertEquals(Arrays.asList(2L, 3L, 1L), rowIds); - } - } - - @Test - void sourceWaitingDoesNotRetryInTheSameRoundOrBlockOtherTargets() { - insert(job(1, "a", 100), job(2, "a", 200), job(3, "b", 300), job(4, "b", 400)); - AutoSaveRecordRebuildService rebuild = readyRebuildService(); - when(rebuild.rebuild(any(AutoSaveExportJob.class), any())).thenAnswer(invocation -> { - AutoSaveExportJob job = invocation.getArgument(0); - if (job.getId() == 1L) { - throw new AutoSaveSourceNotReadyException("active source"); - } - return AutoSaveExecutionRequest.builder().requestId(job.getRequestId()).build(); - }); - List writes = new ArrayList<>(); - - assertEquals(3, worker(recordingExecution(writes), rebuild, 50).processOnce()); - assertEquals(Arrays.asList(3L, 4L), writes); - assertEquals("WAITING_SOURCE", mapper.selectById(1L).getStatus()); - assertEquals(0, mapper.selectById(1L).getRetryCount()); - assertEquals("PENDING", mapper.selectById(2L).getStatus()); - } - - @Test - void retryBecomingImmediatelyDueStillConsumesOnlyOneAttemptInTheRound() { - AutoSaveExportJob job = job(1, "a", 100); - insert(job); - AutoSaveExecutionService execution = mock(AutoSaveExecutionService.class); - when(execution.executeResolvedFileGroup(any(), any())).thenReturn(AutoSaveExecutionResult.failure("retry")); - AutoSaveExportJobService alreadyDueRetryService = new AutoSaveExportJobService(mapper) { - @Override - public void markRetryWaiting(AutoSaveExportJob attempted, String message, - AutoSaveExecutionResult result, LocalDateTime now) { - super.markRetryWaiting(attempted, message, result, now.minusMinutes(1)); - } - }; - AutoSaveAsyncExportWorker worker = new AutoSaveAsyncExportWorker( - alreadyDueRetryService, readyRebuildService(), execution, "sqlite-worker", 50); - - assertEquals(1, worker.processOnce()); - assertEquals(1, mapper.selectById(1L).getRetryCount()); - } - - @Test - void eachTargetGetsATurnAndCursorContinuesAcrossFiftyTaskBatches() { - for (int target = 0; target < 60; target++) { - insert(job(target + 1, String.format("target-%02d", target), target + 100)); - insert(job(target + 101, String.format("target-%02d", target), target + 200)); - } - List writes = new ArrayList<>(); - AutoSaveAsyncExportWorker worker = worker(recordingExecution(writes), readyRebuildService(), 100); - - assertEquals(50, worker.processOnce()); - assertEquals(50, writes.size()); - assertEquals(1L, writes.get(0)); - assertEquals(50L, writes.get(49)); - assertEquals(50, worker.processOnce()); - assertEquals(Arrays.asList(51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L), writes.subList(50, 60)); - assertEquals(101L, writes.get(60)); - } - - @Test - void ordinaryFailuresUseTwoAndFourSecondBackoffsThenTheThirdAttemptTerminates() { - AutoSaveExportJob initial = job(1, "result", 100); - initial.setMaxRetryCount(null); - insert(initial); - AutoSaveExportJob first = mapper.selectById(1L); - assertTrue(service.claimForExport(first, "worker", "first", NOW)); - service.markRetryWaiting(first, "database temporarily unavailable", null, NOW); - AutoSaveExportJob persisted = mapper.selectById(1L); - assertEquals(1, persisted.getRetryCount()); - assertEquals(3, persisted.getMaxRetryCount()); - assertEquals(NOW.plusSeconds(2), persisted.getNextRetryTime()); - assertFalse(service.claimForExport(persisted, "worker", "early", NOW.plusSeconds(1))); - assertTrue(service.claimForExport(persisted, "worker", "second", NOW.plusSeconds(2))); - service.markRetryWaiting(persisted, "database temporarily unavailable", null, NOW.plusSeconds(2)); - persisted = mapper.selectById(1L); - assertEquals(2, persisted.getRetryCount()); - assertEquals(NOW.plusSeconds(6), persisted.getNextRetryTime()); - assertTrue(service.claimForExport(persisted, "worker", "third", NOW.plusSeconds(6))); - service.markRetryWaiting(persisted, "database temporarily unavailable", null, NOW.plusSeconds(6)); - persisted = mapper.selectById(1L); - assertEquals(3, persisted.getRetryCount()); - assertEquals("DEAD", persisted.getStatus()); - assertEquals("database temporarily unavailable", persisted.getErrorMessage()); - assertNull(persisted.getNextRetryTime()); - assertNull(persisted.getAttemptId()); - assertNull(persisted.getLockedBy()); - assertTrue(readyIds().isEmpty()); - } - - @Test - void exhaustedTaskRetainsTheAttemptCountAndNeverStartsAFourthWrite() { - AutoSaveExportJob exhausted = job(1, "result", 100); - exhausted.setRetryCount(3); - insert(exhausted, job(2, "result", 200)); - List writes = new ArrayList<>(); - - assertEquals(2, worker(recordingExecution(writes), readyRebuildService(), 50).processOnce()); - assertEquals(Collections.singletonList(2L), writes); - assertEquals(3, mapper.selectById(1L).getRetryCount()); - assertEquals("DEAD", mapper.selectById(1L).getStatus()); - } - - @Test - void thirdFailurePreservesWrittenOwnersAndMarksOnlyUnfinishedOwnersFailed() { - AutoSaveExportJob job = job(1, "result", 100); - job.setRetryCount(2); - job.setSavedFilesJson("[\"primary.xlsx\"]"); - job.setConfigurationResultsJson("[{\"threadIndex\":0,\"executionUnitIndex\":0,\"success\":true," - + "\"savedFiles\":[\"primary.xlsx\"]},{\"threadIndex\":0,\"executionUnitIndex\":1," - + "\"success\":false,\"permanentFailure\":false,\"reason\":\"backup unavailable\"}]"); - insert(job); - assertTrue(service.claimForExport(job, "worker", "third", NOW)); - - service.markRetryWaiting(job, "backup unavailable", AutoSaveExecutionResult.failure("backup unavailable"), NOW); - - AutoSaveExportJob persisted = mapper.selectById(1L); - assertEquals("PARTIALLY_EXPORTED", persisted.getStatus()); - assertEquals("[\"primary.xlsx\"]", persisted.getSavedFilesJson()); - assertTrue(JSONUtil.parseArray(persisted.getConfigurationResultsJson()).getJSONObject(0).getBool("success")); - assertTrue(JSONUtil.parseArray(persisted.getConfigurationResultsJson()).getJSONObject(1).getBool("permanentFailure")); - assertTrue(JSONUtil.parseArray(persisted.getConfigurationResultsJson()).getJSONObject(1) - .getStr("reason").contains("3 attempts")); - assertNull(persisted.getAttemptId()); - assertNull(persisted.getNextRetryTime()); - } - - @Test - void alreadyExhaustedCommittedTaskPreservesTheCountAndNewlyObservedOwnerFacts() { - AutoSaveExportJob job = job(1, "result", 100); - job.setRetryCount(3); - insert(job); - assertTrue(service.claimForExport(job, "worker", "observe", NOW)); - AutoSaveExecutionResult observed = AutoSaveExecutionResult.failure("required backup is not committed"); - observed.setSavedFiles(Collections.singletonList(Paths.get("primary.xlsx"))); - observed.setConfigurationResultsJson("[{\"success\":true,\"savedFiles\":[\"primary.xlsx\"]}," - + "{\"success\":false,\"permanentFailure\":false,\"reason\":\"backup pending\"}]"); - - service.markRetryLimitReached(job, observed, observed.getErrorMessage(), NOW); - - AutoSaveExportJob persisted = mapper.selectById(1L); - assertEquals(3, persisted.getRetryCount()); - assertEquals("PARTIALLY_EXPORTED", persisted.getStatus()); - assertTrue(persisted.getErrorMessage().contains("required backup is not committed")); - assertEquals("[\"primary.xlsx\"]", persisted.getSavedFilesJson()); - assertTrue(JSONUtil.parseArray(persisted.getConfigurationResultsJson()).getJSONObject(1).getBool("permanentFailure")); - assertNull(persisted.getAttemptId()); - assertNull(persisted.getNextRetryTime()); - } - - @Test - void emptySuccessfulSelectionDoesNotPretendThatAnotherFailedOwnerProducedPartialOutput() { - AutoSaveExportJob job = job(1, "result", 100); - job.setRetryCount(2); - job.setConfigurationResultsJson("[{\"success\":true,\"savedFiles\":[]},{\"success\":false}]"); - insert(job); - assertTrue(service.claimForExport(job, "worker", "third", NOW)); - - service.markRetryWaiting(job, "write failed", null, NOW); - - assertEquals("DEAD", mapper.selectById(1L).getStatus()); - } - - @Test - void malformedOwnerEvidenceIsPreservedWithoutLeavingAnExhaustedTaskExporting() { - AutoSaveExportJob job = job(1, "result", 100); - job.setRetryCount(2); - job.setConfigurationResultsJson("corrupt old evidence"); - insert(job); - assertTrue(service.claimForExport(job, "worker", "third", NOW)); - - service.markRetryWaiting(job, "file access failed", null, NOW); - - AutoSaveExportJob persisted = mapper.selectById(1L); - assertEquals("DEAD", persisted.getStatus()); - assertEquals("corrupt old evidence", persisted.getConfigurationResultsJson()); - assertTrue(persisted.getErrorMessage().contains("evidence is invalid")); - assertNull(persisted.getAttemptId()); - } - - @ParameterizedTest - @ValueSource(ints = {20, 50}) - void permanentlyMissingSourcesTerminateAndValidSuccessorWritesTheActualFile(int missingCount) throws Exception { - for (int index = 1; index <= missingCount; index++) { - insert(job(index, "result", index + 100)); - } - insert(job(missingCount + 1, "result", 1000)); - execute("INSERT INTO flow_execute_result VALUES (1000, 1, '2026-10-09 10:00:00')"); - FlowExecuteResultService source = mock(FlowExecuteResultService.class); - when(source.findStatusMapByIds(any())).thenReturn(Collections.emptyMap()); - when(source.loadAndHydrateSnapshotDataIfPresent(anyLong())).thenAnswer(invocation -> { - try (PreparedStatement query = session.getConnection().prepareStatement( - "SELECT id FROM flow_execute_result WHERE id = ?")) { - query.setLong(1, invocation.getArgument(0)); - try (ResultSet rows = query.executeQuery()) { - return rows.next() ? new FlowExecuteSnapshotDataDto() : null; - } - } - }); - DebugWorkspaceSnapshotService snapshots = mock(DebugWorkspaceSnapshotService.class); - when(snapshots.loadSnapshot(anyLong(), anyString())).thenReturn(new WorkspaceContextSnapshotDto()); - AutoSaveRecordRebuildService rebuild = new AutoSaveRecordRebuildService(source, snapshots, - mock(SequenceContextRestoreService.class)); - Path output = temporaryDirectory.resolve("recovered.xlsx"); - AutoSaveExecutionService execution = mock(AutoSaveExecutionService.class); - when(execution.executeResolvedFileGroup(any(), any())).thenAnswer(invocation -> { - AutoSaveExportJob job = invocation.getArgument(0); - appendWorkbookRow(output, job.getId()); - return AutoSaveExecutionResult.success(Collections.singletonList(output)); - }); - AutoSaveAsyncExportWorker worker = worker(execution, rebuild, 50); - - assertEquals(Math.min(missingCount + 1, 50), worker.processOnce()); - if (missingCount == 50) { - assertFalse(Files.exists(output)); - assertEquals(1, worker.processOnce()); - } - for (long id = 1; id <= missingCount; id++) { - AutoSaveExportJob missing = mapper.selectById(id); - assertEquals("DEAD", missing.getStatus()); - assertEquals(0, missing.getRetryCount()); - assertTrue(missing.getErrorMessage().contains("record or required snapshot is missing")); - assertNull(missing.getAttemptId()); - assertNull(missing.getNextRetryTime()); - } - assertEquals("EXPORTED", mapper.selectById((long) missingCount + 1).getStatus()); - try (InputStream input = Files.newInputStream(output); XSSFWorkbook workbook = new XSSFWorkbook(input)) { - assertEquals(1, workbook.getSheetAt(0).getPhysicalNumberOfRows()); - assertEquals(missingCount + 1, (long) workbook.getSheetAt(0).getRow(0).getCell(0).getNumericCellValue()); - } - } - - private AutoSaveExportJob job(long id, String target, long sourceRecordId) { - AutoSaveExportJob job = new AutoSaveExportJob(); - job.setId(id); - job.setTenantId(1L); - job.setRequestId(sourceRecordId); - job.setBoxId("box-a"); - job.setSourceRecordId(sourceRecordId); - job.setSourceRecordCreatedTime(NOW.minusHours(1)); - job.setCreatedTime(NOW.minusMinutes(1)); - job.setAutoSaveNodeId(1L); - job.setTriggerMode("SEQUENCE_ORDER"); - job.setTriggerTime(NOW.minusMinutes(1)); - job.setFileType("EXCEL"); - job.setTargetKey(target); - job.setWriterEngine("JAVA_POI"); - job.setFileGroupContextJson("{}"); - job.setConfigSnapshotJson("{\"fileType\":\"EXCEL\",\"path\":\"test-output\",\"fileNamePattern\":\"result\"}"); - job.setStatus("PENDING"); - job.setRetryCount(0); - job.setMaxRetryCount(10); - return job; - } - - private void insert(AutoSaveExportJob... jobs) { - for (AutoSaveExportJob job : jobs) { - assertEquals(1, mapper.insert(job)); - } - } - - private List readyIds() { - return service.listReadyJobs(NOW, 50).stream().map(AutoSaveExportJob::getId).collect(Collectors.toList()); - } - - private AutoSaveAsyncExportWorker worker(AutoSaveExecutionService execution, - AutoSaveRecordRebuildService rebuild, int limit) { - return new AutoSaveAsyncExportWorker(service, rebuild, execution, "sqlite-worker", limit); - } - - private AutoSaveRecordRebuildService readyRebuildService() { - AutoSaveRecordRebuildService rebuild = mock(AutoSaveRecordRebuildService.class); - when(rebuild.loadExecutionStatusMap(any())).thenAnswer(invocation -> { - List jobs = invocation.getArgument(0); - return jobs.stream().collect(Collectors.toMap(AutoSaveExportJob::getRequestId, - job -> FlowExecStatusEnum.EXECUTED.getValue(), (left, right) -> left)); - }); - when(rebuild.rebuild(any(AutoSaveExportJob.class), any())).thenAnswer(invocation -> { - AutoSaveExportJob job = invocation.getArgument(0); - return AutoSaveExecutionRequest.builder().requestId(job.getRequestId()).build(); - }); - return rebuild; - } - - private AutoSaveExecutionService recordingExecution(List writes) { - AutoSaveExecutionService execution = mock(AutoSaveExecutionService.class); - when(execution.executeResolvedFileGroup(any(), any())).thenAnswer(invocation -> { - AutoSaveExportJob job = invocation.getArgument(0); - writes.add(job.getId()); - return AutoSaveExecutionResult.success(Collections.emptyList()); - }); - return execution; - } - - private void appendWorkbookRow(Path output, long id) throws Exception { - XSSFWorkbook workbook; - if (Files.exists(output)) { - try (InputStream input = Files.newInputStream(output)) { - workbook = new XSSFWorkbook(input); - } - } else { - workbook = new XSSFWorkbook(); - workbook.createSheet("records"); - } - try (XSSFWorkbook closeable = workbook) { - Sheet sheet = workbook.getSheetAt(0); - sheet.createRow(sheet.getPhysicalNumberOfRows()).createCell(0).setCellValue(id); - try (OutputStream target = Files.newOutputStream(output)) { - workbook.write(target); - } - } - } - - private void execute(String sql) throws Exception { - try (Statement statement = session.getConnection().createStatement()) { - statement.execute(sql); - } - session.clearCache(); - } - - private void executeUnchecked(String sql) { - try { - execute(sql); - } catch (Exception exception) { - throw new AssertionError(exception); - } - } - - private void executeMigration(Statement statement, String name) throws Exception { - Path repository = Paths.get("").toAbsolutePath(); - while (repository != null && !Files.isDirectory(repository.resolve("apps/backend/crydaq-server"))) { - repository = repository.getParent(); - } - if (repository == null) { - throw new IllegalStateException("OpenTest migration directory was not found"); - } - String sql = new String(Files.readAllBytes(repository.resolve( - "apps/backend/crydaq-server/src/main/resources/db/migration/" + name)), StandardCharsets.UTF_8); - for (String command : sql.split(";")) { - if (!command.trim().isEmpty()) { - statement.execute(command); - } - } - } -} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutorTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutorTest.java deleted file mode 100644 index bcec6098..00000000 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutorTest.java +++ /dev/null @@ -1,990 +0,0 @@ -package com.opentest.service.recording.measurement; - -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.core.MybatisConfiguration; -import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.opentest.common.config.DashboardReplayConfig; -import com.opentest.common.enums.CalculateResultsEnum; -import com.opentest.common.enums.FileBusinessTypeEnum; -import com.opentest.common.enums.FileTypeEnum; -import com.opentest.common.model.dto.ChannelDto; -import com.opentest.common.model.dto.DashboardConfigDto; -import com.opentest.common.model.dto.FlowExecuteSnapshotDataDto; -import com.opentest.common.model.dto.MeasureReanalysisLineageDto; -import com.opentest.common.model.dto.StageArtifactManifestDto; -import com.opentest.common.model.dto.chart.BaseChartConfig; -import com.opentest.common.model.dto.dashboard.CollectConfigDto; -import com.opentest.common.model.dto.dashboard.DashboardItemConfigDto; -import com.opentest.common.model.dto.dashboard.SignalAnalysisConfigDto; -import com.opentest.common.model.entity.DashboardResult; -import com.opentest.common.model.entity.File; -import com.opentest.common.model.entity.FlowExecuteResult; -import com.opentest.common.model.entity.GraphicConfigSnapshot; -import com.opentest.common.model.entity.GraphicResultCode; -import com.opentest.common.utils.CryBusinessUtil; -import com.opentest.common.utils.ChartConfigStorageCodec; -import com.opentest.dal.mapper.sqlite.DashboardResultMapper; -import com.opentest.dal.mapper.sqlite.FileMapper; -import com.opentest.dal.mapper.sqlite.FlowExecuteResultMapper; -import com.opentest.dal.mapper.sqlite.GraphicConfigSnapshotMapper; -import com.opentest.dal.mapper.sqlite.GraphicResultCodeMapper; -import com.opentest.dashboard.api.capability.ExecutionResultDocuments; -import com.opentest.dashboard.api.capability.CaptureStageArtifacts.Key; -import com.opentest.dashboard.api.capability.ProjectTriggers; -import com.opentest.dashboard.api.capability.RelatedResultFiles; -import com.opentest.dashboard.api.capability.ResultOutputConsumer; -import com.opentest.dashboard.api.capability.SavedInputSnapshots; -import com.opentest.dashboard.api.capability.SavedResultCatalog; -import com.opentest.dashboard.api.capability.SavedMeasurementReanalysis; -import com.opentest.dashboard.api.channel.ChannelCatalog; -import com.opentest.dashboard.api.channel.ChannelDirection; -import com.opentest.dashboard.api.channel.ChannelMetadata; -import com.opentest.dashboard.api.definition.DomainType; -import com.opentest.dashboard.api.definition.GraphicType; -import com.opentest.dashboard.api.definition.ResultChannelDefinition; -import com.opentest.dashboard.api.definition.ResultSetDefinition; -import com.opentest.dashboard.api.definition.ResultType; -import com.opentest.dashboard.api.execution.ExecutionChannelFacts; -import com.opentest.dashboard.api.frame.ChannelFrame; -import com.opentest.dashboard.api.frame.ResultFrame; -import com.opentest.dashboard.api.frame.SourceSampleRange; -import com.opentest.dashboard.api.measurement.ExecutionPreparation; -import com.opentest.dashboard.api.measurement.TableDefinition; -import com.opentest.dashboard.api.measurement.TableResultIdentity; -import com.opentest.dashboard.api.measurement.editing.InputDeclaration; -import com.opentest.dashboard.api.measurement.recorded.RecordedInputMode; -import com.opentest.dashboard.api.pipeline.ProcessorPipelineBuilder; -import com.opentest.dashboard.api.pipeline.QueueOverflowPolicy; -import com.opentest.dashboard.api.pipeline.SinkProcessor; -import com.opentest.dashboard.api.pipeline.StartControlProcessor; -import com.opentest.dashboard.api.pipeline.StopControlProcessor; -import com.opentest.dashboard.api.unit.GraphicUnitResolution; -import com.opentest.service.FileService; -import com.opentest.service.VariablesService; -import com.opentest.service.component.ProjectDashboardComponent; -import com.opentest.service.dashboard.framework.DashboardOperationRegistry; -import com.opentest.service.dashboard.framework.MeasurementInputInterpreter; -import com.opentest.service.dashboard.framework.PluginDashboardConfigurationResolver; -import com.opentest.service.dashboard.framework.ProjectTriggerFacts; -import com.opentest.service.export.waveform.source.FrozenWaveformShard; -import com.opentest.service.export.waveform.source.FrozenWaveformSource; -import com.opentest.service.export.waveform.source.NioWaveformSourceReaderFactory; -import com.opentest.service.export.waveform.source.WaveformSourceType; -import com.opentest.service.grpc.core.RawInputArtifact; -import com.opentest.service.grpc.replay.DashboardReplayExecutionCoordinator; -import com.opentest.service.jni.UnitConversionService; -import com.opentest.service.plugin.PluginMeasureExecutor; -import com.opentest.service.plugin.BoundMeasurementExecution; -import com.opentest.service.plugin.MeasureProviderInputs; -import com.opentest.service.recording.RecordedWaveformPlans; -import com.opentest.service.virtualchannel.VirtualChannelTestSupport; -import java.io.IOException; -import java.lang.reflect.Modifier; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.LinkedHashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Optional; -import java.util.OptionalInt; -import java.util.concurrent.CancellationException; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import org.apache.ibatis.mapping.Environment; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; -import org.junit.jupiter.params.provider.ValueSource; -import org.mybatis.spring.SqlSessionTemplate; -import org.mybatis.spring.transaction.SpringManagedTransactionFactory; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.test.util.ReflectionTestUtils; -import org.sqlite.SQLiteDataSource; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyMap; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class MeasureReanalysisExecutorTest { - private static final String PROVIDER = "test:reanalysis"; - private static final String CHANNEL = "physical-1"; - private static final int RATE = 48000; - private static final int SAMPLES = 16391; - private static final long FIRST_SAMPLE = 123; - private static final List STAGES = List.of("background", "formal"); - private static final RecordedCallScope CALLER = new RecordedCallScope(1, 1, "session", Locale.ROOT); - @TempDir Path directory; - - @Test void publishesAChildOwnedWavIndexWithTheOriginalTimingAndLogicalFrameCount() throws Exception { - var fixture = fixture(); - Files.writeString(directory.resolve("raw.idx"), fixture.raw.toString().replace('\\', '/') + "|341.4791666666667|7\n"); - byte[] originalIndex = Files.readAllBytes(directory.resolve("raw.idx")); - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - - fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get); - - var copied = fixture.onlyFile(9, FileTypeEnum.WAV); - var rows = Files.readAllLines(fixture.indexPath(copied)); - assertEquals(List.of(fixture.path(copied).toString().replace('\\', '/') + "|341.4791666666667|7"), rows); - assertArrayEquals(originalIndex, Files.readAllBytes(directory.resolve("raw.idx"))); - assertEquals(source.getSnapshotData(), fixture.history(8).getSnapshotData()); - assertArrayEquals(expectedSamples(1), fixture.read(copied), 1e-12); - } - - @Test void rebindsTimestampedWavShardsInOrderWithoutInventingLogicalFrameCounts() throws Exception { - var fixture = fixture(); - var source = fixture.timestampedShards(); - byte[] originalIndex = Files.readAllBytes(directory.resolve("Measure #3_Ch1_Acquired Waveform.idx")); - var destination = fixture.destination(source, 9); - - fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get); - - var copied = fixture.onlyFile(9, FileTypeEnum.WAV); - assertEquals(2, copied.getShardsNo()); - Path parent = fixture.path(copied).getParent(); - Path first = parent.resolve("Measure #3_Ch1_Acquired Waveform_1_20261113160955.wav"); - Path second = parent.resolve("Measure #3_Ch1_Acquired Waveform_2_20261113160956.wav"); - assertEquals(List.of(first.toString().replace('\\', '/') + "|170.66666666666666|3", - second.toString().replace('\\', '/') + "|341.4791666666667|7"), - Files.readAllLines(fixture.indexPath(copied))); - assertTrue(Files.isRegularFile(first)); assertTrue(Files.isRegularFile(second)); - assertArrayEquals(originalIndex, Files.readAllBytes(directory.resolve("Measure #3_Ch1_Acquired Waveform.idx"))); - fixture.assertCompleteInput(); - } - - @ParameterizedTest - @ValueSource(strings = {"empty", "wrong-shard", "invalid-time", "invalid-frames", "partial", "reversed"}) - void invalidInputIndexesNeverPublishAChildOrMutateOriginals(String damage) throws Exception { - var fixture = fixture(); - var source = fixture.timestampedShards(); - Path index = directory.resolve("Measure #3_Ch1_Acquired Waveform.idx"); - var lines = Files.readAllLines(index); - switch (damage) { - case "empty" -> Files.writeString(index, ""); - case "wrong-shard" -> Files.writeString(index, directory.resolve("unrelated.wav") + "|1|1\n" + lines.getLast() + "\n"); - case "invalid-time" -> Files.writeString(index, lines.getFirst() + "\n" + lines.getLast().replace("341.4791666666667", "NaN") + "\n"); - case "invalid-frames" -> Files.writeString(index, lines.getFirst() + "\n" + lines.getLast().replace("|7", "|2") + "\n"); - case "partial" -> Files.writeString(index, lines.getFirst() + "\n"); - case "reversed" -> Files.writeString(index, lines.getLast() + "\n" + lines.getFirst() + "\n"); - default -> throw new AssertionError(damage); - } - String originalIndex = Files.exists(index) ? Files.readString(index) : null; - var destination = fixture.destination(source, 9); - String draft = destination.getSnapshotData(); - - assertThrows(IllegalArgumentException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - fixture.assertUnpublished(9, draft); - assertFalse(Files.exists(directory.resolve("1/9"))); - assertEquals(source.getSnapshotData(), fixture.history(8).getSnapshotData()); - assertEquals(originalIndex, Files.exists(index) ? Files.readString(index) : null); - verify(fixture.plugins, never()).bindMeasurement(anyString(), anyMap(), anyMap(), any(), any(), any()); - } - - @ParameterizedTest - @ValueSource(booleans = {false, true}) - void indexlessSavedWavsRemainRecalculableWithTruthfulDurationAndNoInventedFrameCount(boolean multishard) throws Exception { - var fixture = fixture(); - var source = multishard ? fixture.timestampedShards() : fixture.history(8); - if (multishard) Files.delete(directory.resolve("Measure #3_Ch1_Acquired Waveform.idx")); - var destination = fixture.destination(source, 9); - - fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get); - - var copied = fixture.onlyFile(9, FileTypeEnum.WAV); - var lines = Files.readAllLines(fixture.indexPath(copied)); - assertEquals(multishard ? 2 : 1, lines.size()); - double lastEnd = 0; - for (String line : lines) { - var fields = line.split("\\|", -1); - assertEquals(2, fields.length, "Absent original append-frame counts must remain absent"); - Path copiedShard = Path.of(fields[0]); - assertTrue(copiedShard.startsWith(fixture.path(copied).getParent())); - assertTrue(Files.isRegularFile(copiedShard)); - assertTrue(Double.parseDouble(fields[1]) >= lastEnd); - lastEnd = Double.parseDouble(fields[1]); - } - assertEquals(SAMPLES * 1000D / RATE, lastEnd, 1e-9); - fixture.assertCompleteInput(); - assertEquals(source.getSnapshotData(), fixture.history(8).getSnapshotData()); - } - - @Test void preservesLegacyTwoColumnIndexTimingWithoutAddingLogicalFrameCounts() throws Exception { - var fixture = fixture(); - Files.writeString(directory.resolve("raw.idx"), fixture.raw.toString().replace('\\', '/') + "|999.125\n"); - var source = fixture.history(8); - fixture.executor.execute(source, fixture.destination(source, 9), CALLER, fixture.cancelled::get); - var copied = fixture.onlyFile(9, FileTypeEnum.WAV); - assertEquals(List.of(fixture.path(copied).toString().replace('\\', '/') + "|999.125"), - Files.readAllLines(fixture.indexPath(copied))); - } - - @Test void readsTheFullOriginalThroughItsLastSampleAndPublishesIndependentFiles() throws Exception { - var fixture = fixture(); - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - String originalSnapshot = source.getSnapshotData(); - byte[] originalWaveform = Files.readAllBytes(fixture.raw); - - fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get); - - fixture.assertCompleteInput(); - assertEquals(512, ((Number) ((Map) fixture.boundConfigurations.getFirst() - .get("signalAnalysisConfig")).get("resolution")).intValue()); - var original = fixture.reanalysis.getFirst(); - assertEquals(8, original.sourceRequestId()); assertEquals(8, original.inputRequestId()); - assertEquals(128, ((Number) ((Map) original.originalConfiguration() - .get("signalAnalysisConfig")).get("resolution")).intValue()); - assertEquals(originalSnapshot, source.getSnapshotData()); - assertEquals(originalSnapshot, fixture.history(8).getSnapshotData()); - assertArrayEquals(originalWaveform, Files.readAllBytes(fixture.raw)); - var saved = FlowExecuteSnapshotDataDto.parseJson(fixture.history(9).getSnapshotData()); - assertEquals(directory.toString(), saved.getInternalStoragePath()); - assertEquals(512, ((Number) saved.getDashboardConfig().getDashboardItemConfig() - .getSignalAnalysisConfig().get("resolution")).intValue()); - assertNotNull(saved.getProviderExecutionResults().get(PROVIDER).get("summary")); - var raw = fixture.onlyFile(9, FileTypeEnum.WAV); - assertNotEquals(10L, raw.getId()); - assertEquals(9L, raw.getRequestId()); - assertEquals(rawBusinessId(9), raw.getBusinessId()); - Path copied = fixture.path(raw); - assertNotEquals(fixture.raw, copied); - assertFalse(Files.isSameFile(fixture.raw, copied)); - assertArrayEquals(originalWaveform, Files.readAllBytes(copied)); - assertArrayEquals(expectedSamples(1), fixture.read(raw), 1e-12); - var result = fixture.onlyFile(9, FileTypeEnum.CRYBIN); - assertEquals(9L, result.getRequestId()); - assertArrayEquals(expectedSamples(2), fixture.read(result), 1e-12); - assertEquals(1, fixture.count("dashboard_result", 9)); - assertEquals(1, fixture.count("graphic_config_snapshot", 9)); - assertEquals(1, fixture.jdbc.queryForObject("select count(*) from graphic_result_code binding " - + "join graphic_config_snapshot graphic on graphic.id=binding.graphic_config_id " - + "join dashboard_result result on result.id=binding.dashboard_result_id " - + "where graphic.request_id=9 and result.request_id=9", Integer.class)); - var binding = fixture.jdbc.queryForMap("select binding.channel_ids,binding.unit,binding.axis_key," - + "binding.auxiliary_axis_keys,graphic.chart_config,graphic.original from graphic_result_code binding " - + "join graphic_config_snapshot graphic on graphic.id=binding.graphic_config_id where graphic.request_id=9"); - assertEquals(CHANNEL, binding.get("channel_ids")); - var json = new ObjectMapper(); - var units = json.readTree((String) binding.get("unit")); - assertTrue(units.isArray()); assertEquals(1, units.size()); - assertEquals(1, units.get(0).path("channelIds").size()); - assertEquals(CHANNEL, units.get(0).path("channelIds").get(0).asText()); - assertEquals("V", units.get(0).path("unit").asText()); - String axisKey = (String) binding.get("axis_key"); - assertNotNull(axisKey); assertFalse(axisKey.isBlank()); - var chart = ChartConfigStorageCodec.decode((String) binding.get("chart_config"), BaseChartConfig.class); - assertTrue(chart.getYAxes().stream().anyMatch(axis -> axisKey.equals(axis.getAxisKey()) && "V".equals(axis.getUnit()))); - var display = json.readTree((String) binding.get("original")); - assertTrue(display.path("yAxesByKey").path(axisKey).isObject()); - if (binding.get("auxiliary_axis_keys") instanceof String auxiliaryJson) { - var auxiliary = json.readTree(auxiliaryJson); - assertTrue(auxiliary.isObject()); - auxiliary.elements().forEachRemaining(key -> { - assertTrue(key.isTextual()); - assertTrue(chart.getYAxes().stream().anyMatch(axis -> key.asText().equals(axis.getAxisKey()))); - }); - } - assertEquals(1, fixture.closed.get()); - } - - @Test void aChildCanBeReanalysedAfterItsParentRecordAndPhysicalFilesAreDeleted() throws Exception { - var fixture = fixture(); - var parent = fixture.history(8); - var snapshot = FlowExecuteSnapshotDataDto.parseJson(parent.getSnapshotData()); - var retired = new StageArtifactManifestDto(); retired.setProviderId(PROVIDER); - retired.setProjectId(1L); retired.setDashboardId(2L); retired.setTenantId(1L); retired.setRequestId(8L); - retired.setState("RETIRED"); retired.setArtifacts(List.of()); - snapshot.setProviderStageInputArtifacts(List.of(retired)); - parent.setSnapshotData(snapshot.toJson()); fixture.histories.updateById(parent); - fixture.executor.execute(parent, fixture.destination(parent, 9), CALLER, fixture.cancelled::get); - var child = fixture.history(9); - String childSnapshot = child.getSnapshotData(); - assertEquals(9L, FlowExecuteSnapshotDataDto.parseJson(childSnapshot) - .getProviderStageInputArtifacts().getFirst().getRequestId()); - Path childRaw = fixture.path(fixture.onlyFile(9, FileTypeEnum.WAV)); - byte[] childBytes = Files.readAllBytes(childRaw); - fixture.jdbc.update("delete from file where request_id=8"); - fixture.jdbc.update("delete from flow_execute_result where id=8"); - Files.delete(fixture.raw); - fixture.received.clear(); fixture.ranges.clear(); fixture.origins.clear(); - when(fixture.variables.getDefaultStoragePath()).thenReturn(directory.resolve("new-output-root").toString()); - - fixture.executor.execute(child, fixture.destination(child, 11), CALLER, fixture.cancelled::get); - - fixture.assertCompleteInput(); - assertEquals(childSnapshot, fixture.history(9).getSnapshotData()); - assertArrayEquals(childBytes, Files.readAllBytes(childRaw)); - var grandchildRaw = fixture.onlyFile(11, FileTypeEnum.WAV); - var grandchild = FlowExecuteSnapshotDataDto.parseJson(fixture.history(11).getSnapshotData()); - assertEquals(11L, grandchild.getProviderStageInputArtifacts().getFirst().getRequestId()); - Path nextRoot = Path.of(grandchild.getInternalStoragePath()); - assertEquals(directory.resolve("new-output-root"), nextRoot); - Path nextRaw = nextRoot.resolve(grandchildRaw.getFileUrl()); - assertFalse(Files.isSameFile(childRaw, nextRaw)); - assertArrayEquals(childBytes, Files.readAllBytes(nextRaw)); - assertEquals(rawBusinessId(11), grandchildRaw.getBusinessId()); - assertEquals(9, fixture.reanalysis.getLast().sourceRequestId()); - assertEquals(8, fixture.reanalysis.getLast().inputRequestId()); - assertEquals(2, fixture.closed.get()); - } - - @Test void providerFactsCopyBothCompleteStagesAndRemainReadableAfterParentDeletion() throws Exception { - var fixture = new Fixture(RecordedInputMode.PROVIDER_FACTS); - var source = fixture.history(8); - String originalSnapshot = source.getSnapshotData(); - assertFalse(Files.exists(fixture.raw)); - - fixture.executor.execute(source, fixture.destination(source, 9), CALLER, fixture.cancelled::get); - - assertTrue(fixture.received.isEmpty()); - assertEquals(originalSnapshot, fixture.history(8).getSnapshotData()); - var child = fixture.history(9); - String childJson = child.getSnapshotData(); - var childSnapshot = FlowExecuteSnapshotDataDto.parseJson(child.getSnapshotData()); - assertEquals(FlowExecuteSnapshotDataDto.parseJson(originalSnapshot).getProviderExecutionInputs(), - childSnapshot.getProviderExecutionInputs()); - assertEquals(2, childSnapshot.getProviderStageInputArtifacts().size()); - assertEquals(3, fixture.count("file", 9)); - for (var manifest : childSnapshot.getProviderStageInputArtifacts()) { - assertEquals(9L, manifest.getRequestId()); - var artifact = manifest.getArtifacts().getFirst(); - var row = fixture.fileRows.selectById(artifact.getFileId()); - assertEquals(9L, row.getRequestId()); assertEquals(row.getFileUrl(), artifact.getFileUrl()); - assertEquals(row.getFileUrl(), artifact.getShards().getFirst().getName()); - Path parent = directory.resolve("1/8/" + manifest.getStage().getPhase() + ".wav"); - assertFalse(Files.isSameFile(parent, fixture.path(row))); - assertArrayEquals(stageWav(manifest.getStage().getPhase()), Files.readAllBytes(fixture.path(row))); - assertArrayEquals(stageWav(manifest.getStage().getPhase()), Files.readAllBytes(parent)); - assertNotEquals(manifest.getStage().getPhase().equals("background") ? 20L : 21L, row.getId()); - } - assertArrayEquals(expectedStageResult(), fixture.read(fixture.onlyFile(9, FileTypeEnum.CRYBIN)), 1e-12); - fixture.jdbc.update("delete from file where request_id=8"); - fixture.jdbc.update("delete from flow_execute_result where id=8"); - for (String phase : STAGES) Files.delete(directory.resolve("1/8/" + phase + ".wav")); - - fixture.executor.execute(child, fixture.destination(child, 11), CALLER, fixture.cancelled::get); - - assertTrue(fixture.received.isEmpty()); - assertEquals(childJson, child.getSnapshotData()); - assertEquals(childJson, fixture.history(9).getSnapshotData()); - assertEquals(2, fixture.stageReadbacks.size()); - for (var readback : fixture.stageReadbacks) { - for (String phase : STAGES) assertArrayEquals(expectedStageSamples(phase), readback.get(phase), 1e-12); - } - var next = FlowExecuteSnapshotDataDto.parseJson(fixture.history(11).getSnapshotData()); - for (var manifest : next.getProviderStageInputArtifacts()) { - assertEquals(11L, manifest.getRequestId()); - var artifact = manifest.getArtifacts().getFirst(); - var row = fixture.fileRows.selectById(artifact.getFileId()); - assertEquals(11L, row.getRequestId()); - var previous = childSnapshot.getProviderStageInputArtifacts().stream() - .filter(value -> value.getStage().getPhase().equals(manifest.getStage().getPhase())).findFirst().orElseThrow(); - Path previousPath = directory.resolve(previous.getArtifacts().getFirst().getFileUrl()); - assertFalse(Files.isSameFile(previousPath, fixture.path(row))); - assertArrayEquals(stageWav(manifest.getStage().getPhase()), Files.readAllBytes(previousPath)); - } - assertArrayEquals(expectedStageResult(), fixture.read(fixture.onlyFile(11, FileTypeEnum.CRYBIN)), 1e-12); - } - - @ParameterizedTest - @ValueSource(strings = {"background", "formal"}) - void aMissingProviderStageRejectsBeforeBindingEvenWhenTheOtherStageIsComplete(String phase) throws Exception { - var fixture = new Fixture(RecordedInputMode.PROVIDER_FACTS); - var source = fixture.history(8); - fixture.executor.inspect(source, CALLER); - var destination = fixture.destination(source, 9); - Files.delete(directory.resolve("1/8/" + phase + ".wav")); - - assertThrows(IllegalArgumentException.class, () -> fixture.executor.inspect(source, CALLER)); - assertThrows(IllegalArgumentException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - verify(fixture.plugins, never()).bindMeasurement(anyString(), anyMap(), anyMap(), any(), any(), any()); - fixture.assertUnpublished(9, destination.getSnapshotData()); - assertFalse(Files.exists(directory.resolve("1/9"))); - } - - @Test void sameNamedSavedInputAndReferenceOutputRetainTheirDirectedMetadata() throws Exception { - var fixture = fixture(); - var source = fixture.history(8); - var snapshot = FlowExecuteSnapshotDataDto.parseJson(source.getSnapshotData()); - var output = new ChannelDto(); output.setId(2L); output.setVchId(CHANNEL); output.setChannelIo(1); - output.setChannelType(2); output.setChannelSource(0); output.setSamplingRate(RATE); - output.setVp(7D); output.setSensitivity(3D); output.setBitDepth(16); - snapshot.getDashboardConfig().getDashboardItemConfig().getSignalAnalysisConfig() - .put("referenceOutputs", List.of(MeasureProviderInputs.jsonMap(output))); - source.setSnapshotData(snapshot.toJson()); fixture.histories.updateById(source); - - fixture.executor.execute(source, fixture.destination(source, 9), CALLER, fixture.cancelled::get); - - fixture.assertCompleteInput(); - var catalog = fixture.catalogs.getFirst(); - assertEquals(2D, catalog.get(ChannelDirection.INPUT, CHANNEL).vp()); - assertEquals(0, catalog.get(ChannelDirection.INPUT, CHANNEL).hardware().channelIo()); - assertEquals(7D, catalog.get(ChannelDirection.OUTPUT, CHANNEL).vp()); - assertEquals(1, catalog.get(ChannelDirection.OUTPUT, CHANNEL).hardware().channelIo()); - assertEquals(3, fixture.frameChannels.size()); - for (var channel : fixture.frameChannels) { - assertEquals(0, channel.hardware().channelIo()); assertEquals(2D, channel.vp()); - } - } - - @ParameterizedTest - @ValueSource(strings = {"missing", "truncated", "short-receipt", "empty", "missing-row"}) - void unavailableOrIncompleteOriginalsNeverBindAnAlgorithm(String damage) throws Exception { - var fixture = fixture(); - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - fixture.executor.inspect(source, CALLER); - switch (damage) { - case "missing" -> Files.delete(fixture.raw); - case "truncated" -> Files.write(fixture.raw, Arrays.copyOf(wav(SAMPLES), 45)); - case "short-receipt" -> Files.write(fixture.raw, wav(SAMPLES - 1)); - case "empty" -> Files.write(fixture.raw, wav(0)); - case "missing-row" -> fixture.jdbc.update("delete from file where id=10"); - default -> throw new AssertionError(damage); - } - - assertThrows(IllegalArgumentException.class, () -> fixture.executor.inspect(source, CALLER)); - assertThrows(IllegalArgumentException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - verify(fixture.plugins, never()).bindMeasurement(anyString(), anyMap(), anyMap(), any(), any(), any()); - fixture.assertUnpublished(9, destination.getSnapshotData()); - } - - @Test void cancellationBeforeExecutionNeverCopiesInputsOrBindsAnAlgorithm() throws Exception { - var fixture = fixture(); - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - fixture.cancelled.set(true); - - assertThrows(CancellationException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - assertFalse(Files.exists(directory.resolve("1/9"))); - verify(fixture.plugins, never()).bindMeasurement(anyString(), anyMap(), anyMap(), any(), any(), any()); - fixture.assertUnpublished(9, destination.getSnapshotData()); - } - - @ParameterizedTest - @EnumSource(value = Outcome.class, names = {"CANCEL_AFTER_OUTPUT", "FAIL_AFTER_OUTPUT", "FAIL_ON_CLOSE", "NO_OUTPUT"}) - void cancellationAndFailuresNeverPublishPartialOutputsOrMutateTheSource(Outcome outcome) throws Exception { - var fixture = fixture(); fixture.outcome = outcome; - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - String originalSnapshot = source.getSnapshotData(); - String draftSnapshot = destination.getSnapshotData(); - byte[] originalWaveform = Files.readAllBytes(fixture.raw); - - assertThrows(RuntimeException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - assertFalse(fixture.received.isEmpty(), "The algorithm must actually run before this failure"); - if (outcome != Outcome.NO_OUTPUT) assertTrue(fixture.emitted.get() > 0); - assertEquals(1, fixture.closed.get()); - fixture.assertUnpublished(9, draftSnapshot); - assertFalse(Files.exists(directory.resolve("1/9"))); - assertEquals(originalSnapshot, source.getSnapshotData()); - assertEquals(originalSnapshot, fixture.history(8).getSnapshotData()); - assertArrayEquals(originalWaveform, Files.readAllBytes(fixture.raw)); - } - - @Test void persistenceFailureRollsBackResultsGraphicsBindingsAndFileRows() throws Exception { - var fixture = fixture(); - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - String draft = destination.getSnapshotData(); - fixture.jdbc.execute("create trigger reject_publication before update of snapshot_data on flow_execute_result " - + "when NEW.id=9 begin " - + "select case when (select count(*) from file where request_id=9)<>2 " - + "or (select count(*) from dashboard_result where request_id=9)<>1 " - + "or (select count(*) from graphic_config_snapshot where request_id=9)<>1 " - + "or (select count(*) from graphic_result_code)<>1 " - + "then raise(ABORT, 'incorrect publication phase') end; " - + "select raise(ABORT, 'controlled final publication failure'); end"); - - var failure = assertThrows(RuntimeException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - Throwable cause = failure; - while (cause.getCause() != null) cause = cause.getCause(); - assertTrue(cause.getMessage().contains("controlled final publication failure")); - fixture.assertCompleteInput(); - assertTrue(fixture.emitted.get() > 0); - fixture.assertUnpublished(9, draft); - assertEquals(draft, destination.getSnapshotData()); - assertFalse(Files.exists(directory.resolve("1/9"))); - } - - @Test void anExistingDestinationDirectoryAndItsContentsAreNeverRemoved() throws Exception { - var fixture = fixture(); - var source = fixture.history(8); - var destination = fixture.destination(source, 9); - Path target = directory.resolve("1/9"); - Files.createDirectories(target.resolve("nested")); - Path sentinel = target.resolve("nested/keep.bin"); - byte[] original = {1, 9, 4, 2}; - Files.write(sentinel, original); - - assertThrows(IllegalArgumentException.class, - () -> fixture.executor.execute(source, destination, CALLER, fixture.cancelled::get)); - - assertTrue(Files.isDirectory(target)); - assertArrayEquals(original, Files.readAllBytes(sentinel)); - try (var entries = Files.walk(target)) { assertEquals(3, entries.count()); } - fixture.assertUnpublished(9, destination.getSnapshotData()); - verify(fixture.plugins, never()).bindMeasurement(anyString(), anyMap(), anyMap(), any(), any(), any()); - } - - @Test void aSourceSnapshotChangedAfterAdmissionCannotExecuteOrPublish() throws Exception { - var fixture = fixture(); - var stale = fixture.history(8); - var destination = fixture.destination(stale, 9); - var changed = FlowExecuteSnapshotDataDto.parseJson(stale.getSnapshotData()); - changed.getDashboardConfig().getDashboardItemConfig().getSignalAnalysisConfig().put("resolution", 256); - fixture.jdbc.update("update flow_execute_result set snapshot_data=? where id=8", changed.toJson()); - - assertThrows(IllegalArgumentException.class, - () -> fixture.executor.execute(stale, destination, CALLER, fixture.cancelled::get)); - - verify(fixture.plugins, never()).bindMeasurement(anyString(), anyMap(), anyMap(), any(), any(), any()); - fixture.assertUnpublished(9, destination.getSnapshotData()); - } - - private Fixture fixture() throws Exception { - return new Fixture(); - } - - private enum Outcome { SUCCESS, CANCEL_AFTER_OUTPUT, FAIL_AFTER_OUTPUT, FAIL_ON_CLOSE, NO_OUTPUT } - - private final class Fixture { - final JdbcTemplate jdbc; - final FlowExecuteResultMapper histories; - final FileMapper fileRows; - final VariablesService variables = mock(VariablesService.class); - final PluginMeasureExecutor plugins = mock(PluginMeasureExecutor.class); - final NioWaveformSourceReaderFactory readers = new NioWaveformSourceReaderFactory(); - final MeasureReanalysisExecutor executor = new MeasureReanalysisExecutor(); - final AtomicBoolean cancelled = new AtomicBoolean(); - final AtomicInteger closed = new AtomicInteger(); - final AtomicInteger emitted = new AtomicInteger(); - final List received = new CopyOnWriteArrayList<>(); - final List ranges = new CopyOnWriteArrayList<>(); - final List origins = new CopyOnWriteArrayList<>(); - final List> boundConfigurations = new CopyOnWriteArrayList<>(); - final List reanalysis = new CopyOnWriteArrayList<>(); - final List catalogs = new CopyOnWriteArrayList<>(); - final List frameChannels = new CopyOnWriteArrayList<>(); - final List> stageReadbacks = new CopyOnWriteArrayList<>(); - final Path raw = directory.resolve("raw.wav"); - final RecordedInputMode mode; - Outcome outcome = Outcome.SUCCESS; - - Fixture() throws Exception { this(RecordedInputMode.WAVEFORM_STREAM); } - - Fixture(RecordedInputMode mode) throws Exception { - this.mode = mode; - var dataSource = new SQLiteDataSource(); - dataSource.setUrl("jdbc:sqlite:" + directory.resolve("reanalysis.db")); - jdbc = new JdbcTemplate(dataSource); - createTable(jdbc, "flow_execute_result", FlowExecuteResult.class); - createTable(jdbc, "file", File.class); - jdbc.execute("alter table file add column wait_del integer default 0"); - createTable(jdbc, "dashboard_result", DashboardResult.class); - createTable(jdbc, "graphic_config_snapshot", GraphicConfigSnapshot.class); - createTable(jdbc, "graphic_result_code", GraphicResultCode.class); - var database = new MybatisConfiguration(); - database.setMapUnderscoreToCamelCase(true); - database.setEnvironment(new Environment("reanalysis", new SpringManagedTransactionFactory(), dataSource)); - database.addMapper(FlowExecuteResultMapper.class); database.addMapper(FileMapper.class); - database.addMapper(DashboardResultMapper.class); database.addMapper(GraphicConfigSnapshotMapper.class); - database.addMapper(GraphicResultCodeMapper.class); - var session = new SqlSessionTemplate(new MybatisSqlSessionFactoryBuilder().build(database)); - histories = session.getMapper(FlowExecuteResultMapper.class); - fileRows = session.getMapper(FileMapper.class); - var files = new FileService() { - @Override public FileMapper getBaseMapper() { return fileRows; } - }; - ReflectionTestUtils.setField(files, "baseMapper", fileRows); - when(variables.getDefaultStoragePath()).thenReturn(directory.toString()); - ReflectionTestUtils.setField(files, "variablesService", variables); - ReflectionTestUtils.setField(files, "waveformSourceReaderFactory", readers); - var providers = mock(PluginDashboardConfigurationResolver.class); - when(providers.ready()).thenReturn(true); - when(providers.available(PROVIDER)).thenReturn(true); - when(providers.measurementInputInterpreter(eq(PROVIDER), any())).thenReturn(Optional.of(savedChannels())); - when(providers.resolveDefinition(eq(PROVIDER), anyMap(), any(), any(), any(), any(ChannelCatalog.class), anyBoolean())) - .thenAnswer(call -> { catalogs.add(call.getArgument(5)); return definition(); }); - when(plugins.recordedMeasurementInputMode(eq(PROVIDER), anyMap(), any())).thenReturn(mode); - when(plugins.bindMeasurement(eq(PROVIDER), anyMap(), anyMap(), any(), any(), any())).thenAnswer(call -> { - boundConfigurations.add(call.getArgument(1)); - return algorithm(); - }); - var channels = VirtualChannelTestSupport.executionChannelResolver(); - var resolver = new SavedInputResolver(fileRows, histories, files, readers, providers, channels); - var triggers = mock(ProjectTriggerFacts.class); - when(triggers.legacy(anyString(), anyMap())).thenReturn(ProjectTriggers.EMPTY); - ReflectionTestUtils.setField(executor, "histories", histories); - ReflectionTestUtils.setField(executor, "results", session.getMapper(DashboardResultMapper.class)); - ReflectionTestUtils.setField(executor, "graphics", session.getMapper(GraphicConfigSnapshotMapper.class)); - ReflectionTestUtils.setField(executor, "bindings", session.getMapper(GraphicResultCodeMapper.class)); - ReflectionTestUtils.setField(executor, "fileRows", fileRows); - ReflectionTestUtils.setField(executor, "files", files); - ReflectionTestUtils.setField(executor, "variables", variables); - ReflectionTestUtils.setField(executor, "plugins", plugins); - ReflectionTestUtils.setField(executor, "providers", providers); - ReflectionTestUtils.setField(executor, "inputs", new RecordedMeasurementInputs(resolver)); - ReflectionTestUtils.setField(executor, "readers", readers); - ReflectionTestUtils.setField(executor, "protection", new SavedHistoryMutationProtection( - new DashboardReplayExecutionCoordinator(new DashboardReplayConfig()))); - ReflectionTestUtils.setField(executor, "units", mock(UnitConversionService.class)); - ReflectionTestUtils.setField(executor, "triggers", triggers); - ReflectionTestUtils.setField(executor, "operations", new DashboardOperationRegistry()); - ReflectionTestUtils.setField(executor, "virtualChannels", channels); - ReflectionTestUtils.setField(executor, "transactions", new DataSourceTransactionManager(dataSource)); - Files.write(raw, wav(SAMPLES)); - var source = new FlowExecuteResult(); source.setId(8L); source.setTenantId(1L); source.setProjectId(1L); - source.setName("Saved measurement"); source.setSnapshotData(sourceSnapshot().toJson()); - histories.insert(source); - var row = new File(); row.setId(10L); row.setTenantId(1L); row.setRequestId(8L); - row.setBusinessId(rawBusinessId(8)); row.setBusinessType(FileBusinessTypeEnum.COMMON.getType()); - row.setType(FileTypeEnum.WAV.getType()); row.setFileName("raw.wav"); row.setFileUrl("raw.wav"); - row.setShardsNo(1); row.setTemporaryResult(0); fileRows.insert(row); - if (mode == RecordedInputMode.PROVIDER_FACTS) savedStages(); - } - - void savedStages() throws IOException { - fileRows.deleteById(10L); Files.delete(raw); - var snapshot = FlowExecuteSnapshotDataDto.parseJson(history(8).getSnapshotData()); - snapshot.setRecordedWaveformPlan(null); - snapshot.setInternalStoragePath(directory.toString()); - snapshot.setProviderExecutionInputs(Map.of(PROVIDER, Map.of("stages", Map.of( - "schema", "test/stages/1", "revision", 1, "values", Map.of("phases", STAGES, "samples", SAMPLES))))); - var manifests = new ArrayList(); - for (int index = 0; index < STAGES.size(); index++) { - String phase = STAGES.get(index); - String name = "1/8/" + phase + ".wav"; - Files.createDirectories(directory.resolve("1/8")); Files.write(directory.resolve(name), stageWav(phase)); - var row = new File(); row.setId(20L + index); row.setTenantId(1L); row.setRequestId(8L); - row.setBusinessId("8_2_" + CHANNEL + "_" + phase + "_stage-raw_0_0_FS"); - row.setBusinessType(FileBusinessTypeEnum.COMMON.getType()); row.setType(FileTypeEnum.WAV.getType()); - row.setFileName(phase + ".wav"); row.setFileUrl(name); row.setShardsNo(1); row.setTemporaryResult(0); - fileRows.insert(row); - var shard = new StageArtifactManifestDto.Shard(); shard.setName(name); shard.setBytes(Files.size(directory.resolve(name))); - var artifact = new StageArtifactManifestDto.Artifact(); artifact.setFileId(row.getId()); - artifact.setBusinessId(row.getBusinessId()); artifact.setFileUrl(name); artifact.setFileType(row.getType()); - artifact.setChannelId(CHANNEL); artifact.setSourceVchId(CHANNEL); artifact.setSourceUnit("FS"); - artifact.setResultHash(phase); artifact.setResultCode("stage-raw"); artifact.setUnitY("FS"); - artifact.setShards(List.of(shard)); artifact.setSampleRate(RATE); artifact.setBitDepth(16); - artifact.setTaskOriginSample(100L); artifact.setSourceStartSample(FIRST_SAMPLE); - artifact.setSampleCount(SAMPLES); artifact.setContiguous(true); - var stage = new StageArtifactManifestDto.Stage(); stage.setGroupId("capture"); stage.setPhase(phase); - stage.setAttempt(1); stage.setGeneration("1"); - var manifest = new StageArtifactManifestDto(); manifest.setProviderId(PROVIDER); manifest.setSnapshotName("stages"); - manifest.setInputRevision(1); manifest.setProjectId(1L); manifest.setDashboardId(2L); manifest.setTenantId(1L); - manifest.setRequestId(8L); manifest.setStage(stage); manifest.setState("COMMITTED"); manifest.setArtifacts(List.of(artifact)); - manifests.add(manifest); - } - snapshot.setProviderStageInputArtifacts(manifests); - var source = history(8); source.setSnapshotData(snapshot.toJson()); histories.updateById(source); - } - - FlowExecuteResult timestampedShards() throws IOException { - Files.delete(raw); - List names = List.of("Measure #3_Ch1_Acquired Waveform_1_20261113160955.wav", - "Measure #3_Ch1_Acquired Waveform_2_20261113160956.wav"); - Files.write(directory.resolve(names.getFirst()), wav(0, 8192)); - Files.write(directory.resolve(names.getLast()), wav(8192, SAMPLES - 8192)); - Files.writeString(directory.resolve("Measure #3_Ch1_Acquired Waveform.idx"), - directory.resolve(names.getFirst()).toString().replace('\\', '/') + "|170.66666666666666|3\n" - + directory.resolve(names.getLast()).toString().replace('\\', '/') + "|341.4791666666667|7\n"); - var row = fileRows.selectById(10L); row.setFileName(names.getFirst()); row.setFileUrl(names.getFirst()); - row.setShardsNo(2); fileRows.updateById(row); - var source = history(8); - var snapshot = FlowExecuteSnapshotDataDto.parseJson(source.getSnapshotData()); - var channels = snapshot.getDashboardConfig().getDashboardItemConfig().getCollectConfig().getChannels(); - var plan = RecordedWaveformPlans.selected(channels); - RecordedWaveformPlans.close(plan, List.of(new RawInputArtifact(CHANNEL, 8, "raw", names, - RATE, 100L, FIRST_SAMPLE, SAMPLES, true))); - snapshot.setRecordedWaveformPlan(plan); source.setSnapshotData(snapshot.toJson()); - histories.updateById(source); - return source; - } - - Path indexPath(File row) { - String relative = ReflectionTestUtils.invokeMethod(new ProjectDashboardComponent(), - "resolveWavIndexRelativePath", row.getFileUrl(), 1L, row.getRequestId()); - return directory.resolve(relative); - } - - FlowExecuteResult history(long id) { return histories.selectById(id); } - - FlowExecuteResult destination(FlowExecuteResult source, long id) { - var snapshot = FlowExecuteSnapshotDataDto.parseJson(source.getSnapshotData()); - var original = MeasureProviderInputs.dashboard(snapshot.getDashboardConfig()); - var prior = snapshot.getReanalysis(); - snapshot.getDashboardConfig().getDashboardItemConfig().getSignalAnalysisConfig().put("resolution", 512); - var lineage = new MeasureReanalysisLineageDto(); lineage.setRootRequestId(8L); - lineage.setParentRequestId(source.getId()); lineage.setSourceRequestId(source.getId()); - lineage.setInputRequestId(prior == null ? source.getId() : prior.getInputRequestId()); - lineage.setOriginalConfiguration(prior == null ? original : prior.getOriginalConfiguration()); - snapshot.setReanalysis(lineage); - var result = new FlowExecuteResult(); result.setId(id); result.setTenantId(1L); result.setProjectId(1L); - result.setSnapshotData(snapshot.toJson()); histories.insert(result); return result; - } - - BoundMeasurementExecution algorithm() { - return new BoundMeasurementExecution() { - @Override public String configurationVersion() { return "1"; } - @Override public Map, Object> pipelineCapabilities() { return Map.of(); } - @Override public void execute(ExecutionPreparation preparation, Map, Object> services) throws Exception { - reanalysis.add((SavedMeasurementReanalysis) services.get(SavedMeasurementReanalysis.class)); - preparation.own(() -> { - closed.incrementAndGet(); - if (outcome == Outcome.FAIL_ON_CLOSE) throw new IllegalStateException("controlled plugin close failure"); - }); - var consumer = (ResultOutputConsumer) services.get(ResultOutputConsumer.class); - var prepared = preparation.prepare(); - prepared.compile(outputs -> { - var requirement = outputs.results().getFirst(); - var sink = new SinkProcessor() { - @Override public void onFrame(ChannelFrame frame) { - frameChannels.add(frame.channel()); - received.add(frame.samples().clone()); ranges.add(frame.sourceRange()); - origins.add(frame.taskOriginSample()); - if (outcome == Outcome.NO_OUTPUT) return; - var values = Arrays.stream(frame.samples()).map(value -> value * 2).toArray(); - var unbound = ResultFrame.uniformX("Reanalysed", "reanalysis-hash", CHANNEL, - "s", "V", 1D / RATE, values, 0, null); - var result = unbound.withDashboardResultId(requirement.dashboardResultId()); - try { consumer.accept(result); emitted.incrementAndGet(); } - finally { result.release(); unbound.release(); } - if (outcome == Outcome.CANCEL_AFTER_OUTPUT) cancelled.set(true); - if (outcome == Outcome.FAIL_AFTER_OUTPUT) - throw new IllegalStateException("controlled calculation failure"); - } - }; - return ProcessorPipelineBuilder.create() - .startControl(new StartControlProcessor() { - @Override public void onFrame(ChannelFrame frame) { context().emit(frame.retain()); } - }, 1, QueueOverflowPolicy.WAIT_FOR_CAPACITY) - .stopControl(new StopControlProcessor() { - @Override public void onFrame(ChannelFrame frame) { context().emit(frame.retain()); } - }, 1, QueueOverflowPolicy.WAIT_FOR_CAPACITY) - .addProcessor("analysis", sink).connectInput(CHANNEL, sink, 1, QueueOverflowPolicy.WAIT_FOR_CAPACITY); - }); - prepared.ready(); prepared.run(); - if (mode == RecordedInputMode.PROVIDER_FACTS) { - var inputs = (SavedInputSnapshots) services.get(SavedInputSnapshots.class); - var document = inputs.require("stages", "test/stages/1"); - assertEquals(STAGES, document.values().get("phases")); - assertEquals(SAMPLES, ((Number) document.values().get("samples")).intValue()); - var related = (RelatedResultFiles) services.get(RelatedResultFiles.class); - var observed = new LinkedHashMap(); - for (String phase : STAGES) { - var facts = inputs.artifacts("stages", new Key("capture", phase, 1, "1")); - assertEquals(1, facts.size()); assertTrue(facts.getFirst().available()); - var pages = new ArrayList(); - long count = related.readCompleteY(facts.getFirst(), 4096, (offset, frame) -> { - assertEquals(pages.stream().mapToLong(page -> page.length).sum(), offset); - pages.add(frame.y()); - }); - assertEquals(SAMPLES, count); - observed.put(phase, pages.stream().flatMapToDouble(Arrays::stream).toArray()); - } - stageReadbacks.add(observed); - double[] values = new double[SAMPLES]; - for (int i = 0; i < SAMPLES; i++) values[i] = observed.get("formal")[i] - observed.get("background")[i]; - var unbound = ResultFrame.uniformX("Reanalysed", "reanalysis-hash", CHANNEL, "s", "V", 1D / RATE, - values, 0, null); - var result = unbound.withDashboardResultId(((SavedResultCatalog) - services.get(SavedResultCatalog.class)).results().getFirst().resultId()); - try { consumer.accept(result); emitted.incrementAndGet(); } - finally { result.release(); unbound.release(); } - } - ((ExecutionResultDocuments) services.get(ExecutionResultDocuments.class)).write("summary", - new ExecutionResultDocuments.Document("test/reanalysis", 1, Map.of("samples", SAMPLES))); - } - }; - } - - void assertCompleteInput() { - assertArrayEquals(expectedSamples(1), received.stream().flatMapToDouble(Arrays::stream).toArray(), 1e-12); - assertEquals(List.of(new SourceSampleRange(FIRST_SAMPLE, FIRST_SAMPLE + 8192), - new SourceSampleRange(FIRST_SAMPLE + 8192, FIRST_SAMPLE + 16384), - new SourceSampleRange(FIRST_SAMPLE + 16384, FIRST_SAMPLE + SAMPLES)), ranges); - assertEquals(List.of(100L, 100L, 100L), origins); - } - - File onlyFile(long request, FileTypeEnum type) { - var found = fileRows.selectList(null).stream().filter(row -> row.getRequestId().equals(request) - && row.getType().equals(type.getType())).toList(); - assertEquals(1, found.size()); return found.getFirst(); - } - - Path path(File row) { return directory.resolve(row.getFileUrl()).normalize(); } - - double[] read(File row) throws IOException { - Path path = path(row); - var source = new FrozenWaveformSource(row.getId(), row.getType().equals(FileTypeEnum.WAV.getType()) - ? WaveformSourceType.WAV : WaveformSourceType.CRYBIN, RATE, - List.of(new FrozenWaveformShard(path, Files.size(path)))); - var blocks = new ArrayList(); - try (var reader = readers.open(source)) { - while (true) { - var block = reader.read(4096); - if (block.getFrameCount() == 0) break; - blocks.add(block.getSamples().clone()); - } - } - return blocks.stream().flatMapToDouble(Arrays::stream).toArray(); - } - - int count(String table, long request) { - return jdbc.queryForObject("select count(*) from " + table + " where request_id=?", Integer.class, request); - } - - void assertUnpublished(long request, String draftSnapshot) { - assertEquals(0, count("file", request)); assertEquals(0, count("dashboard_result", request)); - assertEquals(0, count("graphic_config_snapshot", request)); - assertEquals(0, jdbc.queryForObject("select count(*) from graphic_result_code", Integer.class)); - assertEquals(draftSnapshot, history(request).getSnapshotData()); - } - } - - private static FlowExecuteSnapshotDataDto sourceSnapshot() { - var channel = new ChannelDto(); channel.setId(1L); channel.setVchId(CHANNEL); - channel.setChannelIo(0); channel.setChannelType(2); channel.setChannelSource(0); - channel.setSamplingRate(RATE); channel.setSensitivity(1D); channel.setVp(2D); channel.setBitDepth(16); - var collect = new CollectConfigDto(); collect.setInputChannels(List.of(CHANNEL)); collect.setChannels(List.of(channel)); - var item = new DashboardItemConfigDto(); item.setCollectConfig(collect); - item.setSignalAnalysisConfig(new SignalAnalysisConfigDto(Map.of("resolution", 128))); - var dashboard = new DashboardConfigDto(); dashboard.setId(2L); dashboard.setProjectId(1L); dashboard.setTenantId(1L); - dashboard.setProviderId(PROVIDER); dashboard.setDashboardItemConfig(item); - var snapshot = new FlowExecuteSnapshotDataDto(); snapshot.setDashboardConfig(dashboard); - snapshot.setVirtualChannelExecutionChannels(List.of(channel)); - var plan = RecordedWaveformPlans.selected(List.of(channel)); - RecordedWaveformPlans.close(plan, List.of(new RawInputArtifact(CHANNEL, 8, "raw", List.of("raw.wav"), - RATE, 100L, FIRST_SAMPLE, SAMPLES, true))); - snapshot.setRecordedWaveformPlan(plan); return snapshot; - } - - private static TableDefinition definition() { - var member = ResultChannelDefinition.source(CHANNEL, CHANNEL, ChannelDirection.INPUT, - GraphicUnitResolution.choices("s", "V", "V")); - var result = new ResultSetDefinition("Reanalysed", "reanalysis-hash", "test.reanalysed", ResultType.TIME, - DomainType.TIME_DOMAIN, List.of(), Map.of(), List.of(member), Map.of()); - var identity = new TableResultIdentity.Catalog(result.resultCode(), result.resultType(), null); - return new TableDefinition(List.of(new TableDefinition.CatalogResult(identity, result)), List.of(), null, - List.of(new TableDefinition.DefaultGraphic(List.of(new TableDefinition.Binding(identity, List.of(CHANNEL))), - GraphicType.TIME_HISTORY_CURVE))); - } - - private static MeasurementInputInterpreter savedChannels() { - return new MeasurementInputInterpreter() { - @Override public InputDeclaration declaration() { - return new InputDeclaration(List.of(), List.of(), List.of(), List.of(), List.of(), OptionalInt.of(1)); - } - @Override public List bind(Map configuration, ExecutionChannelFacts facts) { - var inputs = facts.saved(List.of("collectConfig", "channels")); - facts.saved(List.of("signalAnalysisConfig", "referenceOutputs")); - return inputs.stream() - .map(ExecutionChannelFacts.Selection::unchanged).toList(); - } - }; - } - - private static String rawBusinessId(long request) { - String code = CalculateResultsEnum.ACQUIRED_WAVEFORM.getResultCode(); - return FileBusinessTypeEnum.buildBusinessIdWithOutUnit(2L, Long.toString(request), CHANNEL, - CryBusinessUtil.generateAlgorithmKey(code, null), code); - } - - private static short sample(int index) { return index == SAMPLES - 1 ? 30001 : (short) ((index % 251 - 125) * 128); } - - private static double[] expectedSamples(double multiplier) { - double[] values = new double[SAMPLES]; - for (int i = 0; i < SAMPLES; i++) values[i] = multiplier * sample(i) / 32767D; - return values; - } - - private static double[] expectedStageSamples(String phase) { - double[] values = new double[SAMPLES]; - for (int i = 0; i < SAMPLES; i++) values[i] = (phase.equals("background") ? sample(i) / 2 : sample(i)) / 32767D; - return values; - } - - private static double[] expectedStageResult() { - double[] values = expectedStageSamples("formal"); - double[] background = expectedStageSamples("background"); - for (int i = 0; i < SAMPLES; i++) values[i] -= background[i]; - return values; - } - - private static byte[] stageWav(String phase) { - byte[] bytes = wav(SAMPLES); - if (phase.equals("background")) { - var buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); - for (int i = 0; i < SAMPLES; i++) buffer.putShort(44 + i * 2, (short) (sample(i) / 2)); - } - return bytes; - } - - private static byte[] wav(int samples) { - return wav(0, samples); - } - - private static byte[] wav(int offset, int samples) { - int dataBytes = samples * 2; - var buffer = ByteBuffer.allocate(44 + dataBytes).order(ByteOrder.LITTLE_ENDIAN); - buffer.put("RIFF".getBytes(StandardCharsets.US_ASCII)).putInt(36 + dataBytes) - .put("WAVEfmt ".getBytes(StandardCharsets.US_ASCII)).putInt(16) - .putShort((short) 1).putShort((short) 1).putInt(RATE).putInt(RATE * 2) - .putShort((short) 2).putShort((short) 16) - .put("data".getBytes(StandardCharsets.US_ASCII)).putInt(dataBytes); - for (int i = 0; i < samples; i++) buffer.putShort(sample(offset + i)); - return buffer.array(); - } - - private static void createTable(JdbcTemplate jdbc, String table, Class entity) { - var columns = new ArrayList(); - for (Class type = entity; type != Object.class; type = type.getSuperclass()) { - for (var field : type.getDeclaredFields()) { - if (Modifier.isStatic(field.getModifiers())) continue; - var annotation = field.getAnnotation(TableField.class); - if (annotation != null && !annotation.exist()) continue; - String column = field.getName().replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase(Locale.ROOT); - columns.add(column + (column.equals("id") ? " integer primary key" - : Number.class.isAssignableFrom(field.getType()) ? " numeric" : " text")); - } - } - jdbc.execute("create table " + table + "(" + String.join(",", columns) + ")"); - } -} diff --git a/apps/backend/pom.xml b/apps/backend/pom.xml index a6218f49..46527dd8 100644 --- a/apps/backend/pom.xml +++ b/apps/backend/pom.xml @@ -40,7 +40,6 @@ - 1 2.5.0 21 UTF-8 @@ -70,25 +69,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - ${backend.test.forkCount} - - ${project.build.directory}/test-native/${surefire.forkNumber} - - - - - org.apache.maven.plugins - maven-failsafe-plugin - - - ${project.build.directory}/test-native/${surefire.forkNumber} - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/docs/verification/plugin-pipeline-file-output.md b/docs/verification/plugin-pipeline-file-output.md index c4766683..ec271d8b 100644 --- a/docs/verification/plugin-pipeline-file-output.md +++ b/docs/verification/plugin-pipeline-file-output.md @@ -13,7 +13,7 @@ Host tests should use minimal plugins through real HTTP/gRPC preparation and sta | Scenario | Related tests | Expected observations and boundaries | | --- | --- | --- | -| 1. Ordinary real-time Monitor | Host `ContinuousMeasurementEntryTest`; plugin `MonitorPipelineRecordingTest`, `MonitorManualStopContractTest` | Real-time start/stop creates no formal results/files. Monitor requires no recording declaration; even enabled save configuration produces no WAV in preview. Preview and Stop remain available | +| 1. Ordinary real-time Monitor | Plugin `MonitorPipelineRecordingTest`, `MonitorManualStopContractTest`; host real-time start/stop scenario is no longer covered by a dedicated test | Real-time start/stop creates no formal results/files. Monitor requires no recording declaration; even enabled save configuration produces no WAV in preview. Preview and Stop remain available | | 2. Computed results only | `ExplicitPipelineFileOutputEntryTest.resultsOnlyPipelineDoesNotInventOriginalWaveformOutput` | Real crybin is downloadable/readable, with success and no implicit WAV | | 3. Recalculation without originals | `PreparedMeasurementRawInputEntryTest.missingOriginalDataProducesComputedResultsWithoutInventingRawReceipt` | Computed results remain accessible. Historical original-input admission rejects missing receipts without borrowing another execution's data | | 4. Original/computed branches | `ExplicitPipelineFileOutputEntryTest.originalWaveformBranchWritesCapturedValuesAndComputedBranchKeepsOwnResult`; recording tests in the corresponding external plugin sources | Host original WAV samples .6/.7/.8/.9 differ from computed .1/.2/.3/.4; plugin tests likewise distinguish branch origins | diff --git a/docs/verification/plugin-pipeline-file-output_cn.md b/docs/verification/plugin-pipeline-file-output_cn.md index 95707253..fd5afa6c 100644 --- a/docs/verification/plugin-pipeline-file-output_cn.md +++ b/docs/verification/plugin-pipeline-file-output_cn.md @@ -13,7 +13,7 @@ | 场景 | 相关测试 | 观察与边界 | | --- | --- | --- | -| 1. 普通实时 Monitor | 宿主 `ContinuousMeasurementEntryTest`;插件 `MonitorPipelineRecordingTest`、`MonitorManualStopContractTest` | 生产实时启停入口不创建正式结果/文件;真实 Monitor 图无需录音登记,保存配置开启也不产生 WAV,预览与 Stop 保留 | +| 1. 普通实时 Monitor | 插件 `MonitorPipelineRecordingTest`、`MonitorManualStopContractTest`;宿主实时启停场景已无专门测试覆盖 | 生产实时启停入口不创建正式结果/文件;真实 Monitor 图无需录音登记,保存配置开启也不产生 WAV,预览与 Stop 保留 | | 2. 仅计算结果 | `ExplicitPipelineFileOutputEntryTest.resultsOnlyPipelineDoesNotInventOriginalWaveformOutput` | 真实 crybin 可下载读取,成功且没有隐式 WAV | | 3. 无原始输入的重算 | `PreparedMeasurementRawInputEntryTest.missingOriginalDataProducesComputedResultsWithoutInventingRawReceipt` | 计算结果仍可访问;历史原始输入准入拒绝缺失凭据,不借用别次执行 | | 4. 原始与计算分支 | `ExplicitPipelineFileOutputEntryTest.originalWaveformBranchWritesCapturedValuesAndComputedBranchKeepsOwnResult`;相应外部插件的录音测试 | 宿主原始 WAV 为 .6/.7/.8/.9,计算结果为 .1/.2/.3/.4;插件同样用不同样本证明来源分离 | diff --git a/tools/ci/report-backend-test-times.py b/tools/ci/report-backend-test-times.py deleted file mode 100644 index f91c8f12..00000000 --- a/tools/ci/report-backend-test-times.py +++ /dev/null @@ -1,142 +0,0 @@ -#!/usr/bin/env python3 -"""Export Surefire and Failsafe case timings without report environment data.""" - -import argparse -from collections import Counter -from decimal import Decimal, InvalidOperation -import json -import math -from pathlib import Path -import xml.etree.ElementTree as ET - - -parser = argparse.ArgumentParser(description=__doc__) -parser.add_argument("--backend-root", type=Path, default=Path("apps/backend")) -parser.add_argument("--output", type=Path, default=Path("backend-test-times.json")) -parser.add_argument("--since", type=float, help="Only include reports modified since this Unix timestamp.") -args = parser.parse_args() -if args.since is not None and (not math.isfinite(args.since) or args.since < 0): - parser.error("--since must be a finite nonnegative Unix timestamp") - -reports = sorted( - path - for phase in ("surefire", "failsafe") - for path in args.backend_root.glob(f"**/target/{phase}-reports/TEST-*.xml") - if args.since is None or path.stat().st_mtime >= args.since -) -if not reports: - parser.error(f"no Surefire or Failsafe TEST-*.xml reports under {args.backend_root}") - -cases = [] -suites = [] -threshold_seconds = Decimal("0.5") -case_seconds = Decimal(0) -suite_seconds = Decimal(0) -try: - for report in reports: - relative_report = report.relative_to(args.backend_root) - module = report.parent.parent.parent.relative_to(args.backend_root).as_posix() - phase = report.parent.name.removesuffix("-reports") - suite = ET.parse(report).getroot() - if suite.tag != "testsuite": - raise ValueError(f"expected testsuite root, got {suite.tag}") - suite_name = suite.attrib["name"] - if not suite_name: - raise ValueError("empty suite name") - duration = Decimal(suite.attrib["time"]) - if not duration.is_finite() or duration < 0: - raise ValueError("suite time must be finite and nonnegative") - totals = {key: int(suite.attrib[key]) for key in ("tests", "failures", "errors", "skipped")} - if any(value < 0 for value in totals.values()): - raise ValueError("suite counts must be nonnegative") - suite_cases = [] - attributed_seconds = Decimal(0) - statuses = Counter() - for case in suite.findall("testcase"): - class_name = case.attrib["classname"] - case_name = case.attrib["name"] - if not class_name or not case_name: - raise ValueError("empty testcase class or name") - seconds = Decimal(case.attrib["time"]) - if not seconds.is_finite() or seconds < 0: - raise ValueError(f"invalid testcase time for {class_name}#{case_name}") - outcomes = [status for status in ("failure", "error", "skipped") if case.find(status) is not None] - if len(outcomes) > 1: - raise ValueError(f"multiple outcomes for {class_name}#{case_name}") - status = outcomes[0] if outcomes else "passed" - statuses[status] += 1 - attributed_seconds += seconds - suite_cases.append({ - "module": module, - "phase": phase, - "report": relative_report.as_posix(), - "class": class_name, - "case": case_name, - "status": status, - "seconds": float(seconds), - "over_threshold": seconds > threshold_seconds, - }) - observed = { - "tests": len(suite_cases), - "failures": statuses["failure"], - "errors": statuses["error"], - "skipped": statuses["skipped"], - } - if totals != observed: - raise ValueError(f"suite counts {totals} do not match testcases {observed}") - suites.append({ - "module": module, - "phase": phase, - "report": relative_report.as_posix(), - "class": suite_name, - **totals, - "seconds": float(duration), - "case_seconds": float(attributed_seconds), - "unattributed_seconds": float(duration - attributed_seconds), - }) - cases.extend(suite_cases) - case_seconds += attributed_seconds - suite_seconds += duration -except (OSError, ET.ParseError, KeyError, ValueError, InvalidOperation) as error: - parser.error(f"cannot read {report}: {error}") - -cases.sort(key=lambda case: (-case["seconds"], case["report"], case["class"], case["case"])) -status_counts = Counter(case["status"] for case in cases) -summary = { - "report_count": len(reports), - "suite_count": len(suites), - "case_count": len(cases), - "status_counts": {status: status_counts[status] for status in ("passed", "failure", "error", "skipped")}, - "over_threshold_count": sum(case["over_threshold"] for case in cases), - "case_seconds": float(case_seconds), - "suite_seconds": float(suite_seconds), - "unattributed_seconds": float(suite_seconds - case_seconds), -} -result = { - "threshold_seconds": float(threshold_seconds), - "reports_since": args.since, - "timing_notes": [ - "Without --since, all existing matching XML files are included, potentially from older runs.", - "Case times are the XML testcase times and can include framework setup or teardown attributed to a case.", - "Unattributed time is suite time minus summed case time. It can be negative with overlapping cases or rounding.", - "Summed suite times are not build wall time and do not include all Maven or JVM startup overhead.", - "The 500 ms threshold is reported only and does not change test execution or fail this command.", - ], - "summary": summary, - "suites": suites, - "cases": cases, -} -try: - args.output.parent.mkdir(parents=True, exist_ok=True) - args.output.write_text(json.dumps(result, indent=2, ensure_ascii=True, allow_nan=False) + "\n", encoding="utf-8") -except (OSError, ValueError) as error: - parser.error(f"cannot write {args.output}: {error}") - -print(f"Backend timing: {len(reports)} reports, {len(cases)} cases, {summary['over_threshold_count']} over 500 ms.") -print(f"Suite total {suite_seconds:.3f}s; case total {case_seconds:.3f}s; unattributed {suite_seconds - case_seconds:.3f}s.") -for note in result["timing_notes"]: - print(note) -print("Slowest 20 cases:") -for case in cases[:20]: - print(f" {case['seconds'] * 1000:10.3f} ms {case['status']:7} {case['module']} [{case['phase']}] {case['class']}#{case['case']}") -print(f"All case timings written to {args.output}")