TEST_F(OptimizePipeline, PushDownAddFieldsDoNotInternalizeProjection) {
    auto unpackSpecObj = fromjson(
        "{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
        "bucketMaxSpanSeconds: 3600}}");
    auto addFieldsSpec = fromjson("{$addFields: {device: '$myMeta.a', z: {$add : ['$x', '$y']}}}");
    auto projectSpecObj = fromjson("{$project: {x: 1, device: 1, z : 1}}");

    auto pipeline =
        Pipeline::parse(makeVector(unpackSpecObj, addFieldsSpec, projectSpecObj), getExpCtx());

    pipeline->optimizePipeline();

    // We should split $addFields and push down the part depending on 'myMeta'. We cannot
    // internalize the $project.
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(4u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {device: '$meta.a'}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: ['_id', 'device', 'x', 'y', "
                               "'z'], timeField: 'time', metaField: 'myMeta', "
                               "bucketMaxSpanSeconds: 3600, computedMetaProjFields: ['device']}}"),
                      serialized[1]);
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {z: {$add : ['$x', '$y']}}}"), serialized[2]);
    const UnorderedFieldsBSONObjComparator kComparator;
    ASSERT_EQ(
        kComparator.compare(fromjson("{$project: {_id: true, x: true, device: true, z: true}}"),
                            serialized[3]),
        0);
}
TEST_F(OptimizePipeline, PushDownAddFieldsDoNotInternalizeProjection) {
    auto unpackSpecObj = fromjson(
        "{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
        "bucketMaxSpanSeconds: 3600}}");
    auto addFieldsSpec = fromjson("{$addFields: {device: '$myMeta.a', z: {$add : ['$x', '$y']}}}");
    auto projectSpecObj = fromjson("{$project: {x: 1, device: 1, z : 1}}");

    auto pipeline =
        Pipeline::parse(makeVector(unpackSpecObj, addFieldsSpec, projectSpecObj), getExpCtx());

    pipeline->optimizePipeline();

     
     
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(4u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {device: '$meta.a'}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: ['_id', 'device', 'x', 'y', "
                               "'z'], timeField: 'time', metaField: 'myMeta', "
                               "bucketMaxSpanSeconds: 3600, computedMetaProjFields: ['device']}}"),
                      serialized[1]);
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {z: {$add : ['$x', '$y']}}}"), serialized[2]);
    const UnorderedFieldsBSONObjComparator kComparator;
    ASSERT_EQ(
        kComparator.compare(fromjson("{$project: {_id: true, x: true, device: true, z: true}}"),
                            serialized[3]),
        0);
}