-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate utility functions and business logic
- Loading branch information
Showing
18 changed files
with
635 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { config } from '../src/config'; | ||
import { | ||
blockDistraction, | ||
isWithinTimeRange, | ||
unblockDistraction, | ||
isValidDistraction, | ||
isDistractionBlocked, | ||
} from '../src/block'; | ||
|
||
import('../src/socket'); | ||
|
||
jest.mock('child_process', () => ({ | ||
execSync: jest.fn().mockImplementation(() => false), | ||
})); | ||
|
||
beforeEach(() => { | ||
config.blocklist = []; | ||
config.whitelist = []; | ||
config.shield = false; | ||
}); | ||
|
||
test('Should check a distraction', async () => { | ||
expect(isValidDistraction({ name: '' })).toBe(false); | ||
expect(isValidDistraction({ name: '*' })).toBe(false); | ||
expect(isValidDistraction({ name: '*.*' })).toBe(true); | ||
expect(isValidDistraction({ name: '*.example.com' })).toBe(true); | ||
expect(isValidDistraction({ name: 'example.com' })).toBe(true); | ||
expect(isValidDistraction({ name: 'chromium' })).toBe(true); | ||
expect(isValidDistraction({ name: 'chromium', time: 'badtime' })).toBe(false); | ||
expect(isValidDistraction({ name: 'chromium', time: '1m' })).toBe(true); | ||
expect(isValidDistraction({ name: 'inexistent' })).toBe(false); | ||
}); | ||
|
||
test('Should check if a time is within an interval', async () => { | ||
const currentDate = new Date('2021-01-01T12:00:00Z'); | ||
jest.spyOn(global, 'Date').mockImplementation(() => currentDate); | ||
|
||
expect(isWithinTimeRange('0h-23h')).toBe(true); | ||
expect(isWithinTimeRange('0h-19h')).toBe(true); | ||
expect(isWithinTimeRange('20h-23h')).toBe(false); | ||
}); | ||
|
||
test('Should block a distraction', async () => { | ||
blockDistraction({ name: 'example.com' }); | ||
|
||
expect(isDistractionBlocked('example.com')).toEqual(true); | ||
}); | ||
|
||
test('Should block a distraction with a duration', async () => { | ||
blockDistraction({ name: 'twitter.com', time: '2m' }); | ||
|
||
expect(isDistractionBlocked('twitter.com')).toBe(true); | ||
expect(config.blocklist).toEqual([{ name: 'twitter.com', time: '2m', timeout: expect.any(Number) }]); | ||
}); | ||
|
||
test('Should block a distraction with a time-based interval', async () => { | ||
const currentDate = new Date('2021-01-01T12:00:00Z'); | ||
jest.spyOn(global, 'Date').mockImplementation(() => currentDate); | ||
|
||
blockDistraction({ name: 'example.com', time: '0h-23h' }); | ||
|
||
expect(isDistractionBlocked('example.com')).toBe(true); | ||
}); | ||
|
||
test('Should block a specific subdomain', async () => { | ||
blockDistraction({ name: 'www.example.com' }); | ||
|
||
expect(isDistractionBlocked('www.example.com')).toBe(true); | ||
expect(isDistractionBlocked('example.com')).toBe(false); | ||
}); | ||
|
||
test('Should block all subdomains of a domain with a wildcard', async () => { | ||
blockDistraction({ name: '*.example.com' }); | ||
|
||
expect(isDistractionBlocked('www.example.com')).toBe(true); | ||
}); | ||
|
||
test('Should block all subdomains of a domain with a wildcard & a time-based interval', async () => { | ||
const currentDate = new Date('2021-01-01T12:00:00Z'); | ||
jest.spyOn(global, 'Date').mockImplementation(() => currentDate); | ||
|
||
blockDistraction({ name: '*.example.com', time: '0h-19h' }); | ||
|
||
expect(isDistractionBlocked('www.example.com')).toBe(true); | ||
}); | ||
|
||
test('Should block all domains with *.*', async () => { | ||
blockDistraction({ name: '*.*' }); | ||
|
||
expect(isDistractionBlocked('example.com')).toBe(true); | ||
}); | ||
|
||
test('Should not block an app with a time-based interval', async () => { | ||
const currentDate = new Date('2021-01-01T22:00:00Z'); | ||
jest.spyOn(global, 'Date').mockImplementation(() => currentDate); | ||
|
||
blockDistraction({ name: 'chromium', time: '0h-20h' }); | ||
|
||
expect(isDistractionBlocked('chromium')).toBe(false); | ||
}); | ||
|
||
test('Should not block a subdomain of a domain with a wildcard & a time-based interval', async () => { | ||
const currentDate = new Date('2021-01-01T20:00:00Z'); | ||
jest.spyOn(global, 'Date').mockImplementation(() => currentDate); | ||
|
||
blockDistraction({ name: '*.example.com', time: '0h-19h' }); | ||
|
||
expect(isDistractionBlocked('www.example.com')).toBe(false); | ||
}); | ||
|
||
test('Should not block apps if *.* is in the blocklist', async () => { | ||
blockDistraction({ name: '*.*' }); | ||
|
||
expect(isDistractionBlocked('chromium')).toBe(false); | ||
}); | ||
|
||
test('Should unblock a distraction', async () => { | ||
blockDistraction({ name: 'example.com' }); | ||
|
||
unblockDistraction({ name: 'example.com' }); | ||
|
||
expect(isDistractionBlocked('example.com')).toBe(false); | ||
}); | ||
|
||
test('Should run isDistractionBlocked in less than 150ms with a large blocklist', async () => { | ||
config.blocklist = Array.from({ length: 500000 }, (_, i) => ({ name: `${i + 1}.com` })); | ||
|
||
isDistractionBlocked('example.com'); | ||
const start = process.hrtime(); | ||
isDistractionBlocked('example.com'); | ||
const end = process.hrtime(start); | ||
|
||
expect(end[1] / 1000000).toBeLessThan(150); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,51 @@ | ||
import fs from 'fs'; | ||
import * as Utils from '../src/utils'; | ||
|
||
jest.mock('../src/utils'); | ||
|
||
jest.mock('dgram', () => ({ | ||
createSocket: jest.fn().mockReturnThis(), | ||
bind: jest.fn().mockReturnThis(), | ||
on: jest.fn(), | ||
})); | ||
|
||
jest.mock('net', () => ({ | ||
createServer: jest.fn().mockReturnThis(), | ||
listen: jest.fn().mockReturnThis(), | ||
import { config } from '../src/config'; | ||
import { getRunningApps } from '../src/utils'; | ||
import { blockDistraction } from '../src/block'; | ||
import { handleAppBlocking, handleTimeout, updateResolvConf } from '../src/daemon'; | ||
|
||
jest.mock('../src/utils', () => ({ | ||
...jest.requireActual('../src/utils'), | ||
isSudo: jest.fn().mockImplementation(() => true), | ||
})); | ||
|
||
jest.mock('child_process', () => ({ | ||
execSync: jest.fn().mockImplementation(() => false), | ||
})); | ||
|
||
jest.mock('socket.io-client', () => ({ | ||
io: jest.fn(() => ({ | ||
emit: jest.fn(), | ||
on: jest.fn(), | ||
})), | ||
exec: jest.fn().mockImplementation(() => false), | ||
})); | ||
|
||
beforeEach(() => { | ||
config.blocklist = []; | ||
config.whitelist = []; | ||
config.shield = false; | ||
jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
jest.spyOn(Utils, 'isSudo').mockReturnValue(true); | ||
jest.spyOn(Utils, 'getRunningBlockedApps').mockReturnValue([{ name: 'chromium', pid: 123 }]); | ||
jest.spyOn(Utils, 'updateResolvConf').mockImplementation(() => { | ||
fs.writeFileSync(process.env.RESOLV_CONF_PATH, 'nameserver 127.0.0.1', 'utf8'); | ||
}); | ||
}); | ||
|
||
test('Should block a running app', async () => { | ||
await import('../src/daemon'); | ||
blockDistraction({ name: 'node' }); | ||
|
||
handleAppBlocking(); | ||
|
||
expect(console.log).toHaveBeenCalledWith('Blocking chromium'); | ||
expect(console.log).toHaveBeenCalledWith('Blocking node'); | ||
}); | ||
|
||
test('Should get all running apps', async () => { | ||
const apps = getRunningApps(); | ||
|
||
expect(JSON.stringify(apps)).toContain('node'); | ||
}); | ||
|
||
test('Should edit /etc/resolv.conf', async () => { | ||
await import('../src/daemon'); | ||
updateResolvConf('127.0.0.1'); | ||
|
||
expect(fs.existsSync(process.env.RESOLV_CONF_PATH)).toBe(true); | ||
expect(fs.readFileSync(process.env.RESOLV_CONF_PATH, 'utf8')).toBe('nameserver 127.0.0.1'); | ||
}); | ||
|
||
test('Should remove a distraction from blocklist if timeout is reached', async () => { | ||
config.blocklist = [{ name: 'chromium' }, { name: 'example.com', timeout: 1708617136 }]; | ||
|
||
handleTimeout(); | ||
|
||
expect(config.blocklist).toEqual([{ name: 'chromium' }]); | ||
}); |
Oops, something went wrong.