summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_service_startOver.js
blob: 92acd6cf05543d45007ff900ac476273121f6080 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const { Service } = ChromeUtils.import("resource://services-sync/service.js");

function BlaEngine() {
  SyncEngine.call(this, "Bla", Service);
}
BlaEngine.prototype = {
  __proto__: SyncEngine.prototype,

  removed: false,
  async removeClientData() {
    this.removed = true;
  },
};

add_task(async function setup() {
  await Service.engineManager.register(BlaEngine);
});

add_task(async function test_resetLocalData() {
  await configureIdentity();
  Service.status.enforceBackoff = true;
  Service.status.backoffInterval = 42;
  Service.status.minimumNextSync = 23;

  // Verify set up.
  Assert.equal(Service.status.checkSetup(), STATUS_OK);

  // Verify state that the observer sees.
  let observerCalled = false;
  Svc.Obs.add("weave:service:start-over", function onStartOver() {
    Svc.Obs.remove("weave:service:start-over", onStartOver);
    observerCalled = true;

    Assert.equal(Service.status.service, CLIENT_NOT_CONFIGURED);
  });

  await Service.startOver();
  Assert.ok(observerCalled);

  // Verify the site was nuked from orbit.
  Assert.equal(Svc.Prefs.get("username"), undefined);

  Assert.equal(Service.status.service, CLIENT_NOT_CONFIGURED);
  Assert.ok(!Service.status.enforceBackoff);
  Assert.equal(Service.status.backoffInterval, 0);
  Assert.equal(Service.status.minimumNextSync, 0);
});

add_task(async function test_removeClientData() {
  let engine = Service.engineManager.get("bla");

  // No cluster URL = no removal.
  Assert.ok(!engine.removed);
  await Service.startOver();
  Assert.ok(!engine.removed);

  Service.clusterURL = "https://localhost/";

  Assert.ok(!engine.removed);
  await Service.startOver();
  Assert.ok(engine.removed);
});

add_task(async function test_reset_SyncScheduler() {
  // Some non-default values for SyncScheduler's attributes.
  Service.scheduler.idle = true;
  Service.scheduler.hasIncomingItems = true;
  Svc.Prefs.set("clients.devices.desktop", 42);
  Service.scheduler.nextSync = Date.now();
  Service.scheduler.syncThreshold = MULTI_DEVICE_THRESHOLD;
  Service.scheduler.syncInterval = Service.scheduler.activeInterval;

  await Service.startOver();

  Assert.ok(!Service.scheduler.idle);
  Assert.ok(!Service.scheduler.hasIncomingItems);
  Assert.equal(Service.scheduler.numClients, 0);
  Assert.equal(Service.scheduler.nextSync, 0);
  Assert.equal(Service.scheduler.syncThreshold, SINGLE_USER_THRESHOLD);
  Assert.equal(
    Service.scheduler.syncInterval,
    Service.scheduler.singleDeviceInterval
  );
});