github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/server/camlistored/ui/search_session_test.js (about) 1 /* 2 Copyright 2014 The Camlistore Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 var assert = require('assert'); 18 19 goog.require('cam.SearchSession'); 20 21 22 function MockServerConnection(response) { 23 this.response_ = response; 24 } 25 26 MockServerConnection.prototype.search = function(query, describe, limit, continuationToken, callback) { 27 setImmediate(callback.bind(null, this.response_)); 28 }; 29 30 31 describe('cam.SearchSession', function() { 32 var session = null; 33 var response = { 34 blobs: [ 35 {'blob': 'a'}, 36 {'blob': 'b'}, 37 {'blob': 'c'}, 38 {'blob': 'd'}, 39 {'blob': 'e'}, 40 ], 41 description: { 42 meta: { 43 a: { 44 blobRef: 'a', 45 camliType: 'file', 46 file: { 47 fileName: 'foo.txt', 48 }, 49 }, 50 a2: { 51 blobRef: 'a2', 52 camliType: 'file', 53 file: { 54 }, 55 }, 56 b: { 57 blobRef: 'b', 58 camliType: 'permanode', 59 permanode: { 60 attr: { 61 camliContent: ['a'], 62 title: ['permanode b'], 63 } 64 } 65 }, 66 b2: { 67 blobRef: 'b2', 68 camliType: 'permanode', 69 permanode: { 70 attr: { 71 camliContent: ['a'], 72 } 73 } 74 }, 75 c: { 76 blobRef: 'c', 77 camliType: 'permanode', 78 permanode: { 79 attr: { 80 }, 81 } 82 }, 83 d: { 84 blobRef: 'd', 85 camliType: 'permanode', 86 permanode: { 87 attr: { 88 camliContent: ['b'], 89 } 90 } 91 }, 92 e: { 93 blobRef: 'e', 94 camliType: 'permanode', 95 permanode: { 96 attr: { 97 camliContent: ['_non_existant_'], 98 title: 'permanode e', 99 } 100 } 101 }, 102 } 103 } 104 }; 105 106 before(function(done) { 107 var currentUri = null; 108 var query = null; 109 session = new cam.SearchSession(new MockServerConnection(response), currentUri, query); 110 session.addEventListener(cam.SearchSession.SEARCH_SESSION_CHANGED, function() { 111 assert.equal(response.description.meta.a, session.getResolvedMeta('a')); 112 done(); 113 }); 114 session.loadMoreResults(); 115 }); 116 117 describe('#getResolvedMeta', function() { 118 it('should resolve blobrefs correctly', function() { 119 // a is not a permanode, so its resolved value is itself. 120 assert.equal(response.description.meta.a, session.getResolvedMeta('a')); 121 122 // b is a permanode that points to a. 123 assert.equal(response.description.meta.a, session.getResolvedMeta('b')); 124 125 // c is a permanode, but has no camliContent, so its resolved value is itself. 126 assert.equal(response.description.meta.c, session.getResolvedMeta('c')); 127 128 // We currently only resolve one level of indirection via permanodes. 129 assert.equal(response.description.meta.b, session.getResolvedMeta('d')); 130 131 // e is a permanode, but its camliContent doesn't exist. This is legitimate and can happen for a variety of reasons (e.g., during sync). 132 assert.equal(null, session.getResolvedMeta('e')); 133 134 // z doesn't exist at all. 135 assert.equal(null, session.getResolvedMeta('z')); 136 }); 137 }); 138 139 describe('#getTitle', function() { 140 it('should create correct titles', function() { 141 assert.strictEqual(response.description.meta.a.file.fileName, session.getTitle('a')); 142 assert.strictEqual('', session.getTitle('a2')); 143 assert.strictEqual('permanode b', session.getTitle('b')); 144 assert.strictEqual(response.description.meta.a.file.fileName, session.getTitle('b2')); 145 assert.strictEqual('permanode e', session.getTitle('e')); 146 }); 147 }); 148 });