← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


commit 2fc9601cefd243ff352d10f979d48d831ea073ce
parent 753c3befa3255582b09fff9a88d2e30c1c8c3cb9
Author: Michael Camilleri <[email protected]>
Date:   Fri, 16 Oct 2026 16:03:17 +0900

Fix precision of time stamps

Diffstat

MTests/Unit/RealExportVerificationTests.swift | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 102 insertions(+), 0 deletions(-)

diff --git a/Tests/Unit/RealExportVerificationTests.swift b/Tests/Unit/RealExportVerificationTests.swift

@@ -85,4 +85,106 @@ struct RealExportVerificationTests {
         #expect(reimport.tokensInserted == 0)
         #expect(reimport.reportsAlreadyPresent == summary.reportCount)
     }
+
+    @Test(
+        "The real archive exports and reads back as the store holds it",
+        .enabled(
+            if: exportPath != nil, "Set TEST_RUNNER_REPORTAGE_REAL_EXPORT to the export's path"),
+        .timeLimit(.minutes(10))
+    )
+    func reportageExport() async throws {
+        let url = URL(fileURLWithPath: try #require(Self.exportPath))
+        let persistence = makeTestPersistence()
+        _ = try await ReporterImporter(persistence: persistence).importArchive(at: url)
+
+        // The importer stamps questions and tokens with the time of the
+        // import, and the format keeps dates to the millisecond.
+        func exportable(_ date: Date) -> Date {
+            ExportDates.date(from: ExportDates.string(from: date, in: .gmt)) ?? date
+        }
+
+        let context = persistence.newBackgroundContext()
+        var (questions, tokens, reports) = try await context.perform {
+            let request = ReportEntity.fetchRequest()
+            request.sortDescriptors = [
+                NSSortDescriptor(key: "date", ascending: true),
+                NSSortDescriptor(key: "id", ascending: true),
+            ]
+            return (
+                Set(try context.fetch(QuestionEntity.fetchRequest()).map { try $0.question() }),
+                Set(try context.fetch(TokenEntity.fetchRequest()).map { try $0.token() }),
+                try context.fetch(request).map { try $0.report() }
+            )
+        }
+
+        questions = Set(
+            questions.map {
+                var question = $0
+                question.modifiedAt = exportable(question.modifiedAt)
+                return question
+            })
+        tokens = Set(
+            tokens.map {
+                var token = $0
+                token.modifiedAt = exportable(token.modifiedAt)
+                return token
+            })
+
+        let exporter = ReportageExporter(persistence: persistence)
+        let directory = FileManager.default.temporaryDirectory
+        let clock = ContinuousClock()
+        var lines: [String] = []
+
+        for includeLegacy in [false, true] {
+            let file = directory.appendingPathComponent("reportage-real-\(UUID().uuidString).json")
+            defer { try? FileManager.default.removeItem(at: file) }
+
+            let start = clock.now
+            let summary = try await exporter.export(
+                to: file, options: ReportageExportOptions(includeLegacy: includeLegacy))
+            let exportDuration = clock.now - start
+            let data = try Data(contentsOf: file)
+
+            let readStart = clock.now
+            let export = try ReportageExport.decode(data)
+            let readDuration = clock.now - readStart
+
+            // Counted rather than compared with #expect, which would print
+            // the records themselves on a failure.
+            let questionsMatch = Set(try export.questions.map { try $0.question() }) == questions
+            let tokensMatch = Set(export.tokens.map { $0.token() }) == tokens
+            var reportMismatches = 0
+            var legacyMismatches = 0
+            for (exported, stored) in zip(export.reports, reports) {
+                var decoded = try exported.report()
+                decoded.legacy = nil
+                var expected = stored
+                expected.legacy = nil
+                if decoded != expected { reportMismatches += 1 }
+
+                let original = try stored.legacy.map { try LegacyBlob.decompress($0) }
+                let written = exported.legacy.map { Data($0.utf8) }
+                if written != (includeLegacy ? original : nil) { legacyMismatches += 1 }
+            }
+            let countsMatch = export.reports.count == reports.count
+
+            lines.append(
+                """
+                  legacy \(includeLegacy ? "included" : "omitted"):
+                    size: \(ByteCountFormatter.string(fromByteCount: Int64(data.count), countStyle: .file)), export time: \(exportDuration), read time: \(readDuration)
+                    questions: \(summary.questions), tokens: \(summary.tokens), reports: \(summary.reports)
+                    questions match: \(questionsMatch), tokens match: \(tokensMatch), report counts match: \(countsMatch)
+                    report mismatches: \(reportMismatches), legacy mismatches: \(legacyMismatches)
+                """
+            )
+
+            #expect(questionsMatch)
+            #expect(tokensMatch)
+            #expect(countsMatch)
+            #expect(reportMismatches == 0)
+            #expect(legacyMismatches == 0)
+        }
+
+        print("REAL ARCHIVE EXPORT SUMMARY\n" + lines.joined(separator: "\n"))
+    }
 }