From 3a734b84705ccb8174e9cf24b74d7a3c3b2c1beb Mon Sep 17 00:00:00 2001 From: Kostya Date: Sun, 11 Oct 2026 19:28:04 +0300 Subject: ui-fx: move the JavaFX UI into a module that sees only the api Exercise 3 asks for a new desktop client that reuses the Exercise 2 screens, and the course asks that clients depend on the DTO contract, never on the engine. ui-desktop mixed both: its views and controllers only talk to the api interfaces, but AppView and AppState each had a convenience constructor that built a ServiceEngine, which tied every UI class to the service module. Move the screens, controllers, components, FXML, skins and fonts into a new ui-fx module that depends on api alone. Packages are unchanged, so FXML controller names and resource paths keep working as they are. ui-desktop keeps only the Exercise 2 app: Launcher, App, which now builds the ServiceEngine and passes its contexts in, and ServiceEngine itself. The Exercise 3 client can then reuse ui-fx with remote contexts and never put the engine on its classpath. The build proves the split: ui-fx compiles with nothing but api visible. Tests move with the code they cover. AppStateTest drives the real engine, so it stays in ui-desktop. --- .../java/market/guess/ui/desktop/AnimTest.java | 70 ++++++++++++++++++++++ .../java/market/guess/ui/desktop/ChartsTest.java | 32 ++++++++++ .../java/market/guess/ui/desktop/FormatTest.java | 29 +++++++++ 3 files changed, 131 insertions(+) create mode 100644 ui-fx/src/test/java/market/guess/ui/desktop/AnimTest.java create mode 100644 ui-fx/src/test/java/market/guess/ui/desktop/ChartsTest.java create mode 100644 ui-fx/src/test/java/market/guess/ui/desktop/FormatTest.java (limited to 'ui-fx/src/test/java/market/guess/ui/desktop') diff --git a/ui-fx/src/test/java/market/guess/ui/desktop/AnimTest.java b/ui-fx/src/test/java/market/guess/ui/desktop/AnimTest.java new file mode 100644 index 0000000..1a39ba1 --- /dev/null +++ b/ui-fx/src/test/java/market/guess/ui/desktop/AnimTest.java @@ -0,0 +1,70 @@ +package market.guess.ui.desktop; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import market.guess.ui.desktop.components.graphic.GraphicHelper; +import market.guess.ui.desktop.components.graphic.MarketChartGraphic; +import market.guess.ui.desktop.components.graphic.Motion; +import market.guess.ui.desktop.components.graphic.SparklineGraphic; +import org.junit.jupiter.api.Test; + +/** + * The handoff requires "animations off" to show the frozen final frame, fully legible and never + * blank. Since every graphic renders as a function of progress, that contract is exactly: at + * progress 1, every driver lands on its designed end value. + */ +class AnimTest { + private static final double[] BAR1 = {0, .35, .30, 1, .60, .6, 1, .35}; + + @Test + void keyframesHoldTheirEndValue() { + assertEquals(.35, GraphicHelper.at(0, BAR1), 1e-9); + assertEquals(1.0, GraphicHelper.at(.30, BAR1), 1e-9); + assertEquals(.35, GraphicHelper.at(1, BAR1), 1e-9); + assertEquals(.675, GraphicHelper.at(.15, BAR1), 1e-9); // interpolates linearly between stops + assertEquals(.35, Motion.at(0, BAR1), 1e-9); + } + + @Test + void ridersEndOnTheLastPointOfTheirPath() { + assertEquals( + MarketChartGraphic.YES_CURVE[6], + MarketChartGraphic.rideCurve(MarketChartGraphic.YES_CURVE, 1)[0], + 1e-6); + assertEquals( + MarketChartGraphic.YES_CURVE[7], + MarketChartGraphic.rideCurve(MarketChartGraphic.YES_CURVE, 1)[1], + 1e-6); + + int last = SparklineGraphic.SPARK.length; + assertEquals( + SparklineGraphic.SPARK[last - 2], + SparklineGraphic.ridePolyline(SparklineGraphic.SPARK, 1)[0], + 1e-6); + assertEquals( + SparklineGraphic.SPARK[last - 1], + SparklineGraphic.ridePolyline(SparklineGraphic.SPARK, 1)[1], + 1e-6); + } + + @Test + void ridersStartOnTheFirstPointOfTheirPath() { + assertEquals( + MarketChartGraphic.YES_CURVE[0], + MarketChartGraphic.rideCurve(MarketChartGraphic.YES_CURVE, 0)[0], + 1e-6); + assertEquals( + MarketChartGraphic.YES_CURVE[1], + MarketChartGraphic.rideCurve(MarketChartGraphic.YES_CURVE, 0)[1], + 1e-6); + + assertEquals( + SparklineGraphic.SPARK[0], + SparklineGraphic.ridePolyline(SparklineGraphic.SPARK, 0)[0], + 1e-6); + assertEquals( + SparklineGraphic.SPARK[1], + SparklineGraphic.ridePolyline(SparklineGraphic.SPARK, 0)[1], + 1e-6); + } +} diff --git a/ui-fx/src/test/java/market/guess/ui/desktop/ChartsTest.java b/ui-fx/src/test/java/market/guess/ui/desktop/ChartsTest.java new file mode 100644 index 0000000..c5cb280 --- /dev/null +++ b/ui-fx/src/test/java/market/guess/ui/desktop/ChartsTest.java @@ -0,0 +1,32 @@ +package market.guess.ui.desktop; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import market.guess.ui.desktop.util.Charts; +import org.junit.jupiter.api.Test; + +class ChartsTest { + + @Test + void testToEpochSecondFromIndex() { + double t0 = Charts.toEpochSecond(0); + double t1 = Charts.toEpochSecond(1); + double t2 = Charts.toEpochSecond(2); + + assertEquals(Charts.BASE_TIME, t0, 1e-6); + assertEquals(Charts.BASE_TIME + 1800, t1, 1e-6); + assertEquals(Charts.BASE_TIME + 3600, t2, 1e-6); + } + + @Test + void testToEpochSecondIdempotentForTimestamps() { + long epoch = 1_700_000_000L; + assertEquals(epoch, Charts.toEpochSecond(epoch), 1e-6); + } + + @Test + void testBaseTimeIsReasonable() { + assertTrue(Charts.BASE_TIME > 1_700_000_000L); + } +} diff --git a/ui-fx/src/test/java/market/guess/ui/desktop/FormatTest.java b/ui-fx/src/test/java/market/guess/ui/desktop/FormatTest.java new file mode 100644 index 0000000..956a322 --- /dev/null +++ b/ui-fx/src/test/java/market/guess/ui/desktop/FormatTest.java @@ -0,0 +1,29 @@ +package market.guess.ui.desktop; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigDecimal; +import market.guess.ui.desktop.util.Format; +import org.junit.jupiter.api.Test; + +class FormatTest { + + @Test + void testMoneyFormatting() { + assertEquals("$0.00", Format.money((BigDecimal) null)); + assertEquals("$0.00", Format.money(BigDecimal.ZERO)); + assertEquals("$0.50", Format.money(new BigDecimal("0.50"))); + assertEquals("$123.45", Format.money(new BigDecimal("123.45"))); + assertEquals("-$5.00", Format.money(new BigDecimal("-5.00"))); + assertEquals("$100.00", Format.money(100.0)); + assertEquals("-$2.50", Format.money(-2.50)); + } + + @Test + void testProbFormatting() { + assertEquals("0.50", Format.prob(0.50)); + assertEquals("0.75", Format.prob(0.751)); + assertEquals("0.00", Format.prob(0.00)); + assertEquals("1.00", Format.prob(1.00)); + } +} -- cgit v1.2.3