|
| 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 | +}); |
0 commit comments