Skip to content
Repos

DevPlaceApp

C++ 2.6 KB76 lines Opens in site

Directory Raw Download

#pragma once
// Spec: NFR-TEST-01. Shared helpers of the Social*Test files: scripted socket, signed-in mock API, alert recorder.
#include <string>
#include <vector>

#include "TestHarness.hpp"
#include "TestPump.hpp"
#include "TestTempDir.hpp"
#include "api/MessagesSocket.hpp"
#include "api/mock/MockDevPlaceApi.hpp"
#include "api/mock/MockFixtures.hpp"
#include "core/AlertMessage.hpp"
#include "screens/AppContext.hpp"
#include "store/AppState.hpp"
#include "ui/RemoteImages.hpp"
#include "store/SessionStore.hpp"

namespace dp::test {

// Socket that records every call and lets the test inject events.
class FakeSocket : public MessagesSocket {
 public:
  void setEventHandler(EventHandler h) override { handler = std::move(h); }
  void connect() override { ++connects; }
  void disconnect() override { ++disconnects; }
  void send(const std::string& content, const std::vector<std::string>& uids, const std::string& clientId) override {
    sent.push_back({content, uids, clientId});
  }
  void sendTyping() override { ++typings; }
  void sendRead(const std::string& withUid) override { reads.push_back(withUid); }
  void emit(const ChatSocketEvent& e) { if (handler) handler(e); }

  struct Sent {
    std::string content;
    std::vector<std::string> uids;
    std::string clientId;
  };
  EventHandler handler;
  std::vector<Sent> sent;
  std::vector<std::string> reads;
  int typings = 0, connects = 0, disconnects = 0;
};

// Mock API with zero delays and a signed-in user (cheeze_on_wheels).
struct SignedInMock {
  SignedInMock() {
    static TempDir sessionDir;
    SessionStore::instance().rebind(sessionDir.path());
    MockDevPlaceApi::shared().setDelayScale(0.0);
    AppContext::setApi(&MockDevPlaceApi::shared());
    // Never reach the network (and never use another test's stale fetcher): every image load fails at once.
    ui::RemoteImages::Config images;
    images.fetcher = [](const std::string&, std::function<void(Result<HttpResponse>)> done) {
      HttpResponse r;
      r.status = 404;
      done(std::move(r));
    };
    ui::RemoteImages::instance().configure(images);
    ui::RemoteImages::instance().clear();
    AppState::instance().clear();
    bool done = false;
    MockDevPlaceApi::shared().logIn("a@b.c", "pw", [&](Result<Unit>) { done = true; });
    pumpUntil([&] { return done; });
  }
  ~SignedInMock() { AppState::instance().clear(); }
  DevPlaceApi& api() { return MockDevPlaceApi::shared(); }
};

struct AlertLog {
  std::vector<AlertMessage> alerts;
  std::function<void(const AlertMessage&)> sink() {
    return [this](const AlertMessage& m) { alerts.push_back(m); };
  }
};

}  // namespace dp::test
32.6ms