1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 07:13:32 +00:00
Files
browser/libs/platform/src/background-sync/background-sync.service.spec.ts
Addison Beck f759e62aeb fix(browser): restore timer based background syncs (#14031)
* docs: fix a typo

* fix(browser): restore timer-based background syncs

The browser extension was not performing scheduled background syncs every 30 minutes as expected. This was due to missing task scheduling code that was accidentally removed during the web push implementation (PR #11346).

This commit:
- Creates a new BackgroundSyncService to manage sync scheduling
- Properly initializes the sync interval in main.background.ts
- Adds a test to ensure the sync initialization code isn't accidentally removed again
- Organizes platform module structure to support the new service

Fixes PM-19396

* review: remove unecassary await keyword
2025-03-28 14:17:18 -04:00

108 lines
3.4 KiB
TypeScript

import { mock, MockProxy } from "jest-mock-extended";
import { TaskSchedulerService, ScheduledTaskNames } from "@bitwarden/common/platform/scheduling";
import { BackgroundSyncService, DEFAULT_SYNC_INTERVAL_MS } from "./background-sync.service";
describe("BackgroundSyncService", () => {
let taskSchedulerService: MockProxy<TaskSchedulerService>;
let backgroundSyncService: BackgroundSyncService;
beforeEach(() => {
taskSchedulerService = mock<TaskSchedulerService>();
backgroundSyncService = new BackgroundSyncService(taskSchedulerService);
});
describe("register", () => {
it("registers a task handler with the correct task name", () => {
// Arrange
const syncCallback = jest.fn().mockResolvedValue(undefined);
// Act
backgroundSyncService.register(syncCallback);
// Assert
expect(taskSchedulerService.registerTaskHandler).toHaveBeenCalledTimes(1);
expect(taskSchedulerService.registerTaskHandler).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
syncCallback,
);
});
});
describe("init", () => {
it("schedules the sync interval task with default interval", () => {
// Act
backgroundSyncService.init();
// Assert
expect(taskSchedulerService.setInterval).toHaveBeenCalledTimes(1);
expect(taskSchedulerService.setInterval).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
DEFAULT_SYNC_INTERVAL_MS,
);
});
it("schedules the sync interval task with custom interval", () => {
// Arrange
const customInterval = 60000; // 1 minute
// Act
backgroundSyncService.init(customInterval);
// Assert
expect(taskSchedulerService.setInterval).toHaveBeenCalledTimes(1);
expect(taskSchedulerService.setInterval).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
customInterval,
);
});
it("correctly handles zero interval by using default", () => {
// Act
backgroundSyncService.init(0);
// Assert
expect(taskSchedulerService.setInterval).toHaveBeenCalledTimes(1);
expect(taskSchedulerService.setInterval).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
DEFAULT_SYNC_INTERVAL_MS,
);
});
it("correctly handles negative interval by using default", () => {
// Act
backgroundSyncService.init(-1000);
// Assert
expect(taskSchedulerService.setInterval).toHaveBeenCalledTimes(1);
expect(taskSchedulerService.setInterval).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
DEFAULT_SYNC_INTERVAL_MS,
);
});
});
describe("full integration", () => {
it("registers and initializes correctly in sequence", () => {
// Arrange
const syncCallback = jest.fn().mockResolvedValue(undefined);
const customInterval = 45000; // 45 seconds
// Act
backgroundSyncService.register(syncCallback);
backgroundSyncService.init(customInterval);
// Assert
expect(taskSchedulerService.registerTaskHandler).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
syncCallback,
);
expect(taskSchedulerService.setInterval).toHaveBeenCalledWith(
ScheduledTaskNames.scheduleNextSyncInterval,
customInterval,
);
});
});
});