mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
[Client] DeckSyncProfile + sidecar + DeckDiff with tests
This commit is contained in:
parent
11fdfb4610
commit
1e97ab086b
12 changed files with 740 additions and 0 deletions
|
|
@ -407,6 +407,34 @@ cockatrice_add_test(
|
|||
bench
|
||||
)
|
||||
|
||||
cockatrice_add_test(
|
||||
NAME
|
||||
deck_diff_test
|
||||
SOURCES
|
||||
deck_diff_test.cpp
|
||||
LIBS
|
||||
libcockatrice_deck_list
|
||||
libcockatrice_utility
|
||||
)
|
||||
cockatrice_add_test(
|
||||
NAME
|
||||
deck_sync_profile_test
|
||||
SOURCES
|
||||
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/deck_sync/deck_sync_profile.cpp
|
||||
deck_sync_profile_test.cpp
|
||||
INCLUDE_DIRS
|
||||
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/deck_sync
|
||||
)
|
||||
cockatrice_add_test(
|
||||
NAME
|
||||
deck_sync_sidecar_test
|
||||
SOURCES
|
||||
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/deck_sync/deck_sync_sidecar.cpp
|
||||
deck_sync_sidecar_test.cpp
|
||||
INCLUDE_DIRS
|
||||
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/deck_sync
|
||||
)
|
||||
|
||||
# The translation loader test compiles a .qm fixture, which needs lrelease from Qt Linguist.
|
||||
if(TARGET Qt6::lrelease)
|
||||
set(TRANSLATION_LOADER_TEST_QM "${CMAKE_CURRENT_BINARY_DIR}/translation_loader_test_de.qm")
|
||||
|
|
|
|||
137
tests/deck_diff_test.cpp
Normal file
137
tests/deck_diff_test.cpp
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
#include "gtest/gtest.h"
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <libcockatrice/deck_list/deck_diff.h>
|
||||
#include <libcockatrice/deck_list/deck_list.h>
|
||||
#include <libcockatrice/deck_list/tree/deck_list_card_node.h>
|
||||
#include <libcockatrice/deck_list/tree/inner_deck_list_node.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
DeckList makeDeck(std::initializer_list<std::pair<const char *, int>> mainCards,
|
||||
std::initializer_list<std::pair<const char *, int>> sideCards = {})
|
||||
{
|
||||
DeckList deck;
|
||||
for (const auto &[name, count] : mainCards) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
deck.addCard(name, "main");
|
||||
}
|
||||
}
|
||||
for (const auto &[name, count] : sideCards) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
deck.addCard(name, "side");
|
||||
}
|
||||
}
|
||||
return deck;
|
||||
}
|
||||
|
||||
int countOf(const DeckList &deck, const char *zone, const char *name)
|
||||
{
|
||||
int count = 0;
|
||||
deck.forEachCard([&](InnerDecklistNode *zoneNode, DecklistCardNode *card) {
|
||||
if (zoneNode->getName() == zone && card->getName() == name) {
|
||||
count += card->getNumber();
|
||||
}
|
||||
});
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(DeckDiffTest, IdenticalDecksHaveEmptyDiff)
|
||||
{
|
||||
DeckList first = makeDeck({{"Lightning Bolt", 4}});
|
||||
DeckList second = makeDeck({{"Lightning Bolt", 4}});
|
||||
EXPECT_TRUE(DeckDiff::diff(first, second).isEmpty());
|
||||
}
|
||||
|
||||
TEST(DeckDiffTest, ChangesAreSortedByZoneThenName)
|
||||
{
|
||||
DeckList oldDeck = makeDeck({{"Mountain", 20}, {"Lightning Bolt", 4}}, {{"Shock", 2}});
|
||||
DeckList newDeck = makeDeck({{"Mountain", 19}, {"Shock", 5}}, {{"Shock", 1}, {"Teferi", 1}});
|
||||
|
||||
DeckDiffResult result = DeckDiff::diff(oldDeck, newDeck);
|
||||
ASSERT_EQ(result.changes.size(), 5);
|
||||
EXPECT_EQ(result.changes.at(0).zone, "main");
|
||||
EXPECT_EQ(result.changes.at(0).cardName, "Lightning Bolt");
|
||||
EXPECT_EQ(result.changes.at(1).cardName, "Mountain");
|
||||
EXPECT_EQ(result.changes.at(2).cardName, "Shock");
|
||||
EXPECT_EQ(result.changes.at(3).zone, "side");
|
||||
EXPECT_EQ(result.changes.at(3).cardName, "Shock");
|
||||
EXPECT_EQ(result.changes.at(4).cardName, "Teferi");
|
||||
}
|
||||
|
||||
TEST(DeckDiffTest, ReportsAddedAndRemovedCopies)
|
||||
{
|
||||
DeckList oldDeck = makeDeck({{"Lightning Bolt", 4}, {"Mountain", 20}});
|
||||
DeckList newDeck = makeDeck({{"Lightning Bolt", 2}, {"Shock", 4}, {"Mountain", 20}});
|
||||
|
||||
DeckDiffResult result = DeckDiff::diff(oldDeck, newDeck);
|
||||
ASSERT_EQ(result.changes.size(), 2);
|
||||
EXPECT_EQ(result.changes.at(0).cardName, "Lightning Bolt");
|
||||
EXPECT_EQ(result.changes.at(0).oldCount, 4);
|
||||
EXPECT_EQ(result.changes.at(0).newCount, 2);
|
||||
EXPECT_EQ(result.changes.at(0).delta(), -2);
|
||||
EXPECT_EQ(result.changes.at(1).cardName, "Shock");
|
||||
EXPECT_EQ(result.changes.at(1).oldCount, 0);
|
||||
EXPECT_EQ(result.changes.at(1).newCount, 4);
|
||||
}
|
||||
|
||||
TEST(DeckDiffTest, ZoneMoveIsTwoChanges)
|
||||
{
|
||||
DeckList oldDeck = makeDeck({{"Teferi", 1}});
|
||||
DeckList newDeck = makeDeck({}, {{"Teferi", 1}});
|
||||
|
||||
DeckDiffResult result = DeckDiff::diff(oldDeck, newDeck);
|
||||
ASSERT_EQ(result.changes.size(), 2);
|
||||
EXPECT_EQ(result.changes.at(0).zone, "main");
|
||||
EXPECT_EQ(result.changes.at(0).newCount, 0);
|
||||
EXPECT_EQ(result.changes.at(1).zone, "side");
|
||||
EXPECT_EQ(result.changes.at(1).oldCount, 0);
|
||||
}
|
||||
|
||||
TEST(DeckDiffTest, DisjointChangesCanAutoMerge)
|
||||
{
|
||||
DeckList base = makeDeck({{"Lightning Bolt", 4}, {"Mountain", 20}});
|
||||
DeckList local = makeDeck({{"Lightning Bolt", 4}, {"Mountain", 18}});
|
||||
DeckList remote = makeDeck({{"Shock", 2}, {"Lightning Bolt", 4}, {"Mountain", 20}});
|
||||
|
||||
DeckDiffResult localChanges = DeckDiff::diff(base, local);
|
||||
DeckDiffResult remoteChanges = DeckDiff::diff(base, remote);
|
||||
EXPECT_TRUE(DeckDiff::canAutoMerge(localChanges, remoteChanges));
|
||||
|
||||
DeckList merged = DeckDiff::merge(base, localChanges, remoteChanges);
|
||||
EXPECT_EQ(countOf(merged, "main", "Mountain"), 18);
|
||||
EXPECT_EQ(countOf(merged, "main", "Shock"), 2);
|
||||
EXPECT_EQ(countOf(merged, "main", "Lightning Bolt"), 4);
|
||||
}
|
||||
|
||||
TEST(DeckDiffTest, OverlappingChangesCannotAutoMergeAndRemoteWins)
|
||||
{
|
||||
DeckList base = makeDeck({{"Lightning Bolt", 4}});
|
||||
DeckList local = makeDeck({{"Lightning Bolt", 3}});
|
||||
DeckList remote = makeDeck({{"Lightning Bolt", 1}});
|
||||
|
||||
DeckDiffResult localChanges = DeckDiff::diff(base, local);
|
||||
DeckDiffResult remoteChanges = DeckDiff::diff(base, remote);
|
||||
EXPECT_FALSE(DeckDiff::canAutoMerge(localChanges, remoteChanges));
|
||||
|
||||
DeckList merged = DeckDiff::merge(base, localChanges, remoteChanges);
|
||||
EXPECT_EQ(countOf(merged, "main", "Lightning Bolt"), 1);
|
||||
}
|
||||
|
||||
TEST(DeckDiffTest, MergeKeepsBaseMetadata)
|
||||
{
|
||||
DeckList base = makeDeck({{"Lightning Bolt", 4}});
|
||||
base.setName("Affinity v3");
|
||||
DeckList remote = makeDeck({{"Lightning Bolt", 4}, {"Shock", 1}});
|
||||
|
||||
DeckList merged = DeckDiff::merge(base, DeckDiff::diff(base, base), DeckDiff::diff(base, remote));
|
||||
EXPECT_EQ(merged.getName(), "Affinity v3");
|
||||
EXPECT_EQ(countOf(merged, "main", "Shock"), 1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
63
tests/deck_sync_profile_test.cpp
Normal file
63
tests/deck_sync_profile_test.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include "gtest/gtest.h"
|
||||
#include <QString>
|
||||
#include <deck_sync_profile.h>
|
||||
|
||||
TEST(DeckSyncProfileTest, KeyIsStable)
|
||||
{
|
||||
DeckSyncProfile first("woogerworks.com", 4747, "Skog7");
|
||||
DeckSyncProfile second("woogerworks.com", 4747, "Skog7");
|
||||
EXPECT_EQ(first.key(), second.key());
|
||||
EXPECT_FALSE(first.key().isEmpty());
|
||||
}
|
||||
|
||||
TEST(DeckSyncProfileTest, KeyIgnoresCase)
|
||||
{
|
||||
DeckSyncProfile lower("woogerworks.com", 4747, "skog7");
|
||||
DeckSyncProfile upper("WOOGERWORKS.COM", 4747, "SKOG7");
|
||||
EXPECT_EQ(lower.key(), upper.key());
|
||||
}
|
||||
|
||||
TEST(DeckSyncProfileTest, KeyDiffersPerServerAndUser)
|
||||
{
|
||||
DeckSyncProfile base("woogerworks.com", 4747, "skog7");
|
||||
EXPECT_NE(base.key(), DeckSyncProfile("other.server", 4747, "skog7").key());
|
||||
EXPECT_NE(base.key(), DeckSyncProfile("woogerworks.com", 4748, "skog7").key());
|
||||
EXPECT_NE(base.key(), DeckSyncProfile("woogerworks.com", 4747, "other").key());
|
||||
}
|
||||
|
||||
TEST(DeckSyncProfileTest, KeyIsUnambiguousAcrossSeparators)
|
||||
{
|
||||
DeckSyncProfile left("a:1", 2, "b");
|
||||
DeckSyncProfile right("a", 1, "2:b");
|
||||
EXPECT_NE(left.key(), right.key());
|
||||
EXPECT_NE(DeckSyncProfile("::1", 4747, "skog7").key(), DeckSyncProfile("::2", 4747, "skog7").key());
|
||||
}
|
||||
|
||||
TEST(DeckSyncProfileTest, InvalidProfilesHaveNoKey)
|
||||
{
|
||||
EXPECT_TRUE(DeckSyncProfile().key().isEmpty());
|
||||
EXPECT_TRUE(DeckSyncProfile("", 4747, "skog7").key().isEmpty());
|
||||
EXPECT_TRUE(DeckSyncProfile("woogerworks.com", 0, "skog7").key().isEmpty());
|
||||
}
|
||||
|
||||
TEST(DeckSyncProfileTest, KeyIgnoresSurroundingWhitespace)
|
||||
{
|
||||
DeckSyncProfile plain("woogerworks.com", 4747, "skog7");
|
||||
DeckSyncProfile padded(" woogerworks.com ", 4747, " skog7 ");
|
||||
EXPECT_EQ(plain.key(), padded.key());
|
||||
}
|
||||
|
||||
TEST(DeckSyncProfileTest, ValidityRequiresAllFields)
|
||||
{
|
||||
EXPECT_TRUE(DeckSyncProfile("woogerworks.com", 4747, "skog7").isValid());
|
||||
EXPECT_FALSE(DeckSyncProfile("", 4747, "skog7").isValid());
|
||||
EXPECT_FALSE(DeckSyncProfile("woogerworks.com", 0, "skog7").isValid());
|
||||
EXPECT_FALSE(DeckSyncProfile("woogerworks.com", 4747, "").isValid());
|
||||
EXPECT_FALSE(DeckSyncProfile().isValid());
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
126
tests/deck_sync_sidecar_test.cpp
Normal file
126
tests/deck_sync_sidecar_test.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
#include "gtest/gtest.h"
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QString>
|
||||
#include <QTemporaryDir>
|
||||
#include <deck_sync_sidecar.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
QString sidecarPath(QTemporaryDir &dir)
|
||||
{
|
||||
return dir.filePath(".sync/profilekey.json");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(DeckSyncSidecarTest, MissingFileLoadsEmpty)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
DeckSyncSidecar sidecar(sidecarPath(dir));
|
||||
EXPECT_TRUE(sidecar.load());
|
||||
EXPECT_TRUE(sidecar.records().isEmpty());
|
||||
EXPECT_FALSE(sidecar.contains("affinity.cod"));
|
||||
}
|
||||
|
||||
TEST(DeckSyncSidecarTest, RoundTripsRecords)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
DeckSyncSidecar sidecar(sidecarPath(dir));
|
||||
|
||||
DeckSyncRecord record;
|
||||
record.guid = "123e4567-e89b-12d3-a456-426614174000";
|
||||
record.remoteId = 42;
|
||||
record.baseHash = "5cac19qm";
|
||||
record.lastSyncSecs = 1791739200;
|
||||
record.deleted = true;
|
||||
sidecar.set("decks/affinity.cod", record);
|
||||
EXPECT_TRUE(sidecar.save());
|
||||
|
||||
DeckSyncSidecar reloaded(sidecarPath(dir));
|
||||
EXPECT_TRUE(reloaded.load());
|
||||
EXPECT_TRUE(reloaded.contains("decks/affinity.cod"));
|
||||
DeckSyncRecord back = reloaded.get("decks/affinity.cod");
|
||||
EXPECT_EQ(back.guid, record.guid);
|
||||
EXPECT_EQ(back.remoteId, 42);
|
||||
EXPECT_EQ(back.baseHash, "5cac19qm");
|
||||
EXPECT_EQ(back.lastSyncSecs, 1791739200);
|
||||
EXPECT_TRUE(back.deleted);
|
||||
}
|
||||
|
||||
TEST(DeckSyncSidecarTest, UnknownPathReturnsDefaultRecord)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
DeckSyncSidecar sidecar(sidecarPath(dir));
|
||||
DeckSyncRecord back = sidecar.get("missing.cod");
|
||||
EXPECT_EQ(back.remoteId, -1);
|
||||
EXPECT_TRUE(back.guid.isEmpty());
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
bool writeRaw(const QString &path, const QByteArray &bytes)
|
||||
{
|
||||
QDir().mkpath(QFileInfo(path).absolutePath());
|
||||
QFile file(path);
|
||||
return file.open(QIODevice::WriteOnly | QIODevice::Truncate) && file.write(bytes) == bytes.size();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(DeckSyncSidecarTest, GarbageFileFailsToLoad)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
ASSERT_TRUE(writeRaw(sidecarPath(dir), "not json at all"));
|
||||
DeckSyncSidecar sidecar(sidecarPath(dir));
|
||||
EXPECT_FALSE(sidecar.load());
|
||||
}
|
||||
|
||||
TEST(DeckSyncSidecarTest, WrongRecordsShapeFailsToLoad)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
ASSERT_TRUE(writeRaw(sidecarPath(dir), R"({"records":["a.cod"]})"));
|
||||
DeckSyncSidecar missing(sidecarPath(dir));
|
||||
EXPECT_FALSE(missing.load());
|
||||
|
||||
ASSERT_TRUE(writeRaw(sidecarPath(dir), R"({"foo":1})"));
|
||||
DeckSyncSidecar other(sidecarPath(dir));
|
||||
EXPECT_FALSE(other.load());
|
||||
}
|
||||
|
||||
TEST(DeckSyncSidecarTest, NonObjectEntriesAreSkipped)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
ASSERT_TRUE(writeRaw(sidecarPath(dir), R"({"records":{"a.cod":42,"b.cod":{"guid":"g"}}})"));
|
||||
DeckSyncSidecar sidecar(sidecarPath(dir));
|
||||
EXPECT_TRUE(sidecar.load());
|
||||
EXPECT_FALSE(sidecar.contains("a.cod"));
|
||||
EXPECT_TRUE(sidecar.contains("b.cod"));
|
||||
EXPECT_EQ(sidecar.get("b.cod").guid, "g");
|
||||
}
|
||||
|
||||
TEST(DeckSyncSidecarTest, RemoveDropsRecord)
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
ASSERT_TRUE(dir.isValid());
|
||||
DeckSyncSidecar sidecar(sidecarPath(dir));
|
||||
sidecar.set("a.cod", DeckSyncRecord());
|
||||
sidecar.remove("a.cod");
|
||||
EXPECT_FALSE(sidecar.contains("a.cod"));
|
||||
EXPECT_TRUE(sidecar.save());
|
||||
|
||||
DeckSyncSidecar reloaded(sidecarPath(dir));
|
||||
EXPECT_TRUE(reloaded.load());
|
||||
EXPECT_FALSE(reloaded.contains("a.cod"));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue