TEST_F(OptimizePipeline, PushdownSortAndAddFields) {
    auto unpackSpecObj = fromjson(
        "{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
        "bucketMaxSpanSeconds: 3600}}");
    auto pipeline = Pipeline::parse(makeVector(unpackSpecObj,
                                               fromjson("{$addFields: {newMeta: '$myMeta.a'}}"),
                                               fromjson("{$sort: {myMeta: -1}}")),
                                    getExpCtx());

    pipeline->optimizePipeline();
    // We should push down both $sort and $addFields.
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(3u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {newMeta: '$meta.a'}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$sort: {meta: -1}}"), serialized[1]);
    ASSERT_BSONOBJ_EQ(
        fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
                 "bucketMaxSpanSeconds: 3600, computedMetaProjFields: ['newMeta']}}"),
        serialized[2]);
}
TEST_F(OptimizePipeline, PushdownSortAndAddFields) {
    auto unpackSpecObj = fromjson(
        "{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
        "bucketMaxSpanSeconds: 3600}}");
    auto pipeline = Pipeline::parse(makeVector(unpackSpecObj,
                                               fromjson("{$addFields: {newMeta: '$myMeta.a'}}"),
                                               fromjson("{$sort: {myMeta: -1}}")),
                                    getExpCtx());

    pipeline->optimizePipeline();
     
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(3u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {newMeta: '$meta.a'}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$sort: {meta: -1}}"), serialized[1]);
    ASSERT_BSONOBJ_EQ(
        fromjson("{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
                 "bucketMaxSpanSeconds: 3600, computedMetaProjFields: ['newMeta']}}"),
        serialized[2]);
}