|
| 1 | +// Never-saved ("Untitled") document crash-recovery mechanism test |
| 2 | +// (SYS-W9-07). |
| 3 | +// |
| 4 | +// MeshCraftApplication::performUntitledRecoverySave()/ |
| 5 | +// checkForUntitledRecovery()/recoverUntitledScene()/discardUntitledRecovery() |
| 6 | +// (see src/MeshCraft/Application/FileOps.cpp) are CNA-coupled (SDL/ImGui) |
| 7 | +// and not headlessly testable directly -- same limitation as |
| 8 | +// autosave_recovery_test.cpp's own header comment for the named-file case. |
| 9 | +// What IS pure filesystem + Mc3 and fully testable here is the mechanism |
| 10 | +// those methods reduce to: write the in-memory document to a single bounded |
| 11 | +// recovery path, later either load it back (recover) or remove it |
| 12 | +// (discard), and survive a corrupt/garbage file at that path without |
| 13 | +// crashing. `untitledRecoveryPath()` itself (src/MeshCraft/ |
| 14 | +// MeshCraftPrivate.hpp) lives in the config directory rather than beside a |
| 15 | +// real file, since an untitled document has no real path to place a |
| 16 | +// sibling next to -- this test uses a stand-in path in the same shape. |
| 17 | + |
| 18 | +#include <MeshCraft/Mc3/Mc3Document.hpp> |
| 19 | + |
| 20 | +#include <filesystem> |
| 21 | +#include <fstream> |
| 22 | +#include <iostream> |
| 23 | +#include <string> |
| 24 | + |
| 25 | +using namespace MeshCraft::Mc3; |
| 26 | + |
| 27 | +static int failures = 0; |
| 28 | +static void check(bool cond, const std::string& msg) { |
| 29 | + if (cond) { std::cout << "PASS: " << msg << "\n"; } |
| 30 | + else { std::cerr << "FAIL: " << msg << "\n"; ++failures; } |
| 31 | +} |
| 32 | + |
| 33 | +int main() { |
| 34 | + const auto dir = std::filesystem::temp_directory_path(); |
| 35 | + // Stand-in for meshcraftConfigDir() / "untitled.recovery.mc3.xml" -- same |
| 36 | + // shape (a single fixed path, not a sibling of any real document file). |
| 37 | + const auto recoveryPath = dir / "mc3_untitled_recovery_test.mc3.xml"; |
| 38 | + std::filesystem::remove(recoveryPath); |
| 39 | + |
| 40 | + // --- Modified untitled recovery: the periodic save writes real content, |
| 41 | + // and loading it back (what recoverUntitledScene() does) yields it. --- |
| 42 | + { |
| 43 | + Mc3Document doc; |
| 44 | + doc.model = "UntitledWorkInProgress"; |
| 45 | + doc.addObject(Mc3Object::makeSphere("Sphere", 1.0f, 16)); |
| 46 | + doc.saveToFile(recoveryPath); |
| 47 | + |
| 48 | + check(std::filesystem::exists(recoveryPath), |
| 49 | + "performUntitledRecoverySave()'s mechanism: the recovery file exists after saving"); |
| 50 | + |
| 51 | + Mc3Document recovered = Mc3Document::loadFromFile(recoveryPath); |
| 52 | + check(recovered.model == "UntitledWorkInProgress", |
| 53 | + "recoverUntitledScene()'s mechanism: loading the recovery path yields the saved content"); |
| 54 | + check(recovered.objects.size() == 1 && recovered.objects[0] && |
| 55 | + recovered.objects[0]->name == "Sphere", |
| 56 | + "recoverUntitledScene()'s mechanism: the recovered document's objects round-trip"); |
| 57 | + } |
| 58 | + |
| 59 | + // --- Discard: removing the file makes it as if nothing were ever |
| 60 | + // there -- checkForUntitledRecovery()'s existence check next startup |
| 61 | + // correctly finds nothing to offer. --- |
| 62 | + { |
| 63 | + check(std::filesystem::exists(recoveryPath), "discard scenario: recovery file exists before discard"); |
| 64 | + std::error_code ec; |
| 65 | + std::filesystem::remove(recoveryPath, ec); |
| 66 | + check(!ec, "discardUntitledRecovery()'s mechanism: remove() succeeds without error"); |
| 67 | + check(!std::filesystem::exists(recoveryPath), |
| 68 | + "discardUntitledRecovery()'s mechanism: the recovery file no longer exists"); |
| 69 | + } |
| 70 | + |
| 71 | + // --- Clean shutdown / successful Save As cleanup: both reduce to the |
| 72 | + // same "no recovery file left behind" postcondition as discard, just |
| 73 | + // triggered from a different call site (the destructor, or the Save As |
| 74 | + // dialog handler) -- already covered by the discard case above at the |
| 75 | + // filesystem-mechanism level; the *triggering condition* (currentFile_ |
| 76 | + // empty at shutdown, or transitioning away from empty on a successful |
| 77 | + // save) is App-level state this test cannot exercise headlessly. --- |
| 78 | + |
| 79 | + // --- Crash-marker simulation: a recovery file surviving without ever |
| 80 | + // being cleaned up is exactly what an abnormal termination (crash, kill, |
| 81 | + // power loss) looks like, since nothing ran to remove it. Simulated here |
| 82 | + // by simply leaving a file in place and confirming existence is |
| 83 | + // detectable -- checkForUntitledRecovery()'s own logic is a bare |
| 84 | + // exists() check, so this is the whole mechanism. --- |
| 85 | + { |
| 86 | + Mc3Document doc; |
| 87 | + doc.model = "SurvivedACrash"; |
| 88 | + doc.saveToFile(recoveryPath); |
| 89 | + check(std::filesystem::exists(recoveryPath), |
| 90 | + "crash-marker simulation: a file left behind by an unclean exit is detectable"); |
| 91 | + std::filesystem::remove(recoveryPath); |
| 92 | + } |
| 93 | + |
| 94 | + // --- Corrupt recovery file: must fail loudly (a catchable exception), |
| 95 | + // not crash -- recoverUntitledScene() wraps loadFromFile() in exactly |
| 96 | + // this try/catch. --- |
| 97 | + { |
| 98 | + { std::ofstream f(recoveryPath, std::ios::binary); f << "not xml at all {{{"; } |
| 99 | + bool threw = false; |
| 100 | + try { |
| 101 | + (void)Mc3Document::loadFromFile(recoveryPath); |
| 102 | + } catch (const std::exception&) { |
| 103 | + threw = true; |
| 104 | + } |
| 105 | + check(threw, "corrupt recovery file: loading it throws a catchable exception, not a crash"); |
| 106 | + std::filesystem::remove(recoveryPath); |
| 107 | + } |
| 108 | + |
| 109 | + // --- Coexistence with the existing sibling .autosave recovery |
| 110 | + // (SYS-W9-02): the two mechanisms use disjoint path shapes by |
| 111 | + // construction (config-directory single slot vs. a real file's own |
| 112 | + // "<file>.autosave" sibling), so writing both for the same logical |
| 113 | + // session cannot collide. --- |
| 114 | + { |
| 115 | + const auto namedFile = dir / "mc3_untitled_recovery_test_named.mc3.xml"; |
| 116 | + const auto namedAutosave = std::filesystem::path(namedFile.string() + ".autosave"); |
| 117 | + std::filesystem::remove(namedFile); |
| 118 | + std::filesystem::remove(namedAutosave); |
| 119 | + |
| 120 | + Mc3Document untitledDoc; |
| 121 | + untitledDoc.model = "UntitledSlot"; |
| 122 | + untitledDoc.saveToFile(recoveryPath); |
| 123 | + |
| 124 | + Mc3Document namedDoc; |
| 125 | + namedDoc.model = "NamedSaved"; |
| 126 | + namedDoc.saveToFile(namedFile); |
| 127 | + Mc3Document namedAutosaveDoc; |
| 128 | + namedAutosaveDoc.model = "NamedAutosaved"; |
| 129 | + namedAutosaveDoc.saveToFile(namedAutosave); |
| 130 | + |
| 131 | + check(recoveryPath != namedAutosave && recoveryPath != namedFile, |
| 132 | + "coexistence: the untitled-recovery path is distinct from a named file's own " |
| 133 | + "path and its .autosave sibling"); |
| 134 | + check(Mc3Document::loadFromFile(recoveryPath).model == "UntitledSlot" && |
| 135 | + Mc3Document::loadFromFile(namedFile).model == "NamedSaved" && |
| 136 | + Mc3Document::loadFromFile(namedAutosave).model == "NamedAutosaved", |
| 137 | + "coexistence: all three files retain their own independent content"); |
| 138 | + |
| 139 | + std::filesystem::remove(recoveryPath); |
| 140 | + std::filesystem::remove(namedFile); |
| 141 | + std::filesystem::remove(namedAutosave); |
| 142 | + } |
| 143 | + |
| 144 | + if (failures == 0) { std::cout << "All untitled-recovery tests passed.\n"; return 0; } |
| 145 | + std::cerr << failures << " untitled-recovery test(s) failed.\n"; |
| 146 | + return 1; |
| 147 | +} |
0 commit comments