summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/xpcshell/test_reach_experiments.js
blob: 240bda3594dec821b1d6ca552d3f3717f6b679e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const { ObjectUtils } = ChromeUtils.import(
  "resource://gre/modules/ObjectUtils.jsm"
);

const MESSAGES = [
  {
    trigger: { id: "defaultBrowserCheck" },
    targeting:
      "source == 'startup' && !isMajorUpgrade && !activeNotifications && totalBookmarksCount == 5",
  },
  {
    groups: ["eco"],
    trigger: {
      id: "defaultBrowserCheck",
    },
    targeting:
      "source == 'startup' && !isMajorUpgrade && !activeNotifications && totalBookmarksCount == 5",
  },
];

let EXPERIMENT_VALIDATOR;

add_setup(async function setup() {
  EXPERIMENT_VALIDATOR = await schemaValidatorFor(
    "resource://activity-stream/schemas/MessagingExperiment.schema.json"
  );
});

add_task(function test_reach_experiments_validation() {
  for (const [index, message] of MESSAGES.entries()) {
    assertValidates(
      EXPERIMENT_VALIDATOR,
      message,
      `Message ${index} validates as a MessagingExperiment`
    );
  }
});

function depError(has, missing) {
  return {
    instanceLocation: "#",
    keyword: "dependentRequired",
    keywordLocation: "#/oneOf/1/allOf/0/$ref/dependantRequired",
    error: `Instance has "${has}" but does not have "${missing}".`,
  };
}

function assertContains(haystack, needle) {
  Assert.ok(
    haystack.find(item => ObjectUtils.deepEqual(item, needle)) !== null
  );
}

add_task(function test_reach_experiment_dependentRequired() {
  info(
    "Testing that if id is present then content and template are not required"
  );

  {
    const message = {
      ...MESSAGES[0],
      id: "message-id",
    };

    const result = EXPERIMENT_VALIDATOR.validate(message);
    Assert.ok(result.valid, "message should validate");
  }

  info("Testing that if content is present then id and template are required");
  {
    const message = {
      ...MESSAGES[0],
      content: {},
    };

    const result = EXPERIMENT_VALIDATOR.validate(message);
    Assert.ok(!result.valid, "message should not validate");
    assertContains(result.errors, depError("content", "id"));
    assertContains(result.errors, depError("content", "template"));
  }

  info("Testing that if template is present then id and content are required");
  {
    const message = {
      ...MESSAGES[0],
      template: "cfr",
    };

    const result = EXPERIMENT_VALIDATOR.validate(message);
    Assert.ok(!result.valid, "message should not validate");
    assertContains(result.errors, depError("template", "content"));
    assertContains(result.errors, depError("template", "id"));
  }
});