|
| 1 | +-- Standalone Game Store contract tests for the Task Board expansion. |
| 2 | +-- Run from the repository root with: lua tests/lua/test_gamestore_taskboard_expansion.lua |
| 3 | + |
| 4 | +package.path = "data/libs/?.lua;" .. package.path |
| 5 | + |
| 6 | +local passed, failed, errors = 0, 0, {} |
| 7 | + |
| 8 | +local function test(name, fn) |
| 9 | + local ok, err = pcall(fn) |
| 10 | + if ok then |
| 11 | + passed = passed + 1 |
| 12 | + else |
| 13 | + failed = failed + 1 |
| 14 | + table.insert(errors, { name = name, err = err }) |
| 15 | + end |
| 16 | +end |
| 17 | + |
| 18 | +local function assertTrue(value, message) |
| 19 | + if not value then |
| 20 | + error(message or "expected true", 2) |
| 21 | + end |
| 22 | +end |
| 23 | + |
| 24 | +local function assertEqual(expected, actual, message) |
| 25 | + if expected ~= actual then |
| 26 | + error(message or string.format("expected %s, got %s", tostring(expected), tostring(actual)), 2) |
| 27 | + end |
| 28 | +end |
| 29 | + |
| 30 | +package.loaded["gamestore.senders"] = {} |
| 31 | +GameStore = require("gamestore.constants") |
| 32 | + |
| 33 | +local playerMethods = require("gamestore.player") |
| 34 | +local purchases = require("gamestore.purchases") |
| 35 | + |
| 36 | +test("weekly expansion offer uses the official product contract", function() |
| 37 | + local category = dofile("data/modules/scripts/gamestore/catalog/extras_usefull_things.lua") |
| 38 | + local offer |
| 39 | + for _, candidate in ipairs(category.offers) do |
| 40 | + if candidate.type == GameStore.OfferTypes.OFFER_TYPE_WEEKLY_TASK_EXPANSION then |
| 41 | + offer = candidate |
| 42 | + break |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + assertTrue(offer ~= nil) |
| 47 | + assertEqual("Permanent Weekly Task Expansion", offer.name) |
| 48 | + assertEqual(750, offer.price) |
| 49 | + assertEqual(GameStore.SubActions.WEEKLY_TASK_EXPANSION, offer.id) |
| 50 | +end) |
| 51 | + |
| 52 | +test("weekly expansion availability follows the Task Board state", function() |
| 53 | + local unlocked = false |
| 54 | + local testPlayer = {} |
| 55 | + rawset(_G, "Taskboard", { |
| 56 | + expansion = { |
| 57 | + isUnlocked = function(player) |
| 58 | + assertEqual(testPlayer, player) |
| 59 | + return unlocked |
| 60 | + end, |
| 61 | + }, |
| 62 | + }) |
| 63 | + local offer = { type = GameStore.OfferTypes.OFFER_TYPE_WEEKLY_TASK_EXPANSION } |
| 64 | + |
| 65 | + local availability = playerMethods.canBuyOffer(testPlayer, offer) |
| 66 | + assertEqual(0, availability.disabled) |
| 67 | + assertEqual("", availability.disabledReason) |
| 68 | + |
| 69 | + unlocked = true |
| 70 | + availability = playerMethods.canBuyOffer(testPlayer, offer) |
| 71 | + assertEqual(1, availability.disabled) |
| 72 | + assertEqual("You already have the Permanent Weekly Task Expansion.", availability.disabledReason) |
| 73 | +end) |
| 74 | + |
| 75 | +test("weekly expansion purchase delegates to the Task Board module", function() |
| 76 | + local calls = 0 |
| 77 | + local testPlayer = {} |
| 78 | + rawset(_G, "Taskboard", { |
| 79 | + expansion = { |
| 80 | + unlock = function(player) |
| 81 | + assertEqual(testPlayer, player) |
| 82 | + calls = calls + 1 |
| 83 | + return true |
| 84 | + end, |
| 85 | + }, |
| 86 | + }) |
| 87 | + |
| 88 | + purchases.processWeeklyTaskExpansion(testPlayer) |
| 89 | + assertEqual(1, calls) |
| 90 | +end) |
| 91 | + |
| 92 | +test("weekly expansion offer is disabled when Task Board is unavailable", function() |
| 93 | + rawset(_G, "Taskboard", nil) |
| 94 | + local offer = { type = GameStore.OfferTypes.OFFER_TYPE_WEEKLY_TASK_EXPANSION } |
| 95 | + local availability = playerMethods.canBuyOffer({}, offer) |
| 96 | + assertEqual(1, availability.disabled) |
| 97 | + assertEqual("Task Board is unavailable.", availability.disabledReason) |
| 98 | + |
| 99 | + local ok, err = pcall(purchases.processWeeklyTaskExpansion, {}) |
| 100 | + assertEqual(false, ok) |
| 101 | + assertTrue(type(err) == "table") |
| 102 | + assertEqual("Task Board is unavailable.", err.message) |
| 103 | +end) |
| 104 | + |
| 105 | +print(string.format("\n%d passed, %d failed", passed, failed)) |
| 106 | +if #errors > 0 then |
| 107 | + print("\nFailed tests:") |
| 108 | + for _, entry in ipairs(errors) do |
| 109 | + print(string.format(" FAIL: %s\n %s", entry.name, entry.err)) |
| 110 | + end |
| 111 | + os.exit(1) |
| 112 | +end |
0 commit comments