{
  "description": "change-streams",
  "schemaVersion": "1.0",
  "runOnRequirements": [
    {
      "topologies": [
        "replicaset",
        "sharded-replicaset"
      ]
    }
  ],
  "createEntities": [
    {
      "client": {
        "id": "client0",
        "observeEvents": [
          "commandStartedEvent"
        ]
      }
    },
    {
      "database": {
        "id": "database0",
        "client": "client0",
        "databaseName": "database0"
      }
    },
    {
      "collection": {
        "id": "collection0",
        "database": "database0",
        "collectionName": "collection0"
      }
    }
  ],
  "initialData": [
    {
      "collectionName": "collection0",
      "databaseName": "database0",
      "documents": []
    }
  ],
  "tests": [
    {
      "description": "Test array truncation",
      "runOnRequirements": [
        {
          "minServerVersion": "4.7"
        }
      ],
      "operations": [
        {
          "name": "insertOne",
          "object": "collection0",
          "arguments": {
            "document": {
              "_id": 1,
              "a": 1,
              "array": [
                "foo",
                {
                  "a": "bar"
                },
                1,
                2,
                3
              ]
            }
          }
        },
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": []
          },
          "saveResultAsEntity": "changeStream0"
        },
        {
          "name": "updateOne",
          "object": "collection0",
          "arguments": {
            "filter": {
              "_id": 1
            },
            "update": [
              {
                "$set": {
                  "array": [
                    "foo",
                    {
                      "a": "bar"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "changeStream0",
          "expectResult": {
            "operationType": "update",
            "ns": {
              "db": "database0",
              "coll": "collection0"
            },
            "updateDescription": {
              "updatedFields": {},
              "removedFields": [],
              "truncatedArrays": [
                {
                  "field": "array",
                  "newSize": 2
                }
              ]
            }
          }
        }
      ]
    },
    {
      "description": "Test with document comment",
      "runOnRequirements": [
        {
          "minServerVersion": "4.4"
        }
      ],
      "operations": [
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": [],
            "comment": {
              "name": "test1"
            }
          },
          "saveResultAsEntity": "changeStream0"
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "aggregate": "collection0",
                  "pipeline": [
                    {
                      "$changeStream": {}
                    }
                  ],
                  "comment": {
                    "name": "test1"
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Test with document comment - pre 4.4",
      "runOnRequirements": [
        {
          "minServerVersion": "3.6.0",
          "maxServerVersion": "4.2.99"
        }
      ],
      "operations": [
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": [],
            "comment": {
              "name": "test1"
            }
          },
          "expectError": {
            "isClientError": false
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "aggregate": "collection0",
                  "pipeline": [
                    {
                      "$changeStream": {}
                    }
                  ],
                  "comment": {
                    "name": "test1"
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Test with string comment",
      "runOnRequirements": [
        {
          "minServerVersion": "3.6.0"
        }
      ],
      "operations": [
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": [],
            "comment": "comment"
          },
          "saveResultAsEntity": "changeStream0"
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "aggregate": "collection0",
                  "pipeline": [
                    {
                      "$changeStream": {}
                    }
                  ],
                  "comment": "comment"
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Test that comment is set on getMore",
      "runOnRequirements": [
        {
          "minServerVersion": "4.4.0",
          "topologies": [
            "replicaset"
          ]
        }
      ],
      "operations": [
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": [],
            "comment": {
              "key": "value"
            }
          },
          "saveResultAsEntity": "changeStream0"
        },
        {
          "name": "insertOne",
          "object": "collection0",
          "arguments": {
            "document": {
              "_id": 1,
              "a": 1
            }
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "changeStream0"
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "aggregate": "collection0",
                  "pipeline": [
                    {
                      "$changeStream": {}
                    }
                  ],
                  "comment": {
                    "key": "value"
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "insert": "collection0",
                  "documents": [
                    {
                      "_id": 1,
                      "a": 1
                    }
                  ]
                }
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection0",
                  "comment": {
                    "key": "value"
                  }
                },
                "commandName": "getMore",
                "databaseName": "database0"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Test that comment is not set on getMore - pre 4.4",
      "runOnRequirements": [
        {
          "minServerVersion": "3.6.0",
          "maxServerVersion": "4.3.99",
          "topologies": [
            "replicaset"
          ]
        }
      ],
      "operations": [
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": [],
            "comment": "comment"
          },
          "saveResultAsEntity": "changeStream0"
        },
        {
          "name": "insertOne",
          "object": "collection0",
          "arguments": {
            "document": {
              "_id": 1,
              "a": 1
            }
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "changeStream0"
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "aggregate": "collection0",
                  "pipeline": [
                    {
                      "$changeStream": {}
                    }
                  ],
                  "comment": "comment"
                }
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "insert": "collection0",
                  "documents": [
                    {
                      "_id": 1,
                      "a": 1
                    }
                  ]
                }
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection0",
                  "comment": {
                    "$$exists": false
                  }
                },
                "commandName": "getMore",
                "databaseName": "database0"
              }
            }
          ]
        }
      ]
    }
  ]
}
