Files
fs25-adsmartpickup/tests/test_adPrices.lua
T

78 lines
4.1 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Kør: luajit tests/test_adPrices.lua (fra repo-roden)
-- Prisregel: toppris fra årets prisforløb, pris-% pr. salgssted, bedste tilbud.
dofile("FS25_ADSmartPickup/adPrices.lua")
local failures = 0
local function check(name, actual, expected)
if actual == expected then
print("OK " .. name)
else
failures = failures + 1
print(string.format("FAIL %s: forventede %s, fik %s", name, tostring(expected), tostring(actual)))
end
end
-- P1: højeste faktor
check("P1 maxFactor", ADPrices.maxFactor({EARLY_SPRING = 0.9, LATE_AUTUMN = 1.2, MID_SUMMER = 1.0}), 1.2)
check("P1 uden faktorer = 1", ADPrices.maxFactor(nil), 1)
-- P2: toppris = grundpris nu × (maks ÷ nu)
check("P2 topPrice", ADPrices.topPrice(500, 1.0, 1.25), 625)
check("P2 factorNow 0 -> grundpris", ADPrices.topPrice(500, 0, 1.25), 500)
check("P2 grundpris 0 -> 0", ADPrices.topPrice(0, 1, 1.25), 0)
-- P3: procent, afrundet; stor efterspørgsel > 100
check("P3 75 %", ADPrices.percentOfTop(470, 625), 75)
check("P3 112 %", ADPrices.percentOfTop(700, 625), 112)
check("P3 top 0 -> 0", ADPrices.percentOfTop(1, 0), 0)
-- P4: spil-adapter med stationens grundpris
SellingStation = {}
local SUGAR = 5
g_fillTypeManager = {getFillTypeByIndex = function(_, index)
if index == SUGAR then return {economy = {factors = {A = 1.0, B = 1.25}}, pricePerLiter = 400} end
end}
g_currentMission = {environment = {currentPeriod = "A"}, economyManager = {getPriceMultiplier = function() return 1.0 end}}
local station = {
fillTypePrices = {[SUGAR] = 500},
getEffectiveFillTypePrice = function(_, fillType) return 470 end,
isa = function(_, class) return class == SellingStation end,
isSellingPoint = true,
}
local quote = ADPrices.getQuote(station, SUGAR)
check("P4 pris", quote.price, 470)
check("P4 top (pris ved 100 %)", math.floor(quote.top + 0.5), 627)
check("P4 procent", quote.percent, 75)
check("P4 er salgssted", ADPrices.isSellingStation(station), true)
check("P4 skjult = ikke salgssted", ADPrices.isSellingStation({isa = function() return true end, isSellingPoint = true, hideFromPricesMenu = true}), false)
check("P4 fabrik-station = ikke salgssted", ADPrices.isSellingStation({isa = function() return true end, isSellingPoint = false}), false)
-- P5: uden stationens grundpris: kun kurven tæller (faktor nu ÷ faktor max)
local bare = {getEffectiveFillTypePrice = function() return 470 end}
local quote2 = ADPrices.getQuote(bare, SUGAR)
check("P5 procent = kurven alene", quote2.percent, 80)
check("P5 top = effektiv ÷ kurve", math.floor(quote2.top + 0.5), 588)
-- P7: kurven er grundlaget (Distribution Redux-metoden): faktor nu ÷ årets max, uafhængigt af sværhedsgrad
check("P7 curvePercent", ADPrices.curvePercent({1.0, 1.25, 0.9}, 1), 80)
check("P7 curvePercent ved top", ADPrices.curvePercent({1.0, 1.25, 0.9}, 2), 100)
check("P7 flad kurve = 100", ADPrices.curvePercent({1.0, 1.01, 1.0}, 3), 100)
check("P7 ingen kurve = 100", ADPrices.curvePercent(nil, 1), 100)
check("P7 ukendt periode = 100", ADPrices.curvePercent({1.0, 1.25}, nil), 100)
-- P8: stor efterspørgsel ganger stationens egen faktor på: 500 grundpris, 700 effektiv, kurve 80 % -> 112
local demandStation = {fillTypePrices = {[SUGAR] = 500}, getEffectiveFillTypePrice = function() return 700 end}
check("P8 stor efterspørgsel > 100", ADPrices.getQuote(demandStation, SUGAR).percent, 112)
-- P9: sværhedsgraden påvirker ikke procenten (begge priser skaleres ens)
local hardStation = {fillTypePrices = {[SUGAR] = 250}, getEffectiveFillTypePrice = function() return 235 end}
check("P9 uafhængig af sværhedsgrad", ADPrices.getQuote(hardStation, SUGAR).percent, 75)
check("P5 fejlende station -> nil", ADPrices.getQuote({getEffectiveFillTypePrice = function() error("x") end}, SUGAR), nil)
-- P6: bedste tilbud = højeste procent, dernæst pris
local best = ADPrices.bestQuote({{percent = 80, price = 1}, {percent = 92, price = 2}, {percent = 92, price = 3}})
check("P6 højeste procent", best.percent, 92)
check("P6 ved lighed højeste pris", best.price, 3)
check("P6 tom -> nil", ADPrices.bestQuote({}), nil)
print(failures == 0 and "\nALLE TESTS OK" or ("\n" .. failures .. " FEJL"))
os.exit(failures == 0 and 0 or 1)