#include #include "ccm/domain/MagicCard.hpp" #include "ccm/ports/ICollectionRepository.hpp" #include "ccm/ports/IImageStore.hpp" #include "ccm/services/CollectionService.hpp" #include #include #include using namespace ccm; namespace { class InMemoryRepo final : public ICollectionRepository { public: Map storage; Result load(Game) override { return Result::ok(storage); } Result save(Game, const Map& m) override { storage = m; return Result::ok(); } }; class StubImageStore final : public IImageStore { public: std::vector> removed; Result copyIn(Game, const std::filesystem::path&, const std::string& n) override { return Result::ok(n); } Result remove(Game g, const std::string& n) override { removed.emplace_back(g, n); return Result::ok(); } std::filesystem::path resolvePath(Game, const std::string& n) const override { return std::filesystem::path(n); } }; MagicCard makeCard(const std::string& name, std::vector imgs = {}) { MagicCard c; c.name = name; c.set = Set{"alp", "Alpha", "1993/08/05"}; c.images = std::move(imgs); return c; } } // namespace TEST_SUITE("CollectionService") { TEST_CASE("nextId on empty map is 0, then strictly increments") { InMemoryRepo repo; StubImageStore store; CollectionService svc{repo, store}; const auto id0 = svc.add(Game::Magic, makeCard("A")); REQUIRE(id0.isOk()); CHECK(id0.value() == 0); const auto id1 = svc.add(Game::Magic, makeCard("B")); REQUIRE(id1.isOk()); CHECK(id1.value() == 1); const auto listed = svc.list(Game::Magic); REQUIRE(listed.isOk()); CHECK(listed.value().size() == 2); } TEST_CASE("update modifies an existing card and is rejected for unknown ids") { InMemoryRepo repo; StubImageStore store; CollectionService svc{repo, store}; const auto id = svc.add(Game::Magic, makeCard("Initial")).value(); MagicCard updated = makeCard("Renamed"); updated.id = id; CHECK(svc.update(Game::Magic, updated).isOk()); const auto found = svc.findById(Game::Magic, id); REQUIRE(found.isOk()); REQUIRE(found.value().has_value()); CHECK(found.value()->name == "Renamed"); MagicCard ghost = makeCard("Ghost"); ghost.id = 999; CHECK(svc.update(Game::Magic, ghost).isErr()); } TEST_CASE("remove deletes images and the entry") { InMemoryRepo repo; StubImageStore store; CollectionService svc{repo, store}; const auto id = svc.add( Game::Magic, makeCard("With Images", {"a.png", "b.png"})).value(); REQUIRE(svc.remove(Game::Magic, id).isOk()); // Both images should have been requested for deletion. REQUIRE(store.removed.size() == 2); CHECK(store.removed[0].second == "a.png"); CHECK(store.removed[1].second == "b.png"); const auto listed = svc.list(Game::Magic); REQUIRE(listed.isOk()); CHECK(listed.value().empty()); } TEST_CASE("remove of unknown id returns an error") { InMemoryRepo repo; StubImageStore store; CollectionService svc{repo, store}; CHECK(svc.remove(Game::Magic, 12345).isErr()); } }