File: tests-db.cpp

package info (click to toggle)
appstream-generator 0.10.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,344 kB
  • sloc: cpp: 42,038; python: 292; xml: 259; sh: 223; makefile: 20
file content (704 lines) | stat: -rw-r--r-- 24,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
 * Copyright (C) 2019-2025 Matthias Klumpp <matthias@tenstral.net>
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>

#include <fstream>
#include <filesystem>
#include <optional>
#include <cstdlib>
#include <format>
#include <chrono>
#include <logging.h>
#include <thread>

#include "contentsstore.h"
#include "datastore.h"
#include "config.h"
#include "utils.h"
#include "backends/dummy/dummypkg.h"
#include "result.h"

using namespace ASGenerator;

static struct TestSetup {
    TestSetup()
    {
        setVerbose(true);
    }
} testSetup;

TEST_CASE("ContentsStore basic operations", "[contentsstore]")
{
    // Create temporary directory for test database
    auto tempDir = fs::temp_directory_path() / std::format("asgen-test-{}", Utils::randomString(8));
    fs::create_directories(tempDir);

    SECTION("Constructor and basic lifecycle")
    {
        ContentsStore store;
        REQUIRE_NOTHROW(store.open(tempDir.string()));
        REQUIRE_NOTHROW(store.close());
    }

    SECTION("Package operations")
    {
        ContentsStore store;
        store.open(tempDir.string());

        const std::string testPkgId = "testpkg/1.0.0/amd64";

        // Initially package should not exist
        REQUIRE_FALSE(store.packageExists(testPkgId));

        // Add contents to package
        std::vector<std::string> contents = {
            "/usr/bin/testapp",
            "/usr/share/applications/testapp.desktop",
            "/usr/share/icons/hicolor/48x48/apps/testapp.png",
            "/usr/share/icons/hicolor/64x64/apps/testapp.png",
            "/usr/share/pixmaps/testapp.png",
            "/usr/share/locale/de/LC_MESSAGES/testapp.mo",
            "/usr/share/locale/fr/LC_MESSAGES/testapp.mo",
            "/usr/lib/testapp/plugin.so"};

        REQUIRE_NOTHROW(store.addContents(testPkgId, contents));

        // Now package should exist
        REQUIRE(store.packageExists(testPkgId));

        // Retrieve and verify contents
        auto retrievedContents = store.getContents(testPkgId);
        REQUIRE(retrievedContents.size() == contents.size());

        // Check that all original contents are present
        for (const auto &item : contents) {
            REQUIRE(std::find(retrievedContents.begin(), retrievedContents.end(), item) != retrievedContents.end());
        }

        store.close();
    }

    SECTION("Icon and locale filtering")
    {
        ContentsStore store;
        store.open(tempDir.string());

        const std::string testPkgId = "iconpkg/2.0.0/amd64";
        std::vector<std::string> contents = {
            "/usr/bin/app",
            "/usr/share/icons/hicolor/32x32/apps/app.png",
            "/usr/share/icons/hicolor/48x48/apps/app.svg",
            "/usr/share/pixmaps/app.xpm",
            "/usr/share/locale/en/LC_MESSAGES/app.mo",
            "/usr/share/locale/es/LC_MESSAGES/app.mo",
            "/usr/share/doc/app/README",
            "/usr/lib/qt5/translations/app_de.qm"};

        store.addContents(testPkgId, contents);

        // Test icon retrieval
        auto icons = store.getIcons(testPkgId);
        REQUIRE(icons.size() == 3); // 2 hicolor icons + 1 pixmap

        std::vector<std::string> expectedIcons = {
            "/usr/share/icons/hicolor/32x32/apps/app.png",
            "/usr/share/icons/hicolor/48x48/apps/app.svg",
            "/usr/share/pixmaps/app.xpm"};

        for (const auto &icon : expectedIcons) {
            REQUIRE(std::find(icons.begin(), icons.end(), icon) != icons.end());
        }

        // Test locale file retrieval
        auto localeFiles = store.getLocaleFiles(testPkgId);
        REQUIRE(localeFiles.size() == 3); // 2 .mo files + 1 .qm file

        std::vector<std::string> expectedLocaleFiles = {
            "/usr/share/locale/en/LC_MESSAGES/app.mo",
            "/usr/share/locale/es/LC_MESSAGES/app.mo",
            "/usr/lib/qt5/translations/app_de.qm"};

        for (const auto &locale : expectedLocaleFiles) {
            REQUIRE(std::find(localeFiles.begin(), localeFiles.end(), locale) != localeFiles.end());
        }

        store.close();
    }

    SECTION("Maps generation")
    {
        ContentsStore store;
        store.open(tempDir.string());

        // Add multiple packages
        std::vector<std::string> pkgIds = {"pkg1/1.0/amd64", "pkg2/2.0/amd64", "pkg3/3.0/amd64"};

        // Package 1: has icons and regular files
        store.addContents(
            pkgIds[0],
            {"/usr/bin/app1", "/usr/share/icons/hicolor/48x48/apps/app1.png", "/usr/share/applications/app1.desktop"});

        // Package 2: has locale files
        store.addContents(pkgIds[1], {"/usr/bin/app2", "/usr/share/locale/de/LC_MESSAGES/app2.mo"});

        // Package 3: mixed content
        store.addContents(
            pkgIds[2],
            {"/usr/lib/libtest.so", "/usr/share/pixmaps/test.png", "/usr/share/locale/fr/LC_MESSAGES/test.mo"});

        // Test contents map
        auto contentsMap = store.getContentsMap(pkgIds);
        REQUIRE(contentsMap.size() == 8); // All files from all packages
        REQUIRE(contentsMap["/usr/bin/app1"] == pkgIds[0]);
        REQUIRE(contentsMap["/usr/bin/app2"] == pkgIds[1]);
        REQUIRE(contentsMap["/usr/lib/libtest.so"] == pkgIds[2]);

        // Test icon files map
        auto iconMap = store.getIconFilesMap(pkgIds);
        REQUIRE(iconMap.size() == 2); // 2 icon files total
        REQUIRE(iconMap["/usr/share/icons/hicolor/48x48/apps/app1.png"] == pkgIds[0]);
        REQUIRE(iconMap["/usr/share/pixmaps/test.png"] == pkgIds[2]);

        // Test locale map
        auto localeMap = store.getLocaleMap(pkgIds);
        REQUIRE(localeMap.size() == 2); // 2 locale files total
        REQUIRE(localeMap["/usr/share/locale/de/LC_MESSAGES/app2.mo"] == pkgIds[1]);
        REQUIRE(localeMap["/usr/share/locale/fr/LC_MESSAGES/test.mo"] == pkgIds[2]);

        store.close();
    }

    SECTION("Package removal")
    {
        ContentsStore store;
        store.open(tempDir.string());

        const std::string pkgId = "removeme/1.0/amd64";
        std::vector<std::string> contents = {"/usr/bin/removeme", "/usr/share/doc/removeme"};

        store.addContents(pkgId, contents);
        REQUIRE(store.packageExists(pkgId));

        store.removePackage(pkgId);
        REQUIRE_FALSE(store.packageExists(pkgId));

        // Contents should be empty after removal
        auto retrievedContents = store.getContents(pkgId);
        REQUIRE(retrievedContents.empty());

        store.close();
    }

    SECTION("Package ID set operations")
    {
        ContentsStore store;
        store.open(tempDir.string());

        std::vector<std::string> pkgIds = {"pkg-a/1.0/amd64", "pkg-b/2.0/amd64", "pkg-c/3.0/i386"};

        // Add packages
        for (const auto &pkgId : pkgIds) {
            store.addContents(pkgId, {std::format("/usr/bin/{}", pkgId.substr(0, 5))});
        }

        // Get package ID set
        auto pkgIdSet = store.getPackageIdSet();
        REQUIRE(pkgIdSet.size() == 3);

        for (const auto &pkgId : pkgIds) {
            REQUIRE(pkgIdSet.find(pkgId) != pkgIdSet.end());
        }

        // Test bulk removal
        std::unordered_set<std::string> toRemove = {pkgIds[0], pkgIds[2]};
        store.removePackages(toRemove);

        // Only pkg-b should remain
        REQUIRE(store.packageExists(pkgIds[1]));
        REQUIRE_FALSE(store.packageExists(pkgIds[0]));
        REQUIRE_FALSE(store.packageExists(pkgIds[2]));

        store.close();
    }

    SECTION("Sync operation")
    {
        ContentsStore store;
        store.open(tempDir.string());

        store.addContents("sync-test/1.0/amd64", {"/usr/bin/synctest"});

        // Sync should not throw
        REQUIRE_NOTHROW(store.sync());

        store.close();
    }

    // Cleanup
    fs::remove_all(tempDir);
}

TEST_CASE("ContentsStore thread safety", "[contentsstore][threading]")
{
    auto tempDir = fs::temp_directory_path() / std::format("asgen-test-mt-{}", Utils::randomString(8));
    fs::create_directories(tempDir);

    ContentsStore store;
    store.open(tempDir.string());

    const int numThreads = 4;
    const int packagesPerThread = 10;
    std::vector<std::thread> threads;

    // Launch multiple threads that add packages concurrently
    for (int t = 0; t < numThreads; ++t) {
        threads.emplace_back([&store, t, packagesPerThread]() {
            for (int i = 0; i < packagesPerThread; ++i) {
                std::string pkgId = std::format("thread{}-pkg{}/1.0/amd64", t, i);
                std::vector<std::string> contents = {
                    std::format("/usr/bin/app-{}-{}", t, i), std::format("/usr/share/doc/app-{}-{}/README", t, i)};
                store.addContents(pkgId, contents);
            }
        });
    }

    // Wait for all threads
    for (auto &thread : threads)
        thread.join();

    // Verify all packages were added
    auto pkgSet = store.getPackageIdSet();
    REQUIRE(pkgSet.size() == numThreads * packagesPerThread);

    store.close();
    fs::remove_all(tempDir);
}

TEST_CASE("DataStore basic operations", "[datastore]")
{
    // Create temporary directory for test database
    auto tempDir = fs::temp_directory_path() / std::format("asgen-datastore-test-{}", Utils::randomString(8));
    auto mediaDir = fs::temp_directory_path() / std::format("asgen-media-test-{}", Utils::randomString(8));
    fs::create_directories(tempDir);
    fs::create_directories(mediaDir);

    SECTION("Constructor and basic lifecycle")
    {
        DataStore store;
        REQUIRE_NOTHROW(store.open(tempDir.string(), mediaDir.string()));
        REQUIRE_NOTHROW(store.close());
    }

    SECTION("Metadata storage and retrieval")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        const std::string gcid = "org.example.test";
        const std::string xmlData = R"(<component type="desktop-application">
    <id>org.example.test</id>
    <name>Test App</name>
  </component>)";

        const std::string yamlData = R"(Type: desktop-application
ID: org.example.test
Name:
  C: Test App
)";

        // Initially, metadata should not exist
        REQUIRE_FALSE(store.metadataExists(DataType::XML, gcid));
        REQUIRE_FALSE(store.metadataExists(DataType::YAML, gcid));

        // Store metadata
        REQUIRE_NOTHROW(store.setMetadata(DataType::XML, gcid, xmlData));
        REQUIRE_NOTHROW(store.setMetadata(DataType::YAML, gcid, yamlData));

        // Metadata should exist now and be retrievable
        REQUIRE(store.metadataExists(DataType::XML, gcid));
        REQUIRE(store.metadataExists(DataType::YAML, gcid));

        auto retrievedXml = store.getMetadata(DataType::XML, gcid);
        auto retrievedYaml = store.getMetadata(DataType::YAML, gcid);

        REQUIRE_FALSE(retrievedXml.empty());
        REQUIRE_FALSE(retrievedYaml.empty());
        REQUIRE(retrievedXml == xmlData);
        REQUIRE(retrievedYaml == yamlData);

        store.close();
    }

    SECTION("Package operations")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        const std::string pkgId = "testpkg/1.0.0/amd64";

        // Initially, package should not exist
        REQUIRE_FALSE(store.packageExists(pkgId));
        REQUIRE_FALSE(store.isIgnored(pkgId));

        // Test package value operations
        std::string pkgValue = store.getPackageValue(pkgId);
        REQUIRE(pkgValue.empty());

        // Mark package as ignored
        REQUIRE_NOTHROW(store.setPackageIgnore(pkgId));
        REQUIRE(store.packageExists(pkgId));
        REQUIRE(store.isIgnored(pkgId));

        // Remove package
        REQUIRE_NOTHROW(store.removePackage(pkgId));
        REQUIRE_FALSE(store.packageExists(pkgId));

        store.close();
    }

    SECTION("Hints storage and retrieval")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        const std::string pkgId = "hintpkg/2.0.0/amd64";
        const std::string hintsJson = R"({
    "hints": {
        "sugar-emulator.desktop": [
            {
                "tag": "no-metainfo",
                "vars": {}
            },
            {
                "tag": "description-missing",
                "vars": {
                    "kind": "desktop-application"
                }
            }
        ]
    },
    "package": "sugar-emulator-0.96\/0.96.1-2.1\/all"
})";

        // Initially, hints should not exist
        REQUIRE_FALSE(store.hasHints(pkgId));

        // Store hints
        REQUIRE_NOTHROW(store.setHints(pkgId, hintsJson));

        // Hints should exist now
        REQUIRE(store.hasHints(pkgId));

        // Retrieve and verify hints
        std::string retrievedHints = store.getHints(pkgId);
        REQUIRE_FALSE(retrievedHints.empty());
        REQUIRE(retrievedHints == hintsJson);

        store.close();
    }

    SECTION("Statistics operations")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        // Create test statistics data using the new binary format
        std::unordered_map<std::string, std::variant<std::int64_t, std::string, double>> statsData = {
            {"suite",         std::string("testing")},
            {"section",       std::string("main")   },
            {"totalInfos",    123                   },
            {"totalWarnings", 24                    },
            {"totalErrors",   8                     },
            {"totalMetadata", 42                    }
        };

        // Add statistics
        REQUIRE_NOTHROW(store.addStatistics(statsData));

        // Retrieve statistics
        auto allStats = store.getStatistics();
        REQUIRE_FALSE(allStats.empty());
        REQUIRE(allStats.size() >= 1);

        // Verify the data in the first entry
        const auto &firstEntry = allStats[0];
        REQUIRE(firstEntry.data.contains("suite"));
        REQUIRE(firstEntry.data.contains("section"));
        REQUIRE(firstEntry.data.contains("totalInfos"));

        REQUIRE(std::get<std::string>(firstEntry.data.at("suite")) == "testing");
        REQUIRE(std::get<std::string>(firstEntry.data.at("section")) == "main");
        REQUIRE(std::get<std::int64_t>(firstEntry.data.at("totalInfos")) == 123);
        REQUIRE(std::get<std::int64_t>(firstEntry.data.at("totalWarnings")) == 24);
        REQUIRE(std::get<std::int64_t>(firstEntry.data.at("totalErrors")) == 8);
        REQUIRE(std::get<std::int64_t>(firstEntry.data.at("totalMetadata")) == 42);

        store.close();
    }

    SECTION("Repository info operations")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        const std::string suite = "focal";
        const std::string section = "main";
        const std::string arch = "amd64";

        RepoInfo repoInfo;
        repoInfo.data["mtime"] = std::int64_t(1753758538);
        repoInfo.data["last_updated"] = 1643723400.0; // timestamp as double

        // Set repository info
        REQUIRE_NOTHROW(store.setRepoInfo(suite, section, arch, repoInfo));

        // Retrieve repository info
        RepoInfo retrievedRepoInfo = store.getRepoInfo(suite, section, arch);
        REQUIRE_FALSE(retrievedRepoInfo.data.empty());

        // Verify the content
        REQUIRE(retrievedRepoInfo.data.contains("mtime"));
        REQUIRE(retrievedRepoInfo.data.contains("last_updated"));

        REQUIRE(std::get<std::int64_t>(retrievedRepoInfo.data.at("mtime")) == 1753758538);
        REQUIRE(std::get<double>(retrievedRepoInfo.data.at("last_updated")) == 1643723400.0);

        // Remove repository info
        REQUIRE_NOTHROW(store.removeRepoInfo(suite, section, arch));

        // Verify removal - should return empty RepoInfo
        RepoInfo removedRepoInfo = store.getRepoInfo(suite, section, arch);
        REQUIRE(removedRepoInfo.data.empty());

        store.close();
    }

    SECTION("GCID operations")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        const std::string pkgId = "gcidpkg/1.0/amd64";

        // Initially no GCIDs
        auto gcids = store.getGCIDsForPackage(pkgId);
        REQUIRE(gcids.empty());

        // Create a dummy package for testing using the existing DummyPackage class
        auto dummyPkg = std::make_shared<DummyPackage>("gcidpkg", "1.0", "amd64");
        dummyPkg->setMaintainer("Test Maintainer <test@example.org>");
        dummyPkg->setDescription("A test package for GCID operations", "C");

        // Create a GeneratorResult with test components
        GeneratorResult gres(dummyPkg);

        // Create test components manually using AppStream
        g_autoptr(AsComponent) cpt1 = as_component_new();
        as_component_set_kind(cpt1, AS_COMPONENT_KIND_DESKTOP_APP);
        as_component_set_id(cpt1, "org.example.abiword");
        as_component_set_name(cpt1, "AbiWord", "C");
        as_component_set_summary(cpt1, "Word Processor", "C");

        g_autoptr(AsComponent) cpt2 = as_component_new();
        as_component_set_kind(cpt2, AS_COMPONENT_KIND_DESKTOP_APP);
        as_component_set_id(cpt2, "org.kde.ark");
        as_component_set_name(cpt2, "Ark", "C");
        as_component_set_summary(cpt2, "Archive Manager", "C");

        // Add components to the result
        gres.addComponent(cpt1);
        gres.addComponent(cpt2);

        // Verify components were added
        REQUIRE(gres.componentsCount() == 2);

        // Test XML metadata generation and storage
        REQUIRE_NOTHROW(store.addGeneratorResult(DataType::XML, gres, false));

        // Now we should have GCIDs for the package
        auto retrievedGcids = store.getGCIDsForPackage(pkgId);
        REQUIRE_FALSE(retrievedGcids.empty());
        REQUIRE(retrievedGcids.size() == 2);

        // Get the actual GCIDs that were generated
        auto actualGcids = gres.getComponentGcids();
        REQUIRE(actualGcids.size() == 2);

        // Verify that the stored GCIDs match what was generated
        for (const auto &gcid : actualGcids) {
            REQUIRE(std::find(retrievedGcids.begin(), retrievedGcids.end(), gcid) != retrievedGcids.end());
        }

        // Test that metadata was actually stored for each GCID
        for (const auto &gcid : actualGcids) {
            REQUIRE(store.metadataExists(DataType::XML, gcid));
            auto metadata = store.getMetadata(DataType::XML, gcid);
            REQUIRE_FALSE(metadata.empty());

            // Verify the metadata contains expected component information
            if (gcid.find("abiword") != std::string::npos || metadata.find("AbiWord") != std::string::npos) {
                REQUIRE(metadata.find("org.example.abiword") != std::string::npos);
            } else if (gcid.find("ark") != std::string::npos || metadata.find("Ark") != std::string::npos) {
                REQUIRE(metadata.find("org.kde.ark") != std::string::npos);
            }
        }

        // Test YAML metadata generation as well
        GeneratorResult gresYaml(dummyPkg);
        gresYaml.addComponent(cpt1);
        gresYaml.addComponent(cpt2);

        REQUIRE_NOTHROW(store.addGeneratorResult(DataType::YAML, gresYaml, false));

        // Verify YAML metadata was stored
        for (const auto &gcid : gresYaml.getComponentGcids()) {
            REQUIRE(store.metadataExists(DataType::YAML, gcid));
            auto yamlMetadata = store.getMetadata(DataType::YAML, gcid);
            REQUIRE_FALSE(yamlMetadata.empty());
        }

        // Test getMetadataForPackage
        auto xmlMetadataList = store.getMetadataForPackage(DataType::XML, pkgId);
        REQUIRE(xmlMetadataList.size() == 2);

        auto yamlMetadataList = store.getMetadataForPackage(DataType::YAML, pkgId);
        REQUIRE(yamlMetadataList.size() == 2);

        // Test regeneration behavior (alwaysRegenerate = false)
        GeneratorResult gresNoRegen(dummyPkg);
        gresNoRegen.addComponent(cpt1);
        gresNoRegen.addComponent(cpt2);

        // This should not regenerate since metadata already exists
        store.addGeneratorResult(DataType::XML, gresNoRegen, false);

        // Test forced regeneration (alwaysRegenerate = true)
        GeneratorResult gresForceRegen(dummyPkg);
        gresForceRegen.addComponent(cpt1);
        gresForceRegen.addComponent(cpt2);

        // This should regenerate even though metadata exists
        store.addGeneratorResult(DataType::XML, gresForceRegen, true);

        store.close();
    }

    SECTION("Package matching")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        // Add some test packages
        const std::vector<std::string> testPackages = {
            "myapp/1.0/amd64", "myapp/2.0/amd64", "mylib/1.5/amd64", "otherapp/3.0/i386"};

        for (const auto &pkgId : testPackages) {
            store.setPackageIgnore(pkgId);
        }

        // Test prefix matching
        auto matches = store.getPkidsMatching("myapp");
        REQUIRE(matches.size() == 2);
        REQUIRE(std::find(matches.begin(), matches.end(), "myapp/1.0/amd64") != matches.end());
        REQUIRE(std::find(matches.begin(), matches.end(), "myapp/2.0/amd64") != matches.end());

        auto libMatches = store.getPkidsMatching("mylib");
        REQUIRE(libMatches.size() == 1);
        REQUIRE(libMatches[0] == "mylib/1.5/amd64");

        auto noMatches = store.getPkidsMatching("nonexistent");
        REQUIRE(noMatches.empty());

        store.close();
    }

    SECTION("Package ID sets")
    {
        DataStore store;
        store.open(tempDir.string(), mediaDir.string());

        const std::vector<std::string> testPackages = {"pkg1/1.0/amd64", "pkg2/2.0/amd64", "pkg3/3.0/i386"};

        // Add packages
        for (const auto &pkgId : testPackages)
            store.setPackageIgnore(pkgId);

        // Get package ID set
        auto pkgSet = store.getPackageIdSet();
        REQUIRE(pkgSet.size() == testPackages.size());

        for (const auto &pkgId : testPackages)
            REQUIRE(pkgSet.contains(pkgId));

        // Test bulk removal
        std::unordered_set<std::string> toRemove = {testPackages[0], testPackages[2]};
        REQUIRE_NOTHROW(store.removePackages(toRemove));

        // Verify removal
        REQUIRE_FALSE(store.packageExists(testPackages[0]));
        REQUIRE(store.packageExists(testPackages[1])); // Should still exist
        REQUIRE_FALSE(store.packageExists(testPackages[2]));

        store.close();
    }

    // Cleanup
    fs::remove_all(tempDir);
    fs::remove_all(mediaDir);
}

TEST_CASE("DataStore thread safety", "[datastore][threading]")
{
    auto tempDir = fs::temp_directory_path() / std::format("asgen-datastore-test-mt-{}", Utils::randomString(8));
    auto mediaDir = fs::temp_directory_path() / std::format("asgen-media-test-mt-{}", Utils::randomString(8));
    fs::create_directories(tempDir);
    fs::create_directories(mediaDir);

    DataStore store;
    store.open(tempDir.string(), mediaDir.string());

    const int numThreads = 4;
    const int itemsPerThread = 10;
    std::vector<std::thread> threads;

    // Launch multiple threads that store metadata concurrently
    for (int t = 0; t < numThreads; ++t) {
        threads.emplace_back([&store, t, itemsPerThread]() {
            for (int i = 0; i < itemsPerThread; ++i) {
                std::string gcid = std::format("org.example.thread{}.item{}", t, i);
                std::string xmlData = std::format(
                    R"(<?xml version="1.0"?>
<component type="desktop-application">
  <id>{}</id>
  <name>Thread {} Item {}</name>
</component>)",
                    gcid,
                    t,
                    i);

                store.setMetadata(DataType::XML, gcid, xmlData);
            }
        });
    }

    // Wait for all threads
    for (auto &thread : threads)
        thread.join();

    // Verify thread safety by checking a few items
    for (int t = 0; t < 2; ++t) {
        for (int i = 0; i < 2; ++i) {
            std::string gcid = std::format("org.example.thread{}.item{}", t, i);
            REQUIRE(store.metadataExists(DataType::XML, gcid));
        }
    }

    store.close();
    fs::remove_all(tempDir);
    fs::remove_all(mediaDir);
}