tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_reach_experiments.js (2637B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { ObjectUtils } = ChromeUtils.importESModule(
      5  "resource://gre/modules/ObjectUtils.sys.mjs"
      6 );
      7 
      8 const MESSAGES = [
      9  {
     10    trigger: { id: "defaultBrowserCheck" },
     11    targeting:
     12      "source == 'startup' && !isMajorUpgrade && !activeNotifications && totalBookmarksCount == 5",
     13  },
     14  {
     15    groups: ["eco"],
     16    trigger: {
     17      id: "defaultBrowserCheck",
     18    },
     19    targeting:
     20      "source == 'startup' && !isMajorUpgrade && !activeNotifications && totalBookmarksCount == 5",
     21  },
     22 ];
     23 
     24 let EXPERIMENT_VALIDATOR;
     25 
     26 add_setup(async function setup() {
     27  EXPERIMENT_VALIDATOR = await schemaValidatorFor(
     28    "chrome://browser/content/asrouter/schemas/MessagingExperiment.schema.json"
     29  );
     30 });
     31 
     32 add_task(function test_reach_experiments_validation() {
     33  for (const [index, message] of MESSAGES.entries()) {
     34    assertValidates(
     35      EXPERIMENT_VALIDATOR,
     36      message,
     37      `Message ${index} validates as a MessagingExperiment`
     38    );
     39  }
     40 });
     41 
     42 function depError(has, missing) {
     43  return {
     44    instanceLocation: "#",
     45    keyword: "dependentRequired",
     46    keywordLocation: "#/oneOf/1/allOf/0/$ref/dependantRequired",
     47    error: `Instance has "${has}" but does not have "${missing}".`,
     48  };
     49 }
     50 
     51 function assertContains(haystack, needle) {
     52  Assert.notStrictEqual(
     53    haystack.find(item => ObjectUtils.deepEqual(item, needle)),
     54    null
     55  );
     56 }
     57 
     58 add_task(function test_reach_experiment_dependentRequired() {
     59  info(
     60    "Testing that if id is present then content and template are not required"
     61  );
     62 
     63  {
     64    const message = {
     65      ...MESSAGES[0],
     66      id: "message-id",
     67    };
     68 
     69    const result = EXPERIMENT_VALIDATOR.validate(message);
     70    Assert.ok(result.valid, "message should validate");
     71  }
     72 
     73  info("Testing that if content is present then id and template are required");
     74  {
     75    const message = {
     76      ...MESSAGES[0],
     77      content: {},
     78    };
     79 
     80    const result = EXPERIMENT_VALIDATOR.validate(message);
     81    Assert.ok(!result.valid, "message should not validate");
     82    assertContains(result.errors, depError("content", "id"));
     83    assertContains(result.errors, depError("content", "template"));
     84  }
     85 
     86  info("Testing that if template is present then id and content are required");
     87  {
     88    const message = {
     89      ...MESSAGES[0],
     90      template: "cfr",
     91    };
     92 
     93    const result = EXPERIMENT_VALIDATOR.validate(message);
     94    Assert.ok(!result.valid, "message should not validate");
     95    assertContains(result.errors, depError("template", "content"));
     96    assertContains(result.errors, depError("template", "id"));
     97  }
     98 });