summaryrefslogtreecommitdiffstats
path: root/browser/components/pocket/test/unit/browser_pocket_pktTelemetry.js
blob: 197a152ee5bab4b9d35b1e51422899e6fabbb3b9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  pktTelemetry: "chrome://pocket/content/pktTelemetry.sys.mjs",
});

function test_runner(test) {
  let testTask = async () => {
    // Before each
    const sandbox = sinon.createSandbox();
    try {
      await test({ sandbox });
    } finally {
      // After each
      sandbox.restore();
    }
  };

  // Copy the name of the test function to identify the test
  Object.defineProperty(testTask, "name", { value: test.name });
  add_task(testTask);
}

test_runner(async function test_createPingPayload({ sandbox }) {
  const impressionId = "{7fd5a1ac-6089-4212-91a7-fcdec1d2f533}";
  const creationDate = "18578";
  await SpecialPowers.pushPrefEnv({
    set: [["browser.newtabpage.activity-stream.impressionId", impressionId]],
  });
  sandbox.stub(pktTelemetry, "_profileCreationDate").returns(creationDate);
  const result = pktTelemetry.createPingPayload({ test: "test" });

  Assert.deepEqual(result, {
    test: "test",
    pocket_logged_in_status: false,
    profile_creation_date: creationDate,
    impression_id: impressionId,
  });
});

test_runner(async function test_generateStructuredIngestionEndpoint({
  sandbox,
}) {
  sandbox
    .stub(pktTelemetry, "_generateUUID")
    .returns("{7fd5a1ac-6089-4212-91a7-fcdec1d2f533}");
  const endpoint = pktTelemetry._generateStructuredIngestionEndpoint();
  Assert.equal(
    endpoint,
    "https://incoming.telemetry.mozilla.org/submit/activity-stream/pocket-button/1/7fd5a1ac-6089-4212-91a7-fcdec1d2f533"
  );
});