JEST: Fix flaky stock market test (#1834)

This commit is contained in:
catloversg 2024-12-12 17:58:35 +07:00 committed by GitHub
parent 4137a0e522
commit b66eff0379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -31,7 +31,7 @@ import {
getSellTransactionGain, getSellTransactionGain,
processTransactionForecastMovement, processTransactionForecastMovement,
} from "../../src/StockMarket/StockMarketHelpers"; } 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", () => ({ // jest.mock("../src/ui/React/createPopup.tsx", () => ({
// createPopup: jest.fn(), // createPopup: jest.fn(),
@ -464,6 +464,7 @@ describe("Stock Market Tests", function () {
StockMarket.lastUpdate = new Date().getTime() - 5e3; StockMarket.lastUpdate = new Date().getTime() - 5e3;
processStockPrices(1e9); processStockPrices(1e9);
let testOmegaSoftwareStock = false;
// Both price and 'otlkMag' should be different // Both price and 'otlkMag' should be different
for (const stockName in StockMarket) { for (const stockName in StockMarket) {
const stock = StockMarket[stockName]; const stock = StockMarket[stockName];
@ -473,12 +474,41 @@ describe("Stock Market Tests", function () {
const initValue = initialValues[stock.symbol]; const initValue = initialValues[stock.symbol];
expect(initValue.price).not.toEqual(stock.price); expect(initValue.price).not.toEqual(stock.price);
if (initValue.otlkMag === stock.otlkMag && initValue.b === stock.b) { 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( throw new Error(
"expected either price or otlkMag to be different: " + "expected either price or otlkMag to be different: " +
`stock: ${stockName} otlkMag: ${stock.otlkMag} b: ${stock.b}`, `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}`);
}
}); });
}); });
}); });