Skip to content

Commit 1363de7

Browse files
committed
text: add test for privacy_aware_path
1 parent d30d62f commit 1363de7

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

src/gui/src/initgui.js

+3-32
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { AntiCSRFService } from './services/AntiCSRFService.js';
4949
import { IPCService } from './services/IPCService.js';
5050
import { ExecService } from './services/ExecService.js';
5151
import { DebugService } from './services/DebugService.js';
52+
import { privacy_aware_path } from './util/desktop.js';
5253

5354
const launch_services = async function (options) {
5455
// === Services Data Structures ===
@@ -1461,35 +1462,5 @@ $(document).on('contextmenu', '.disable-context-menu', function(e){
14611462
}
14621463
})
14631464

1464-
/**
1465-
* Converts a file system path to a privacy-aware path.
1466-
* - Paths starting with `~/` are returned unchanged.
1467-
* - Paths starting with the user's home path are replaced with `~`.
1468-
* - Absolute paths not starting with the user's home path are returned unchanged.
1469-
* - Relative paths are prefixed with `~/`.
1470-
* - Other paths are returned unchanged.
1471-
*
1472-
* @param {string} fspath - The file system path to be converted.
1473-
* @returns {string} The privacy-aware path.
1474-
*/
1475-
window.privacy_aware_path = function(fspath){
1476-
// e.g. /my_username/test.txt -> ~/test.txt
1477-
if(fspath.startsWith('~/'))
1478-
return fspath;
1479-
// e.g. /my_username/test.txt -> ~/test.txt
1480-
else if(fspath.startsWith(
1481-
window.home_path.endsWith('/')
1482-
? window.home_path
1483-
: window.home_path + '/'
1484-
))
1485-
return fspath.replace(window.home_path, '~');
1486-
// e.g. /other_username/test.txt -> /other_username/test.txt
1487-
else if(fspath.startsWith('/') && !fspath.startsWith(window.home_path))
1488-
return fspath;
1489-
// e.g. test.txt -> ~/test.txt
1490-
else if(!fspath.startsWith('/'))
1491-
return '~/' + fspath;
1492-
// e.g. /username/path/to/item -> /username/path/to/item
1493-
else
1494-
return fspath;
1495-
};
1465+
// util/desktop.js
1466+
window.privacy_aware_path = privacy_aware_path({ window });

src/gui/src/util/desktop.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This file contains functions that are used by Puter's desktop GUI.
3+
* Functions moved here are not bound to the `window` object, making it
4+
* easier to write unit tests for them.
5+
*
6+
* Functions here may be bound to `window` at any of the following locations:
7+
* - src/gui/src/initgui.js
8+
*
9+
* ^ Please add to the above list as necessary when moving functions here.
10+
*/
11+
12+
/**
13+
* Converts a file system path to a privacy-aware path.
14+
* - Paths starting with `~/` are returned unchanged.
15+
* - Paths starting with the user's home path are replaced with `~`.
16+
* - Absolute paths not starting with the user's home path are returned unchanged.
17+
* - Relative paths are prefixed with `~/`.
18+
* - Other paths are returned unchanged.
19+
*
20+
* @param {string} fspath - The file system path to be converted.
21+
* @returns {string} The privacy-aware path.
22+
*/
23+
export const privacy_aware_path = world => function privacy_aware_path (fspath) {
24+
// e.g. /my_username/test.txt -> ~/test.txt
25+
if(fspath.startsWith('~/'))
26+
return fspath;
27+
// e.g. /my_username/test.txt -> ~/test.txt
28+
else if(fspath.startsWith(
29+
world.window.home_path.endsWith('/')
30+
? world.window.home_path
31+
: world.window.home_path + '/'
32+
))
33+
return fspath.replace(world.window.home_path, '~');
34+
// e.g. /other_username/test.txt -> /other_username/test.txt
35+
else if(fspath.startsWith('/') && !fspath.startsWith(world.window.home_path))
36+
return fspath;
37+
// e.g. test.txt -> ~/test.txt
38+
else if(!fspath.startsWith('/'))
39+
return '~/' + fspath;
40+
// e.g. /username/path/to/item -> /username/path/to/item
41+
else
42+
return fspath;
43+
};
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import assert from 'assert';
2+
import { privacy_aware_path } from '../src/util/desktop.js';
3+
4+
const cases = [
5+
{
6+
title: 'path in user home',
7+
username: 'user',
8+
input: '/home/user/test.txt',
9+
expected: '~/test.txt',
10+
},
11+
{
12+
title: 'path on user desktop',
13+
username: 'user',
14+
input: '/home/user/Desktop/test.txt',
15+
expected: '~/Desktop/test.txt',
16+
},
17+
{
18+
title: 'prefix (ed3/ed) bug',
19+
username: 'ed',
20+
input: '/home/ed3/Desktop/test.txt',
21+
expected: '/home/ed3/Desktop/test.txt',
22+
},
23+
];
24+
25+
describe('window.privacy_aware_path', () => {
26+
for (const { title, username, input, expected } of cases) {
27+
it(title, () => {
28+
assert.equal(privacy_aware_path({
29+
window: { home_path: `/home/${username}` },
30+
})(input), expected);
31+
});
32+
}
33+
});

0 commit comments

Comments
 (0)