import { Mongo } from 'meteor/mongo';
import sinon from 'sinon';
import jsc from 'jsverify';

export function union (array1, array2) {
  const result = new Set(array1);
  for (const item of array2) {
    result.add(item);
  }
  return Array.from(result);
}

export function property (arbitrary, verify) {
  let error = null;
  const test = jsc.forall(arbitrary, function (...args) {
    try {
      return verify(...args);
    } catch (err) {
      error = err;
    }
    return false;
  });
  return function () {
    try {
      jsc.assert(test);
    } catch (err) {
      if (error) {
        error.message = err.message + '; Original message: ' + error.message;
        throw error;
      }
      throw err;
    }
  };
}

export class Sandbox {
  constructor () {
    this.sandbox = sinon.sandbox.create();
  }
  collection (collection, documents=[]) {
    const testCollection = new Mongo.Collection(null, {
      transform: collection._transform
    });
    [
      'find',
      'findOne',
      'insert',
      'update',
      'remove',
    ].forEach(name => {
      this.sandbox.stub(collection, name, testCollection[name].bind(testCollection));
    });
    for (const doc of documents) {
      testCollection.insert(doc);
    }
  }
  restore () {
    this.sandbox.restore();
  }
}

@@ -91,172 +91,8 @@
 ';%0A%0A
-export function union (array1, array2) %7B%0A  const result = new Set(array1);%0A  for (const item of array2) %7B%0A    result.add(item);%0A  %7D%0A  return Array.from(result);%0A%7D%0A%0A
 expo
@@ -842,16 +842,32 @@
 nsert',%0A
+      'upsert',%0A
       'u

Create stub for "upsert" as well