From a8bc9dce528abb6340315f7daa61ad76be4a8739 Mon Sep 17 00:00:00 2001 From: Kostya Date: Tue, 13 Oct 2026 10:42:15 +0300 Subject: ui-client: add desktop client that talks to the server New module for the Ex3 desktop app. It reuses the ui-fx views and depends on the api only, never on the engine. ServerClient owns the OkHttp session and the last snapshot pulled from /api/sync. RemoteCatalogContext, RemoteGuessMarketContext and RemoteAccountContext implement the api interfaces on top of it: reads are answered from the snapshot, so AppState never waits on the network, and writes go over HTTP and pull a fresh snapshot before returning. A daemon thread polls once a second with blocking calls and refreshes AppState through Platform.runLater only when the server's version moved. AppState reselects the event and user by id, so a refresh keeps the selection. Server and user name come from --server= and --user= until the login screen exists. --- .../market/guess/ui/client/RemoteContextsTest.java | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java (limited to 'ui-client/src/test/java/market') diff --git a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java new file mode 100644 index 0000000..5c1057f --- /dev/null +++ b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java @@ -0,0 +1,93 @@ +package market.guess.ui.client; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; +import java.nio.file.Path; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import market.guess.model.event.EventStatus; +import market.guess.server.TestServer; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class RemoteContextsTest { + @TempDir static Path base; + static TestServer server; + + @BeforeAll + static void start() throws Exception { + server = new TestServer(base); + } + + @AfterAll + static void stop() throws Exception { + server.close(); + } + + @Test + void twoClientsShareOneMarket() throws Exception { + try (var maya = new ServerClient(server.api); + var tom = new ServerClient(server.api); + var impostor = new ServerClient(server.api)) { + assertEquals("Maya", maya.login("Maya").getData()); + assertEquals("Tom", tom.login("Tom").getData()); + var taken = impostor.login("maya"); + assertFalse(taken.isSuccess()); + assertTrue(taken.getMessage().contains("already in use"), taken.getMessage()); + + var catalog = new RemoteCatalogContext(maya); + var accounts = new RemoteAccountContext(maya); + + var loaded = catalog.loadEvents(Path.of("test-data/ex-3/small.xml")); + assertTrue(loaded.isSuccess(), loaded.getMessage()); + assertEquals(1, loaded.getData().eventsLoaded()); + var event = catalog.getAllEvents().getData().get(0); + assertEquals("Mujtaba is Dead", event.name()); + assertEquals("Maya", event.marketMaker()); + assertTrue(catalog.getEvent(event.key()).isSuccess()); + + var broke = catalog.openEvent(event.key()); + assertFalse(broke.isSuccess()); + assertFalse(catalog.loadEvents(Path.of("test-data/ex-3/small.xml")).isSuccess()); + + assertTrue(accounts.deposit("Maya", "1000").isSuccess()); + assertEquals("Maya", accounts.getAllAccounts().getData().get(0).owner()); + var opened = catalog.openEvent(event.key()); + assertTrue(opened.isSuccess(), opened.getMessage()); + assertEquals(EventStatus.ACTIVE, catalog.getEvent(event.key()).getData().summary().status()); + + assertTrue(tom.pull()); + assertFalse(tom.pull()); + var tomMarket = new RemoteGuessMarketContext(tom); + assertTrue(new RemoteAccountContext(tom).deposit("Tom", "500").isSuccess()); + + maya.pull(); + var heard = new CountDownLatch(1); + maya.startPolling(heard::countDown); + var bought = + tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10); + assertTrue(bought.isSuccess(), bought.getMessage()); + assertTrue(heard.await(5, TimeUnit.SECONDS), "the poller should report the change"); + assertFalse(catalog.getEvent(event.key()).getData().history().isEmpty()); + + var tomAsMayaSeesHim = accounts.getAccount("Tom").getData(); + assertTrue(tomAsMayaSeesHim.entries().isEmpty()); + + maya.logout(); + assertTrue(impostor.login("maya").isSuccess()); + } + } + + @Test + void anUnreachableServerIsAFailedResultNotAnException() { + try (var nobody = new ServerClient("http://localhost:1/api")) { + assertFalse(nobody.login("Zed").isSuccess()); + assertFalse(nobody.pull()); + } + } +} -- cgit v1.2.3