UNPKG

7.34 kBJavaScriptView Raw
1const DwebTransports = require('./index.js');
2
3async function p_test({transport=["GUN"]}={}) {
4 if (Array.isArray(transport)) {
5 for (tname of transport) {
6 await p_test({transport: tname}); // Note this is going to run in parallel
7 }
8 } else {
9 let tclass = DwebTransports._transportclasses[transport];
10 await tclass.p_test();
11 }
12}
13async function test_transports() {
14 console.log("Transports.test")
15 try {
16 /* Could convert this - copied fom YJS to do a test at the "Transports" level
17 let testurl = "yjs:/yjs/THISATEST"; // Just a predictable number can work with
18 let res = await transport.p_rawlist(testurl);
19 let listlen = res.length; // Holds length of list run intermediate
20 console.log("rawlist returned ", ...utils.consolearr(res));
21 transport.listmonitor(testurl, (obj) => console.log("Monitored", obj));
22 let sig = new Dweb.Signature({urls: ["123"], date: new Date(Date.now()), signature: "Joe Smith", signedby: [testurl]});
23 await transport.p_rawadd(testurl, sig);
24 console.log("TransportIPFS.p_rawadd returned ");
25 res = await transport.p_rawlist(testurl);
26 console.log("rawlist returned ", ...utils.consolearr(res)); // Note not showing return
27 await delay(500);
28 res = await transport.p_rawlist(testurl);
29 console.assert(res.length === listlen + 1, "Should have added one item");
30 */
31 //console.log("TransportYJS test complete");
32 /* TODO-KEYVALUE reenable these tests,s but catch http examples
33 let db = await this.p_newdatabase("TESTNOTREALLYAKEY"); // { privateurls, publicurls }
34 console.assert(db.privateurls[0] === "yjs:/yjs/TESTNOTREALLYAKEY");
35 let table = await this.p_newtable("TESTNOTREALLYAKEY","TESTTABLE"); // { privateurls, publicurls }
36 let mapurls = table.publicurls;
37 console.assert(mapurls[0] === "yjs:/yjs/TESTNOTREALLYAKEY/TESTTABLE");
38 await this.p_set(mapurls, "testkey", "testvalue");
39 let res = await this.p_get(mapurls, "testkey");
40 console.assert(res === "testvalue");
41 await this.p_set(mapurls, "testkey2", {foo: "bar"});
42 res = await this.p_get(mapurls, "testkey2");
43 console.assert(res.foo === "bar");
44 await this.p_set(mapurls, "testkey3", [1,2,3]);
45 res = await this.p_get(mapurls, "testkey3");
46 console.assert(res[1] === 2);
47 res = await this.p_keys(mapurls);
48 console.assert(res.length === 3 && res.includes("testkey3"));
49 res = await this.p_getall(mapurls);
50 console.assert(res.testkey2.foo === "bar");
51 */
52
53 } catch(err) {
54 console.log("Exception thrown in Transports.test:", err.message);
55 throw err;
56 }
57}
58
59function canonicalNameTests() {
60 // Test the regexps
61 [ // Each test shows the URL and the expected return of protocol and internal string, it doesnt test failing cases
62 ["https://dweb.me/ipfs/internal", "ipfs", "internal"],
63 ["https://dweb.ipfs.foo.bar/internal", "ipfs", "internal"],
64 ["dweb://ipfs/internal", "ipfs", "internal"],
65 ["ipfs://internal", "ipfs", "internal"],
66 ["gun://ipfs/internal", "ipfs", "internal"],
67 ["/ipfs/internal", "ipfs", "internal"],
68 ["/dweb/ipfs/internal", "ipfs", "internal"],
69 [ "magnet:?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json",
70 "magnet","?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json"],
71 ["http://dweb.dom.ain/internal", "arc", "dom.ain/internal"],
72 ["http://localhost:123/archive.org/internal", "arc", "archive.org/internal"],
73 ["https://dweb.arc.dom.ain/internal", "arc", "dom.ain/internal"],
74 ["https://foo.bar/baz/splat", "https", "foo.bar/baz/splat"]
75 ].forEach((t) => {
76 console.log(t[0])
77 let res = DwebTransports.canonicalName(t[0]);
78 if (!(res && res["proto"] === t[1] && res["internal"] === t[2])) {
79 console.log("ERROR", t, res);
80 }
81 })
82}
83
84/*
85p_test({transport: ["GUN"])
86.then(() => test_transports);
87*/
88
89/*
90// Intentionally testing this with no connection
91const sampleMagnetURL = "magnet:?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json";
92const sampleMagnetURLMirrorresolve = "http://localhost:4244/magnet/?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json"
93const sampleGatewayURL = "http://dweb.me/arc/archive.org/download/foo/bar";
94const sampleHttpURL = "http://somewhere.com/zzz/xxx/download/foo/bar"; // XXX Looks like this is handled wrong
95const sampleGatewayURLMirrorResolve = "http://localhost:4244/arc/archive.org/download/foo/bar";
96let tests = [
97 {u: sampleMagnetURL, cn_proto:"magnet", gw: sampleMagnetURLMirrorresolve, resolve:sampleMagnetURL, resolveM: sampleMagnetURLMirrorresolve},
98 {u: sampleGatewayURL, cn_proto: "arc", gw: sampleGatewayURLMirrorResolve, resolve: sampleGatewayURL, resolveM: sampleGatewayURLMirrorResolve},
99 //FAILS ! {u: sampleHttpURL, cn_proto: "http", gw: sampleHttpURL, resolve: sampleHttpURL, resolveM: sampleHttpURL}
100 ]
101tests.forEach(t => {
102 let url = t.u;
103 let res = DwebTransports.canonicalName(url).proto; console.assert( res=== t.cn_proto, "Canonical fail",url, t.cn_proto, "!==", res);
104 DwebTransports.mirror = undefined;
105 res = DwebTransports.p_resolveNames([url]).then(res => console.assert(res[0] === t.resolve, "Resolve", url, t.resolve, "!==", res ))
106 DwebTransports.mirror = "http://localhost:4244";
107 res = DwebTransports.gatewayUrl(url); console.assert( res === t.gw, "GatewayURL:", url, t.gw,"!==", res);
108 res = DwebTransports.p_resolveNames([url]).then(res => console.assert(res[0] === t.resolveM, "Resolve with Mirror", url, t.resolveM, "!==", res ))
109})
110*/
\No newline at end of file