aboutsummaryrefslogtreecommitdiffstats
path: root/test/NamespaceReferenceManager.cpp
blob: 27f0dcd1d008ba34f73a012446b0fe7cb5768bb8 (plain) (blame)
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
#include <catch2/catch_test_macros.hpp>

#include "InMemoryBackend.h"
#include "Merkle.h"
#include "NamespaceReferenceManager.h"
#include "ObjectManager.h"

#include <memory>

// ---------------------------------------------------------------------------
// Resolver — two-level reparse through the branch tree.
//
// NamespaceReferenceManager::Resolve rewrites
//   /system/branches/<name>/multigroups/<mg>/<sub>...
// into
//   /system/snapshots/<mg_hash>/<sub>...
// by reading branch.target_hash (a Merkle<string> root) from storage and
// looking up <mg>. Session paths consult the per-session override map first
// and fall back to the global branch when no override is set.
// ---------------------------------------------------------------------------

namespace {

using Path = std::vector<std::string>;

// Pad a label into a deterministic 64-char hex string so the merkle codec can
// decode it as a 32-byte payload.
std::string mk_hash(char c, char d = '0')
{
    std::string h(64, '0');
    h[0] = c;
    h[1] = d;
    return h;
}

std::unique_ptr<nt::ObjectManager::object_type> make_type(OBJECT_TYPE label)
{
    auto t      = std::make_unique<nt::ObjectManager::object_type>();
    t->label    = label;
    t->methods  = { OPEN, CLOSE };
    return t;
}

void register_branch(nt::ObjectManager& om,
                     const std::string& name,
                     const std::string& target_hash)
{
    auto br         = std::make_unique<nt::ObjectManager::Branch>();
    br->name        = name;
    br->target_hash = target_hash;
    om.Register({"system", "branches", name}, std::move(br), make_type(BRANCH));
}

void register_session(nt::ObjectManager& om,
                      const std::string& id,
                      std::map<std::string, std::string> overrides = {})
{
    auto s = std::make_unique<nt::ObjectManager::Session>();
    s->branch_overrides = std::move(overrides);
    om.Register({"system", "sessions", id}, std::move(s), make_type(SESSION));
}

// Builds a branch tree with a single (mg_name → mg_hash) entry. Returns the
// branch-tree root hex.
std::string build_branch_tree(nt::InMemoryBackend& store,
                               const std::string& mg_name,
                               const std::string& mg_hash_hex)
{
    return nt::Merkle<std::string>::Insert(store, "", mg_name,
                                            nt::hex_to_bin(mg_hash_hex));
}

}  // namespace

TEST_CASE("Branch sub-path rewrites through the branch tree to the mg snapshot",
          "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;

    const std::string mg_hash = mk_hash('a');
    const std::string tree    = build_branch_tree(store, "warehouse", mg_hash);
    register_branch(om, "main", tree);

    nt::NamespaceReferenceManager nrm(om, store);
    REQUIRE(nrm.Resolve({"system", "branches", "main", "multigroups", "warehouse", "relations", "items"})
            == Path{"system", "snapshots", mg_hash, "relations", "items"});
}

TEST_CASE("Resolve on the branch object itself is left unchanged", "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    const std::string tree = build_branch_tree(store, "warehouse", mk_hash('a'));
    register_branch(om, "main", tree);

    nt::NamespaceReferenceManager nrm(om, store);
    const Path p{"system", "branches", "main"};
    REQUIRE(nrm.Resolve(p) == p);
}

TEST_CASE("Resolve on an unborn branch leaves the path unchanged", "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    register_branch(om, "main", "");  // unborn

    nt::NamespaceReferenceManager nrm(om, store);
    const Path p{"system", "branches", "main", "multigroups", "warehouse", "relations", "items"};
    REQUIRE(nrm.Resolve(p) == p);
}

TEST_CASE("Resolve on an absent multigroup leaves the path unchanged", "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    const std::string tree = build_branch_tree(store, "warehouse", mk_hash('a'));
    register_branch(om, "main", tree);

    nt::NamespaceReferenceManager nrm(om, store);
    const Path p{"system", "branches", "main", "multigroups", "audit", "relations", "events"};
    REQUIRE(nrm.Resolve(p) == p);
}

TEST_CASE("Resolve on an unknown branch leaves the path unchanged", "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    nt::NamespaceReferenceManager nrm(om, store);
    const Path p{"system", "branches", "ghost", "multigroups", "warehouse", "relations", "items"};
    REQUIRE(nrm.Resolve(p) == p);
}

TEST_CASE("Non-branch paths pass through unchanged", "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    nt::NamespaceReferenceManager nrm(om, store);
    const Path p{"relations", "anywhere"};
    REQUIRE(nrm.Resolve(p) == p);
}

TEST_CASE("Session override takes precedence over the global branch HEAD",
          "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;

    const std::string mg_global  = mk_hash('a');
    const std::string mg_session = mk_hash('b');
    const std::string tree_global  =
        build_branch_tree(store, "warehouse", mg_global);
    const std::string tree_session =
        build_branch_tree(store, "warehouse", mg_session);

    register_branch(om, "main", tree_global);
    register_session(om, "sess123", {{"main", tree_session}});

    nt::NamespaceReferenceManager nrm(om, store);
    REQUIRE(nrm.Resolve({"system", "sessions", "sess123", "branches", "main",
                         "multigroups", "warehouse", "relations", "items"})
            == Path{"system", "snapshots", mg_session, "relations", "items"});
}

TEST_CASE("Session resolution falls back to global branch when override is absent",
          "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;

    const std::string mg_global = mk_hash('a');
    const std::string tree_global =
        build_branch_tree(store, "warehouse", mg_global);

    register_branch(om, "main", tree_global);
    register_session(om, "sess123");  // no overrides

    nt::NamespaceReferenceManager nrm(om, store);
    REQUIRE(nrm.Resolve({"system", "sessions", "sess123", "branches", "main",
                         "multigroups", "warehouse", "relations", "items"})
            == Path{"system", "snapshots", mg_global, "relations", "items"});
}

TEST_CASE("Session resolution with neither override nor global branch is unchanged",
          "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    register_session(om, "sess123");

    nt::NamespaceReferenceManager nrm(om, store);
    const Path p{"system", "sessions", "sess123", "branches", "main",
                 "multigroups", "warehouse", "relations", "items"};
    REQUIRE(nrm.Resolve(p) == p);
}

TEST_CASE("Empty input path is returned unchanged", "[resolve]")
{
    nt::ObjectManager om;
    nt::InMemoryBackend store;
    nt::NamespaceReferenceManager nrm(om, store);
    REQUIRE(nrm.Resolve({}).empty());
}