<commit_before>var selftest = require('../selftest.js');
var testUtils = require('../test-utils.js');
var stats = require('../stats.js');
var Sandbox = selftest.Sandbox;

var testStatsServer = "https://test-package-stats.meteor.com";
process.env.METEOR_PACKAGE_STATS_SERVER_URL = testStatsServer;

// NOTE: This test will fail if your machine's time is skewed by more
// than 30 minutes. This is because the `fetchAppPackageUsage` method
// works by passing an hour time range.
selftest.define("report-stats", ["slow"], function () {
  var s = new Sandbox;

  var run = s.run("create", "foo");
  run.expectExit(0);
  s.cd("foo");

  // verify that identifier file exists for new apps
  var identifier = s.read(".meteor/identifier");
  selftest.expectEqual(!! identifier, true);
  selftest.expectEqual(identifier.length > 0, true);

  // verify that identifier file when running 'meteor bundle' on apps
  // with no identifier file (eg pre-0.9.0 apps)
  bundleWithFreshIdentifier(s);
  identifier = s.read(".meteor/identifier");
  selftest.expectEqual(!! identifier, true);
  selftest.expectEqual(identifier.length > 0, true);

  // we just ran 'meteor bundle' so let's test that we actually sent
  // package usage stats
  var usage = fetchPackageUsageForApp(identifier);
  selftest.expectEqual(usage.packages, stats.packageList(s.cwd));

  // verify that the stats server recorded that with no userId
  var appPackages = stats.getPackagesForAppIdInTest(s.cwd);
  selftest.expectEqual(appPackages.appId, identifier);
  selftest.expectEqual(appPackages.userId, null);
  selftest.expectEqual(appPackages.packages, stats.packageList(s.cwd));

  // now bundle again while logged in. verify that the stats server
  // recorded that with the right userId
  testUtils.login(s, "test", "testtest");
  bundleWithFreshIdentifier(s);
  appPackages = stats.getPackagesForAppIdInTest(s.cwd);
  selftest.expectEqual(appPackages.userId, testUtils.getUserId(s));

  // Add the opt-out package, verify that no stats are recorded for the
  // app.
  run = s.run("add", "package-stats-opt-out");
  run.waitSecs(15);
  run.expectExit(0);
  bundleWithFreshIdentifier(s);
  appPackages = stats.getPackagesForAppIdInTest(s.cwd);
  selftest.expectEqual(appPackages, undefined);

  // Remove the opt-out package, verify that stats get sent again.
  run = s.run("remove", "package-stats-opt-out");
  run.waitSecs(15);
  run.expectExit(0);
  bundle(s);
  appPackages = stats.getPackagesForAppIdInTest(s.cwd);
  selftest.expectEqual(appPackages.userId, testUtils.getUserId(s));
  selftest.expectEqual(appPackages.packages, stats.packageList(s.cwd));
});

// Bundle the app in the current working directory after deleting its
// identifier file (meaning a new one will be created).
// @param s {Sandbox}
var bundleWithFreshIdentifier = function (s) {
  s.unlink(".meteor/identifier");
  bundle(s);
};

// Bundle the app in the current working directory.
// @param s {Sandbox}
var bundle = function (s) {
  var run = s.run("bundle", "foo.tar.gz");
  run.waitSecs(30);
  run.expectExit(0);
};

// Contact the package stats server and look for a given app
// identifier reported in the range (now - 30 minutes, now + 30
// minutes). Fails if packages for the same app was not recorded, or
// was recorded more than once.
//
// Returns the (unique) package usage document for the given app.
var fetchPackageUsageForApp = function (identifier) {
  var stats = testUtils.ddpConnect(testStatsServer);
  var nowMinus30Minutes = new Date(new Date - 1000 * 60 * 30 /*ms*/);<commit_msg>Fix package stats test<commit_after>var _ = require('underscore');

var selftest = require('../selftest.js');
var testUtils = require('../test-utils.js');
var stats = require('../stats.js');
var Sandbox = selftest.Sandbox;
var project = require('../project.js');

var testStatsServer = "https://test-package-stats.meteor.com";
process.env.METEOR_PACKAGE_STATS_SERVER_URL = testStatsServer;

// NOTE: This test will fail if your machine's time is skewed by more
// than 30 minutes. This is because the `fetchAppPackageUsage` method
// works by passing an hour time range.
selftest.define("report-stats", ["slow"], function () {
  var s = new Sandbox;

  var run = s.run("create", "foo");
  run.expectExit(0);
  s.cd("foo");

  project.project.setRootDir(s.cwd);

  // verify that identifier file exists for new apps
  var identifier = s.read(".meteor/identifier");
  selftest.expectEqual(!! identifier, true);
  selftest.expectEqual(identifier.length > 0, true);

  // verify that identifier file when running 'meteor bundle' on apps
  // with no identifier file (eg pre-0.9.0 apps)
  bundleWithFreshIdentifier(s);
  identifier = s.read(".meteor/identifier");
  selftest.expectEqual(!! identifier, true);
  selftest.expectEqual(identifier.length > 0, true);

  // we just ran 'meteor bundle' so let's test that we actually sent
  // package usage stats
  var usage = fetchPackageUsageForApp(identifier);
  selftest.expectEqual(_.sortBy(usage.packages, "name"),
                       _.sortBy(stats.packageList(), "name"));

  // verify that the stats server recorded that with no userId
  var appPackages = stats.getPackagesForAppIdInTest();
  selftest.expectEqual(appPackages.appId, identifier);
  selftest.expectEqual(appPackages.userId, null);
  selftest.expectEqual(_.sortBy(appPackages.packages, "name"),
                       _.sortBy(stats.packageList(), "name"));

  // now bundle again while logged in. verify that the stats server
  // recorded that with the right userId
  testUtils.login(s, "test", "testtest");
  bundleWithFreshIdentifier(s);
  appPackages = stats.getPackagesForAppIdInTest();
  selftest.expectEqual(appPackages.userId, testUtils.getUserId(s));

  // Add the opt-out package, verify that no stats are recorded for the
  // app.
  run = s.run("add", "package-stats-opt-out");
  run.waitSecs(15);
  run.expectExit(0);
  bundleWithFreshIdentifier(s);
  appPackages = stats.getPackagesForAppIdInTest();
  selftest.expectEqual(appPackages, undefined);

  // Remove the opt-out package, verify that stats get sent again.
  run = s.run("remove", "package-stats-opt-out");
  run.waitSecs(15);
  run.expectExit(0);
  bundle(s);
  appPackages = stats.getPackagesForAppIdInTest();
  selftest.expectEqual(appPackages.userId, testUtils.getUserId(s));
  selftest.expectEqual(_.sortBy(appPackages.packages, "name"),
                       _.sortBy(stats.packageList(), "name"));
});

// Bundle the app in the current working directory after deleting its
// identifier file (meaning a new one will be created).
// @param s {Sandbox}
var bundleWithFreshIdentifier = function (s) {
  s.unlink(".meteor/identifier");
  bundle(s);
};

// Bundle the app in the current working directory.
// @param s {Sandbox}
var bundle = function (s) {
  var run = s.run("bundle", "foo.tar.gz");
  run.waitSecs(30);
  run.expectExit(0);
  // pick up new app identifier and/or packages added/removed
  // XXX not sure why this is necessary (i.e. why project can't detect
  // that .meteor/identifier or .meteor/packages has changed and figure
  // out that it needs to reload itself)
  project.project.reload();
};

// Contact the package stats server and look for a given app
// identifier reported in the range (now - 30 minutes, now + 30
// minutes). Fails if packages for the same app was not recorded, or
// was recorded more than once.
//
// Returns the (unique) package usage document for the given app.
var fetchPackageUsageForApp = function (identifier) {
  var stats = testUtils.ddpConnect(testStatsServer);
  var nowMinus30Minutes = new Date(new Date - 1000 * 60 * 30 /*ms*/);
{'fraction_non_alphanumeric': 0.12809754818408287, 'fraction_numerical': 0.008129015340238625, 'mean_word_length': 4.098930481283422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '636952', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2344, 'n_words': 700}