#include #include #include #include #include #include #include #include #include #include "backend/AppState.h" #include "backend/FormatReader.h" #include "backend/HistoryModel.h" #include "backend/formats/FormatRegistry.h" #include "backend/formats/sources/ZipByteSource.h" static FormatRegistry makeRegistry() { return FormatRegistry({ FormatSpec::comic(QStringLiteral("cbz"), false, QStringLiteral("cbz"), [] { return std::make_unique(); }), }); } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- static bool addEntry(zipFile zf, const char *name, const QByteArray &data) { zip_fileinfo fi{}; if (zipOpenNewFileInZip(zf, name, &fi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK) return false; if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) { zipCloseFileInZip(zf); return false; } return zipCloseFileInZip(zf) == ZIP_OK; } static QByteArray makePng(int w = 4, int h = 4, QRgb color = qRgb(255, 0, 0)) { QImage img(w, h, QImage::Format_RGB32); img.fill(color); QByteArray data; QBuffer buf(&data); buf.open(QIODevice::WriteOnly); img.save(&buf, "PNG"); return data; } static QString buildCbzWithCover() { static const char kComicInfo[] = "" "" " Cover Test Book" " Test Author" ""; QTemporaryFile tmp; tmp.setFileTemplate(QDir::tempPath() + "/hbm_cover_test_XXXXXX.cbz"); tmp.setAutoRemove(false); if (!tmp.open()) return {}; const QString path = tmp.fileName(); tmp.close(); zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE); if (!zf) return {}; bool ok = true; ok = ok && addEntry(zf, "ComicInfo.xml", QByteArray(kComicInfo)); ok = ok && addEntry(zf, "001_cover.png", makePng()); zipClose(zf, nullptr); return ok ? path : QString{}; } // --------------------------------------------------------------------------- // Test class // --------------------------------------------------------------------------- class HistoryModelTest : public QObject { Q_OBJECT private slots: void addOrUpdateAndRoles() { FormatRegistry registry = makeRegistry(); HistoryModel model(registry); QSignalSpy countSpy(&model, &HistoryModel::countChanged); model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.42, QStringLiteral("2026-01-01T12:00:00")); QCOMPARE(countSpy.count(), 1); QCOMPARE(model.rowCount(), 1); const QModelIndex idx = model.index(0, 0); QCOMPARE(model.data(idx, HistoryModel::FilePathRole).toString(), QStringLiteral("/path/to/book.cbz")); QCOMPARE(model.data(idx, HistoryModel::ProgressRole).toDouble(), 0.42); QCOMPARE(model.data(idx, HistoryModel::LastOpenedRole).toString(), QStringLiteral("2026-01-01T12:00:00")); QCOMPARE(model.data(idx, HistoryModel::FormatRole).toString(), QStringLiteral("CBZ")); QCOMPARE(model.lastOpened(0), QDateTime::fromString(QStringLiteral("2026-01-01T12:00:00"), Qt::ISODate)); } void loadMetadataPopulatesTitleAndCoverUrl() { const QString cbzPath = buildCbzWithCover(); QVERIFY(!cbzPath.isEmpty()); FormatRegistry registry = makeRegistry(); HistoryModel model(registry); QSignalSpy dataSpy(&model, &QAbstractListModel::dataChanged); model.addOrUpdateBook(cbzPath, 0.0); QTRY_VERIFY_WITH_TIMEOUT(dataSpy.count() > 0, 5000); QFile::remove(cbzPath); const QModelIndex idx = model.index(0, 0); QCOMPARE(model.data(idx, HistoryModel::TitleRole).toString(), QStringLiteral("Cover Test Book")); QCOMPARE(model.data(idx, HistoryModel::AuthorRole).toString(), QStringLiteral("Test Author")); const QString coverUrl = model.data(idx, HistoryModel::CoverImageUrlRole).toString(); QVERIFY(!coverUrl.isEmpty()); QVERIFY(coverUrl.startsWith(QStringLiteral("image://cbz/001_cover.png?src="))); } void removeBook() { FormatRegistry registry = makeRegistry(); HistoryModel model(registry); model.addOrUpdateBook(QStringLiteral("/path/to/1.cbz"), 0.1); model.addOrUpdateBook(QStringLiteral("/path/to/2.cbz"), 0.2); QCOMPARE(model.rowCount(), 2); model.removeBook(QStringLiteral("/path/to/1.cbz")); QCOMPARE(model.rowCount(), 1); QCOMPARE(model.filePath(0), QStringLiteral("/path/to/2.cbz")); } void loadAndSaveWithAppState() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-test-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); FormatRegistry registry = makeRegistry(); { HistoryModel model(registry); model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.75, QStringLiteral("2026-10-25T14:00:00")); model.save(appState); } { HistoryModel model2(registry); model2.load(appState); QCOMPARE(model2.rowCount(), 1); QCOMPARE(model2.filePath(0), QStringLiteral("/path/to/book.cbz")); QCOMPARE(model2.progress(0), 0.75); QCOMPARE(model2.lastOpened(0), QDateTime::fromString(QStringLiteral("2026-10-25T14:00:00"), Qt::ISODate)); } } void bogusDateDroppedOnLoad() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-bogus-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); appState.saveArray(QStringLiteral("history"), { { {QStringLiteral("filePath"), QStringLiteral("/path/valid1.cbz")}, {QStringLiteral("progress"), 0.1}, {QStringLiteral("lastOpened"), QStringLiteral("2026-10-20T10:00:00")}, }, { {QStringLiteral("filePath"), QStringLiteral("/path/bogus_empty.cbz")}, {QStringLiteral("progress"), 0.2}, {QStringLiteral("lastOpened"), QStringLiteral("")}, }, { {QStringLiteral("filePath"), QStringLiteral("/path/valid2.cbz")}, {QStringLiteral("progress"), 0.3}, {QStringLiteral("lastOpened"), QStringLiteral("2026-10-25T15:30:00")}, }, { {QStringLiteral("filePath"), QStringLiteral("/path/bogus_text.cbz")}, {QStringLiteral("progress"), 0.4}, {QStringLiteral("lastOpened"), QStringLiteral("not-a-valid-date")}, }, { {QStringLiteral("filePath"), QStringLiteral("/path/bogus_invalid_date.cbz")}, {QStringLiteral("progress"), 0.5}, {QStringLiteral("lastOpened"), QStringLiteral("2026-02-31T00:00:00")}, }, { {QStringLiteral("filePath"), QStringLiteral("")}, {QStringLiteral("progress"), 0.6}, {QStringLiteral("lastOpened"), QStringLiteral("2026-10-30T10:00:00")}, }, }); FormatRegistry registry = makeRegistry(); HistoryModel model(registry); model.load(appState); QCOMPARE(model.rowCount(), 2); QCOMPARE(model.filePath(0), QStringLiteral("/path/valid2.cbz")); QCOMPARE(model.filePath(1), QStringLiteral("/path/valid1.cbz")); } void bogusDateDroppedOnAddOrUpdate() { FormatRegistry registry = makeRegistry(); HistoryModel model(registry); model.addOrUpdateBook(QStringLiteral("/path/bad1.cbz"), 0.5, QDateTime()); QCOMPARE(model.rowCount(), 0); model.addOrUpdateBook(QStringLiteral("/path/bad2.cbz"), 0.5, QStringLiteral("corrupted-date")); QCOMPARE(model.rowCount(), 0); model.addOrUpdateBook(QString(), 0.5, QDateTime::currentDateTime()); QCOMPARE(model.rowCount(), 0); const QDateTime dt = QDateTime::fromString(QStringLiteral("2026-10-25T12:00:00"), Qt::ISODate); model.addOrUpdateBook(QStringLiteral("/path/good.cbz"), 0.5, dt); QCOMPARE(model.rowCount(), 1); model.addOrUpdateBook(QStringLiteral("/path/good.cbz"), 0.8, QStringLiteral("garbage")); QCOMPARE(model.lastOpened(0), dt); QCOMPARE(model.progress(0), 0.5); } void loadSortsEntriesByLastOpenedDesc() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-sort-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); appState.saveArray(QStringLiteral("history"), { { {QStringLiteral("filePath"), QStringLiteral("/path/oldest.cbz")}, {QStringLiteral("progress"), 0.1}, {QStringLiteral("lastOpened"), QStringLiteral("2026-01-01T10:00:00")}, }, { {QStringLiteral("filePath"), QStringLiteral("/path/newest.cbz")}, {QStringLiteral("progress"), 0.5}, {QStringLiteral("lastOpened"), QStringLiteral("2026-10-30T10:00:00")}, }, { {QStringLiteral("filePath"), QStringLiteral("/path/middle.cbz")}, {QStringLiteral("progress"), 0.3}, {QStringLiteral("lastOpened"), QStringLiteral("2026-06-15T10:00:00")}, }, }); FormatRegistry registry = makeRegistry(); HistoryModel model(registry); model.load(appState); QCOMPARE(model.rowCount(), 3); QCOMPARE(model.filePath(0), QStringLiteral("/path/newest.cbz")); QCOMPARE(model.filePath(1), QStringLiteral("/path/middle.cbz")); QCOMPARE(model.filePath(2), QStringLiteral("/path/oldest.cbz")); } void saveAndLoadPreservesChronologicalOrder() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-pres-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); FormatRegistry registry = makeRegistry(); { HistoryModel model(registry); model.addOrUpdateBook(QStringLiteral("/path/a.cbz"), 0.1, QStringLiteral("2026-10-10T10:00:00")); model.addOrUpdateBook(QStringLiteral("/path/b.cbz"), 0.2, QStringLiteral("2026-10-20T10:00:00")); model.addOrUpdateBook(QStringLiteral("/path/c.cbz"), 0.3, QStringLiteral("2026-10-30T10:00:00")); QCOMPARE(model.filePath(0), QStringLiteral("/path/c.cbz")); QCOMPARE(model.filePath(1), QStringLiteral("/path/b.cbz")); QCOMPARE(model.filePath(2), QStringLiteral("/path/a.cbz")); model.save(appState); } { HistoryModel model2(registry); model2.load(appState); QCOMPARE(model2.rowCount(), 3); QCOMPARE(model2.filePath(0), QStringLiteral("/path/c.cbz")); QCOMPARE(model2.filePath(1), QStringLiteral("/path/b.cbz")); QCOMPARE(model2.filePath(2), QStringLiteral("/path/a.cbz")); } } void addOrUpdateMaintainsChronologicalOrder() { FormatRegistry registry = makeRegistry(); HistoryModel model(registry); model.addOrUpdateBook(QStringLiteral("/path/1.cbz"), 0.1, QStringLiteral("2026-05-01T12:00:00")); model.addOrUpdateBook(QStringLiteral("/path/2.cbz"), 0.2, QStringLiteral("2026-01-01T12:00:00")); model.addOrUpdateBook(QStringLiteral("/path/3.cbz"), 0.3, QStringLiteral("2026-03-01T12:00:00")); QCOMPARE(model.rowCount(), 3); QCOMPARE(model.filePath(0), QStringLiteral("/path/1.cbz")); QCOMPARE(model.filePath(1), QStringLiteral("/path/3.cbz")); QCOMPARE(model.filePath(2), QStringLiteral("/path/2.cbz")); // Update book 2 (Jan) to June (newest) -> moves to index 0 model.addOrUpdateBook(QStringLiteral("/path/2.cbz"), 0.5, QStringLiteral("2026-06-01T12:00:00")); QCOMPARE(model.filePath(0), QStringLiteral("/path/2.cbz")); QCOMPARE(model.filePath(1), QStringLiteral("/path/1.cbz")); QCOMPARE(model.filePath(2), QStringLiteral("/path/3.cbz")); // Update book 2 (now index 0) to February (between Jan and Mar) -> moves to index 2 model.addOrUpdateBook(QStringLiteral("/path/2.cbz"), 0.6, QStringLiteral("2026-02-01T12:00:00")); QCOMPARE(model.filePath(0), QStringLiteral("/path/1.cbz")); QCOMPARE(model.filePath(1), QStringLiteral("/path/3.cbz")); QCOMPARE(model.filePath(2), QStringLiteral("/path/2.cbz")); } }; QTEST_MAIN(HistoryModelTest) #include "HistoryModelTest.moc"