<commit_before>USERS_COLLECTION = 'Users_meteor_middleware_tests'

Users = new Meteor.Collection USERS_COLLECTION
LogActions = new Meteor.Collection 'LogActionsActions_meteor_middleware_tests'

if Meteor.isServer
  # Initialize the database
  Meteor.methods
    'initialize-database': ->
      Users.remove {}
      LogActions.remove {}

  Meteor.call 'initialize-database'

  allUsers = new PublishEndpoint 'all-users', (argument1, argument2) ->
    assert _.isEqual @params(), [argument1, argument2]
    assert.equal argument1, 'first'
    assert.equal argument2, 'second'

    Users.find()

  new PublishEndpoint null, ->
    LogActions.find()

  class TestMiddleware extends PublishMiddleware
    constructor: (@name) ->
      super

    added: (publish, collection, id, fields) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'added'
        args: [collection, id, fields]

      super

    changed: (publish, collection, id, fields) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'changed'
        args: [collection, id, fields]

      super

    removed: (publish, collection, id) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'removed'
        args: [collection, id]

      super

    onReady: (publish) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'onReady'
        args: []

      super

    onStop: (publish) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'onStop'
        args: []

      super

    onError: (publish, error) =>
      # Here we only check if params exists, but not really
      # the content, so that the error can propagate.
      assert publish.params

      LogActions.insert
        name: @name,
        type: 'error'
        args: [error]

      super

  class HasPostsMiddleware extends PublishMiddleware
    added: (publish, collection, id, fields) =>
      fields.hasPosts = !!fields.posts
      super publish, collection, id, fields

    changed: (publish, collection, id, fields) =>
      if 'posts' of fields
        fields.hasPosts = !!fields.posts
      super publish, collection, id, fields

  allUsers.use new TestMiddleware 'first'
  allUsers.use new HasPostsMiddleware()
  allUsers.use new TestMiddleware 'last'

if Meteor.isClient
  testAsyncMulti 'middleware - basic', [
    (test, expect) ->
      Meteor.call 'initialize-database', expect (error) ->
        test.isFalse error
  ,
    (test, expect) ->
      # Let's wait a bit for log to be cleared from the client
      Meteor.setTimeout expect(), 100
  ,
    (test, expect) ->
      test.equal Users.find().count(), 0
      test.equal LogActions.find().count(), 0

      @log = []

      @subscribe = Meteor.subscribe 'all-users', 'first', 'second',
        onReady: expect()
        onError: (error) ->
          test.exception error
  ,
    (test, expect) ->
      # Let's wait a bit for log to be pushed to the client
      Meteor.setTimeout expect(), 100
  ,
    (test, expect) -><commit_msg>Tests simplified.<commit_after>USERS_COLLECTION = 'Users_meteor_middleware_tests'

Users = new Meteor.Collection USERS_COLLECTION
LogActions = new Meteor.Collection 'LogActionsActions_meteor_middleware_tests'

if Meteor.isServer
  # Initialize the database
  Meteor.methods
    'initialize-database': ->
      Users.remove {}
      LogActions.remove {}

  Meteor.call 'initialize-database'

  allUsers = new PublishEndpoint 'all-users', (argument1, argument2) ->
    assert _.isEqual @params(), [argument1, argument2]
    assert.equal argument1, 'first'
    assert.equal argument2, 'second'

    Users.find()

  new PublishEndpoint null, ->
    LogActions.find()

  class TestMiddleware extends PublishMiddleware
    constructor: (@name) ->
      super

    added: (publish, collection, id, fields) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'added'
        args: [collection, id, fields]

      super

    changed: (publish, collection, id, fields) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'changed'
        args: [collection, id, fields]

      super

    removed: (publish, collection, id) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'removed'
        args: [collection, id]

      super

    onReady: (publish) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'onReady'
        args: []

      super

    onStop: (publish) =>
      assert.equal publish.params().length, 2

      LogActions.insert
        name: @name,
        type: 'onStop'
        args: []

      super

    onError: (publish, error) =>
      # Here we only check if params exists, but not really
      # the content, so that the error can propagate.
      assert publish.params

      LogActions.insert
        name: @name,
        type: 'error'
        args: [error]

      super

  class HasPostsMiddleware extends PublishMiddleware
    added: (publish, collection, id, fields) =>
      fields.hasPosts = !!fields.posts

      super

    changed: (publish, collection, id, fields) =>
      fields.hasPosts = !!fields.posts if 'posts' of fields

      super

  allUsers.use new TestMiddleware 'first'
  allUsers.use new HasPostsMiddleware()
  allUsers.use new TestMiddleware 'last'

if Meteor.isClient
  testAsyncMulti 'middleware - basic', [
    (test, expect) ->
      Meteor.call 'initialize-database', expect (error) ->
        test.isFalse error
  ,
    (test, expect) ->
      # Let's wait a bit for log to be cleared from the client
      Meteor.setTimeout expect(), 100
  ,
    (test, expect) ->
      test.equal Users.find().count(), 0
      test.equal LogActions.find().count(), 0

      @log = []

      @subscribe = Meteor.subscribe 'all-users', 'first', 'second',
        onReady: expect()
        onError: (error) ->
          test.exception error
  ,
    (test, expect) ->
      # Let's wait a bit for log to be pushed to the client
      Meteor.setTimeout expect(), 100
  ,
    (test, expect) ->
{'fraction_non_alphanumeric': 0.1106389675794775, 'fraction_numerical': 0.005980484734025811, 'mean_word_length': 2.451928299837045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '3893546', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1823, 'n_words': 531}