Cockatrice/tests/game/card_state_test.cpp
BruebachL 3cf8bdb00e
[Tests] Add a headless game logic regression suite (#7702)
* [Tests] Add headless game logic regression suite

Cover CardState, CardZoneLogic and the Command_MoveCard protobufs PlayerActions emits, against libcockatrice_game with a recording AbstractClient and no graphics. The old game_smoke_test cases are migrated into the new suite.

* [Tests] Cover game event application through GameEventHandler

Feeds GameEventContainers through the real dispatcher and asserts the resulting zone and card state: move (including face down), the set-card-attr attributes plus the all-cards and does-not-untap branches, card counters, attach/detach, zone reveal properties, and drawing named cards from the deck.

* [Tests] Replay a synthetic game state through the dispatcher

Builds a GameReplay in code, feeds its containers through the same GameEventHandler path the client uses, and asserts the materialized players, zones, cards and attachments plus event ordering for move, attributes and counters. A small headless zone view stands in for the graphics side where processPlayerInfo asks for card items.

* [Tests] Cover card flip, destroy, token and turn events

Adds the remaining state-mutating event handlers: flip (face down keeps the ref, face up adopts the printing), destroy (removes the card and un-attaches its attached cards), create token (database P/T unless provided or face down) and player properties changed under the concede/unconcede contexts, plus active player and phase.

* [Tests] Cover player lifecycle and counter events

Adds the player-level events that mutate session state: leave removes the player from the manager, kicked emits gameClosed, game closed flips the closed flag and clears the started flag, host changed moves the host role to another player, and create/set/delete update the player counters with unknown ids ignored.

* [Tests] Drive the zone view request through addCard

The test emitted createCardRequested itself and then asserted it was emitted. Registering a ZoneViewZoneLogic on the zone and adding a card exercises the real prepareAddCard -> signal path, so the assertion now fails if addCard stops asking the graphics side to build the card.

* [Tests] Skip the card database when listing tests

gtest_discover_tests runs the binary with --gtest_list_tests during the build. That process built the card database and replaced its default path provider, leaving allocations that the sanitizer leg's default LeakSanitizer check reports at exit, so discovery failed on Ubuntu 26.04. No test runs in list mode, so return before the database setup.

* [Tests] Fix fixture comment nit

Wraps the RecordingClient::sent member name in backticks in the comment.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-11-02 07:35:41 +01:00

110 lines
3.3 KiB
C++

#include "gtest/gtest.h"
#include <QMetaType>
#include <QString>
#include <QVariantList>
#include <libcockatrice/game/board/card_state.h>
#include <libcockatrice/utility/card_ref.h>
namespace
{
CardRef cardRefFor(const QString &name)
{
CardRef cardRef;
cardRef.name = name;
return cardRef;
}
} // namespace
TEST(CardStateTest, AttachmentGraphKeepsBothEndsConsistent)
{
CardState host(nullptr, nullptr, cardRefFor(QStringLiteral("Host")), 1);
CardState guest(nullptr, nullptr, cardRefFor(QStringLiteral("Guest")), 2);
guest.setAttachedTo(&host);
host.addAttachedCard(&guest);
EXPECT_EQ(guest.getAttachedTo(), &host);
ASSERT_EQ(host.getAttachedCards().size(), 1);
EXPECT_EQ(host.getAttachedCards().first(), &guest);
host.removeAttachedCard(&guest);
guest.setAttachedTo(nullptr);
EXPECT_TRUE(host.getAttachedCards().isEmpty());
EXPECT_EQ(guest.getAttachedTo(), nullptr);
}
TEST(CardStateTest, CountersAddUpdateAndClear)
{
CardState card(nullptr, nullptr, cardRefFor(QStringLiteral("Cat")), 1);
int changes = 0;
QObject::connect(&card, &CardState::countersChanged, [&changes]() { ++changes; });
card.insertCounter(1, 2);
EXPECT_EQ(card.getCounters().value(1), 2);
card.setCounter(1, 3);
EXPECT_EQ(card.getCounters().value(1), 3);
card.setCounter(1, 0);
EXPECT_FALSE(card.getCounters().contains(1));
card.setCounter(2, 1);
card.clearCounters();
EXPECT_TRUE(card.getCounters().isEmpty());
EXPECT_EQ(changes, 5);
}
TEST(CardStateTest, TapFlipAttackingAndReset)
{
CardState card(nullptr, nullptr, cardRefFor(QStringLiteral("Cat")), 1);
card.setTapped(true);
card.setFaceDown(true);
card.setAttacking(true);
card.setAnnotation(QStringLiteral("note"));
card.setPT(QStringLiteral("4/4"));
card.setDoesntUntap(true);
card.setDestroyOnZoneChange(true);
card.setColor(QStringLiteral("red"));
card.insertCounter(1, 2);
card.resetState(true);
EXPECT_FALSE(card.getTapped());
EXPECT_FALSE(card.getAttacking());
EXPECT_TRUE(card.getCounters().isEmpty());
EXPECT_TRUE(card.getPT().isEmpty());
EXPECT_FALSE(card.getDoesntUntap());
EXPECT_EQ(card.getAnnotation(), QStringLiteral("note"));
// resetState() deliberately leaves these three alone.
EXPECT_TRUE(card.getFaceDown());
EXPECT_EQ(card.getColor(), QStringLiteral("red"));
EXPECT_TRUE(card.getDestroyOnZoneChange());
card.setAnnotation(QStringLiteral("other"));
card.resetState(false);
EXPECT_TRUE(card.getAnnotation().isEmpty());
}
TEST(CardStateTest, ParsePT)
{
const QVariantList plain = CardState::parsePT(QStringLiteral("3/3"));
ASSERT_EQ(plain.size(), 2);
EXPECT_EQ(plain.at(0).toString(), QStringLiteral("3"));
EXPECT_EQ(plain.at(1).toString(), QStringLiteral("3"));
const QVariantList negative = CardState::parsePT(QStringLiteral("-1/2"));
ASSERT_EQ(negative.size(), 2);
EXPECT_EQ(negative.at(0).typeId(), QMetaType::Int);
EXPECT_EQ(negative.at(0).toInt(), -1);
EXPECT_EQ(negative.at(1).toString(), QStringLiteral("2"));
const QVariantList star = CardState::parsePT(QStringLiteral("*"));
ASSERT_EQ(star.size(), 1);
EXPECT_EQ(star.at(0).toString(), QStringLiteral("*"));
EXPECT_TRUE(CardState::parsePT(QString()).isEmpty());
}