From c5c9fffd8ea73540a9f7e8d2a8e77c223de580b5 Mon Sep 17 00:00:00 2001 From: CatLover <152669316+catloversg@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:47:46 +0700 Subject: [PATCH] JEST: Fix flaky stock market test --- test/jest/StockMarket.test.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/test/jest/StockMarket.test.ts b/test/jest/StockMarket.test.ts index 322b43156b..a497a29dcc 100644 --- a/test/jest/StockMarket.test.ts +++ b/test/jest/StockMarket.test.ts @@ -31,7 +31,7 @@ import { getSellTransactionGain, processTransactionForecastMovement, } from "../../src/StockMarket/StockMarketHelpers"; -import { CompanyName, OrderType, PositionType } from "../../src/Enums"; +import { CompanyName, LocationName, OrderType, PositionType } from "../../src/Enums"; // jest.mock("../src/ui/React/createPopup.tsx", () => ({ // createPopup: jest.fn(), @@ -464,6 +464,7 @@ describe("Stock Market Tests", function () { StockMarket.lastUpdate = new Date().getTime() - 5e3; processStockPrices(1e9); + let testOmegaSoftwareStock = false; // Both price and 'otlkMag' should be different for (const stockName in StockMarket) { const stock = StockMarket[stockName]; @@ -473,12 +474,41 @@ describe("Stock Market Tests", function () { const initValue = initialValues[stock.symbol]; expect(initValue.price).not.toEqual(stock.price); if (initValue.otlkMag === stock.otlkMag && initValue.b === stock.b) { + // In edge cases, the forecast of OMGA is not changed. We will rerun tests for this stock later. + if (stock.name === LocationName.IshimaOmegaSoftware && stock.otlkMag === 0.5) { + testOmegaSoftwareStock = true; + continue; + } throw new Error( "expected either price or otlkMag to be different: " + `stock: ${stockName} otlkMag: ${stock.otlkMag} b: ${stock.b}`, ); } } + + if (!testOmegaSoftwareStock) { + return; + } + deleteStockMarket(); + initStockMarket(); + initSymbolToStockMap(); + const currentOtlkMag = StockMarket[LocationName.IshimaOmegaSoftware].otlkMag; + let isForeCastChanged = false; + /** + * Update the stock market and check the forecast 10000 times. It's improbable that the forecast is still not + * changed. + */ + for (let i = 0; i < 10000; i++) { + StockMarket.lastUpdate = new Date().getTime() - 5e3; + processStockPrices(1e9); + if (currentOtlkMag !== StockMarket[LocationName.IshimaOmegaSoftware].otlkMag) { + isForeCastChanged = true; + break; + } + } + if (!isForeCastChanged) { + throw new Error(`Forecast of OMGA is not changed: ${StockMarket[LocationName.IshimaOmegaSoftware].otlkMag}`); + } }); }); });