summaryrefslogtreecommitdiffstats
path: root/toolkit/components/nimbus/test/unit/head.js
blob: e2e42ee4c9733c56599cad8db3380bcf22d9288a (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
"use strict";
// Globals

const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);
const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
ChromeUtils.defineESModuleGetters(this, {
  ExperimentFakes: "resource://testing-common/NimbusTestUtils.sys.mjs",
  ExperimentTestUtils: "resource://testing-common/NimbusTestUtils.sys.mjs",
  ObjectUtils: "resource://gre/modules/ObjectUtils.sys.mjs",
});

// Sinon does not support Set or Map in spy.calledWith()
function onFinalizeCalled(spyOrCallArgs, ...expectedArgs) {
  function mapToObject(map) {
    return Object.assign(
      {},
      ...Array.from(map.entries()).map(([k, v]) => ({ [k]: v }))
    );
  }

  function toPlainObjects(args) {
    return [
      args[0],
      {
        ...args[1],
        invalidBranches: mapToObject(args[1].invalidBranches),
        invalidFeatures: mapToObject(args[1].invalidFeatures),
        missingLocale: Array.from(args[1].missingLocale),
        missingL10nIds: mapToObject(args[1].missingL10nIds),
      },
    ];
  }

  const plainExpected = toPlainObjects(expectedArgs);

  if (Array.isArray(spyOrCallArgs)) {
    return ObjectUtils.deepEqual(toPlainObjects(spyOrCallArgs), plainExpected);
  }

  for (const args of spyOrCallArgs.args) {
    if (ObjectUtils.deepEqual(toPlainObjects(args), plainExpected)) {
      return true;
    }
  }

  return false;
}

/**
 * Assert the store has no active experiments or rollouts.
 */
async function assertEmptyStore(store, { cleanup = false } = {}) {
  Assert.deepEqual(
    store
      .getAll()
      .filter(e => e.active)
      .map(e => e.slug),
    [],
    "Store should have no active enrollments"
  );

  store
    .getAll()
    .filter(e => !e.active)
    .forEach(e => store._deleteForTests(e.slug));

  Assert.deepEqual(
    store
      .getAll()
      .filter(e => !e.active)
      .map(e => e.slug),
    [],
    "Store should have no inactive enrollments"
  );

  if (cleanup) {
    // We need to call finalize first to ensure that any pending saves from
    // JSONFile.saveSoon overwrite files on disk.
    store._store.saveSoon();
    await store._store.finalize();
    await IOUtils.remove(store._store.path);
  }
}