diff --git a/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutor.java b/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutor.java index 6f131573..be70fb55 100644 --- a/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutor.java +++ b/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisExecutor.java @@ -46,6 +46,8 @@ import com.opentest.service.dashboard.framework.ProjectTriggerFacts; import com.opentest.service.dashboard.framework.SavedMeasurementInputFacts; import com.opentest.service.export.waveform.source.FrozenWaveformSource; +import com.opentest.service.export.waveform.source.FrozenWaveformShard; +import com.opentest.service.export.waveform.source.WaveformSourceType; import com.opentest.service.export.waveform.source.WaveformSourceReader; import com.opentest.service.export.waveform.source.WaveformSourceReaderFactory; import com.opentest.service.grpc.provider.ProviderExecutionInputSnapshots; @@ -56,8 +58,11 @@ import com.opentest.service.virtualchannel.VirtualChannelExecutionChannelResolver; import jakarta.annotation.Resource; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.time.Instant; import java.util.ArrayList; import java.util.Comparator; @@ -233,6 +238,8 @@ private List copyInputs(ReadPlan saved, FlowExecuteSnapshotDataDto target, if (Files.size(root.resolve(path)) != shard.getExpectedBytes()) throw new IOException("Incomplete saved input copy"); names.add(path.toString().replace('\\', '/')); } + if (frozen.getSourceType() == WaveformSourceType.WAV) + writeChildWavIndex(frozen, names.stream().map(root::resolve).toList(), cancelled); row.setFileName(names.getFirst()); row.setFileUrl(names.getFirst()); row.setShardsNo(names.size()); copied.add(row); byId.put(source.getId(), row); shardNames.put(source.getId(), names); } @@ -252,6 +259,67 @@ private List copyInputs(ReadPlan saved, FlowExecuteSnapshotDataDto target, return List.copyOf(copied); } + private void writeChildWavIndex(FrozenWaveformSource source, List copies, + BooleanSupplier cancelled) throws IOException { + var original = wavIndexPath(source.getShards().getFirst().getPath()); + var entries = new ArrayList(); + if (Files.exists(original, LinkOption.NOFOLLOW_LINKS)) { + if (!Files.isRegularFile(original)) throw new IllegalArgumentException("Saved WAV index is unavailable"); + var lines = Files.readAllLines(original, StandardCharsets.UTF_8); + if (lines.size() != copies.size()) throw new IllegalArgumentException("Saved WAV index does not match its frozen shards"); + double end = 0; + long frames = 0; + for (int index = 0; index < lines.size(); index++) { + checkCancelled(cancelled); + var fields = lines.get(index).split("\\|", -1); + if (fields.length < 2 || fields.length > 3 || fields[0].isBlank()) + throw new IllegalArgumentException("Invalid saved WAV index entry"); + var indexed = Path.of(fields[0].replace('\\', '/')); + if (!indexed.isAbsolute()) indexed = original.getParent().resolve(indexed); + if (!indexed.toAbsolutePath().normalize().equals(source.getShards().get(index).getPath().toAbsolutePath().normalize())) + throw new IllegalArgumentException("Saved WAV index names a different frozen shard"); + double nextEnd = Double.parseDouble(fields[1]); + if (!Double.isFinite(nextEnd) || nextEnd < end) + throw new IllegalArgumentException("Saved WAV index time is invalid or regressing"); + end = nextEnd; + if (fields.length == 3) { + long nextFrames = Long.parseLong(fields[2].trim()); + if (nextFrames < frames) throw new IllegalArgumentException("Saved WAV index frame count is regressing"); + frames = nextFrames; + } + entries.add(copies.get(index).toAbsolutePath().normalize().toString().replace('\\', '/') + + "|" + fields[1] + (fields.length == 3 ? "|" + fields[2] : "")); + } + } else { + long samples = 0; + for (var copy : copies) { + checkCancelled(cancelled); + var copied = new FrozenWaveformSource(source.getSourceFileId(), WaveformSourceType.WAV, + source.getFrozenSamplingRate(), List.of(new FrozenWaveformShard(copy, Files.size(copy))), + source.getFrozenBitDepth(), source.getFrozenFloatingPoint()); + try (var reader = readers.open(copied)) { + while (true) { + checkCancelled(cancelled); + int count = reader.read(8192).getFrameCount(); + if (count == 0) break; + samples = Math.addExact(samples, count); + } + } + entries.add(copy.toAbsolutePath().normalize().toString().replace('\\', '/') + + "|" + samples * 1000D / source.getFrozenSamplingRate()); + } + } + checkCancelled(cancelled); + Files.write(wavIndexPath(copies.getFirst()), entries, StandardCharsets.UTF_8, + StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); + } + + private static Path wavIndexPath(Path firstShard) { + String name = firstShard.getFileName().toString(); + String stem = name.substring(0, name.lastIndexOf('.')).replaceFirst("_\\d+(?:_\\d{14})$", ""); + return firstShard.resolveSibling(stem + ".idx"); + } + private Map, Object> services(ReadPlan source, FlowExecuteSnapshotDataDto target, Map configuration, MeasureReanalysisOutputPlan plan, MeasureReanalysisOutputs output, BoundExecutionGates gates) { diff --git a/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputs.java b/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputs.java index ca15ffbf..7f7d5ecc 100644 --- a/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputs.java +++ b/apps/backend/crydaq-service/src/main/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputs.java @@ -204,7 +204,7 @@ synchronized List finish() { row.setType(FileTypeEnum.CRYBIN.getType()); row.setShardsNo(shards.size()); String filename = Path.of(name).getFileName().toString(); row.setBusinessId(filename.substring(0, filename.length() - ".crybin".length())); - row.setFileName(shards.getFirst()); row.setFileUrl(root.resolve(shards.getFirst()).normalize().toString()); rows.add(row); + row.setFileName(shards.getFirst()); row.setFileUrl(shards.getFirst()); rows.add(row); } writer.close(); active(); state = State.FINISHED; }); 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 index 6f6fc1dd..bcec6098 100644 --- 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 @@ -62,6 +62,7 @@ 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; @@ -137,6 +138,112 @@ class MeasureReanalysisExecutorTest { 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); @@ -609,6 +716,34 @@ void savedStages() throws IOException { 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) { @@ -823,6 +958,10 @@ private static byte[] stageWav(String phase) { } 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) @@ -830,7 +969,7 @@ private static byte[] wav(int samples) { .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(i)); + for (int i = 0; i < samples; i++) buffer.putShort(sample(offset + i)); return buffer.array(); } 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 new file mode 100644 index 00000000..2532b115 --- /dev/null +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputPathContractTest.java @@ -0,0 +1,215 @@ +package com.opentest.service.recording.measurement; + +import cn.hutool.core.bean.BeanUtil; +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder; +import com.opentest.common.enums.ChannelTypeEnum; +import com.opentest.common.model.dto.ChannelDto; +import com.opentest.common.model.dto.DashboardConfigDto; +import com.opentest.common.model.dto.DashboardResultDto; +import com.opentest.common.model.dto.FlowExecuteSnapshotDataDto; +import com.opentest.common.model.dto.GraphicConfigDto; +import com.opentest.common.model.dto.GraphicResultCodeDto; +import com.opentest.common.model.dto.RenderDataDto; +import com.opentest.common.model.dto.SimpleChannelDto; +import com.opentest.common.model.dto.UnitInfo; +import com.opentest.common.model.entity.File; +import com.opentest.common.model.entity.FlowExecuteResult; +import com.opentest.dal.mapper.sqlite.DashboardResultMapper; +import com.opentest.dal.mapper.sqlite.FileMapper; +import com.opentest.dal.mapper.sqlite.FlowExecuteResultMapper; +import com.opentest.dashboard.api.definition.ResultType; +import com.opentest.dashboard.api.frame.ResultFrame; +import com.opentest.dashboard.api.measurement.CompletedResultFileIdentity; +import com.opentest.platform.file.crybin.CrybinReader; +import com.opentest.service.FileService; +import com.opentest.service.FlowExecuteResultService; +import com.opentest.service.ResultLimitRenderDataAttachService; +import com.opentest.service.VariablesService; +import com.opentest.service.axis.RenderDataAxisBindingService; +import com.opentest.service.component.dashboard.DashboardHistoricalResultArtifactResolver; +import com.opentest.service.component.dashboard.ProviderDashboardExecuteStrategy; +import com.opentest.service.dashboard.framework.ProviderDashboardResultReader; +import com.opentest.service.unit.contract.AlgorithmResultContractRegistry; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.apache.ibatis.mapping.Environment; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mybatis.spring.SqlSessionTemplate; +import org.mybatis.spring.transaction.SpringManagedTransactionFactory; +import org.sqlite.SQLiteDataSource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.util.ReflectionTestUtils; + +import static com.opentest.service.recording.measurement.MeasureReanalysisOutputPlanTest.CHANNEL; +import static com.opentest.service.recording.measurement.MeasureReanalysisOutputPlanTest.plan; +import static com.opentest.service.recording.measurement.MeasureReanalysisOutputPlanTest.result; +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.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class MeasureReanalysisOutputPathContractTest { + @TempDir Path directory; + + @ParameterizedTest + @CsvSource({"dashboard,FREQUENCY,FFT Spectrum,Hz", "dashboard,WAVEFORM,Acquired Waveform,ms", + "flow,FREQUENCY,FFT Spectrum,Hz", "flow,WAVEFORM,Acquired Waveform,ms"}) + void savedChildOutputRoundTripsThroughHistoricalMapperWithOneStorageRoot( + String exit, ResultType type, String code, String unitX) throws Exception { + Path root = Files.createDirectory(directory.resolve("storage root")); + var plan = plan(result(code, "frozen-hash", type, unitX, "FS")); + File registration; + try (var output = new MeasureReanalysisOutputs(plan, root, 7L, () -> false)) { + var target = plan.outputs.results().getFirst(); + var raw = ResultFrame.explicitX(code, target.hash(), target.vchId(), unitX, "FS", + new double[]{0, 1, 2}, new double[]{0.25, 0.5, 0.75}, 20D, null); + var frame = raw.withDashboardResultId(target.dashboardResultId()); + raw.release(); + try { output.accept(frame); } finally { frame.release(); } + registration = output.finish().getFirst(); + } + + var variables = mock(VariablesService.class); + when(variables.getDefaultStoragePath()).thenReturn(root.toString()); + var files = files(variables); + assertTrue(files.save(registration)); + File saved = files.findById(registration.getId(), false); + assertNotNull(saved); + assertEquals(registration.getFileUrl(), saved.getFileUrl()); + assertEquals(registration.getBusinessId(), saved.getBusinessId()); + assertEquals(30L, saved.getRequestId()); + assertEquals(1, saved.getShardsNo()); + + RenderDataDto rendered; + if (exit.equals("dashboard")) { + rendered = dashboardHistory(plan, saved, files, variables, unitX); + } else { + var service = new FlowExecuteResultService(); + ReflectionTestUtils.setField(service, "variablesService", variables); + rendered = new RenderDataDto(); + ReflectionTestUtils.invokeMethod(service, "applyRenderDataFile", rendered, saved, "FS"); + } + + String relative = saved.getFileName(); + assertFalse(Path.of(saved.getFileUrl()).isAbsolute()); + assertEquals(relative, saved.getFileUrl()); + assertTrue(relative.startsWith("10/30/")); + assertEquals(saved.getId(), rendered.getFileId()); + 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()); + assertTrue(Files.isRegularFile(Path.of(rendered.getFilePath()))); + try (var input = Files.newInputStream(Path.of(rendered.getFilePath()))) { + var read = CrybinReader.read(input, saved.getFileName()); + assertFalse(read.hasIncompleteTail()); + assertFalse(read.hasFormatError()); + assertEquals(1, read.getBlocks().size()); + assertArrayEquals(new double[]{0, 1, 2}, (double[]) read.getBlocks().getFirst().getXData()); + assertArrayEquals(new double[]{0.25, 0.5, 0.75}, (double[]) read.getBlocks().getFirst().getYData()); + assertEquals(48000D, read.getMetadata().getSamplingRate()); + assertEquals(20D, read.getBlocks().getFirst().getBlockMetadata().getTaskRelativeTimeMs()); + } + } + + private FileService files(VariablesService variables) { + var database = new SQLiteDataSource(); + database.setUrl("jdbc:sqlite:" + directory.resolve("reanalysis-output-path.db")); + new JdbcTemplate(database).execute(""" + CREATE TABLE file ( + id INTEGER PRIMARY KEY, tenant_id INTEGER, created_time TEXT, updated_time TEXT, + business_id TEXT, request_id INTEGER, business_type INTEGER, file_name TEXT, + file_url TEXT, type INTEGER, shards_no INTEGER, + temporary_result INTEGER NOT NULL DEFAULT 0 CHECK (temporary_result IN (0,1)) + ) + """); + var configuration = new MybatisConfiguration(); + configuration.setMapUnderscoreToCamelCase(true); + configuration.setEnvironment(new Environment("reanalysis-output-path", + new SpringManagedTransactionFactory(), database)); + configuration.addMapper(FileMapper.class); + var mapper = new SqlSessionTemplate(new MybatisSqlSessionFactoryBuilder().build(configuration)) + .getMapper(FileMapper.class); + var files = new FileService() { + @Override public FileMapper getBaseMapper() { return mapper; } + }; + ReflectionTestUtils.setField(files, "baseMapper", mapper); + ReflectionTestUtils.setField(files, "variablesService", variables); + return files; + } + + private RenderDataDto dashboardHistory(MeasureReanalysisOutputPlan plan, File saved, FileService files, + VariablesService variables, String unitX) { + var result = plan.results.getFirst(); + var row = result.row(); + var dto = BeanUtil.copyProperties(row, DashboardResultDto.class); + dto.setProviderDefined(true); + dto.setProviderId("opentest.test.frozen"); + var member = new SimpleChannelDto(); + member.setVchId(CHANNEL.vchId()); + member.setChannelIo(0); + member.setChannelType(ChannelTypeEnum.VOLTAGE.getValue()); + member.setChannelUnitY("FS"); + dto.setChannels(List.of(member)); + var dashboard = new DashboardConfigDto(); + dashboard.setId(20L); + dashboard.setProviderId(dto.getProviderId()); + var snapshot = new FlowExecuteSnapshotDataDto(); + snapshot.setDashboardConfig(dashboard); + var execution = new FlowExecuteResult(); + execution.setId(30L); + execution.setProjectId(10L); + execution.setTenantId(7L); + execution.setSnapshotData(snapshot.toJson()); + + var results = mock(DashboardResultMapper.class); + var executions = mock(FlowExecuteResultMapper.class); + var provider = mock(ProviderDashboardResultReader.class); + when(results.selectById(row.getId())).thenReturn(row); + when(executions.selectById(30L)).thenReturn(execution); + when(provider.ownsSavedResultFiles(dashboard)).thenReturn(true); + when(provider.describeSavedResultFiles(dashboard, row)).thenReturn(List.of( + new CompletedResultFileIdentity(row.getHash() + "_" + row.getPrimaryCode()))); + when(provider.describeSavedTableDefinition(dashboard, row)).thenReturn(Optional.of(result.definition())); + when(provider.readSavedTableDefinition(row, dashboard, result.definition())).thenReturn(dto); + var resolver = new DashboardHistoricalResultArtifactResolver(AlgorithmResultContractRegistry.createDefault()); + ReflectionTestUtils.setField(resolver, "savedResults", results); + ReflectionTestUtils.setField(resolver, "executions", executions); + ReflectionTestUtils.setField(resolver, "providerResults", provider); + ReflectionTestUtils.setField(resolver, "files", files); + + var binding = new GraphicResultCodeDto(); + binding.setDashboardResult(dto); + binding.setDashboardResultId(row.getId()); + binding.setResultCode(row.getPrimaryCode()); + var units = new UnitInfo(); + units.setUnit("FS"); + units.setChannelIds(List.of(CHANNEL.vchId())); + binding.setUnitInfos(List.of(units)); + var channel = new ChannelDto(); + channel.setVchId(CHANNEL.vchId()); + channel.setChannelType(ChannelTypeEnum.VOLTAGE.getValue()); + var graphic = mock(GraphicConfigDto.class); + when(graphic.getRealUnitX()).thenReturn(unitX); + when(graphic.getAlgorithmCode()).thenReturn(row.getAlgorithmCode()); + var strategy = new ProviderDashboardExecuteStrategy(null); + ReflectionTestUtils.setField(strategy, "variablesService", variables); + ReflectionTestUtils.setField(strategy, "dashboardHistoricalResultArtifactResolver", resolver); + ReflectionTestUtils.setField(strategy, "resultLimitRenderDataAttachService", mock(ResultLimitRenderDataAttachService.class)); + ReflectionTestUtils.setField(strategy, "renderDataAxisBindingService", mock(RenderDataAxisBindingService.class)); + return ReflectionTestUtils.invokeMethod(strategy, "buildRenderData", graphic, + Map.of(saved.getBusinessId(), saved), "30", binding, CHANNEL.vchId(), 0, + Map.of(CHANNEL.vchId(), channel), Map.of(), true); + } +} diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputsTest.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputsTest.java index 0afd258d..e56df1ec 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputsTest.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/recording/measurement/MeasureReanalysisOutputsTest.java @@ -71,9 +71,10 @@ private static CompletedResultCurve curve(MeasureReanalysisOutputPlan plan, int } private CrybinReadResult read(File file) throws Exception { - assertTrue(Path.of(file.getFileUrl()).isAbsolute()); - assertTrue(Files.isRegularFile(Path.of(file.getFileUrl()))); - try (var stream = Files.newInputStream(Path.of(file.getFileUrl()))) { + assertFalse(Path.of(file.getFileUrl()).isAbsolute()); + var path = root.resolve(file.getFileUrl()); + assertTrue(Files.isRegularFile(path)); + try (var stream = Files.newInputStream(path)) { var read = CrybinReader.read(stream, file.getFileName()); assertFalse(read.hasIncompleteTail()); assertFalse(read.hasFormatError()); return read; } @@ -192,7 +193,7 @@ private CrybinReadResult read(File file) throws Exception { var row = output.finish().getFirst(); assertEquals(FileBusinessTypeEnum.PLAY_AND_COLLECT.getType(), row.getBusinessType()); var read = read(row); assertEquals(1D / 48, read.getMetadata().getXStep()); - try (var stream = Files.newInputStream(Path.of(row.getFileUrl())); var raw = new CrybinBlockStreamReader(stream)) { + try (var stream = Files.newInputStream(root.resolve(row.getFileUrl())); var raw = new CrybinBlockStreamReader(stream)) { assertEquals(0, raw.nextBlock().getXCount()); } assertArrayEquals(new double[]{0.25, 0.5}, (double[]) read.getBlocks().getFirst().getYData()); diff --git a/apps/desktop/tests/unit/native-runtime.test.mjs b/apps/desktop/tests/unit/native-runtime.test.mjs index 0cae4e02..88c08072 100644 --- a/apps/desktop/tests/unit/native-runtime.test.mjs +++ b/apps/desktop/tests/unit/native-runtime.test.mjs @@ -8,6 +8,7 @@ const fs = require('node:fs') const path = require('node:path') const vm = require('node:vm') const { assertNativeRuntimeFiles } = require('../../src/config/native-runtime') +const { algorithmLibraries } = require('../../src/config/algorithm-libraries') function createRuntimeFixture(t) { const fixtureRoot = path.resolve(__dirname, '../../.runtime/tests') @@ -22,8 +23,8 @@ function createRuntimeFixture(t) { fs.writeFileSync(path.join(directory, name, 'index.js'), '// fixture') fs.writeFileSync(path.join(directory, name, binary), 'fixture native binary') } - for (const dll of require('../../src/config/algorithm-libraries').algorithmLibraries('win32')) { - fs.writeFileSync(path.join(directory, 'algorithm-bridge', dll), 'fixture dynamic library') + for (const library of algorithmLibraries()) { + fs.writeFileSync(path.join(directory, 'algorithm-bridge', library), 'fixture dynamic library') } t.onTestFinished(() => { const target = path.resolve(directory) @@ -62,17 +63,17 @@ test('分别缺少三个本机模块时,明确指出文件和构建命令', t test('缺失的动态库、空文件和伪装成文件的目录均不能通过启动检查', t => { const directory = createRuntimeFixture(t) - fs.unlinkSync(path.join(directory, 'algorithm-bridge/front-algorithm.dll')) - fs.writeFileSync(path.join(directory, 'algorithm-bridge/cry_analyst.dll'), '') - fs.unlinkSync(path.join(directory, 'algorithm-bridge/fftc3.dll')) - fs.mkdirSync(path.join(directory, 'algorithm-bridge/fftc3.dll')) + const [missingLibrary, , emptyLibrary, directoryLibrary] = algorithmLibraries() + const missingFiles = [missingLibrary, emptyLibrary, directoryLibrary] + .map(library => `algorithm-bridge/${library}`) + fs.unlinkSync(path.join(directory, missingFiles[0])) + fs.writeFileSync(path.join(directory, missingFiles[1]), '') + fs.unlinkSync(path.join(directory, missingFiles[2])) + fs.mkdirSync(path.join(directory, missingFiles[2])) assert.throws(() => assertNativeRuntimeFiles(directory), error => { - assert.deepEqual(error.missingFiles, [ - 'algorithm-bridge/front-algorithm.dll', - 'algorithm-bridge/cry_analyst.dll', - 'algorithm-bridge/fftc3.dll' - ]) - assert.match(error.message, /vendor\/algorithm-bridge/) + assert.equal(error.code, 'OPENTEST_NATIVE_RUNTIME_INCOMPLETE') + assert.deepEqual(error.missingFiles, missingFiles) + if (process.platform === 'win32') assert.match(error.message, /vendor\/algorithm-bridge/) return true }) }) diff --git a/apps/desktop/tests/unit/windows-signing.test.mjs b/apps/desktop/tests/unit/windows-signing.test.mjs index 57635252..3e881c09 100644 --- a/apps/desktop/tests/unit/windows-signing.test.mjs +++ b/apps/desktop/tests/unit/windows-signing.test.mjs @@ -27,6 +27,7 @@ let directory let config const thumbprint = 'A'.repeat(40) beforeEach(() => { + vi.stubGlobal('process', { ...process, platform: 'win32' }) calls.length = 0 directory = mkdtempSync(path.join(os.tmpdir(), 'opentest-sign-verify-')) const signtool = path.join(directory, 'signtool.exe')