| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "db_test_util.hpp"
- #include "errors.hpp"
- #include "collection_registry.hpp"
- using namespace svapi;
- TEST(CollectionMeta, JsonRoundTrip) {
- CollectionMeta m{"docs", "vector", 1536, "text-embedding-3-small", 123};
- CollectionMeta r = CollectionMeta::fromJson(m.toJson());
- EXPECT_EQ(r.name, "docs"); EXPECT_EQ(r.kind, "vector");
- EXPECT_EQ(r.vectorDimension, 1536u); EXPECT_EQ(r.embeddingModel, "text-embedding-3-small");
- }
- TEST(CollectionRegistry, IsolatedPerProject) {
- SVAPI_REQUIRE_DB(db);
- const std::string pa = testutil::tmpName("rega"), pb = testutil::tmpName("regb");
- db->ensureProject(pa); db->ensureProject(pb);
- CollectionRegistry reg(*db);
- reg.ensure(pa); reg.ensure(pb);
- EXPECT_TRUE(reg.add(pa, {"users", "json", 0, "", 0}));
- EXPECT_TRUE(reg.get(pa, "users").has_value());
- EXPECT_FALSE(reg.get(pb, "users").has_value()); // isolation across projects
- EXPECT_EQ(reg.list(pa).size(), 1u);
- EXPECT_EQ(reg.list(pb).size(), 0u);
- EXPECT_TRUE(reg.remove(pa, "users"));
- EXPECT_FALSE(reg.get(pa, "users").has_value());
- db->dropProject(pa); db->dropProject(pb);
- }
- // Regression guard for issue #1: a freshly-constructed registry (as on a new
- // server process) must surface previously-stored entries for every project once
- // bootstrap() has re-opened them. The startup path warmed only the default
- // project before the fix, so named projects' collections went missing on restart.
- TEST(CollectionRegistry, BootstrapWarmsAllProjects) {
- SVAPI_REQUIRE_DB(db);
- const std::string pa = testutil::tmpName("bootA"), pb = testutil::tmpName("bootB");
- db->ensureProject(pa); db->ensureProject(pb);
- {
- CollectionRegistry seed(*db);
- seed.ensure(pa); seed.ensure(pb);
- EXPECT_TRUE(seed.add(pa, {"alpha", "json", 0, "", 0}));
- EXPECT_TRUE(seed.add(pb, {"beta", "vector", 8, "m", 0}));
- }
- CollectionRegistry fresh(*db);
- fresh.bootstrap({pa, pb}); // the startup warm-up that fixes issue #1
- EXPECT_EQ(fresh.list(pa).size(), 1u);
- EXPECT_EQ(fresh.list(pb).size(), 1u);
- EXPECT_TRUE(fresh.get(pa, "alpha").has_value());
- EXPECT_TRUE(fresh.get(pb, "beta").has_value());
- db->dropProject(pa); db->dropProject(pb);
- }
- // A transient database failure must not be reported as "no such collection".
- //
- // Client::get returns nullopt for BOTH "no such document" and a transport
- // failure (it only throws for PERMISSION_DENIED), so the registry cannot tell
- // them apart from the return value alone. Left unhandled that reaches a caller
- // as 404, which a careful client cannot retry - adding 404 to a retry set
- // would retry every genuine absence - so a consumer lost a 6-minute ingest of
- // an 8 GB mailbox to a blip on a collection that existed before and after.
- //
- // Driven against an UNREACHABLE gateway, which is the sustained form of the
- // same ambiguity: every get() returns nullopt because nothing answers.
- TEST(RegistryTransient, AnUnreachableDatabaseIs503NotAMissingCollection) {
- // Port 1 on loopback: nothing listens, so every call fails.
- svapi::DbGateway dead("127.0.0.1:1", 100);
- // connect() FIRST, and its failure is expected and ignored. The client
- // crashes rather than throwing on an RPC issued before connect() has built
- // its stub, so a test that skipped this would segfault and say nothing
- // about the registry. Production always connects (main() exits 1 if the
- // database is unreachable at startup), so connecting here is also the
- // faithful shape - what is being tested is a database that goes away
- // AFTER a successful start.
- (void)dead.connect();
- svapi::CollectionRegistry registry(dead);
- try {
- (void)registry.get("someproject", "somecollection");
- FAIL() << "an unreachable database reported a definite answer about existence";
- } catch (const svapi::ApiError& e) {
- EXPECT_EQ(svapi::httpStatus(e.code), 503)
- << "must be retryable, not a 404 the caller can never retry";
- EXPECT_EQ(e.slug, "db_error");
- }
- }
- // The other half: against a REACHABLE database, a genuinely absent collection
- // must still be reported as absent rather than as an outage. The fix must not
- // turn every 404 into a 503.
- TEST(RegistryTransient, AGenuinelyAbsentCollectionIsStillAbsent) {
- auto db = testutil::connectOrNull();
- if (!db) GTEST_SKIP() << "smartbotic-database not reachable";
- svapi::CollectionRegistry registry(*db);
- const std::string project = testutil::tmpName("transient");
- db->ensureProject(project);
- registry.ensure(project);
- EXPECT_FALSE(registry.get(project, "definitely-not-there").has_value());
- db->dropProject(project);
- }
|