From 7afdc5c1a66ddc8659e511c14bb2b7019d3ae8b2 Mon Sep 17 00:00:00 2001 From: kayjaydee Date: Thu, 23 Apr 2026 18:49:18 +0200 Subject: [PATCH] test(04-01): add failing tests for WandSelectionStore pure-data store --- .../wand/WandSelectionStoreTest.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/test/java/com/mythlane/gravityflip/wand/WandSelectionStoreTest.java diff --git a/src/test/java/com/mythlane/gravityflip/wand/WandSelectionStoreTest.java b/src/test/java/com/mythlane/gravityflip/wand/WandSelectionStoreTest.java new file mode 100644 index 0000000..d9d7eae --- /dev/null +++ b/src/test/java/com/mythlane/gravityflip/wand/WandSelectionStoreTest.java @@ -0,0 +1,114 @@ +package com.mythlane.gravityflip.wand; + +import org.junit.jupiter.api.Test; + +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +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.assertTrue; + +/** + * Pure-data tests for {@link WandSelectionStore} — no Hytale runtime dependency. + * Covers set/get per UUID, overwrite semantics, unknown UUID default, clear, + * and concurrent writes. + */ +class WandSelectionStoreTest { + + @Test + void setPos1AndPos2_thenGet_returnsBothCoords() { + WandSelectionStore store = new WandSelectionStore(); + UUID u = UUID.randomUUID(); + store.setPos1(u, 1, 2, 3); + store.setPos2(u, 4, 5, 6); + + WandSelectionStore.Selection sel = store.get(u); + assertNotNull(sel); + assertArrayEquals(new int[]{1, 2, 3}, sel.pos1); + assertArrayEquals(new int[]{4, 5, 6}, sel.pos2); + assertTrue(sel.isComplete()); + } + + @Test + void independentPerUuid() { + WandSelectionStore store = new WandSelectionStore(); + UUID a = UUID.randomUUID(); + UUID b = UUID.randomUUID(); + store.setPos1(a, 10, 10, 10); + store.setPos2(b, 20, 20, 20); + + WandSelectionStore.Selection selA = store.get(a); + WandSelectionStore.Selection selB = store.get(b); + assertArrayEquals(new int[]{10, 10, 10}, selA.pos1); + assertNull(selA.pos2); + assertNull(selB.pos1); + assertArrayEquals(new int[]{20, 20, 20}, selB.pos2); + } + + @Test + void setPos1_overwritesPreviousPos1_withoutTouchingPos2() { + WandSelectionStore store = new WandSelectionStore(); + UUID u = UUID.randomUUID(); + store.setPos1(u, 1, 1, 1); + store.setPos2(u, 2, 2, 2); + store.setPos1(u, 9, 9, 9); + + WandSelectionStore.Selection sel = store.get(u); + assertArrayEquals(new int[]{9, 9, 9}, sel.pos1); + assertArrayEquals(new int[]{2, 2, 2}, sel.pos2); + } + + @Test + void get_unknownUuid_returnsEmptySelection() { + WandSelectionStore store = new WandSelectionStore(); + WandSelectionStore.Selection sel = store.get(UUID.randomUUID()); + assertNotNull(sel); + assertNull(sel.pos1); + assertNull(sel.pos2); + assertFalse(sel.isComplete()); + } + + @Test + void clear_removesSelectionForUuid() { + WandSelectionStore store = new WandSelectionStore(); + UUID u = UUID.randomUUID(); + store.setPos1(u, 1, 2, 3); + store.setPos2(u, 4, 5, 6); + store.clear(u); + + WandSelectionStore.Selection sel = store.get(u); + assertNull(sel.pos1); + assertNull(sel.pos2); + } + + @Test + void concurrentSetPos1AndPos2_noLostWrites() throws InterruptedException { + WandSelectionStore store = new WandSelectionStore(); + UUID u = UUID.randomUUID(); + CountDownLatch start = new CountDownLatch(1); + CountDownLatch done = new CountDownLatch(2); + + Thread t1 = new Thread(() -> { + try { start.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; } + store.setPos1(u, 7, 7, 7); + done.countDown(); + }); + Thread t2 = new Thread(() -> { + try { start.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; } + store.setPos2(u, 8, 8, 8); + done.countDown(); + }); + t1.start(); + t2.start(); + start.countDown(); + assertTrue(done.await(2, TimeUnit.SECONDS)); + + WandSelectionStore.Selection sel = store.get(u); + assertArrayEquals(new int[]{7, 7, 7}, sel.pos1); + assertArrayEquals(new int[]{8, 8, 8}, sel.pos2); + } +}