diff options
| author | Kostya <mail@sartin.in> | 2026-10-13 10:42:15 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-13 10:42:40 +0300 |
| commit | a8bc9dce528abb6340315f7daa61ad76be4a8739 (patch) | |
| tree | 23395c6cc47adc04419ff7591f4e1b095329cd1a /ui-client/src/test/java/market | |
| parent | afefb6d1c4099fbedef39de5fc0cebabe44895fc (diff) | |
| download | guess-market-a8bc9dce528abb6340315f7daa61ad76be4a8739.tar.gz guess-market-a8bc9dce528abb6340315f7daa61ad76be4a8739.tar.xz guess-market-a8bc9dce528abb6340315f7daa61ad76be4a8739.zip | |
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.
Diffstat (limited to 'ui-client/src/test/java/market')
| -rw-r--r-- | ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java | 93 |
1 files changed, 93 insertions, 0 deletions
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 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 6 | |||
| 7 | import java.math.BigDecimal; | ||
| 8 | import java.nio.file.Path; | ||
| 9 | import java.util.concurrent.CountDownLatch; | ||
| 10 | import java.util.concurrent.TimeUnit; | ||
| 11 | import market.guess.model.event.EventStatus; | ||
| 12 | import market.guess.server.TestServer; | ||
| 13 | import org.junit.jupiter.api.AfterAll; | ||
| 14 | import org.junit.jupiter.api.BeforeAll; | ||
| 15 | import org.junit.jupiter.api.Test; | ||
| 16 | import org.junit.jupiter.api.io.TempDir; | ||
| 17 | |||
| 18 | class RemoteContextsTest { | ||
| 19 | @TempDir static Path base; | ||
| 20 | static TestServer server; | ||
| 21 | |||
| 22 | @BeforeAll | ||
| 23 | static void start() throws Exception { | ||
| 24 | server = new TestServer(base); | ||
| 25 | } | ||
| 26 | |||
| 27 | @AfterAll | ||
| 28 | static void stop() throws Exception { | ||
| 29 | server.close(); | ||
| 30 | } | ||
| 31 | |||
| 32 | @Test | ||
| 33 | void twoClientsShareOneMarket() throws Exception { | ||
| 34 | try (var maya = new ServerClient(server.api); | ||
| 35 | var tom = new ServerClient(server.api); | ||
| 36 | var impostor = new ServerClient(server.api)) { | ||
| 37 | assertEquals("Maya", maya.login("Maya").getData()); | ||
| 38 | assertEquals("Tom", tom.login("Tom").getData()); | ||
| 39 | var taken = impostor.login("maya"); | ||
| 40 | assertFalse(taken.isSuccess()); | ||
| 41 | assertTrue(taken.getMessage().contains("already in use"), taken.getMessage()); | ||
| 42 | |||
| 43 | var catalog = new RemoteCatalogContext(maya); | ||
| 44 | var accounts = new RemoteAccountContext(maya); | ||
| 45 | |||
| 46 | var loaded = catalog.loadEvents(Path.of("test-data/ex-3/small.xml")); | ||
| 47 | assertTrue(loaded.isSuccess(), loaded.getMessage()); | ||
| 48 | assertEquals(1, loaded.getData().eventsLoaded()); | ||
| 49 | var event = catalog.getAllEvents().getData().get(0); | ||
| 50 | assertEquals("Mujtaba is Dead", event.name()); | ||
| 51 | assertEquals("Maya", event.marketMaker()); | ||
| 52 | assertTrue(catalog.getEvent(event.key()).isSuccess()); | ||
| 53 | |||
| 54 | var broke = catalog.openEvent(event.key()); | ||
| 55 | assertFalse(broke.isSuccess()); | ||
| 56 | assertFalse(catalog.loadEvents(Path.of("test-data/ex-3/small.xml")).isSuccess()); | ||
| 57 | |||
| 58 | assertTrue(accounts.deposit("Maya", "1000").isSuccess()); | ||
| 59 | assertEquals("Maya", accounts.getAllAccounts().getData().get(0).owner()); | ||
| 60 | var opened = catalog.openEvent(event.key()); | ||
| 61 | assertTrue(opened.isSuccess(), opened.getMessage()); | ||
| 62 | assertEquals(EventStatus.ACTIVE, catalog.getEvent(event.key()).getData().summary().status()); | ||
| 63 | |||
| 64 | assertTrue(tom.pull()); | ||
| 65 | assertFalse(tom.pull()); | ||
| 66 | var tomMarket = new RemoteGuessMarketContext(tom); | ||
| 67 | assertTrue(new RemoteAccountContext(tom).deposit("Tom", "500").isSuccess()); | ||
| 68 | |||
| 69 | maya.pull(); | ||
| 70 | var heard = new CountDownLatch(1); | ||
| 71 | maya.startPolling(heard::countDown); | ||
| 72 | var bought = | ||
| 73 | tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10); | ||
| 74 | assertTrue(bought.isSuccess(), bought.getMessage()); | ||
| 75 | assertTrue(heard.await(5, TimeUnit.SECONDS), "the poller should report the change"); | ||
| 76 | assertFalse(catalog.getEvent(event.key()).getData().history().isEmpty()); | ||
| 77 | |||
| 78 | var tomAsMayaSeesHim = accounts.getAccount("Tom").getData(); | ||
| 79 | assertTrue(tomAsMayaSeesHim.entries().isEmpty()); | ||
| 80 | |||
| 81 | maya.logout(); | ||
| 82 | assertTrue(impostor.login("maya").isSuccess()); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | @Test | ||
| 87 | void anUnreachableServerIsAFailedResultNotAnException() { | ||
| 88 | try (var nobody = new ServerClient("http://localhost:1/api")) { | ||
| 89 | assertFalse(nobody.login("Zed").isSuccess()); | ||
| 90 | assertFalse(nobody.pull()); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||