Update unit tests to autodetect testing host
[bookreader.git] / BookReaderIA / test / unit / Common.js
index b416494..98ddb30 100644 (file)
@@ -1,9 +1,27 @@
 // Defines common variables for testing
 
-// $$$ TODO make test host auto-magic
-common = {
-    testHost: 'http://www-testflip.archive.org'
-    //testHost: 'http://www-mang.archive.org'    
+// What host to use for testing
+function testHost() {
+    // Autodetect if running from a home directory, or use live site
+    var user = null;
+    var patterns = [ new RegExp('.*?/~(.*?)/'), new RegExp('www-(.*?)\\.') ]
+    for (index in patterns) {
+        var match = patterns[index].exec(document.location.href);
+        if (match) {
+            user = match[1];
+            break;
+        }
+    }
+    
+    if (user) {
+        return 'http://www-' + user + '.archive.org';
+    }
+    
+    return 'http://www.archive.org'; // live site
+}
+
+var common = {
+    testHost: testHost(),
 }
 
 // Set up dummy BookReader class for JSLocate
@@ -24,14 +42,19 @@ function jsLocateURL(identifier, book) {
     return bookURL;
 }
 
+// Page should be in ['cover','title','preview']
 function previewURL(identifier, subPrefix, page) {
+    var bookPrefix = subPrefix || identifier;
+    var previewPage = bookPrefix + '_' + page;
+    return imagePermalink(identifier, subPrefix, previewPage);
+}
+
+// Page should be e.g. page5.jpg, n4.jpg, cover_t.jpg, n4_r3.jpg
+function imagePermalink(identifier, subPrefix, page) {
     var imageURL = common.testHost + '/download/' + identifier;
-    var bookPrefix = identifier;
     if (subPrefix) {
         imageURL += '/' + subPrefix;
-        var subPrefixParts = subPrefix.split('/')
-        bookPrefix = subPrefixParts[subPrefixParts.length - 1];
     }
-    imageURL += '/page/' + bookPrefix + '_' + page + '.jpg';
+    imageURL += '/page/' + page;
     return imageURL;
-}
\ No newline at end of file
+}