<commit_before>/// <reference path="../typings/index.d.ts" />
import { Collection } from "../src";
import {
  IDDPDocument,
  IDDPMessageDocumentAdded,
  IDDPMessageDocumentChanged,
} from "../src/collection/models";

import { prepareUniqueDDPObject } from "./utils";

import { expect } from "chai";
import * as EJSON from "ejson";
import "mocha";

describe("Collection", () => {
  describe("collection attribute", () => {
    let collectionName = "testCollection";
    let ddpClient = prepareUniqueDDPObject();
    let testCollection = new Collection(collectionName, ddpClient);

    it("should return empty array if nothing is initialized", (done) => {
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        expect(collection).to.eql([]);
        done();
      });
      subscription.unsubscribe();
    })

    it("should trigger with new value on added message after subscription", (done) => {
      let addedMessage: IDDPMessageDocumentAdded = {
        msg: "added",
        collection: collectionName,
        id: "testId1",
        fields: {
          _id: "testId1",
          testField: "testField1",
        },
      };
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        // Skip initial empty value
        if (collection.length > 0) {
          expect(collection).to.eql([{"_id":"testId1","testField":"testField1"}]);
          done();
        }
      });
      ddpClient.subscription.next(EJSON.stringify(addedMessage));
      subscription.unsubscribe();
    });

    it("should show last value on subscription(Replay last) initialization even if there are no new messages", (done) => {
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        // Skip initial empty value
        if (collection.length > 0) {
          expect(collection).to.eql([{"_id":"testId1","testField":"testField1"}]);
          done();
        }
      });
      subscription.unsubscribe();
    });

    it("should trigger on next added message with aggregated values", (done) => {
      let addedMessage: IDDPMessageDocumentAdded = {
        msg: "added",
        collection: collectionName,
        id: "testId2",
        fields: {
          _id: "testId2",
          testField: "testField2",
          testObject: {
            a: "b",
            c: "d",
          },
        },
      };
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        // Skip initial empty value
        if (collection.length > 1) {
          expect(collection).to.eql([
            {"_id":"testId1","testField":"testField1"},
            {
              _id: "testId2",
              testField: "testField2",
              testObject: {
                a: "b",
                c: "d",
              },
            }
          ]);
          done();
        }
      });
      ddpClient.subscription.next(EJSON.stringify(addedMessage));
      subscription.unsubscribe();
    });

    it("should trigger next with updated collection on changed message", (done) => {
      let updatedMessage: IDDPMessageDocumentChanged = {
        msg: "changed",
        collection: collectionName,
        id: "testId2",
        fields: {
          testField: "testField2Updated",
          testObject: {
            c: "e",
            f: "g",
          },
          newField: "newFieldValue",
          newArrayField: ["a", "b", "c"],
          newObjectField: {
            a: "b",
            c: "d",
          },
        },
      };
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        // Skip initial empty value
        for (let key in collection) {
          let document = collection[key];
          if (document["testField"] === "testField2Updated") {
            expect(collection).to.eql([
              {_id:"testId1", testField:"testField1"},
              {
                _id:"testId2",
                testField:"testField2Updated",
                testObject: {
                  c: "e",
                  f: "g",
                },
                newField: "newFieldValue",
                newArrayField: ["a", "b", "c"],
                newObjectField: {
                  a: "b",
                  c: "d",
                },
              },
            ]);
            done();
          }
        }
      });
      ddpClient.subscription.next(EJSON.stringify(updatedMessage));
      subscription.unsubscribe();
    });

    it("should delete", () => {
      /*
      let addedMessage2: IDDPMessageDocumentAdded = {
        msg: "added",
        collection: collectionName,
        id: "testId2",
        fields: {
          _id: "testId2",
          testField: "testField2",
        },
      };
      ddpClient.subscription.next(EJSON.stringify(addedMessage2));
      */
      testCollection.collection.subscribe((value) => {
        //expect(value).to.eql([{"_id":"testId1","testField":"testField1"},{"_id":"testId2","testField":"testField2"}]);
        //done();
      });
    });
  });
});<commit_msg>collection removal test done!<commit_after>/// <reference path="../typings/index.d.ts" />
import { Collection } from "../src";
import {
  IDDPDocument,
  IDDPMessageDocumentAdded,
  IDDPMessageDocumentChanged,
  IDDPMessageDocumentRemoved,
} from "../src/collection/models";

import { prepareUniqueDDPObject } from "./utils";

import { expect } from "chai";
import * as EJSON from "ejson";
import "mocha";

describe("Collection", () => {
  describe("collection attribute", () => {
    let collectionName = "testCollection";
    let ddpClient = prepareUniqueDDPObject();
    let testCollection = new Collection(collectionName, ddpClient);

    it("should return empty array if nothing is initialized", (done) => {
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        expect(collection).to.eql([]);
        done();
      });
      subscription.unsubscribe();
    })

    it("should trigger with new value on added message after subscription", (done) => {
      let addedMessage: IDDPMessageDocumentAdded = {
        msg: "added",
        collection: collectionName,
        id: "testId1",
        fields: {
          _id: "testId1",
          testField: "testField1",
        },
      };
      let subscription = testCollection.collection.subscribe((collection: Array<IDDPDocument>) => {
        // Skip initial empty value
        if (collection.length > 0) {
          expect(collection).to.eql([{"_id":"testId1","testField":"testField1"}]);
          done();
        }
      });
      ddpClient.subscription.next(EJSON.stringify(addedMessage));
      subscription.unsubscribe();
    });

    it("should show last value on subscription(Replay last) initialization even if there are no new messages", (done) => {
      let subscription = testCollection.collection.subscribe((collection: IDDPDocument[]) => {
        // Skip initial empty value
        if (collection.length > 0) {
          expect(collection).to.eql([{"_id":"testId1","testField":"testField1"}]);
          done();
        }
      });
      subscription.unsubscribe();
    });

    it("should trigger on next added message with aggregated values", (done) => {
      let addedMessage: IDDPMessageDocumentAdded = {
        msg: "added",
        collection: collectionName,
        id: "testId2",
        fields: {
          _id: "testId2",
          testField: "testField2",
          testObject: {
            a: "b",
            c: "d",
          },
        },
      };
      let subscription = testCollection.collection.subscribe((collection: IDDPDocument[]) => {
        // Skip initial empty value
        if (collection.length > 1) {
          expect(collection).to.eql([
            {"_id":"testId1","testField":"testField1"},
            {
              _id: "testId2",
              testField: "testField2",
              testObject: {
                a: "b",
                c: "d",
              },
            }
          ]);
          done();
        }
      });
      ddpClient.subscription.next(EJSON.stringify(addedMessage));
      subscription.unsubscribe();
    });

    it("should trigger next with updated collection on changed message", (done) => {
      let updatedMessage: IDDPMessageDocumentChanged = {
        msg: "changed",
        collection: collectionName,
        id: "testId2",
        fields: {
          testField: "testField2Updated",
          testObject: {
            c: "e",
            f: "g",
          },
          newField: "newFieldValue",
          newArrayField: ["a", "b", "c"],
          newObjectField: {
            a: "b",
            c: "d",
          },
        },
      };
      let subscription = testCollection.collection.subscribe((collection: IDDPDocument[]) => {
        // Skip initial empty value
        for (let key in collection) {
          let document = collection[key];
          if (document["testField"] === "testField2Updated") {
            expect(collection).to.eql([
              {_id:"testId1", testField:"testField1"},
              {
                _id:"testId2",
                testField:"testField2Updated",
                testObject: {
                  c: "e",
                  f: "g",
                },
                newField: "newFieldValue",
                newArrayField: ["a", "b", "c"],
                newObjectField: {
                  a: "b",
                  c: "d",
                },
              },
            ]);
            done();
          }
        }
      });
      ddpClient.subscription.next(EJSON.stringify(updatedMessage));
      subscription.unsubscribe();
    });

    it("should delete on removed message", (done) => {
      let removedMessage: IDDPMessageDocumentRemoved = {
        msg: "removed",
        collection: collectionName,
        id: "testId2",
      };
      let subscription = testCollection.collection.subscribe((collection: IDDPDocument[]) => {
        if (collection.length < 2) {
          expect(collection).to.eql([{_id:"testId1", testField:"testField1"},]);
          done();
        }
      });
      ddpClient.subscription.next(EJSON.stringify(removedMessage));
      subscription.unsubscribe();
    });
  });
});
{'fraction_non_alphanumeric': 0.14119585231127046, 'fraction_numerical': 0.005911425525729238, 'mean_word_length': 1.9443651925820258, 'pattern_counts': {'":': 16, '<': 13, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5865846', 'n_tokens_mistral': 2780, 'n_tokens_neox': 2760, 'n_words': 577}