Repository navigation
Expand file tree
/
Copy pathchecker_tests.cpp
More file actions
416 lines (394 loc) · 17.3 KB
/
Copy pathchecker_tests.cpp
File metadata and controls
416 lines (394 loc) · 17.3 KB
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
#include "../../tests/support/checker_fixture.hpp"
#include <teaql/runtime/module.hpp>
#include <teaql/runtime/generated_checker.hpp>
#include <teaql/runtime/detail/graph_fix_scope.hpp>
#include <cstdlib>
#include <iostream>
#include <thread>
using namespace teaql;
using namespace checker_fixture;
static void check(bool condition, const char* message) {
if (!condition) {
throw std::runtime_error(message);
}
}
template <class E, class F>
static void rejected(F&& action, const char* message) {
bool caught = false;
try {
action();
} catch (const E&) {
caught = true;
}
check(caught, message);
}
static void set(EntityValues& values, std::string_view name, Value value) {
values.fields.insert_or_assign(String(name, values.memory_resource()), std::move(value));
}
static void generated_rule_mechanics() {
GlobalContext global;
auto user_context = global.make_user_context();
auto r = user_context->memory_resource();
ObjectLocation location(std::allocator_arg, Allocator(r));
CheckResults results(r);
GeneratedCheckerLogic::requiredText("\xc2\xa0\xe3\x80\x80", "name", location, results);
GeneratedCheckerLogic::minStringLength(
"\xc3\xa9\xf0\x9f\x90\xbe", "name", 3, location, results);
GeneratedCheckerLogic::maxStringLength(
"\xc3\xa9\xf0\x9f\x90\xbe", "name", 1, location, results);
check(results.size() == 3 && results[0].rule == CheckRule::Required &&
results[1].rule == CheckRule::MinStringLength &&
results[2].rule == CheckRule::MaxStringLength,
"logic helpers use Rust Unicode whitespace and scalar length rules");
results.clear();
std::uint64_t assigned = 0;
auto set_root = [&](std::uint64_t id) {
assigned = id;
};
GeneratedFieldChecks::context_root(*user_context,
CheckObjectStatus::Create,
"Note",
"tenant",
"Tenant",
0,
set_root,
location,
results);
check(results.size() == 1 && results.front().rule == CheckRule::ContextRootMissing &&
assigned == 0,
"missing trusted Context root rejects before assigning a value");
results.clear();
user_context->with_active_root("Tenant", 42);
auto& state = user_context->make<detail::GraphFixState>(Timestamp(1234));
{
detail::GraphFixActivation active(*user_context, state);
GeneratedFieldChecks::context_root(*user_context,
CheckObjectStatus::Create,
"Note",
"tenant",
"Tenant",
0,
set_root,
location,
results);
Timestamp created(0), updated(0);
GeneratedFieldChecks::clock(*user_context,
CheckObjectStatus::Create,
false,
"Note",
"created",
[&](Timestamp time) {
created = time;
});
GeneratedFieldChecks::clock(
*user_context, CheckObjectStatus::Create, true, "Note", "updated", [&](Timestamp time) {
updated = time;
});
check(assigned == 42 && created == Timestamp(1234) && updated == Timestamp(1234) &&
state.evidence.size() == 3 && results.empty(),
"Context and clock fixes share the operation time and record exact evidence");
GeneratedFieldChecks::context_root(*user_context,
CheckObjectStatus::Update,
"Note",
"tenant",
"Tenant",
7,
set_root,
location,
results);
check(results.back().rule == CheckRule::ContextRootMismatch && state.evidence.size() == 3,
"root mismatches do not manufacture successful fix evidence");
}
}
static void checked_registration_and_retention() {
GlobalContext global;
auto user = global.make_user_context();
auto checker = user->make_global<Plain>("fill");
auto registry = user->make_global<InMemoryCheckerRegistry>();
user->get(registry).with_checker(*user, checker);
user->with_checker_registry(registry);
check(user->has_checker("Item") && !user->has_checker("Missing"),
"checked lookup preserves missing entity");
auto& data = user->make<EntityValues>();
auto result = user->check_and_fix_values("Item", data);
check(bool(result) &&
data.fields.at(String("name", user->memory_resource())).try_text() == "fixed",
"User-only global registration executes the selected checker");
auto& local = user->make<InMemoryCheckerRegistry>();
{
Plain temporary(std::allocator_arg, Allocator(user->memory_resource()), "none");
rejected<std::invalid_argument>(
[&] {
local.register_checker(*user, temporary);
},
"temporary native checker cannot be registered");
}
auto scoped = user->make_ref<Plain>("none");
rejected<std::invalid_argument>(
[&] {
user->get(registry).register_checker(*user, scoped);
},
"Global registry rejects User-owned checker retention");
auto next = global.make_user_context();
rejected<ReferenceError>(
[&] {
next->get(next->rebind_global(registry)).register_checker(*next, checker);
},
"another User must explicitly rebind a Global checker ID");
// Stack carriers finish before their arena owner is destroyed.
result = RuntimeHookResult::ok(user->memory_resource(), {});
user->close();
next->with_checker_registry(next->rebind_global(registry));
auto& next_data = next->make<EntityValues>();
check(bool(next->check_and_fix_values("Item", next_data)),
"Global registration survives old User access expiry");
auto rebound = next->rebind_global(checker);
next->destroy(rebound);
rejected<ReferenceError>(
[&] {
(void)next->has_checker("Item");
},
"lookup rejects early-destroyed checker");
next->destroy(next->rebind_global(registry));
rejected<ReferenceError>(
[&] {
(void)next->has_checker("Missing");
},
"registry is checked before virtual dispatch");
}
static void typed_copies_and_sparse_values() {
GlobalContext global;
auto user = global.make_user_context();
auto fix = user->make_global<Fix>("fill");
auto adapter = user->make_ref<TypedEntityChecker<Item, Fix>>(*user, fix);
auto retained = user->make_global<TypedEntityChecker<Item, Fix>>(user->get(adapter));
auto module = user->make_global<RuntimeModule>();
user->get(module)
.descriptor(Item::entity_descriptor(user->get(module).memory_resource()))
.checker(*user, retained);
user->get(module).apply_to(*user);
auto& values = user->make<EntityValues>();
set(values, "id", Value(user->memory_resource(), std::uint64_t{7}));
set(values, "score", Value(user->memory_resource(), std::int64_t{3}));
set(values,
"unmapped",
Value(user->memory_resource(),
"Long original value retained outside the typed model projection"));
check(bool(user->check_and_fix_values("Item", values)) && values.fields.size() == 4,
"fix merges changed name but does not add omitted version or remove unmapped data");
auto copy = user->make_global<EntityValues>(values);
Ref<RuntimeError> error;
{
auto& missing = user->make<EntityValues>();
auto outcome = user->check_and_fix_values("Item", missing);
check(!outcome && outcome.error().check_results().size() == 2,
"typed required checks and metadata ID check preserve source ordering");
error = user->make_global<RuntimeError>(outcome.error());
}
user.reset();
auto next = global.make_user_context();
next->get(next->rebind_global(module)).apply_to(*next);
auto& kept = next->get(next->rebind_global(copy));
check(kept.fields.size() == 4 &&
kept.fields.find("unmapped")->second.try_text()->starts_with("Long original"),
"nested mutation values are explicitly copied into Global");
auto& diagnostic = next->get(next->rebind_global(error));
check(diagnostic.check_results().size() == 2 && diagnostic.check_results()[0].message &&
diagnostic.check_results()[0].location.model_path(next->memory_resource()) == "score",
"translated errors survive actual source User destruction");
auto& second = next->make<EntityValues>();
set(second, "id", Value(next->memory_resource(), std::uint64_t{9}));
set(second, "score", Value(next->memory_resource(), std::int64_t{0}));
check(bool(next->check_and_fix_values("Item", second)),
"copied typed adapter executes through copied module");
check(
next->get(next->rebind_global(fix)).calls.size() == 3,
"registry/module copies retain the selected checker state without retaining User storage");
next->destroy(next->rebind_global(fix));
rejected<ReferenceError>(
[&] {
(void)next->check_and_fix_values("Item", second);
},
"typed callback is checked before materialization takes mutation values");
check(second.fields.size() == 3, "dead typed callback leaves caller mutation boundary intact");
}
struct Throwing final : Checker {
std::string_view entity() const override {
return "Item";
}
void check_and_fix(const UserContext&,
EntityValues&,
const ObjectLocation& path,
CheckResults& results) const override {
results.emplace_back(
CheckResult::required(results.get_allocator().resource(),
detail::clone_in(results.get_allocator().resource(), path)));
throw std::runtime_error("callback failure");
}
};
struct Closing final : Checker {
UserContext& user;
explicit Closing(UserContext& user) : user(user) {}
std::string_view entity() const override {
return "Item";
}
void check_and_fix(const UserContext&,
EntityValues&,
const ObjectLocation&,
CheckResults&) const override {
user.close();
}
};
struct ClosingName final : Checker {
UserContext& user;
bool close;
explicit ClosingName(UserContext& user, bool close = true) : user(user), close(close) {}
std::string_view entity() const override {
if (close) {
user.close();
}
throw std::runtime_error("registration returned after closing its destination");
}
void check_and_fix(const UserContext&,
EntityValues&,
const ObjectLocation&,
CheckResults&) const override {}
};
struct ClosingTyped final : TypedChecker<Item> {
bool close;
explicit ClosingTyped(bool close = true) : close(close) {}
void check_and_fix_typed(const UserContext& context,
Item&,
CheckObjectStatus,
const ObjectLocation&,
CheckResults&) const override {
if (close) {
const_cast<UserContext&>(context).close();
}
throw std::runtime_error("typed callback returned after closing its context");
}
};
static void direct_callback_exception_cleanup() {
GlobalContext global;
for (bool typed : {false, true}) {
auto user = global.make_user_context();
if (typed) {
auto& checker = global.make_resource<ClosingTyped>(false);
auto& adapter = global.make<TypedEntityChecker<Item, ClosingTyped>>(global, checker);
auto& values = global.make<EntityValues>();
auto& location = global.make<ObjectLocation>();
auto& results = global.make<CheckResults>();
rejected<std::runtime_error>(
[&] {
adapter.check_and_fix(*user, values, location, results);
},
"direct typed callback propagates its original exception");
} else {
auto& checker = global.make_resource<ClosingName>(*user, false);
auto& registry = user->make<InMemoryCheckerRegistry>();
rejected<std::runtime_error>(
[&] {
registry.register_checker(global, checker);
},
"checker name callback propagates its original exception");
check(!registry.checker(user->memory_resource(), "Item"),
"failed registration leaves the checker map unchanged");
}
user.reset();
check(global.live_user_bytes() == 0,
"direct callback exceptions release guards before actual User reclamation");
}
}
static void cleanup_and_metadata_expiry() {
GlobalContext global;
auto user = global.make_user_context();
auto registry = user->make_ref<InMemoryCheckerRegistry>();
auto callback = user->make_resource_ref<Throwing>();
user->get(registry).register_checker(*user, callback);
user->set_checker_registry(registry);
auto& values = user->make<EntityValues>();
rejected<std::runtime_error>(
[&] {
(void)user->check_and_fix_values("Item", values);
},
"callback exception unwinds synchronous registration");
auto empty = user->make_ref<InMemoryCheckerRegistry>();
user->set_checker_registry(empty);
auto metadata = user->make_ref<InMemoryMetadataStore>();
user->get(metadata).register_entity(Item::entity_descriptor(user->memory_resource()));
user->set_metadata(*user, user->get(metadata));
user->destroy(metadata);
rejected<ReferenceError>(
[&] {
(void)user->check_and_fix_values("Item", values);
},
"required-property checks reject early-destroyed metadata");
user->close();
check(user->allocation_stats().live_bytes == 0,
"exception paths release synchronous close guard and all arena blocks");
rejected<ReferenceError>(
[&] {
(void)user->has_checker("Item");
},
"closed Context rejects checker lookup");
}
int main(int argc, char** argv) {
if (argc == 2 && (std::string_view(argv[1]) == "registration-close" ||
std::string_view(argv[1]) == "typed-direct-close")) {
std::set_terminate([] {
std::_Exit(0);
});
try {
GlobalContext global;
auto user = global.make_user_context();
if (std::string_view(argv[1]) == "registration-close") {
auto& checker = global.make_resource<ClosingName>(*user);
auto& registry = user->make<InMemoryCheckerRegistry>();
registry.register_checker(global, checker);
} else {
auto& checker = global.make_resource<ClosingTyped>();
auto& adapter =
global.make<TypedEntityChecker<Item, ClosingTyped>>(global, checker);
auto& values = global.make<EntityValues>();
auto& location = global.make<ObjectLocation>();
auto& results = global.make<CheckResults>();
adapter.check_and_fix(*user, values, location, results);
}
} catch (...) {
std::_Exit(2);
}
return 1;
}
if (argc == 2 && std::string_view(argv[1]) == "reentrant-close") {
std::set_terminate([] {
std::_Exit(0);
});
try {
GlobalContext global;
auto user = global.make_user_context();
auto registry = user->make_ref<InMemoryCheckerRegistry>();
auto callback = user->make_resource_ref<Closing>(*user);
user->get(registry).register_checker(*user, callback);
user->set_checker_registry(registry);
auto& values = user->make<EntityValues>();
(void)user->check_and_fix_values("Item", values);
} catch (...) {
std::_Exit(2);
}
return 1;
}
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
try {
generated_rule_mechanics();
checked_registration_and_retention();
typed_copies_and_sparse_values();
cleanup_and_metadata_expiry();
direct_callback_exception_cleanup();
std::pmr::set_default_resource(previous);
std::cout << "Checker/fix Context ownership contracts passed\n";
} catch (const std::exception& error) {
std::pmr::set_default_resource(previous);
std::cerr << error.what() << '\n';
return 1;
}
}