Skip to content

Commit e4c8740

Browse files
committed
add tests for js/rtorrent.js
1 parent beaeb52 commit e4c8740

15 files changed

Lines changed: 914 additions & 0 deletions

tests/.babelrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env'],
3+
};

tests/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
node_modules
3+
package-lock.json

tests/.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require all denied

tests/js/rtorrent.spec.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { readFileSync } from "fs";
2+
window.$ = require("jquery");
3+
window.theWebUI = {
4+
settings: {
5+
"webui.needmessage": true,
6+
},
7+
8+
showFlags: 0xffff,
9+
systemInfo: {
10+
rTorrent: {
11+
apiVersion: 10,
12+
iVersion: 0x908,
13+
started: true,
14+
},
15+
},
16+
};
17+
18+
for (const src of [
19+
"../lang/en.js",
20+
"../js/common.js",
21+
"../js/content.js",
22+
"../js/rtorrent.js",
23+
]) {
24+
const scriptEl = document.createElement("script");
25+
scriptEl.textContent = readFileSync(src, { encoding: "utf-8" });
26+
document.body.appendChild(scriptEl);
27+
}
28+
correctContent();
29+
30+
function h(char) {
31+
return Array.from({ length: 40 }, () => char).join("");
32+
}
33+
34+
function loadXML(action) {
35+
const fileName = `xml-responses/${action}.xml`;
36+
const parser = new DOMParser();
37+
const doc = parser.parseFromString(readFileSync(fileName), "application/xml");
38+
if (doc.querySelector("parsererror")) {
39+
throw new Error(`Error parsing xml file: ${fileName}`);
40+
}
41+
return doc;
42+
}
43+
44+
describe("xmlrpc calls", () => {
45+
it("should parse getprops response", () => {
46+
const stub = new rTorrentStub(`?action=getprops&hash=${h("A")}`);
47+
//console.log(stub.content);
48+
const ret = stub.getResponse(loadXML(stub.action));
49+
//console.log(ret);
50+
expect(ret).toStrictEqual({
51+
[h("A")]: {
52+
pex: "0",
53+
peers_max: "100",
54+
peers_min: "40",
55+
tracker_numwant: "-1",
56+
ulslots: "50",
57+
superseed: 0,
58+
},
59+
});
60+
});
61+
62+
it("should parse gettotal response", () => {
63+
const stub = new rTorrentStub("?action=gettotal");
64+
//console.log(stub.content);
65+
const ret = stub.getResponse(loadXML(stub.action));
66+
//console.log(ret);
67+
expect(ret).toStrictEqual({ UL: 2222, DL: 1111, rateUL: 222, rateDL: 111 });
68+
});
69+
70+
it("should parse getopen response", () => {
71+
const stub = new rTorrentStub("?action=getopen");
72+
//console.log(stub.content);
73+
const ret = stub.getResponse(loadXML(stub.action));
74+
//console.log(ret);
75+
expect(ret).toStrictEqual({ http: 1, sock: 22, fd: -1 });
76+
});
77+
78+
it("should parse getsettings response", () => {
79+
const stub = new rTorrentStub("?action=getsettings");
80+
//console.log(stub.content);
81+
const ret = stub.getResponse(loadXML(stub.action));
82+
//console.log(ret);
83+
expect(ret.dht).toBe(1);
84+
expect(ret.directory).toBe("/downloads");
85+
expect(ret.session).toBe("/rtorrent-path/.session/");
86+
const ret2 = stub.getResponse(loadXML("getsettings-nodht"));
87+
//console.log(ret2);
88+
expect(ret2.dht).toBe(0);
89+
expect(ret2.directory).toBe("/downloads");
90+
expect(ret2.session).toBe("/rtorrent-path/.session/");
91+
});
92+
93+
it("should parse getalltrackers response", () => {
94+
const stub = new rTorrentStub(
95+
`?action=getalltrackers&hash=${h("A")}&hash=${h("B")}`
96+
);
97+
//console.log(stub.content);
98+
const ret = stub.getResponse(loadXML(stub.action));
99+
//console.log(ret);
100+
expect(Object.keys(ret).length).toBe(2);
101+
expect(ret[h("A")][0].name).toBe(
102+
"http://sometracker.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/announce"
103+
);
104+
expect(ret[h("B")][0].name).toBe(
105+
"http://sometracker2.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/announce"
106+
);
107+
});
108+
109+
it("should parse getfiles response", () => {
110+
const stub = new rTorrentStub(`?action=getfiles&hash=${h("A")}`);
111+
//console.log(stub.content);
112+
const ret = stub.getResponse(loadXML(stub.action));
113+
//console.log(ret);
114+
expect(Object.keys(ret).length).toBe(1);
115+
const size = 512 * 512;
116+
expect(ret[h("A")]).toStrictEqual([
117+
{ name: "File 1", size, done: 0 * size, priority: "1" },
118+
{ name: "File 2", size, done: 0.5 * size, priority: "2" },
119+
{ name: "File 3", size, done: 1 * size, priority: "3" },
120+
]);
121+
});
122+
123+
it("should parse getpeers response", () => {
124+
const stub = new rTorrentStub(`?action=getpeers&hash=${h("A")}`);
125+
//console.log(stub.content);
126+
const ret = stub.getResponse(loadXML(stub.action));
127+
//console.log(ret);
128+
expect(Object.keys(ret).length).toBe(2);
129+
expect(ret[h("X")].ip).toBe("111.111.111.111");
130+
expect(ret[h("Y")].ip).toBe("222.222.222.222");
131+
});
132+
133+
it("should parse gettrackers response", () => {
134+
const stub = new rTorrentStub(`?action=gettrackers&hash=${h("A")}`);
135+
//console.log(stub.content);
136+
const ret = stub.getResponse(loadXML(stub.action));
137+
//console.log(ret);
138+
expect(Object.keys(ret).length).toBe(1);
139+
expect(ret[h("A")][0].name).toBe(
140+
"https://tracker.site.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/announce"
141+
);
142+
expect(ret[h("A")][1].name).toBe(
143+
"https://tracker.site2.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/announce"
144+
);
145+
});
146+
147+
it("should parse list response", () => {
148+
const stub = new rTorrentStub("?list=1");
149+
//console.log(stub.content);
150+
const ret = stub.getResponse(loadXML(stub.action));
151+
//console.log(ret);
152+
for (const [hash, label, comment] of [
153+
[h("A"), "Cat A", ""],
154+
[h("B"), "Cat A/Sec 1", ""],
155+
[h("C"), "Cat B/Sec 1", "some comment"],
156+
[h("D"), "Cat C", "https://site.com/content"],
157+
]) {
158+
expect(ret.torrents[hash].name).toBe(`Name of ${hash}`);
159+
expect(ret.torrents[hash].save_path).toBe("/path/to/torrent");
160+
expect(ret.torrents[hash].label).toBe(label);
161+
expect(ret.torrents[hash].comment).toBe(comment);
162+
}
163+
});
164+
});

tests/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"private": true,
3+
"version": "0.0.0",
4+
"description": "ruTorrent javascript tests",
5+
"name": "rutorrent-tests",
6+
"devDependencies": {
7+
"@babel/core": "7.17.5",
8+
"@babel/preset-env": "7.16.11",
9+
"babel-jest": "27.5.1",
10+
"jest": "27.5.1"
11+
},
12+
"dependencies": {
13+
"jquery": "3.6.0"
14+
},
15+
"scripts": {
16+
"test": "jest"
17+
},
18+
"jest": {
19+
"testEnvironment": "jsdom",
20+
"testEnvironmentOptions": {
21+
"resources": "usable",
22+
"runScripts": "dangerously"
23+
},
24+
"verbose": true,
25+
"silent": false
26+
},
27+
"repository": {
28+
"type": "git",
29+
"url": "../"
30+
},
31+
"keywords": [
32+
"test",
33+
"rutorrent"
34+
],
35+
"target": "es6",
36+
"module": "commonjs",
37+
"author": "TrimmingFool",
38+
"license": "GPL-3.0"
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<methodResponse>
3+
<params>
4+
<param><value><array><data>
5+
<value><array><data>
6+
<value><array><data>
7+
<value><array><data>
8+
<value><string>http://sometracker.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/announce</string></value>
9+
<value><i8>1</i8></value>
10+
<value><i8>1</i8></value>
11+
<value><i8>0</i8></value>
12+
<value><i8>22</i8></value>
13+
<value><i8>1</i8></value>
14+
<value><i8>222</i8></value>
15+
<value><i8>1111</i8></value>
16+
<value><i8>2222222222</i8></value>
17+
</data></array></value>
18+
</data></array></value>
19+
</data></array></value>
20+
<value><array><data>
21+
<value><array><data>
22+
<value><array><data>
23+
<value><string>http://sometracker2.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/announce</string></value>
24+
<value><i8>1</i8></value>
25+
<value><i8>1</i8></value>
26+
<value><i8>0</i8></value>
27+
<value><i8>22</i8></value>
28+
<value><i8>1</i8></value>
29+
<value><i8>222</i8></value>
30+
<value><i8>1111</i8></value>
31+
<value><i8>2222222222</i8></value>
32+
</data></array></value>
33+
</data></array></value>
34+
</data></array></value>
35+
</data></array></value></param>
36+
</params>
37+
</methodResponse>

tests/xml-responses/getfiles.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<methodResponse>
3+
<params>
4+
<param><value><array><data>
5+
<value><array><data>
6+
<value><string>File 1</string></value>
7+
<value><i8>0</i8></value>
8+
<value><i8>512</i8></value>
9+
<value><i8>262144</i8></value>
10+
<value><i8>1</i8></value>
11+
<value><i8>0</i8></value>
12+
<value><i8>0</i8></value>
13+
</data></array></value>
14+
<value><array><data>
15+
<value><string>File 2</string></value>
16+
<value><i8>256</i8></value>
17+
<value><i8>512</i8></value>
18+
<value><i8>262144</i8></value>
19+
<value><i8>2</i8></value>
20+
<value><i8>0</i8></value>
21+
<value><i8>0</i8></value>
22+
</data></array></value>
23+
<value><array><data>
24+
<value><string>File 3</string></value>
25+
<value><i8>512</i8></value>
26+
<value><i8>512</i8></value>
27+
<value><i8>262144</i8></value>
28+
<value><i8>3</i8></value>
29+
<value><i8>0</i8></value>
30+
<value><i8>0</i8></value>
31+
</data></array></value>
32+
</data></array></value></param>
33+
</params>
34+
</methodResponse>

tests/xml-responses/getopen.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<methodResponse>
3+
<params>
4+
<param><value><array><data>
5+
<value><array><data>
6+
<value><i8>1</i8></value>
7+
</data></array></value>
8+
<value><array><data>
9+
<value><i8>22</i8></value>
10+
</data></array></value>
11+
</data></array></value></param>
12+
</params>
13+
</methodResponse>

tests/xml-responses/getpeers.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<methodResponse>
3+
<params>
4+
<param><value><array><data>
5+
<value><array><data>
6+
<value><string>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</string></value>
7+
<value><string>111.111.111.111</string></value>
8+
<value><string>Transmission 2.9.4.0</string></value>
9+
<value><i8>1</i8></value>
10+
<value><i8>0</i8></value>
11+
<value><i8>0</i8></value>
12+
<value><i8>95</i8></value>
13+
<value><i8>0</i8></value>
14+
<value><i8>222222222</i8></value>
15+
<value><i8>0</i8></value>
16+
<value><i8>2121</i8></value>
17+
<value><string></string></value>
18+
<value><i8>222222222</i8></value>
19+
<value><i8>111111111111</i8></value>
20+
<value><i8>1212</i8></value>
21+
</data></array></value>
22+
<value><array><data>
23+
<value><string>YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY</string></value>
24+
<value><string>222.222.222.222</string></value>
25+
<value><string>Transmission 2.9.4.0</string></value>
26+
<value><i8>1</i8></value>
27+
<value><i8>0</i8></value>
28+
<value><i8>0</i8></value>
29+
<value><i8>95</i8></value>
30+
<value><i8>0</i8></value>
31+
<value><i8>111111111</i8></value>
32+
<value><i8>0</i8></value>
33+
<value><i8>2121</i8></value>
34+
<value><string></string></value>
35+
<value><i8>22222222</i8></value>
36+
<value><i8>111111111111</i8></value>
37+
<value><i8>1212</i8></value>
38+
</data></array></value>
39+
</data></array></value></param>
40+
</params>
41+
</methodResponse>

tests/xml-responses/getprops.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<methodResponse>
3+
<params>
4+
<param><value><array><data>
5+
<value><array><data>
6+
<value><i8>0</i8></value>
7+
</data></array></value>
8+
<value><array><data>
9+
<value><i8>100</i8></value>
10+
</data></array></value>
11+
<value><array><data>
12+
<value><i8>40</i8></value>
13+
</data></array></value>
14+
<value><array><data>
15+
<value><i8>-1</i8></value>
16+
</data></array></value>
17+
<value><array><data>
18+
<value><i8>50</i8></value>
19+
</data></array></value>
20+
<value><array><data>
21+
<value><i8>0</i8></value>
22+
</data></array></value>
23+
<value><array><data>
24+
<value><string></string></value>
25+
</data></array></value>
26+
</data></array></value></param>
27+
</params>
28+
</methodResponse>

0 commit comments

Comments
 (0)