TEST_F(OptimizePipeline, MetaMatchThenCountPushedDown) {
    auto pipeline = Pipeline::parse(
        makeVector(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: "
                            "'myMeta', bucketMaxSpanSeconds: 3600}}"),
                   fromjson("{$match: {myMeta: {$eq: 'abc'}}}"),
                   fromjson("{$count: 'foo'}")),
        getExpCtx());
    ASSERT_EQ(4u, pipeline->getSources().size());  // $count is expanded into $group and $project.

    pipeline->optimizePipeline();

    // We should push down the $match and internalize the empty dependency set.
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(4u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$match: {meta: {$eq: 'abc'}}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: [], timeField: 'time', "
                               "metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                      serialized[1]);
    ASSERT_BSONOBJ_EQ(fromjson("{$group: {_id: {$const: null}, foo: {$sum: {$const: 1}}}}"),
                      serialized[2]);
    ASSERT_BSONOBJ_EQ(fromjson("{$project: {foo: true, _id: false}}"), serialized[3]);
}
TEST_F(OptimizePipeline, MetaMatchThenCountPushedDown) {
    auto pipeline = Pipeline::parse(
        makeVector(fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: "
                            "'myMeta', bucketMaxSpanSeconds: 3600}}"),
                   fromjson("{$match: {myMeta: {$eq: 'abc'}}}"),
                   fromjson("{$count: 'foo'}")),
        getExpCtx());
    ASSERT_EQ(4u, pipeline->getSources().size());   

    pipeline->optimizePipeline();

     
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(4u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$match: {meta: {$eq: 'abc'}}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: [], timeField: 'time', "
                               "metaField: 'myMeta', bucketMaxSpanSeconds: 3600}}"),
                      serialized[1]);
    ASSERT_BSONOBJ_EQ(fromjson("{$group: {_id: {$const: null}, foo: {$sum: {$const: 1}}}}"),
                      serialized[2]);
    ASSERT_BSONOBJ_EQ(fromjson("{$project: {foo: true, _id: false}}"), serialized[3]);
}