Skip to content
Repos

DevPlaceApp

C++ 5.0 KB74 lines Opens in site

Directory Raw Download

#pragma once
// Spec: shell.md section 6 (MockDevPlaceApi). Offline implementation for `--mock`: every call waits its Swift delay
// on a dp::Timer (UI thread), runs refreshTokenIfNeeded() (no-op unless the token is near expiry) and returns the
// MockFixtures data. logIn stores a "bearer" token with a random UUID, valid 3610 s, and Profile.mock.user.
#include "api/DevPlaceApi.hpp"

namespace dp {

class MockDevPlaceApi : public DevPlaceApi {
 public:
  using DevPlaceApi::feed;
  using DevPlaceApi::messages;
  using DevPlaceApi::notifications;

  static MockDevPlaceApi& shared();
  // Delay scale for tests: 1.0 = Swift delays, 0.0 = next timer tick.
  void setDelayScale(double scale) { delayScale_ = scale; }

  CancelPtr logIn(const std::string& email, const std::string& password, Done<Unit> done) override;
  CancelPtr signUp(const std::string& username, const std::string& email, const std::string& password,
                   const std::string& confirmPassword, const BirthDate& birthDate, bool acceptTerms, Done<Unit> done) override;
  CancelPtr deleteAccount(const std::string& password, Done<Unit> done) override;
  CancelPtr feed(const std::optional<Date>& before, Done<Feed> done) override;
  CancelPtr postDetail(const std::string& slug, Done<PostDetail> done) override;
  CancelPtr createPost(const Str& title, const std::optional<PostTopic>& topic, const std::string& content,
                       const std::vector<UploadResponse>& attachments, const Str& pollQuestion,
                       const std::vector<std::string>& pollOptions, const Str& projectLink, Done<Unit> done) override;
  CancelPtr editPost(const std::string& slug, const std::string& title, PostTopic topic, const std::string& content,
                     const std::string& projectLink, Done<Unit> done) override;
  CancelPtr deletePost(const std::string& slug, Done<Unit> done) override;
  CancelPtr deleteComment(const std::string& uid, Done<Unit> done) override;
  CancelPtr editComment(const std::string& uid, const std::string& content, Done<Unit> done) override;
  CancelPtr createComment(TargetType targetType, const std::string& targetId, const std::string& content,
                          const Str& parentId, const std::vector<UploadResponse>& attachments, Done<Unit> done) override;
  CancelPtr profile(const Str& username, Done<Profile> done) override;
  CancelPtr followUser(const std::string& username, Done<Unit> done) override;
  CancelPtr unfollowUser(const std::string& username, Done<Unit> done) override;
  CancelPtr blockUser(const std::string& username, Done<Unit> done) override;
  CancelPtr unblockUser(const std::string& username, Done<Unit> done) override;
  CancelPtr muteUser(const std::string& username, Done<Unit> done) override;
  CancelPtr unmuteUser(const std::string& username, Done<Unit> done) override;
  CancelPtr messages(const Str& withUid, Done<MessagesInbox> done) override;
  CancelPtr sendMessage(const std::string& receiverId, const std::string& content,
                        const std::vector<UploadResponse>& attachments, Done<Unit> done) override;
  std::unique_ptr<MessagesSocket> makeMessagesSocket(const std::string& otherUserId) override;
  CancelPtr usersForMentioning(const std::string& query, Done<UserSearch> done) override;
  CancelPtr notifications(const std::optional<Date>& before, Done<Notifications> done) override;
  CancelPtr openNotification(const std::string& uid, Done<NotificationOpen> done) override;
  CancelPtr notificationCounts(Done<NotificationCounts> done) override;
  CancelPtr markNotificationRead(const std::string& uid, Done<Unit> done) override;
  CancelPtr markAllNotificationsRead(Done<Unit> done) override;
  CancelPtr registerPushDevice(const std::string& deviceToken, Done<Unit> done) override;
  CancelPtr unregisterPushDevice(const std::string& deviceToken, Done<Unit> done) override;
  CancelPtr vote(TargetType targetType, const std::string& targetId, Vote vote, Done<Unit> done) override;
  CancelPtr submitPollChoice(const std::string& pollId, const std::string& optionId, Done<Unit> done) override;
  CancelPtr react(TargetType targetType, const std::string& targetId, const std::string& emoji, Done<Unit> done) override;
  CancelPtr reportReasons(Done<std::vector<ReportReason>> done) override;
  CancelPtr report(TargetType targetType, const std::string& targetId, const std::string& reason, const Str& detail,
                   Done<Unit> done) override;
  CancelPtr uploadFile(const std::string& data, const std::string& filename, const std::string& mimeType,
                       Done<UploadResponse> done) override;
  CancelPtr uploadFromUrl(const std::string& url, const Str& filename, Done<UploadResponse> done) override;
  CancelPtr deleteAttachment(const std::string& uid, Done<Unit> done) override;

 private:
  // Waits `delayMs`, optionally refreshes the token, then answers produce().
  template <class T>
  CancelPtr respond(unsigned delayMs, bool refresh, std::function<Result<T>()> produce, Done<T> done);
  CancelPtr unitAfter(unsigned delayMs, Done<Unit> done);

  double delayScale_ = 1.0;
};

}  // namespace dp
46.2ms