Skip to content

MongoDB slow tests failing - pre-v7.6.0 migration tests #949

Description

@JoshuaRowePhantom

Summary

Seven MongoDB slow tests fail due to three distinct root causes: one production-code bug in MigrateAsync and two test-code bugs (wrong _id type and wrong JSON field name).

Root Cause 1 — Production Bug: ApplyMigrationBatchAsync never synthesizes a Versions entry from pre-v7.6.0 data

File: Phantom.Workspaces.Data.MongoDB/MongoDbEntityDataAccessLayer.cs, ApplyMigrationBatchAsync (~line 650)

Pre-v7.6.0 MongoDB documents have a lowercase "versions": [] field (empty). The C# typed driver serializes MongoDbEntityDocument.Versions as PascalCase "Versions" (no [BsonElement] attribute), so it never reads the legacy lowercase field — document.Versions is always empty for these documents.

All reads (GetAsync, ExportAsync) resolve entity data exclusively via document.Versions.LastOrDefault(). When Versions is empty this returns null → entities are silently invisible after migration.

ApplyMigrationBatchAsync backfills current.name-parent-prefixes and current.participant-ids but never writes a synthetic "Versions" entry from the existing current.data:

// Lines 690–706 — $set only touches current.*, never creates Versions
var update = new BsonDocument
{
    { "$set", new BsonDocument
        {
            { "current.name-parent-prefixes", prefixArray },
            { "current.participant-ids", participantIdsArray },
        }
    },
    { "$unset", new BsonDocument { { "current.names", "" }, { "current.type-names", "" } } },
    // ← NO Versions entry synthesized from current.data
};

Fix: When migrating a document whose "Versions" array is absent or empty, synthesize a bootstrap entry from current.data:

if (!hasTypedVersions && data is not null)
{
    var versionId = /* parse current.modified-version ObjectId, or GenerateNewId() */;
    var timestampUtc = /* parse current.modified-time-utc, or UtcNow */;

    var syntheticVersion = new BsonDocument
    {
        { "VersionId", versionId },
        { "TimestampUtc", timestampUtc },
        { "data", data.DeepClone() },
    };
    update["$set"].AsBsonDocument.Add("Versions", new BsonArray { syntheticVersion });
}
// Also $unset the stale lowercase "versions" key
update["$unset"].AsBsonDocument.Add("versions", "");

Affected tests:

  • InitializeAsync_Succeeds_WhenPreV760EntitiesExistInMongoDB (SlowTests.cs:915)
  • GetAsync_ByEntityName_FindsPreV760Entity (SlowTests.cs:552)
  • GetAsync_ByEntityType_FindsPreV760Entity (SlowTests.cs:578)
  • EntityRepository_CreateAsync_WithPreMigrationDatabase_EntitiesAreReadableAfterStartup (EntityRepositoryMongoDbSlowTests.cs:79)

Root Cause 2 — Test Bug: _id set to random ObjectId instead of string entity-id

File: Phantom.Workspaces.Data.MongoDB.Tests/MongoDbEntityDataAccessLayerSlowTests.cs

Two manually-inserted test documents set _id to new ObjectId():

// Line 186 (GetAsync_DocumentWithUnknownCurrentField_DoesNotThrow)
// Line 379 (MigrateAsync_ThenGetAsync_WithLegacyCurrentFields_Succeeds)
{ "_id", new ObjectId() },   // ← random ObjectId

The DAL queries { _id: entityId.ToString() } (a string). MongoDB type-strict equality never matches an ObjectId _id with a string filter → zero results → Assert.Single fails.

Fix:

{ "_id", entityId.ToString() },   // string, matching the DAL's filter

Root Cause 3 — Test Bug: ParseEntityDataWithType uses legacy "type-names" instead of "entity-types"

File: Phantom.Workspaces.Data.MongoDB.Tests/MongoDbEntityDataAccessLayerSlowTests.cs, ParseEntityDataWithType (~line 444)

// Line 453 — stores "type-names" in current.data
"type-names": ["entity", "{{type}}"],

BuildGetFilterDocument queries current.data.entity-types (field constant EntityTypesField = "current.data.entity-types"). The stored JSON has "type-names" not "entity-types" → filter produces zero results.

Fix:

"entity-types": ["entity", "{{type}}"],

Affected test: GetAsync_ByEntityType_ReturnsMatchingEntities (SlowTests.cs:299)

Affected Files

File Root Cause What to change
Phantom.Workspaces.Data.MongoDB/MongoDbEntityDataAccessLayer.cs #1 ApplyMigrationBatchAsync: synthesize Versions entry from current.data; $unset lowercase "versions"
Phantom.Workspaces.Data.MongoDB.Tests/MongoDbEntityDataAccessLayerSlowTests.cs #2, #3 Fix _id in two documents; fix ParseEntityDataWithType field name

Failing Tests → Root Cause Map

Test File Root Cause
InitializeAsync_Succeeds_WhenPreV760EntitiesExistInMongoDB SlowTests.cs:915 #1
GetAsync_ByEntityName_FindsPreV760Entity SlowTests.cs:552 #1
GetAsync_ByEntityType_FindsPreV760Entity SlowTests.cs:578 #1
EntityRepository_CreateAsync_WithPreMigrationDatabase_EntitiesAreReadableAfterStartup EntityRepositoryMongoDbSlowTests.cs:79 #1
GetAsync_ByEntityType_ReturnsMatchingEntities SlowTests.cs:299 #3
GetAsync_DocumentWithUnknownCurrentField_DoesNotThrow SlowTests.cs:176 #2 (+#1 for Versions)
MigrateAsync_ThenGetAsync_WithLegacyCurrentFields_Succeeds SlowTests.cs:372 #2 (+#1 for Versions)

Expected Tests

The 7 tests listed above are the expected tests. They must all pass after the fixes are applied.

Metadata

Metadata

Labels

bugSomething isn't workingdiagnosedRoot cause identifiedneeds-slow-testsRequires full test suite including slow Git tests at checkinverified-locallyImplementation has been verified locally

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions