|
| 1 | +#include <QtTest> |
| 2 | + |
| 3 | +#include <QTemporaryDir> |
| 4 | + |
| 5 | +#include <core/nodeidentifier.h> |
| 6 | +#include <core/nodeinfo.h> |
| 7 | +#include <core/servicelocator.h> |
| 8 | +#include <core/services/notebookcoreservice.h> |
| 9 | +#include <models/notebooknodemodel.h> |
| 10 | + |
| 11 | +#include <vxcore/vxcore.h> |
| 12 | + |
| 13 | +using namespace vnotex; |
| 14 | + |
| 15 | +namespace tests { |
| 16 | + |
| 17 | +class TestNotebookNodeModel : public QObject { |
| 18 | + Q_OBJECT |
| 19 | + |
| 20 | +private slots: |
| 21 | + void initTestCase(); |
| 22 | + void cleanupTestCase(); |
| 23 | + |
| 24 | + // Regression test: empty-state contract |
| 25 | + void testEmptyStateAfterSetNotebookId(); |
| 26 | + |
| 27 | +private: |
| 28 | + VxCoreContextHandle m_context = nullptr; |
| 29 | + NotebookCoreService *m_service = nullptr; |
| 30 | + ServiceLocator m_services; |
| 31 | +}; |
| 32 | + |
| 33 | +void TestNotebookNodeModel::initTestCase() { |
| 34 | + // CRITICAL: Enable test mode BEFORE creating vxcore context |
| 35 | + // to prevent tests from corrupting real user data |
| 36 | + vxcore_set_test_mode(1); |
| 37 | + |
| 38 | + // Initialize VxCore context |
| 39 | + QString configJson = "{}"; |
| 40 | + VxCoreError err = vxcore_context_create(configJson.toUtf8().constData(), &m_context); |
| 41 | + QCOMPARE(err, VXCORE_OK); |
| 42 | + QVERIFY(m_context != nullptr); |
| 43 | + |
| 44 | + // Create NotebookCoreService and register it |
| 45 | + m_service = new NotebookCoreService(m_context, this); |
| 46 | + QVERIFY(m_service != nullptr); |
| 47 | + m_services.registerService<NotebookCoreService>(m_service); |
| 48 | +} |
| 49 | + |
| 50 | +void TestNotebookNodeModel::cleanupTestCase() { |
| 51 | + // Destroy service before context (service depends on context) |
| 52 | + delete m_service; |
| 53 | + m_service = nullptr; |
| 54 | + |
| 55 | + if (m_context) { |
| 56 | + vxcore_context_destroy(m_context); |
| 57 | + m_context = nullptr; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void TestNotebookNodeModel::testEmptyStateAfterSetNotebookId() { |
| 62 | + // Create a temporary directory for the test notebook |
| 63 | + QTemporaryDir tmpDir; |
| 64 | + QVERIFY(tmpDir.isValid()); |
| 65 | + |
| 66 | + // Create a test notebook with a child file to ensure populated state |
| 67 | + QString configJson = R"({ |
| 68 | + "name": "Test", |
| 69 | + "description": "Test notebook", |
| 70 | + "version": "1" |
| 71 | + })"; |
| 72 | + QString nbId = |
| 73 | + m_service->createNotebook(tmpDir.filePath("nb"), configJson, NotebookType::Bundled); |
| 74 | + QVERIFY(!nbId.isEmpty()); |
| 75 | + |
| 76 | + // Add a child file so the populated assertion is meaningful |
| 77 | + QString fileId = m_service->createFile(nbId, QString(), QStringLiteral("note.md")); |
| 78 | + QVERIFY(!fileId.isEmpty()); |
| 79 | + |
| 80 | + // Create the model and set it to the populated notebook |
| 81 | + NotebookNodeModel model(m_services); |
| 82 | + model.setNotebookId(nbId); |
| 83 | + |
| 84 | + // Fetch children to populate the model |
| 85 | + QModelIndex root; // invalid == notebook root |
| 86 | + if (model.canFetchMore(root)) { |
| 87 | + model.fetchMore(root); |
| 88 | + } |
| 89 | + |
| 90 | + // PRE-RESET SANITY: Verify the model is populated before the reset |
| 91 | + int rowCountBefore = model.rowCount(root); |
| 92 | + QVERIFY2(rowCountBefore > 0, "Fixture must be non-empty for a meaningful regression test. " |
| 93 | + "Model should have at least one child (the created file)."); |
| 94 | + |
| 95 | + // THE TEST: Set notebook ID to empty string |
| 96 | + model.setNotebookId(QString()); |
| 97 | + |
| 98 | + // ASSERTION 1: rowCount must be 0 after reset |
| 99 | + int rowCountAfter = model.rowCount(QModelIndex()); |
| 100 | + QCOMPARE(rowCountAfter, 0); |
| 101 | + |
| 102 | + // ASSERTION 2: getNotebookId must return empty string |
| 103 | + QVERIFY(model.getNotebookId().isEmpty()); |
| 104 | + |
| 105 | + // Cleanup: close the notebook |
| 106 | + m_service->closeNotebook(nbId); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace tests |
| 110 | + |
| 111 | +QTEST_GUILESS_MAIN(tests::TestNotebookNodeModel) |
| 112 | +#include "test_notebooknodemodel.moc" |
0 commit comments