GoBack190hlTests.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Round 190hl (hotfix of 0.1.28): Help › "Go back to <version>..." (round 190gh) in the released 0.1.28
  2. // (/mnt/e/lynx-work/upd0128: shots s01–s15, update.log):
  3. // (a) the running "0.1.28" (CMake, no suffix) compared newer than its own tag "v0.1.28-alpha", so going back picked
  4. // 0.1.28 itself: a version without a prerelease now equals every prerelease of the same numbers;
  5. // (b) 0.1.27 read the new 3-line installing.txt as version "0.0.0" ("updated to 0.0.0"): "version=" is written last;
  6. // (c) the skip of the version left was saved only after the restart, by the version installed, and 0.1.27 does not
  7. // know that step: the skip is written into updates.txt ("skipped", which every version reads) before the installer.
  8. #include "core/UpdateCheck.h"
  9. namespace hl190
  10. {
  11. // the release list of 0.1.29 time: the hotfix as a four-number tag too, and a draft
  12. const char* releasesJson = R"json([
  13. {"tag_name":"v0.1.30-alpha","draft":true,"prerelease":true,"assets":[]},
  14. {"tag_name":"v0.1.29-alpha","draft":false,"prerelease":true,"assets":[]},
  15. {"tag_name":"v0.1.28.1-alpha","draft":false,"prerelease":true,"assets":[]},
  16. {"tag_name":"v0.1.28-alpha","draft":false,"prerelease":true,"assets":[]},
  17. {"tag_name":"v0.1.27-alpha","draft":false,"prerelease":true,"assets":[]}
  18. ])json";
  19. // LYNX SHARE 0.1.27's reader of installing.txt (src/ui/Updater.cpp at v0.1.27-alpha, afterStart): everything after
  20. // the first "version=", trimmed
  21. std::string versionAsRead0127 (const std::string& installing)
  22. {
  23. const auto at = installing.find ("version=");
  24. if (at == std::string::npos)
  25. return {};
  26. auto rest = installing.substr (at + 8);
  27. const auto a = rest.find_first_not_of (" \t\r\n"), b = rest.find_last_not_of (" \t\r\n");
  28. return a == std::string::npos ? std::string() : rest.substr (a, b - a + 1);
  29. }
  30. // LYNX SHARE 0.1.28's reader (round 190gh): line by line, "version=" / "from=" / "rollback=" at the line start
  31. lynx::update::InstallingNote asRead0128 (const std::string& installing)
  32. {
  33. lynx::update::InstallingNote note;
  34. std::size_t start = 0;
  35. while (start <= installing.size())
  36. {
  37. auto end = installing.find ('\n', start);
  38. if (end == std::string::npos)
  39. end = installing.size();
  40. const auto line = installing.substr (start, end - start);
  41. auto value = [&line] { return line.substr (line.find ('=') + 1); };
  42. if (line.rfind ("version=", 0) == 0 && note.version.empty()) note.version = value();
  43. if (line.rfind ("from=", 0) == 0) note.from = value();
  44. if (line.rfind ("rollback=", 0) == 0) note.rollback = value() == "1";
  45. start = end + 1;
  46. }
  47. return note;
  48. }
  49. } // namespace hl190
  50. TEST (goback190hl_a_the_running_build_equals_its_release_tag)
  51. {
  52. using namespace lynx::update;
  53. auto cmp = [] (const char* a, const char* b) { return compare (parseVersion (a), parseVersion (b)); };
  54. // the build of 0.1.28 (CMake "0.1.28") is the release v0.1.28-alpha
  55. CHECK (cmp ("0.1.28", "0.1.28-alpha") == 0 && cmp ("0.1.28-alpha", "0.1.28") == 0);
  56. CHECK (cmp ("0.1.27-alpha", "0.1.28") < 0 && cmp ("0.1.28", "0.1.27-alpha") > 0);
  57. CHECK (cmp ("0.1.29-alpha", "0.1.28") > 0 && cmp ("0.1.28", "0.1.29-alpha") < 0);
  58. CHECK (cmp ("0.1.27-alpha", "0.1.29-alpha") < 0);
  59. // two prereleases still order by semver
  60. CHECK (cmp ("0.1.28-alpha", "0.1.28-beta") < 0 && cmp ("0.1.28-alpha.2", "0.1.28-alpha.10") < 0);
  61. // four numbers (a hotfix "0.1.28.1"): above 0.1.28 in every form, below 0.1.29
  62. const auto hot = parseVersion ("v0.1.28.1-alpha");
  63. CHECK (hot.valid && hot.revision == 1 && hot.text() == "0.1.28.1-alpha" && parseVersion ("0.1.28.1").text() == "0.1.28.1");
  64. CHECK (parseVersion ("0.1.28.0").text() == "0.1.28" && cmp ("0.1.28.0", "0.1.28") == 0);
  65. CHECK (cmp ("0.1.28.1", "0.1.28-alpha") > 0 && cmp ("0.1.28.1", "0.1.28") > 0 && cmp ("0.1.28.1", "0.1.28.1-alpha") == 0);
  66. CHECK (cmp ("0.1.28.1-alpha", "0.1.29-alpha") < 0 && cmp ("0.1.28.2", "0.1.28.10") < 0);
  67. const auto releases = parseReleases (hl190::releasesJson);
  68. CHECK (releases.size() == 5);
  69. if (releases.size() != 5)
  70. return;
  71. auto at = [&] (int i) { return i >= 0 ? releases[(std::size_t) i].version.text() : std::string ("none"); };
  72. auto atIn = [] (const std::vector<Release>& list, int i) { return i >= 0 ? list[(std::size_t) i].version.text() : std::string ("none"); };
  73. // update checks: the running build is up to date with its own tag, the next one is offered
  74. CHECK (at (newestIndex (releases, parseVersion ("0.1.29"))) == "none");
  75. CHECK (at (newestIndex (releases, parseVersion ("0.1.28.1"))) == "0.1.29-alpha");
  76. CHECK (at (newestIndex (releases, parseVersion ("0.1.28"))) == "0.1.29-alpha");
  77. CHECK (at (newestIndex (releases, parseVersion ("0.1.27"))) == "0.1.29-alpha");
  78. const std::vector<Release> upTo0128 (releases.begin() + 2, releases.end()); // 0.1.28.1, 0.1.28, 0.1.27
  79. CHECK (atIn (upTo0128, newestIndex (upTo0128, parseVersion ("0.1.28"))) == "0.1.28.1-alpha");
  80. CHECK (newestIndex (upTo0128, parseVersion ("0.1.28.1")) < 0);
  81. const std::vector<Release> upTo0128only (releases.begin() + 3, releases.end()); // 0.1.28, 0.1.27
  82. CHECK (newestIndex (upTo0128only, parseVersion ("0.1.28")) < 0); // "You're up to date" (as 0.1.28 said)
  83. // going back: never the running release itself (0.1.28 picked 0.1.28-alpha), never a newer one or the draft
  84. CHECK (atIn (upTo0128only, rollbackIndex (upTo0128only, parseVersion ("0.1.28"), "")) == "0.1.27-alpha");
  85. CHECK (atIn (upTo0128only, rollbackIndex (upTo0128only, parseVersion ("0.1.28"), "0.1.27")) == "0.1.27-alpha");
  86. // (s08–s10: after 0.1.28 reinstalled itself the recorded previous was "0.1.28-alpha", its own tag)
  87. CHECK (atIn (upTo0128only, rollbackIndex (upTo0128only, parseVersion ("0.1.28"), "0.1.28-alpha")) == "0.1.27-alpha");
  88. CHECK (at (rollbackIndex (releases, parseVersion ("0.1.29"), "")) == "0.1.28.1-alpha");
  89. CHECK (at (rollbackIndex (releases, parseVersion ("0.1.29"), "0.1.28")) == "0.1.28-alpha"); // recorded bare by 0.1.28
  90. CHECK (at (rollbackIndex (releases, parseVersion ("0.1.28.1"), "0.1.28")) == "0.1.28-alpha");
  91. CHECK (at (rollbackIndex (releases, parseVersion ("0.1.28.1"), "")) == "0.1.28-alpha");
  92. CHECK (at (rollbackIndex (releases, parseVersion ("0.1.28.1"), "0.1.27")) == "0.1.27-alpha");
  93. CHECK (at (rollbackIndex (releases, parseVersion ("0.1.28.1"), "0.1.29-alpha")) == "0.1.28-alpha"); // (newer: ignored)
  94. CHECK (rollbackIndex (releases, parseVersion ("0.1.27"), "") < 0);
  95. // the skip: "0.1.28" (left by going back) hides its tag on the automatic check, not the next release
  96. Settings s;
  97. s = afterRollback (s, parseVersion ("0.1.28"));
  98. Release r;
  99. r.version = parseVersion ("v0.1.28-alpha");
  100. CHECK (! shouldShow (r, s, false) && shouldShow (r, s, true));
  101. r.version = parseVersion ("v0.1.28.1-alpha");
  102. CHECK (shouldShow (r, s, false));
  103. // a skipped tag and the bare version of the same release are the same: no change
  104. Settings tagged;
  105. tagged.skipped = "0.1.28-alpha";
  106. CHECK (skipLeft (tagged, parseVersion ("0.1.28")).skipped == "0.1.28-alpha");
  107. }
  108. TEST (goback190hl_b_installing_txt_reads_right_in_0127_0128_and_now)
  109. {
  110. using namespace lynx::update;
  111. InstallingNote note;
  112. note.version = "0.1.27-alpha";
  113. note.from = "0.1.29";
  114. note.rollback = true;
  115. const auto text = formatInstalling (note);
  116. CHECK (text.size() >= 21 && text.compare (text.size() - 21, 21, "version=0.1.27-alpha\n") == 0); // (the last line)
  117. // 0.1.27: the version, not "0.0.0"
  118. CHECK (hl190::versionAsRead0127 (text) == "0.1.27-alpha" && parseVersion (hl190::versionAsRead0127 (text)).valid);
  119. // round 190gh's layout (version first) is what 0.1.27 misread
  120. const std::string old190gh = "version=0.1.27-alpha\nfrom=0.1.28\nrollback=1\n";
  121. CHECK (! parseVersion (hl190::versionAsRead0127 (old190gh)).valid);
  122. // 0.1.28 and this version read all three, in either layout
  123. const auto by0128 = hl190::asRead0128 (text);
  124. CHECK (by0128.version == "0.1.27-alpha" && by0128.from == "0.1.29" && by0128.rollback);
  125. const auto now = parseInstalling (text);
  126. CHECK (now.version == "0.1.27-alpha" && now.from == "0.1.29" && now.rollback);
  127. const auto legacy = parseInstalling (old190gh);
  128. CHECK (legacy.version == "0.1.27-alpha" && legacy.from == "0.1.28" && legacy.rollback);
  129. const auto plain = parseInstalling ("version=0.1.28-alpha\n"); // 0.1.27's own note
  130. CHECK (plain.version == "0.1.28-alpha" && plain.from.empty() && ! plain.rollback);
  131. CHECK (parseInstalling ("version= 0.1.28-alpha \r\nfrom=0.1.27\r\nrollback=0\r\n").from == "0.1.27");
  132. // an update (not going back) from an unknown version: still readable by 0.1.27
  133. InstallingNote up;
  134. up.version = "0.1.29-alpha";
  135. const auto upText = formatInstalling (up);
  136. CHECK (hl190::versionAsRead0127 (upText) == "0.1.29-alpha" && ! parseInstalling (upText).rollback && parseInstalling (upText).from.empty());
  137. }
  138. TEST (goback190hl_c_the_skip_is_in_the_settings_older_versions_read)
  139. {
  140. using namespace lynx::update;
  141. Settings s;
  142. s.previous = "0.1.28";
  143. s.lastCheckMs = 1763200000000LL;
  144. // before the installer of the older version runs: only the skip, the previous stays until the restart
  145. const auto before = skipLeft (s, parseVersion ("0.1.29"));
  146. CHECK (before.skipped == "0.1.29" && before.previous == "0.1.28" && before.lastCheckMs == s.lastCheckMs);
  147. const auto text = formatSettings (before);
  148. CHECK (text.find ("\nskipped=0.1.29\n") != std::string::npos); // the key and value 0.1.21+ read (three numbers)
  149. // what 0.1.27 does with it: parse, then hide v0.1.29-alpha on the automatic check, offer it on a manual one
  150. const auto read = parseSettings (text);
  151. Release next;
  152. next.version = parseVersion ("v0.1.29-alpha");
  153. CHECK (read.skipped == "0.1.29" && ! shouldShow (next, read, false) && shouldShow (next, read, true));
  154. next.version = parseVersion ("v0.1.30-alpha");
  155. CHECK (shouldShow (next, read, false)); // the release after it is offered
  156. // after the restart a version that knows going back uses the previous up; the skip stays the same
  157. const auto after = afterRollback (before, parseVersion ("0.1.29"));
  158. CHECK (after.skipped == "0.1.29" && after.previous.empty());
  159. // an already skipped newer version stays; a version that is no version changes nothing
  160. Settings later;
  161. later.skipped = "0.1.31-alpha";
  162. CHECK (skipLeft (later, parseVersion ("0.1.29")).skipped == "0.1.31-alpha");
  163. CHECK (skipLeft (s, Version {}).skipped.empty());
  164. // a hotfix with four numbers is skipped as written (0.1.28 and older can't read it, nor see its release: consistent)
  165. CHECK (skipLeft (Settings {}, parseVersion ("0.1.28.1")).skipped == "0.1.28.1" && parseSettings ("skipped=0.1.28.1\n").skipped == "0.1.28.1");
  166. }