Skip to content
Closed
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
6 changes: 0 additions & 6 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1439,9 +1439,6 @@
}
},
"packages/phishing-controller/src/PhishingController.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
},
"jest/unbound-method": {
"count": 7
}
Expand Down Expand Up @@ -1483,9 +1480,6 @@
"packages/phishing-controller/src/utils.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 2
},
"import-x/namespace": {
"count": 5
}
},
"packages/phishing-controller/src/utils.ts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"nock": "^13.3.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.5",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ import type {
MessengerEvents,
} from '@metamask/messenger';
import nock, { cleanAll, disableNetConnect, enableNetConnect } from 'nock';
import { useFakeTimers } from 'sinon';
import type { SinonFakeTimers } from 'sinon';

import type { AnalyticsDataRegulationServiceMessenger } from './AnalyticsDataRegulationService';
import { AnalyticsDataRegulationService } from './AnalyticsDataRegulationService';
import { DATA_DELETE_RESPONSE_STATUSES, DATA_DELETE_STATUSES } from './types';

describe('AnalyticsDataRegulationService', () => {
let clock: SinonFakeTimers;
const segmentSourceId = 'test-source-id';
const segmentRegulationsEndpoint = 'https://proxy.example.com/v1beta';

beforeEach(() => {
clock = useFakeTimers();
jest.useFakeTimers('legacy');
cleanAll();
disableNetConnect();
});

afterEach(() => {
clock.restore();
jest.useRealTimers();
cleanAll();
enableNetConnect();
});
Expand Down Expand Up @@ -359,7 +356,7 @@ describe('AnalyticsDataRegulationService', () => {

const onRetryListener = jest.fn();
service.onRetry(() => {
clock.nextAsync().catch(console.error);
jest.runOnlyPendingTimers();
onRetryListener();
});

Expand All @@ -383,7 +380,7 @@ describe('AnalyticsDataRegulationService', () => {

const { service, rootMessenger } = getService();
service.onRetry(() => {
clock.nextAsync().catch(console.error);
jest.runOnlyPendingTimers();
});

const onBreakListener = jest.fn();
Expand Down Expand Up @@ -414,21 +411,29 @@ describe('AnalyticsDataRegulationService', () => {
});

describe('onDegraded', () => {
it('calls onDegraded listener when request takes longer than 5 seconds', async () => {
it('calls onDegraded listener when request takes longer than threshold', async () => {
// Use real timers for this test since cockatiel measures actual elapsed time
jest.useRealTimers();

const { service, rootMessenger } = getService({
options: {
policyOptions: {
degradedThreshold: 50, // Set a low threshold (50ms) for faster testing
},
},
});

nock(segmentRegulationsEndpoint)
.post(`/regulations/sources/${segmentSourceId}`)
.reply(200, () => {
clock.tick(6000);
return {
.delay(100) // 100ms delay exceeds the 50ms threshold
.reply(200, {
data: {
data: {
data: {
regulateId: 'test-regulate-id',
},
regulateId: 'test-regulate-id',
},
};
},
});

const { service, rootMessenger } = getService();
const onDegradedListener = jest.fn();
service.onDegraded(onDegradedListener);

Expand All @@ -438,6 +443,9 @@ describe('AnalyticsDataRegulationService', () => {
);

expect(onDegradedListener).toHaveBeenCalled();

// Restore fake timers for subsequent tests
jest.useFakeTimers('legacy');
});

it('calls onDegraded listener when maximum number of retries is exceeded', async () => {
Expand All @@ -448,7 +456,7 @@ describe('AnalyticsDataRegulationService', () => {

const { service, rootMessenger } = getService();
service.onRetry(() => {
clock.nextAsync().catch(console.error);
jest.runOnlyPendingTimers();
});
const onDegradedListener = jest.fn();
service.onDegraded(onDegradedListener);
Expand Down
1 change: 0 additions & 1 deletion packages/app-metadata-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"@types/jest": "^27.5.2",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.5",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"@types/jest": "^27.5.2",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.5",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/core-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@types/jest": "^27.5.2",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.5",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/phishing-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"nock": "^13.3.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.5",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/phishing-controller/src/BulkTokenScan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
MockAnyNamespace,
} from '@metamask/messenger';
import nock, { cleanAll } from 'nock';
import sinon from 'sinon';

import {
PhishingController,
Expand Down Expand Up @@ -118,7 +117,7 @@ describe('PhishingController - Bulk Token Scanning', () => {
});

afterEach(() => {
sinon.restore();
jest.restoreAllMocks();
cleanAll();
consoleErrorSpy.mockRestore();
consoleWarnSpy.mockRestore();
Expand Down
30 changes: 14 additions & 16 deletions packages/phishing-controller/src/CacheManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import sinon from 'sinon';

import { CacheManager } from './CacheManager';
import * as utils from './utils';

describe('CacheManager', () => {
let clock: sinon.SinonFakeTimers;
let updateStateSpy: sinon.SinonSpy;
let updateStateSpy: jest.Mock;
let cache: CacheManager<{ value: string }>;

beforeEach(() => {
clock = sinon.useFakeTimers();
sinon
.stub(utils, 'fetchTimeNow')
.callsFake(() => Math.floor(Date.now() / 1000));
updateStateSpy = sinon.spy();
jest.useFakeTimers();
jest
.spyOn(utils, 'fetchTimeNow')
.mockImplementation(() => Math.floor(Date.now() / 1000));
updateStateSpy = jest.fn();
cache = new CacheManager<{ value: string }>({
cacheTTL: 300, // 5 minutes
maxCacheSize: 3,
Expand All @@ -22,7 +19,8 @@ describe('CacheManager', () => {
});

afterEach(() => {
sinon.restore();
jest.useRealTimers();
jest.restoreAllMocks();
});

describe('constructor', () => {
Expand Down Expand Up @@ -69,7 +67,7 @@ describe('CacheManager', () => {
cache.set('key1', { value: 'value1' });

// Fast forward time past TTL
clock.tick(301 * 1000);
jest.advanceTimersByTime(301 * 1000);

expect(cache.get('key1')).toBeUndefined();
});
Expand All @@ -89,7 +87,7 @@ describe('CacheManager', () => {

it('should call updateState when adding entries', () => {
cache.set('key1', { value: 'value1' });
expect(updateStateSpy.calledOnce).toBe(true);
expect(updateStateSpy).toHaveBeenCalledTimes(1);
});

it('should evict oldest entries when cache exceeds max size', () => {
Expand Down Expand Up @@ -118,9 +116,9 @@ describe('CacheManager', () => {

it('should call updateState when deleting entries', () => {
cache.set('key1', { value: 'value1' });
updateStateSpy.resetHistory();
updateStateSpy.mockClear();
cache.delete('key1');
expect(updateStateSpy.calledOnce).toBe(true);
expect(updateStateSpy).toHaveBeenCalledTimes(1);
});
});

Expand All @@ -135,9 +133,9 @@ describe('CacheManager', () => {

it('should call updateState', () => {
cache.set('key1', { value: 'value1' });
updateStateSpy.resetHistory();
updateStateSpy.mockClear();
cache.clear();
expect(updateStateSpy.calledOnce).toBe(true);
expect(updateStateSpy).toHaveBeenCalledTimes(1);
});
});

Expand Down
Loading