|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +import { expect } from 'aegir/utils/chai.js' |
| 4 | +import { Key } from 'interface-datastore/key' |
| 5 | + |
| 6 | +const pathSep = '/' |
| 7 | + |
| 8 | +describe('Key', () => { |
| 9 | + /** |
| 10 | + * @param {string} s |
| 11 | + */ |
| 12 | + const clean = (s) => { |
| 13 | + let fixed = s |
| 14 | + if (fixed.startsWith(pathSep + pathSep)) { |
| 15 | + fixed = fixed.slice(1) |
| 16 | + } |
| 17 | + if (fixed.length > 1 && fixed.endsWith(pathSep)) { |
| 18 | + fixed = fixed.slice(0, -1) |
| 19 | + } |
| 20 | + |
| 21 | + return fixed |
| 22 | + } |
| 23 | + |
| 24 | + describe('basic', () => { |
| 25 | + /** |
| 26 | + * @param {string} s |
| 27 | + */ |
| 28 | + const validKey = (s) => it(s, () => { |
| 29 | + const fixed = clean(pathSep + s) |
| 30 | + const namespaces = fixed.split(pathSep).slice(1) |
| 31 | + const lastNamespace = namespaces[namespaces.length - 1] |
| 32 | + const lnparts = lastNamespace.split(':') |
| 33 | + let ktype = '' |
| 34 | + if (lnparts.length > 1) { |
| 35 | + ktype = lnparts.slice(0, -1).join(':') |
| 36 | + } |
| 37 | + const kname = lnparts[lnparts.length - 1] |
| 38 | + const kchild = clean(fixed + '/cchildd') |
| 39 | + const kparent = pathSep + namespaces.slice(0, -1).join(pathSep) |
| 40 | + const kpath = clean(kparent + pathSep + ktype) |
| 41 | + const kinstance = fixed + ':inst' |
| 42 | + |
| 43 | + const k = new Key(s) |
| 44 | + expect(k.toString()).to.eql(fixed) |
| 45 | + expect(k).to.eql(new Key(s)) |
| 46 | + expect(k.toString()).to.eql(new Key(s).toString()) |
| 47 | + expect(k.name()).to.eql(kname) |
| 48 | + expect(k.type()).to.eql(ktype) |
| 49 | + expect(k.path().toString()).to.eql(kpath) |
| 50 | + expect(k.instance('inst').toString()).to.eql(kinstance) |
| 51 | + |
| 52 | + const child = new Key('cchildd') |
| 53 | + expect(k.child(child).toString()).to.eql(kchild) |
| 54 | + expect(k.child(child).parent().toString()).to.eql(fixed) |
| 55 | + expect(k.parent().toString()).to.eql(kparent) |
| 56 | + expect(k.list()).to.have.length(namespaces.length) |
| 57 | + expect(k.namespaces()).to.have.length(namespaces.length) |
| 58 | + k.list().forEach((e, i) => { |
| 59 | + expect(namespaces[i]).to.eql(e) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + validKey('') |
| 64 | + validKey('abcde') |
| 65 | + validKey('disahfidsalfhduisaufidsail') |
| 66 | + validKey('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/') |
| 67 | + validKey('4215432143214321432143214321') |
| 68 | + validKey('a/b/c/d/') |
| 69 | + validKey('abcde:fdsfd') |
| 70 | + validKey('disahfidsalfhduisaufidsail:fdsa') |
| 71 | + validKey('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/:') |
| 72 | + validKey('4215432143214321432143214321:') |
| 73 | + }) |
| 74 | + |
| 75 | + it('ancestry', () => { |
| 76 | + const k1 = new Key('/A/B/C') |
| 77 | + const k2 = new Key('/A/B/C/D') |
| 78 | + |
| 79 | + expect(k1.toString()).to.be.eql('/A/B/C') |
| 80 | + expect(k2.toString()).to.be.eql('/A/B/C/D') |
| 81 | + |
| 82 | + const checks = [ |
| 83 | + k1.isAncestorOf(k2), |
| 84 | + k2.isDecendantOf(k1), |
| 85 | + new Key('/A').isAncestorOf(k2), |
| 86 | + new Key('/A').isAncestorOf(k1), |
| 87 | + !new Key('/A').isDecendantOf(k2), |
| 88 | + !new Key('/A').isDecendantOf(k1), |
| 89 | + k2.isDecendantOf(new Key('/A')), |
| 90 | + k1.isDecendantOf(new Key('/A')), |
| 91 | + !k2.isAncestorOf(new Key('/A')), |
| 92 | + !k1.isAncestorOf(new Key('/A')), |
| 93 | + !k2.isAncestorOf(k2), |
| 94 | + !k1.isAncestorOf(k1) |
| 95 | + ] |
| 96 | + |
| 97 | + checks.forEach((check) => expect(check).to.equal(true)) |
| 98 | + |
| 99 | + expect(k1.child(new Key('D')).toString()).to.eql(k2.toString()) |
| 100 | + expect(k1.toString()).to.eql(k2.parent().toString()) |
| 101 | + expect(k1.path().toString()).to.eql(k2.parent().path().toString()) |
| 102 | + }) |
| 103 | + |
| 104 | + it('type', () => { |
| 105 | + const k1 = new Key('/A/B/C:c') |
| 106 | + const k2 = new Key('/A/B/C:c/D:d') |
| 107 | + |
| 108 | + expect(k1.isAncestorOf(k2)).to.eql(true) |
| 109 | + expect(k2.isDecendantOf(k1)).to.eql(true) |
| 110 | + |
| 111 | + expect(k1.type()).to.eql('C') |
| 112 | + expect(k2.type()).to.eql('D') |
| 113 | + expect(k1.type()).to.eql(k2.parent().type()) |
| 114 | + }) |
| 115 | + |
| 116 | + it('random', () => { |
| 117 | + /** @type {Record<string, boolean>} */ |
| 118 | + const keys = {} |
| 119 | + const k = 100 |
| 120 | + for (let i = 0; i < k; i++) { |
| 121 | + const r = Key.random() |
| 122 | + expect(keys).to.not.have.key(r.toString()) |
| 123 | + keys[r.toString()] = true |
| 124 | + } |
| 125 | + |
| 126 | + expect(Object.keys(keys)).to.have.length(k) |
| 127 | + }) |
| 128 | + |
| 129 | + it('less', () => { |
| 130 | + /** |
| 131 | + * @param {string | Uint8Array} a |
| 132 | + * @param {string | Uint8Array} b |
| 133 | + */ |
| 134 | + const checkLess = (a, b) => { |
| 135 | + const ak = new Key(a) |
| 136 | + const bk = new Key(b) |
| 137 | + |
| 138 | + expect(ak.less(bk)).to.eql(true) |
| 139 | + expect(bk.less(ak)).to.eql(false) |
| 140 | + } |
| 141 | + |
| 142 | + checkLess('/a/b/c', '/a/b/c/d') |
| 143 | + checkLess('/a/b', '/a/b/c/d') |
| 144 | + checkLess('/a', '/a/b/c/d') |
| 145 | + checkLess('/a/a/c', '/a/b/c') |
| 146 | + checkLess('/a/a/d', '/a/b/c') |
| 147 | + checkLess('/a/b/c/d/e/f/g/h', '/b') |
| 148 | + checkLess(pathSep, '/a') |
| 149 | + }) |
| 150 | + |
| 151 | + it('concat', () => { |
| 152 | + const originalKey = new Key('/a/b/c') |
| 153 | + |
| 154 | + const concattedKey = originalKey.concat(new Key('/d/e/f')) |
| 155 | + expect(concattedKey.toString()).to.equal('/a/b/c/d/e/f') |
| 156 | + |
| 157 | + // Original key is not changed |
| 158 | + expect(originalKey.toString()).to.equal('/a/b/c') |
| 159 | + |
| 160 | + const concattedMultipleKeys = originalKey.concat(new Key('/d/e'), new Key('/f/g')) |
| 161 | + expect(concattedMultipleKeys.toString()).to.equal('/a/b/c/d/e/f/g') |
| 162 | + |
| 163 | + // New instance of Key is always created |
| 164 | + expect(originalKey.concat()).to.not.equal(originalKey) |
| 165 | + // but has the same value |
| 166 | + expect(originalKey.concat().toString()).to.equal('/a/b/c') |
| 167 | + }) |
| 168 | + |
| 169 | + it('uint8Array', () => { |
| 170 | + const arr = Uint8Array.from(['/'.charCodeAt(0), 0, 1, 2, 3]) |
| 171 | + const key = new Key(arr) |
| 172 | + const buf = key.uint8Array() |
| 173 | + |
| 174 | + expect(buf).to.deep.equal(arr) |
| 175 | + }) |
| 176 | + |
| 177 | + it('uint8Array with surplus bytes', () => { |
| 178 | + const arr = Uint8Array.from(['/'.charCodeAt(0), 0, 1, 2, 3, 4]) |
| 179 | + const view = new Uint8Array(arr.buffer, 0, arr.length - 1) |
| 180 | + |
| 181 | + // should be same buffer |
| 182 | + expect(view.buffer).to.equal(arr.buffer) |
| 183 | + expect(view.buffer.byteLength).to.equal(arr.buffer.byteLength) |
| 184 | + |
| 185 | + // view should be shorter than wrapped buffer |
| 186 | + expect(view.length).to.be.lessThan(arr.buffer.byteLength) |
| 187 | + expect(view.byteLength).to.be.lessThan(arr.buffer.byteLength) |
| 188 | + |
| 189 | + const key = new Key(view) |
| 190 | + const buf = key.uint8Array() |
| 191 | + |
| 192 | + expect(buf).to.deep.equal(view) |
| 193 | + }) |
| 194 | + |
| 195 | + it('uint8Array with trailing slashes', () => { |
| 196 | + const slash = '/'.charCodeAt(0) |
| 197 | + const arrWithSlashes = Uint8Array.from([slash, 0, 1, 2, 3, slash, slash, slash]) |
| 198 | + const arrWithoutSlashes = Uint8Array.from([slash, 0, 1, 2, 3]) |
| 199 | + const key = new Key(arrWithSlashes) |
| 200 | + const buf = key.uint8Array() |
| 201 | + |
| 202 | + // slashes should have been stripped |
| 203 | + expect(buf).to.deep.equal(arrWithoutSlashes) |
| 204 | + |
| 205 | + // should be a view on the original buffer |
| 206 | + expect(buf.buffer).to.equal(arrWithSlashes.buffer) |
| 207 | + }) |
| 208 | +}) |
0 commit comments