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

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

    pipeline->optimizePipeline();

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

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

    pipeline->optimizePipeline();

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