Skip to content

Commit bf119cd

Browse files
authored
Merge pull request #4 from getulysse/refactoring
[RFR] Refactoring
2 parents 7ef2797 + 8675681 commit bf119cd

24 files changed

+650
-581
lines changed

.env.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
GUN_SERVER=http://localhost:8765/gun
21
CONFIG_PATH=/tmp/ulysse/config.json
32
RESOLV_CONF_PATH=/tmp/resolv.conf
43
SOCKET_PATH=/tmp/ulysse.sock

README.md

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![Version](https://img.shields.io/npm/v/ulysse?label=Version&style=flat&colorA=2B323B&colorB=1e2329)](https://www.npmjs.com/package/ulysse)
44
[![License](https://img.shields.io/badge/license-GPL%20v3%2B-yellow.svg?label=License&style=flat&colorA=2B323B&colorB=1e2329)](https://raw.githubusercontent.com/johackim/ulysse/master/LICENSE.txt)
5-
[![Code Climate](https://img.shields.io/codeclimate/maintainability/johackim/ulysse.svg?label=Maintainability&style=flat&colorA=2B323B&colorB=1e2329)](https://codeclimate.com/github/johackim/ulysse)
65

76
Ulysse is a simple CLI tool for blocking your distracting apps and websites.
87

@@ -18,12 +17,14 @@ Prevent distractions by blocking your most distracting apps and websites, even i
1817
> ```bash
1918
> rm /etc/sudoers.d/ulysse
2019
> usermod -s /bin/bash root # Or edit /etc/passwd file
21-
> echo 'nameserver 9.9.9.9' | sudo tee /etc/resolv.conf
20+
> echo 'nameserver 9.9.9.9' | tee /etc/resolv.conf
2221
> ```
2322
2423
## 📋 Requirements
2524
25+
- X11
2626
- Linux
27+
- Systemd
2728
- Node.js >= 14.0.0
2829
2930
## ✨ Features
@@ -40,38 +41,27 @@ npm i -g ulysse
4041
## 📖 Usage
4142

4243
```txt
43-
Usage: ulysse [OPTIONS]
44+
Usage: ulysse [options] [command]
4445
45-
Ulysse: A simple and powerful tool to block your distracting apps and websites.
46+
A simple CLI tool for blocking your distracting apps and websites.
4647
4748
Options:
48-
-b, --block TARGET [-t, --time VALUE]
49-
Block a specific website or application. Optionally, add a time-based setting.
50-
The VALUE format can include usage limits, specific time intervals,
51-
or a quick block duration.
52-
Examples:
53-
'ulysse -b example.com' (block indefinitely)
54-
'ulysse -b example.com -t 10m' (block for a short duration)
55-
'ulysse -b MyAppName -t 10h-18h' (block during specific hours)
56-
57-
-u, --unblock TARGET Unblock a specific website or application.
58-
Example: 'ulysse -u example.com' or 'ulysse -u MyAppName'.
59-
60-
-w, --whitelist TARGET Whitelist a specific website.
61-
Example: 'ulysse -w example.com'.
62-
63-
-s, --shield [on|off] [-p, --password VALUE]
64-
Enable or disable shield mode to prevent jailbreak.
65-
By default, the shield mode is on. Use 'off' along with a password to disable it.
66-
The password is required to disable the shield mode.
67-
Example: 'ulysse -s on' to enable or 'ulysse -s off -p myp@ssw0rd' to disable.
68-
69-
-d, --daemon Run Ulysse as a daemon.
70-
Example: 'ulysse -d' or 'ulysse --daemon'.
71-
72-
-v, --version Show the version and exit.
73-
74-
-h, --help Show this message and exit.
49+
-v, --version Show the version and exit
50+
-h, --help Show this help message and exit
51+
52+
Commands:
53+
daemon Start the Ulysse daemon
54+
blocklist Manage the blocklist
55+
whitelist Manage the whitelist
56+
shield Enable or disable the shield mode
57+
help [command] display help for command
58+
59+
Examples:
60+
ulysse daemon start
61+
ulysse blocklist add --app firefox
62+
ulysse whitelist add --website wikipedia.org
63+
ulysse blocklist add --website youtube.com -t 8h-20h
64+
ulysse shield enable
7565
```
7666

7767
## 🎁 Support me

__tests__/block.spec.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
getRunningBlockedApps,
1111
} from '../src/block';
1212

13+
jest.mock('../src/utils.js', () => ({
14+
...jest.requireActual('../src/utils.js'),
15+
}));
16+
1317
beforeEach(async () => {
1418
await disableShieldMode('ulysse');
1519
await editConfig(DEFAULT_CONFIG);
@@ -160,7 +164,7 @@ test('Should get running blocked apps', () => {
160164
});
161165
});
162166

163-
test('Should block all apps and websites', async () => {
167+
test.skip('Should block all apps and websites', async () => {
164168
await blockDistraction({ name: '*' });
165169

166170
expect(isDistractionBlocked('example.com')).toEqual(true);
@@ -173,14 +177,6 @@ test('Should block all apps and websites', async () => {
173177
});
174178
});
175179

176-
test('Should not block system process', async () => {
177-
blockDistraction({ name: '*' });
178-
179-
const runningBlockedApps = JSON.stringify(getRunningBlockedApps());
180-
181-
expect(runningBlockedApps).not.toContain('/sbin/init');
182-
});
183-
184180
test('Should not block all websites outside of a time range', async () => {
185181
const currentDate = new Date('2021-01-01T12:00:00Z');
186182
jest.spyOn(global, 'Date').mockImplementation(() => currentDate);
@@ -189,3 +185,22 @@ test('Should not block all websites outside of a time range', async () => {
189185

190186
expect(isDistractionBlocked('example.com')).toEqual(false);
191187
});
188+
189+
test('Should block a distraction with time interval parameter', async () => {
190+
const currentDate = new Date('2021-01-01T15:00:00Z');
191+
jest.spyOn(global, 'Date').mockImplementation(() => currentDate);
192+
193+
await blockDistraction({ name: 'youtube.com', time: '8h-20h' });
194+
195+
expect(isDistractionBlocked('youtube.com')).toBe(true);
196+
expect(config.blocklist).toEqual([{ name: 'youtube.com', time: '8h-20h' }]);
197+
});
198+
199+
test('Should not block a distraction outside of time interval', async () => {
200+
const currentDate = new Date('2021-01-01T22:00:00Z');
201+
jest.spyOn(global, 'Date').mockImplementation(() => currentDate);
202+
203+
await blockDistraction({ name: 'youtube.com', time: '8h-20h' });
204+
205+
expect(isDistractionBlocked('youtube.com')).toBe(false);
206+
});

__tests__/commands.spec.js

Lines changed: 0 additions & 133 deletions
This file was deleted.

__tests__/daemon.spec.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import { config, editConfig, readConfig } from '../src/config';
33
import { getRunningApps } from '../src/utils';
4+
import { listActiveWindows } from '../src/x11';
45
import { blockDistraction } from '../src/block';
56
import { DEFAULT_CONFIG } from '../src/constants';
67
import { disableShieldMode } from '../src/shield';
@@ -9,6 +10,12 @@ import { handleAppBlocking, handleTimeout, updateResolvConf } from '../src/daemo
910
jest.mock('../src/utils', () => ({
1011
...jest.requireActual('../src/utils'),
1112
isSudo: jest.fn().mockImplementation(() => true),
13+
getRunningApps: jest.fn(),
14+
}));
15+
16+
jest.mock('../src/x11', () => ({
17+
listActiveWindows: jest.fn().mockResolvedValue([]),
18+
closeWindow: jest.fn(),
1219
}));
1320

1421
jest.mock('child_process', () => ({
@@ -24,16 +31,28 @@ beforeEach(async () => {
2431
});
2532

2633
test('Should block a running app', async () => {
27-
blockDistraction({ name: 'node' });
34+
getRunningApps.mockReturnValue([{ name: 'signal-desktop' }]);
35+
await blockDistraction({ name: 'signal-desktop' });
36+
37+
await handleAppBlocking();
38+
39+
expect(console.log).toHaveBeenCalledWith('Blocking signal-desktop');
40+
});
41+
42+
test('Should block a window', async () => {
43+
listActiveWindows.mockResolvedValue([{ name: 'signal' }]);
44+
blockDistraction({ name: 'signal' });
2845

29-
handleAppBlocking();
46+
await handleAppBlocking();
3047

31-
expect(console.log).toHaveBeenCalledWith('Blocking node');
48+
expect(console.log).toHaveBeenCalledWith('Blocking signal');
3249
});
3350

3451
test('Should get all running apps', async () => {
52+
getRunningApps.mockReturnValue([{ name: 'node', pid: 1234 }]);
3553
const apps = getRunningApps();
3654

55+
expect(getRunningApps).toHaveBeenCalled();
3756
expect(JSON.stringify(apps)).toContain('node');
3857
});
3958

__tests__/index.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { program } from '../src/index';
2+
import * as utilsModule from '../src/utils';
3+
import { description, version } from '../package.json';
4+
5+
const runCommand = (command) => {
6+
const args = command.split(' ');
7+
return program.parseAsync(['node', 'src/index.js', ...args]);
8+
};
9+
10+
beforeEach(async () => {
11+
jest.spyOn(console, 'log').mockImplementation(() => {});
12+
jest.spyOn(utilsModule, 'isDaemonRunning').mockReturnValue(true);
13+
});
14+
15+
test('Should display the version', async () => {
16+
const mockWrite = jest.fn();
17+
program.exitOverride().configureOutput({ writeOut: mockWrite });
18+
19+
await expect(runCommand('--version')).rejects.toThrow();
20+
21+
expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining(version));
22+
});
23+
24+
test('Should display the help message', async () => {
25+
const mockWrite = jest.fn();
26+
program.exitOverride().configureOutput({ writeOut: mockWrite });
27+
28+
await expect(runCommand('--help')).rejects.toThrow();
29+
30+
expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining(description));
31+
});
32+
33+
test('Should block a domain', async () => {
34+
await runCommand('blocklist add --website example.com');
35+
36+
expect(console.log).toHaveBeenCalledWith('Blocking website example.com');
37+
});

0 commit comments

Comments
 (0)