/**
 * Test the change events generated by pipeline-based updates are expected with delta format oplog.
 *
 * @tags: [
 * ]
 */

import {assertDropAndRecreateCollection} from "jstests/libs/collection_drop_recreate.js";
import {ChangeStreamTest} from "jstests/libs/query/change_stream_util.js";

assertDropAndRecreateCollection(db, "t1");

const kLargeStr = '*'.repeat(512);

assert.commandWorked(db.t1.insert({
    _id: 100,
    "a": 1,
    "b": 2,
    "obj": {"a": 1, "b": 2, "str": kLargeStr},
}));

const cst = new ChangeStreamTest(db);
const changeStreamCursor =
    cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});

function testPipelineStyleUpdate(pipeline, expectedChange, operationType) {
    assert.commandWorked(db.t1.update({_id: 100}, pipeline));
    const expected = Object.assign({
        documentKey: {_id: 100},
        ns: {db: "test", coll: "t1"},
        operationType: operationType,
    },
                                   expectedChange);
    cst.assertNextChangesEqual({cursor: changeStreamCursor, expectedChanges: [expected]});
}

jsTestLog("Testing pipeline-based update with $set.");
let updatePipeline = [{$set: {a: 2}}];
let expected = {
    updateDescription: {
        updatedFields: {"a": 2},
        removedFields: [],
        truncatedArrays: [],
    },
};
testPipelineStyleUpdate(updatePipeline, expected, "update");

jsTestLog("Testing pipeline-based update with $unset.");
updatePipeline = [{$unset: ["a"]}];
expected = {
    updateDescription: {
        updatedFields: {},
        removedFields: ["a"],
        truncatedArrays: [],
    },
};
testPipelineStyleUpdate(updatePipeline, expected, "update");

jsTestLog("Testing pipeline-based update with $replaceRoot.");
updatePipeline =
    [{$replaceRoot: {newRoot: {_id: 100, b: 2, "obj": {"a": 2, "b": 2, "str": kLargeStr}}}}];
expected = {
    updateDescription: {
        updatedFields: {"obj.a": 2},
        removedFields: [],
        truncatedArrays: [],
    },
};
testPipelineStyleUpdate(updatePipeline, expected, "update");

cst.cleanUp();
