Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"transcoding",
"transpiled",
"typedoc",
"Unregisters",
"untracked",
"videostateupdate",
"VITE",
Expand Down
2 changes: 2 additions & 0 deletions src/cpu-info.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* A class that provides information about the CPU.
* @deprecated Use `SystemInfo` instead.
*/
export class CpuInfo {
/**
* Gets the number of logical CPU cores.
* @deprecated Use `SystemInfo.getNumLogicalCores()` instead.
*
* @returns The number of logical CPU cores, or undefined if not available.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './browser-info';
export * from './web-capabilities';
export * from './cpu-info';
export * from './system-info';
export * from './web-capabilities';
194 changes: 194 additions & 0 deletions src/system-info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let pressureObserverCallback: any;

/**
* Mock implementation of PressureObserver to simulate CPU pressure states.
*/
class MockPressureObserver {
/**
* Mock implementation of PressureObserver to simulate CPU pressure states.
*
* @param callback - The callback to be called with pressure records.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(callback: (list: any) => void) {
pressureObserverCallback = callback;
}

/**
* Attaches the observer to a source to observe state changes.
*/
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-empty-function
observe() {}
}

Object.defineProperty(window, 'PressureObserver', {
writable: true,
configurable: true,
value: MockPressureObserver,
});

// Import the SystemInfo class after defining the PressureObserver mock
// This ensures that the mock is available when SystemInfo is imported.
// eslint-disable-next-line import/first
import { SystemInfo } from './system-info';

describe('SystemInfo', () => {
describe('isPressureObserverSupported', () => {
it('should return true when PressureObserver is supported', () => {
expect.hasAssertions();

Object.defineProperty(window, 'PressureObserver', {
writable: true,
configurable: true,
value: {},
});

expect(SystemInfo.isPressureObserverSupported()).toBe(true);
});

it('should return false when PressureObserver is not supported', () => {
expect.hasAssertions();

// Ensure that PressureObserver is not defined
delete window.PressureObserver;

expect(SystemInfo.isPressureObserverSupported()).toBe(false);
});
});

describe('getCpuPressure', () => {
describe('when PressureObserver is supported', () => {
beforeEach(() => {
Object.defineProperty(window, 'PressureObserver', {
writable: true,
configurable: true,
value: MockPressureObserver,
});
});

it('should return undefined when information is not available', () => {
expect.hasAssertions();

expect(SystemInfo.getCpuPressure()).toBeUndefined();
});

['nominal', 'fair', 'serious', 'critical'].forEach((state) => {
it(`should return the last CPU pressure state as ${state}`, () => {
expect.hasAssertions();

pressureObserverCallback([{ source: 'cpu', state }]);

expect(SystemInfo.getCpuPressure()).toBe(state);
});
});
});

describe('when PressureObserver is not supported', () => {
beforeEach(() => {
// Ensure that PressureObserver is not defined
delete window.PressureObserver;
});

it('should return undefined', () => {
expect.hasAssertions();

expect(SystemInfo.getCpuPressure()).toBeUndefined();
});
});
});

describe('onCpuPressureChange', () => {
describe('when PressureObserver is supported', () => {
beforeEach(() => {
Object.defineProperty(window, 'PressureObserver', {
writable: true,
configurable: true,
value: MockPressureObserver,
});
});

it('should call the callback when CPU pressure state changes', () => {
expect.hasAssertions();

const callback = jest.fn();
SystemInfo.onCpuPressureChange(callback);

pressureObserverCallback([{ source: 'cpu', state: 'nominal' }]);
expect(callback).toHaveBeenCalledWith('nominal');
});

it('should not call the callback if the CPU pressure state does not change', () => {
expect.hasAssertions();

const callback = jest.fn();
SystemInfo.onCpuPressureChange(callback);

expect(callback).toHaveBeenCalledWith('nominal');

// Call with the same state
pressureObserverCallback([{ source: 'cpu', state: 'nominal' }]);
expect(callback).toHaveBeenCalledTimes(1); // Should not be called again

// Call with a different state
pressureObserverCallback([{ source: 'cpu', state: 'fair' }]);
expect(callback).toHaveBeenCalledWith('fair');
});

it('should not emit if callback was deregistered', () => {
expect.hasAssertions();

const callback = jest.fn();
SystemInfo.onCpuPressureChange(callback);
expect(callback).toHaveBeenCalledTimes(1);

// Call with the same state
pressureObserverCallback([{ source: 'cpu', state: 'nominal' }]);
expect(callback).toHaveBeenCalledWith('nominal');
expect(callback).toHaveBeenCalledTimes(2);

// Deregister the callback
SystemInfo.offCpuPressureChange(callback);

// Call with a different state
pressureObserverCallback([{ source: 'cpu', state: 'fair' }]);
expect(callback).toHaveBeenCalledTimes(2); // Should not be called again
});
});

describe('when PressureObserver is not supported', () => {
beforeEach(() => {
// Ensure that PressureObserver is not defined
delete window.PressureObserver;
});

it('should not attach listener', () => {
expect.hasAssertions();

const callback = jest.fn();

SystemInfo.onCpuPressureChange(callback);

expect(callback).toHaveBeenCalledTimes(0);
});
});
});

describe('getNumLogicalCores', () => {
it('should return the number of logical CPU cores when the information is available', () => {
expect.assertions(1);

jest.spyOn(Navigator.prototype, 'hardwareConcurrency', 'get').mockReturnValue(1);

expect(SystemInfo.getNumLogicalCores()).toBe(1);
});

it('should return undefined when the logical CPU cores information is not available', () => {
expect.assertions(1);

jest.spyOn(Navigator.prototype, 'hardwareConcurrency', 'get').mockImplementation();

expect(SystemInfo.getNumLogicalCores()).toBeUndefined();
});
});
});
Loading