TEST_F(OptimizePipeline, ExclusionProjectThenMatchPushDown) {
    auto pipeline = Pipeline::parse(
        makeVector(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time',"
                            "metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                   fromjson("{$project: {'myMeta.a': 0}}"),
                   fromjson("{$match: {'myMeta.b': {$lt: 10}}}")),
        getExpCtx());
    ASSERT_EQ(3u, pipeline->getSources().size());

    pipeline->optimizePipeline();

    // We should push down the exclusion project on the metaField then push down the $match.
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(3u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$match: {'meta.b': {$lt: 10}}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$project: {meta: {a: false}, _id: true}}"), serialized[1]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', "
                               "metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                      serialized[2]);
}
TEST_F(OptimizePipeline, ExclusionProjectThenMatchPushDown) {
    auto pipeline = Pipeline::parse(
        makeVector(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time',"
                            "metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                   fromjson("{$project: {'myMeta.a': 0}}"),
                   fromjson("{$match: {'myMeta.b': {$lt: 10}}}")),
        getExpCtx());
    ASSERT_EQ(3u, pipeline->getSources().size());

    pipeline->optimizePipeline();

     
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(3u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$match: {'meta.b': {$lt: 10}}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$project: {meta: {a: false}, _id: true}}"), serialized[1]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', "
                               "metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                      serialized[2]);
}