Files
WoW-Hybrid-gateway/tests/equipment_set_manager_test.cpp
2026-11-23 03:28:32 +02:00

147 lines
6.8 KiB
C++

#include "equipment_set_manager.hpp"
#include <iostream>
#include <stdexcept>
using namespace gateway;
namespace eq = gateway::equipment_sets;
static void check(bool ok) { if (!ok) throw std::runtime_error("equipment fixture failed"); }
template<class F> static void rejected(F f) {
try { f(); } catch (const Error&) { return; }
throw std::runtime_error("equipment fixture accepted a rejected operation");
}
static Guid128 id(uint8_t low) {
Guid128 g{};
if (low) { g[0] = low; g[15] = 12; }
return g;
}
// Independent 70334 fixture for the documented packed GUID and bit lengths.
// Set 2, name "DPS", icon "17", optional spec index 1.
static Bytes save_wire(uint8_t a, uint8_t b) {
Bytes body;
le(body, 0, 4); le(body, 0, 8); le(body, 2, 4); le(body, 1u << 18, 4);
for (size_t i = 0; i < 19; ++i) {
uint8_t low = i == 0 ? a : i == 1 ? b : 0;
if (low) append(body, Bytes{1, 128, low, 12});
else append(body, Bytes{0, 0});
le(body, 0, 4);
}
body.resize(body.size()+24, 0);
append(body, Bytes{0x81, 0x80, 0x80, 1, 0, 0, 0, 'D', 'P', 'S', '1', '7'});
return body;
}
static Bytes use_wire(const eq::Set& s) {
Bytes body{0x40, 255, 0};
for (auto& item : s.pieces) {
append(body, pack_guid(item)); append(body, Bytes{250, 250}); // advisory hints
}
le(body, s.guid, 8);
return body;
}
static void last_result(eq::Manager& m, uint32_t reason, uint64_t guid) {
check(m.replies.size() == 1 && m.replies.front().opcode == eq::Result);
Reader r(m.replies.front().body);
check(r.le(4) == reason && r.le(8) == guid); r.end(); m.replies.clear();
}
int main(int argc, char** argv) {
if (argc != 2) return 2;
auto root = std::filesystem::path(argv[1]);
root /= "equipment-set-" + std::to_string(eq::Clock::now().time_since_epoch().count());
auto file = eq::path(7, id(21), root);
eq::Manager m;
eq::Carried inv;
for (uint8_t i=0; i<19; ++i) inv.push_back({255, i, {}});
inv.push_back({255, 23, {}}); inv.push_back({19, 0, {}});
inv[0].item = id(4); inv[1].item = id(6);
auto now = eq::Clock::now();
auto body = save_wire(4, 6);
auto handle = [&](uint32_t op, const Bytes& data) { m.handle(op, data, file, inv, now); };
rejected([&] { handle(eq::Save, body); });
m.loaded = true;
auto bad = body;
bad.push_back(0);
rejected([&] { handle(eq::Save, bad); });
rejected([&] { handle(eq::Delete, body); });
rejected([&] { handle(eq::Save, save_wire(12, 6)); });
check(!std::filesystem::exists(file));
handle(eq::Save, body); handle(eq::Save, body);
check(m.replies.size() == 2 && m.replies.back().opcode == eq::SetId);
Bytes ack(4, 0); le(ack, 2, 4); le(ack, 1, 8);
check(m.replies[0].body == ack && m.replies[1].body == ack); m.replies.clear();
auto saved = eq::load(file).sets.at(2);
check(saved.guid == 1 && saved.name == "DPS" && saved.icon == "17" && saved.spec == 1);
Bytes initial{1,0,0,0}; auto expected = body; expected[4] = 1; append(initial, expected);
check(eq::load(file).packet() == initial);
check(eq::load(eq::path(7, id(22), root)).sets.empty());
check(eq::load(eq::path(8, id(21), root)).sets.empty());
rejected([&] { eq::path(7, {}, root); });
Bytes key; le(key, saved.guid, 8);
auto assign = key; le(assign, 0, 4); handle(eq::Assign, assign);
check(eq::load(file).sets.at(2).spec == 0);
assign = key; le(assign, UINT32_MAX, 4); handle(eq::Assign, assign);
check(!eq::load(file).sets.at(2).spec); m.replies.clear();
inv[18].item = id(20);
handle(eq::Use, use_wire(saved)); last_result(m, 0, saved.guid);
std::swap(inv[0].item, inv[20].item);
inv[2].item = id(18);
handle(eq::Use, use_wire(saved));
check(m.pending && m.pending->next->source.opcode == 0x10c);
check(m.pending->next->source.body == Bytes({255, 0, 19, 0}));
m.sent(now); m.advance(inv, now);
check(m.pending && m.pending->waiting && m.replies.empty());
std::swap(inv[0].item, inv[20].item);
m.advance(inv, now);
check(m.pending->next->source.opcode == 0x10b && m.pending->next->source.body == Bytes({255, 2, 0}));
m.sent(now); inv[2].item = {}; m.advance(inv, now);
check(m.pending && m.replies.empty()); // Wait for the source bag to confirm the item arrived.
inv[19].item = id(18); m.advance(inv, now); last_result(m, 0, 1);
check(inv[18].item == id(20));
std::swap(inv[0].item, inv[1].item);
handle(eq::Use, use_wire(saved));
check(m.pending->next->source.opcode == 0x10d && m.pending->next->source.body == Bytes({1, 0}));
m.sent(now); m.expire(now + std::chrono::seconds(9)); last_result(m, 1, 1);
std::swap(inv[0].item, inv[1].item);
inv[0].item = {}; handle(eq::Use, use_wire(saved)); last_result(m, 1, 1);
// The closed-bank view omitted the missing helmet above. A live banker
// adds both source root slots and zero-based slots inside purchased bank bags.
for (eq::Position bank : {eq::Position{255, 39, id(4), true}, eq::Position{63, 3, id(4), true}}) {
inv.push_back(bank);
handle(eq::Use, use_wire(saved));
check(m.pending && m.pending->next->from_bank);
check(m.pending->next->source.opcode == 0x10c &&
m.pending->next->source.body == Bytes({255, 0, bank.bag, bank.slot}));
m.sent(now); m.advance(inv, now);
check(m.replies.empty());
std::swap(inv[0].item, inv.back().item);
m.advance(inv, now); last_result(m, 0, 1);
inv.pop_back(); inv[0].item = {};
}
eq::Carried occupied_bank;
for (uint8_t i=0; i<19; ++i) occupied_bank.push_back({255, i, {}});
occupied_bank[0].item = id(4); occupied_bank[1].item = id(11);
occupied_bank.push_back({255, 39, id(6), true});
m.handle(eq::Use, use_wire(saved), file, occupied_bank, now);
check(m.pending && m.pending->next->from_bank &&
m.pending->next->source.body == Bytes({255, 1, 255, 39}));
m.sent(now);
occupied_bank[1].item = id(6); // Equipment update arrived before the bank-slot update.
m.advance(occupied_bank, now);
check(m.pending && m.pending->waiting && m.replies.empty());
occupied_bank.back().item = id(11);
m.advance(occupied_bank, now); last_result(m, 0, 1);
inv[0].item = id(4); inv[2].item = id(17); inv[20].item = id(13);
inv.push_back({255, 41, {}, true}); // Bank space is never used to unequip.
handle(eq::Use, use_wire(saved)); last_result(m, 4, 1);
inv[2].item = {}; handle(eq::Delete, key);
check(eq::load(file).sets.empty()); m.replies.clear();
handle(eq::Save, body); check(eq::load(file).sets.at(2).guid == 2);
rejected([&] { handle(eq::Delete, key); });
{
eq::Lock held(file);
rejected([&] { handle(eq::Save, body); });
}
std::filesystem::remove_all(root);
std::cout << "PASS equipment-set persistence, wire format, inventory moves and source confirmation\n";
}