|
| 1 | +package io.runcycles.protocol.data.repository; |
| 2 | + |
| 3 | +import io.runcycles.protocol.data.service.EvidenceEmitter; |
| 4 | +import io.runcycles.protocol.model.*; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.util.EnumSet; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +import static org.mockito.ArgumentMatchers.*; |
| 15 | +import static org.mockito.Mockito.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Covers the reservation -> evidence linkage (spec v0.1.25.9): persisting the |
| 19 | + * computed evidence ref onto the reservation hash, hydrating it back into the |
| 20 | + * {@code evidence} field, and the {@code include=evidence} list projection. |
| 21 | + */ |
| 22 | +@DisplayName("RedisReservationRepository — evidence linkage") |
| 23 | +class RedisReservationEvidenceLinkTest extends BaseRedisReservationRepositoryTest { |
| 24 | + |
| 25 | + private static final String HEX_A = "a".repeat(64); |
| 26 | + private static final String HEX_B = "b".repeat(64); |
| 27 | + private static final String HEX_C = "c".repeat(64); |
| 28 | + |
| 29 | + // ---- hydration (getReservationById builds detail.evidence) ---- |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("getReservationById hydrates reserve + commit evidence refs from the hash") |
| 33 | + void hydratesEvidence() { |
| 34 | + when(jedisPool.getResource()).thenReturn(jedis); |
| 35 | + doNothing().when(jedis).close(); |
| 36 | + Map<String, String> fields = reservationFields("res-ev", "COMMITTED"); |
| 37 | + fields.put("charged_amount", "3000"); |
| 38 | + fields.put("reserve_evidence_id", HEX_A); |
| 39 | + fields.put("reserve_evidence_url", "http://h/v1/evidence/" + HEX_A); |
| 40 | + fields.put("commit_evidence_id", HEX_B); |
| 41 | + fields.put("commit_evidence_url", "http://h/v1/evidence/" + HEX_B); |
| 42 | + when(jedis.hgetAll("reservation:res_res-ev")).thenReturn(fields); |
| 43 | + |
| 44 | + ReservationDetail detail = repository.getReservationById("res-ev"); |
| 45 | + |
| 46 | + assertThat(detail.getEvidence()).isNotNull(); |
| 47 | + assertThat(detail.getEvidence().getReserve().getEvidenceId()).isEqualTo(HEX_A); |
| 48 | + assertThat(detail.getEvidence().getReserve().getCyclesEvidenceUrl()) |
| 49 | + .isEqualTo("http://h/v1/evidence/" + HEX_A); |
| 50 | + assertThat(detail.getEvidence().getCommit().getEvidenceId()).isEqualTo(HEX_B); |
| 51 | + assertThat(detail.getEvidence().getRelease()).isNull(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("getReservationById leaves evidence null when no refs were recorded") |
| 56 | + void noEvidenceWhenAbsent() { |
| 57 | + when(jedisPool.getResource()).thenReturn(jedis); |
| 58 | + doNothing().when(jedis).close(); |
| 59 | + when(jedis.hgetAll("reservation:res_bare")).thenReturn(reservationFields("bare", "ACTIVE")); |
| 60 | + |
| 61 | + assertThat(repository.getReservationById("bare").getEvidence()).isNull(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("a half-written ref (id without url) is ignored") |
| 66 | + void partialRefIgnored() { |
| 67 | + when(jedisPool.getResource()).thenReturn(jedis); |
| 68 | + doNothing().when(jedis).close(); |
| 69 | + Map<String, String> fields = reservationFields("res-half", "ACTIVE"); |
| 70 | + fields.put("reserve_evidence_id", HEX_A); // url missing |
| 71 | + when(jedis.hgetAll("reservation:res_res-half")).thenReturn(fields); |
| 72 | + |
| 73 | + assertThat(repository.getReservationById("res-half").getEvidence()).isNull(); |
| 74 | + } |
| 75 | + |
| 76 | + // ---- persistence (persistEvidenceRef writes id + url) ---- |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("persistEvidenceRef HSETs both id and url onto the reservation hash") |
| 80 | + void persistsRef() throws Exception { |
| 81 | + when(jedisPool.getResource()).thenReturn(jedis); |
| 82 | + doNothing().when(jedis).close(); |
| 83 | + EvidenceEmitter.EvidenceRef ref = |
| 84 | + new EvidenceEmitter.EvidenceRef(HEX_C, "http://h/v1/evidence/" + HEX_C); |
| 85 | + |
| 86 | + invokePersist("res-p", "commit", ref); |
| 87 | + |
| 88 | + verify(jedis).hset(eq("reservation:res_res-p"), argThat((Map<String, String> m) -> |
| 89 | + HEX_C.equals(m.get("commit_evidence_id")) |
| 90 | + && ("http://h/v1/evidence/" + HEX_C).equals(m.get("commit_evidence_url")))); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @DisplayName("persistEvidenceRef is a no-op for a null ref or null reservation id") |
| 95 | + void persistNoOp() throws Exception { |
| 96 | + invokePersist("res-p", "reserve", null); |
| 97 | + invokePersist(null, "reserve", |
| 98 | + new EvidenceEmitter.EvidenceRef(HEX_C, "http://h/v1/evidence/" + HEX_C)); |
| 99 | + // Never touched the pool / wrote anything. |
| 100 | + verify(jedisPool, never()).getResource(); |
| 101 | + verify(jedis, never()).hset(anyString(), anyMap()); |
| 102 | + } |
| 103 | + |
| 104 | + // ---- projection (toSummary gates evidence on include=evidence) ---- |
| 105 | + |
| 106 | + @Test |
| 107 | + @DisplayName("toSummary projects evidence only when include=evidence") |
| 108 | + void projectionGated() throws Exception { |
| 109 | + ReservationDetail detail = new ReservationDetail(); |
| 110 | + detail.setReservationId("res-x"); |
| 111 | + detail.setEvidence(ReservationEvidence.builder() |
| 112 | + .reserve(CyclesEvidenceRef.builder().evidenceId(HEX_A) |
| 113 | + .cyclesEvidenceUrl("http://h/v1/evidence/" + HEX_A).build()) |
| 114 | + .build()); |
| 115 | + |
| 116 | + assertThat(invokeToSummary(detail, EnumSet.of(ReservationInclude.EVIDENCE)).getEvidence()) |
| 117 | + .isNotNull(); |
| 118 | + assertThat(invokeToSummary(detail, EnumSet.noneOf(ReservationInclude.class)).getEvidence()) |
| 119 | + .isNull(); |
| 120 | + } |
| 121 | + |
| 122 | + // ---- reflection helpers ---- |
| 123 | + |
| 124 | + private void invokePersist(String id, String artifact, EvidenceEmitter.EvidenceRef ref) throws Exception { |
| 125 | + Method m = RedisReservationRepository.class.getDeclaredMethod( |
| 126 | + "persistEvidenceRef", String.class, String.class, EvidenceEmitter.EvidenceRef.class); |
| 127 | + m.setAccessible(true); |
| 128 | + m.invoke(repository, id, artifact, ref); |
| 129 | + } |
| 130 | + |
| 131 | + private ReservationSummary invokeToSummary(ReservationDetail detail, Set<ReservationInclude> include) |
| 132 | + throws Exception { |
| 133 | + Method m = RedisReservationRepository.class.getDeclaredMethod( |
| 134 | + "toSummary", ReservationDetail.class, Set.class); |
| 135 | + m.setAccessible(true); |
| 136 | + return (ReservationSummary) m.invoke(repository, detail, include); |
| 137 | + } |
| 138 | +} |
0 commit comments