|
| 1 | +package com.evsuite.chargepilot |
| 2 | + |
| 3 | +import com.google.gson.JsonParser |
| 4 | +import java.nio.file.Files |
| 5 | +import java.util.zip.ZipFile |
| 6 | +import org.junit.Assert.assertEquals |
| 7 | +import org.junit.Assert.assertNotNull |
| 8 | +import org.junit.Assert.assertTrue |
| 9 | +import org.junit.Test |
| 10 | + |
| 11 | +class NavGuidanceProbeArtifactTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + fun `a saved trace leaves the car inside the diagnostic USB bundle`() { |
| 15 | + val filesDir = Files.createTempDirectory("nav-probe-files").toFile() |
| 16 | + val usb = Files.createTempDirectory("nav-probe-usb").toFile() |
| 17 | + try { |
| 18 | + val evidence = filesDir.resolve("evidence") |
| 19 | + val saved = EvidenceCaptureFileStore(evidence) { 1_700_000_000_000L } |
| 20 | + .write(artifact().toJson(), NavGuidanceProbeArtifact.KIND, "SWI69") |
| 21 | + |
| 22 | + val bundle = DiagnosticExporter.writeBundle("report", evidence, usb, 0L) |
| 23 | + |
| 24 | + assertNotNull(saved) |
| 25 | + assertNotNull(bundle) |
| 26 | + ZipFile(bundle).use { zip -> |
| 27 | + val entry = zip.entries().toList().single { it.name.startsWith("evidence/") } |
| 28 | + assertEquals("evidence/${saved!!.name}", entry.name) |
| 29 | + val json = JsonParser.parseString( |
| 30 | + zip.getInputStream(entry).reader().use { it.readText() } |
| 31 | + ).asJsonObject |
| 32 | + assertEquals(NavGuidanceProbeArtifact.PROBE, json["probe"].asString) |
| 33 | + assertEquals(2, json["trace"].asJsonArray.size()) |
| 34 | + assertEquals(1, json["undecodedCodes"].asJsonArray.size()) |
| 35 | + assertEquals(999, json["undecodedCodes"].asJsonArray[0].asInt) |
| 36 | + } |
| 37 | + } finally { |
| 38 | + filesDir.deleteRecursively() |
| 39 | + usb.deleteRecursively() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** A full ring of the widest lines the probe can produce must still fit the bundle. */ |
| 44 | + @Test |
| 45 | + fun `a full trace stays under the per-file evidence ceiling`() { |
| 46 | + val line = "%5ds n=%-4d status=%-4s dist=%-8s min=%-6s turn=%-8s road=%s".format( |
| 47 | + 10_800, 999_999, "9999", "99999999", "999999", "99999999", "é".repeat(40), |
| 48 | + ) |
| 49 | + val json = artifact(trace = List(300) { line }, traceComplete = false).toJson() |
| 50 | + |
| 51 | + assertTrue( |
| 52 | + json.toByteArray(Charsets.UTF_8).size < DiagnosticExporter.MAX_EVIDENCE_BYTES |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + private fun artifact( |
| 57 | + trace: List<String> = listOf(" 1s n=1 road=A9", " 2s n=2 road=A9"), |
| 58 | + traceComplete: Boolean = true, |
| 59 | + ) = NavGuidanceProbeArtifact.of( |
| 60 | + savedAtMs = 1_700_000_000_000L, |
| 61 | + firmware = "SWI69", |
| 62 | + adapterBound = true, |
| 63 | + listenerRegistered = true, |
| 64 | + callbacks = trace.size, |
| 65 | + census = mapOf(1 to 4, 999 to 2), |
| 66 | + censusBeyondCeiling = 0, |
| 67 | + trace = trace, |
| 68 | + traceComplete = traceComplete, |
| 69 | + ) |
| 70 | +} |
0 commit comments