Files
code_of_conquest_dnd/client/tests/unit/test_move_applier.gd
Phillip Tarrant 8e1238c316 fix(currency): cap repeated currency move tags at one per reply
TagExtractor collects every [MOVE:] tag occurrence with no dedupe, and
MoveValidator is a pure membership test against available_moves, so N
copies of one legal move all pass validation. MoveApplier then looped
and applied every copy. A model could therefore pick an arbitrary
amount of money in unary by repeating one tag: [MOVE: accept_item(copper)]
x47 against a 47c purse drains it to 0; [MOVE: give_item(gold)] x10
mints +100,000c. The eight-move vocabulary (§6) takes no quantity
argument precisely so the AI cannot choose a number — this closed that
gap by another door and was a §2 breach (AI owns text, never state).

MoveApplier.apply() now tracks currency moves (give_item/accept_item on
a denomination) already applied within one call, keyed on name+denom,
and skips repeats. Non-currency moves are untouched — repeated
give_item(amulet) still yields multiple copies, which stays bounded by
the pre-existing cross-reply gifts_given gate and is a separate,
already-reported issue. move_validator.gd and tag_extractor.gd are
untouched.

Also adds the Finding-2 test the currency spec (§7) named but never
delivered: after GameState.grant("gold", 1), inventory stays empty, so
accept_item(gold) in available_moves comes only from the affordability
loop, never double-offered via the inventory.keys() loop.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QYa9u7Kdxv5gX4AnwWexy8
2026-07-12 18:47:19 -05:00

144 lines
5.5 KiB
GDScript

# client/tests/unit/test_move_applier.gd
extends "res://addons/gut/test.gd"
const MoveApplier = preload("res://scripts/npc/move_applier.gd")
const ContentDB = preload("res://scripts/content/content_db.gd")
func _m(name, args := []) -> Dictionary:
return {"name": name, "args": args}
func _content() -> ContentDB:
var c = ContentDB.new()
c.quests = {"find_the_ledger": {"id": "find_the_ledger",
"name": "The Missing Ledger", "objective": "Find who took it"}}
return c
func test_offer_quest_adds_active_quest():
var log = CanonLog.new()
MoveApplier.apply([_m("offer_quest", ["find_the_ledger"])],
GameState.new(), log, _content(), "fenn")
assert_true(log.has_quest("find_the_ledger"))
assert_eq(log.active_quests[0].name, "The Missing Ledger")
func test_reveal_marks_topic():
var gs = GameState.new()
MoveApplier.apply([_m("reveal", ["varrell_twins"])], gs, CanonLog.new(), _content(), "fenn")
assert_true(gs.is_revealed("varrell_twins"))
func test_adjust_disposition_clamps_to_max_delta():
var gs = GameState.new()
MoveApplier.apply([_m("adjust_disposition", ["+50"])], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.npc_dispositions.get("fenn", 0), MoveApplier.MAX_DELTA)
func test_become_hostile_sets_floor():
var gs = GameState.new()
MoveApplier.apply([_m("become_hostile")], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.npc_dispositions.get("fenn", 0), MoveApplier.HOSTILE_FLOOR)
func test_accept_item_removes_from_inventory():
var gs = GameState.new()
gs.add_item("worn_shortsword", 2)
MoveApplier.apply([_m("accept_item", ["worn_shortsword"])], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.inventory.get("worn_shortsword", 0), 1)
func test_give_item_currency_pays_the_purse():
var gs = GameState.new()
MoveApplier.apply([_m("give_item", ["gold"])], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 10000)
assert_eq(gs.inventory.size(), 0, "money is never an inventory row")
func test_give_item_currency_is_not_a_one_shot_gift():
# gifts_given is a GLOBAL one-shot keyed by item id. If money were marked, an
# NPC could hand over coin exactly once, ever, for the whole campaign.
var gs = GameState.new()
MoveApplier.apply([_m("give_item", ["gold"])], gs, CanonLog.new(), _content(), "fenn")
MoveApplier.apply([_m("give_item", ["gold"])], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 20000)
assert_false(gs.is_gift_given("gold"), "currency is exempt from the one-shot gate")
func test_give_item_real_item_still_marks_the_one_shot_gate():
var gs = GameState.new()
MoveApplier.apply([_m("give_item", ["amulet"])], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.inventory.get("amulet", 0), 1)
assert_true(gs.is_gift_given("amulet"))
func test_accept_item_currency_spends_the_purse():
var gs = GameState.new()
gs.grant("gold", 1)
MoveApplier.apply([_m("accept_item", ["silver"])], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 9900)
func test_refuse_and_end_are_noops():
var gs = GameState.new()
MoveApplier.apply([_m("refuse"), _m("end_conversation")], gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.npc_dispositions.size(), 0)
# --- Finding 1: a repeated currency move tag in ONE reply must not let the AI
# pick an arbitrary amount by unary repetition (§2 breach). A currency move
# (give_item/accept_item on a denomination) applies AT MOST ONCE per apply()
# call, keyed on move name + denomination.
func test_repeated_accept_item_silver_applies_once():
var gs = GameState.new()
gs.purse_copper = 10000
MoveApplier.apply([_m("accept_item", ["silver"]), _m("accept_item", ["silver"])],
gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 9900, "second accept_item(silver) tag must be a no-op")
func test_repeated_give_item_gold_applies_once():
var gs = GameState.new()
var moves: Array = []
for i in range(10):
moves.append(_m("give_item", ["gold"]))
MoveApplier.apply(moves, gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 10000, "nine of the ten give_item(gold) tags must be no-ops")
func test_repeated_accept_item_copper_applies_once():
var gs = GameState.new()
gs.purse_copper = 47
var moves: Array = []
for i in range(47):
moves.append(_m("accept_item", ["copper"]))
MoveApplier.apply(moves, gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 46, "46 of the 47 accept_item(copper) tags must be no-ops")
func test_different_currency_moves_both_still_apply():
# Proves dedup is keyed per (move name + denomination), not per reply as a whole.
var gs = GameState.new()
MoveApplier.apply([_m("give_item", ["gold"]), _m("accept_item", ["silver"])],
gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.purse_copper, 9900)
func test_repeated_non_currency_give_item_still_applies_each_time():
# Pins the scope line: real-item duplicate handling is UNCHANGED by this fix
# (it stays bounded only by the cross-reply gifts_given gate, reported separately).
var gs = GameState.new()
MoveApplier.apply([_m("give_item", ["amulet"]), _m("give_item", ["amulet"]), _m("give_item", ["amulet"])],
gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.inventory.get("amulet", 0), 3, "non-currency give_item is out of scope for this fix")
func test_repeated_non_currency_accept_item_still_applies_each_time():
var gs = GameState.new()
gs.add_item("worn_shortsword", 5)
MoveApplier.apply([_m("accept_item", ["worn_shortsword"]), _m("accept_item", ["worn_shortsword"])],
gs, CanonLog.new(), _content(), "fenn")
assert_eq(gs.inventory.get("worn_shortsword", 0), 3, "non-currency accept_item is out of scope for this fix")