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

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

    pipeline->optimizePipeline();

    // We should internalize the $project and push down the $addFields.
    auto serialized = pipeline->serializeToBson();
    ASSERT_EQ(2u, serialized.size());
    ASSERT_BSONOBJ_EQ(fromjson("{$addFields: {newMeta: '$meta.a'}}"), serialized[0]);
    ASSERT_BSONOBJ_EQ(fromjson("{$_internalUnpackBucket: { include: ['_id', 'newMeta', 'x', 'y', "
                               "'myMeta'], timeField: 'time', metaField: 'myMeta', "
                               "bucketMaxSpanSeconds: 3600, computedMetaProjFields: ['newMeta']}}"),
                      serialized[1]);
}
TEST_F(OptimizePipeline, InternalizeProjectAndPushdownAddFields) {
    auto unpackSpecObj = fromjson(
        "{$_internalUnpackBucket: { exclude: [], timeField: 'time', metaField: 'myMeta', "
        "bucketMaxSpanSeconds: 3600}}");
    auto projectSpecObj = fromjson("{$project: {x: true, y: true, myMeta: true}}");
    auto addFieldsSpec = fromjson("{$addFields: {newMeta: '$myMeta.a'}}");

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

    pipeline->optimizePipeline();

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