UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

245 lines (244 loc) 11.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var test_1 = require("../test"); var _1 = require("."); describe('Links', function () { describe('encoding', function () { it('encodeKey => decodeKey', function () { var test = function (input, encoded) { var res = { encoded: _1.Links.encodeKey(input), decoded: _1.Links.decodeKey(_1.Links.encodeKey(input)), }; (0, test_1.expect)(res.encoded).to.eql(encoded); (0, test_1.expect)(res.decoded).to.eql(input); }; test('foo', 'foo'); test('foo|bar', 'foo|bar'); test('[foo]', '[foo]'); test('.foo', ':foo'); test('[.foo]', '[:foo]'); test('foo.png', 'foo:png'); test('foo.bar.baz', 'foo:bar:baz'); test('foo/bar', 'foo::bar'); test('foo/bar/baz', 'foo::bar::baz'); test('.foo.', ':foo:'); test('..foo...', '[::]foo[:::]'); test('...foo.', '[:::]foo:'); test('...foo.png', '[:::]foo:png'); test('...foo/bar..png', '[:::]foo::bar[::]png'); test('[..]foo[...]', '[[::]]foo[[:::]]'); }); it('toKey (encoded)', function () { var test = function (prefix, input, output) { var res1 = _1.Links.toKey(prefix, input); var res2 = _1.Links.create(prefix).toKey(input); (0, test_1.expect)(res1).to.eql(output); (0, test_1.expect)(res2).to.eql(output); }; test('ref', 'foo', 'ref:foo'); test('fs', 'foo', 'fs:foo'); test('fs:', 'foo', 'fs:foo'); test('fs:::', 'foo', 'fs:foo'); test(' fs ', 'foo', 'fs:foo'); test(' fs:: ', 'foo', 'fs:foo'); test('fs', 'foo.png', 'fs:foo:png'); test('fs', '/foo.png', 'fs:foo:png'); test('fs', '//foo.png', 'fs:foo:png'); test('fs', 'fs.foo.png', 'fs:fs:foo:png'); test('fs', 'cat&bird.png', 'fs:cat&bird:png'); test('fs', 'foo/bar.png', 'fs:foo::bar:png'); test('fs', 'foo/bar/zoo.png', 'fs:foo::bar::zoo:png'); test('fs', '/foo/bar.png', 'fs:foo::bar:png'); test('fs', '///foo/bar.png', 'fs:foo::bar:png'); test('fs', 'foo/bar/', 'fs:foo::bar'); test('fs', 'foo/bar.png/', 'fs:foo::bar:png'); }); it('toKey: throw if empty', function () { var test = function (input) { var fn = function () { return _1.Links.toKey('prefix', input); }; (0, test_1.expect)(fn).to.throw(/Link key must have a value/); }; test(); test(''); test(' '); }); }); it('isKey', function () { var test = function (prefix, key, expected) { var res1 = _1.Links.isKey(prefix, key); var res2 = _1.Links.create(prefix).isKey(key); (0, test_1.expect)(res1).to.eql(expected); (0, test_1.expect)(res2).to.eql(expected); }; test('ref', undefined, false); test('ref', '', false); test('ref', ' ', false); test('ref', 'ref:func:wasm', true); test('fs', ' fs:func:wasm ', true); }); it('total', function () { var test = function (prefix, links, expected) { var res1 = _1.Links.total(prefix, links); var res2 = _1.Links.create(prefix).total(links); (0, test_1.expect)(res1).to.eql(expected); (0, test_1.expect)(res2).to.eql(expected); }; test('ref', undefined, 0); test('ref', {}, 0); test('ref', { foo: 'bar' }, 0); test('ref', { 'ref:foo:png': '...' }, 1); test('ref', { foo: 'bar', 'ref:foo:png': '...' }, 1); test('ref', { foo: 'bar', 'ref:file1:png': '...', 'ref:file2:jpg': '...', }, 2); }); describe('parseKey', function () { it('name', function () { var prefix = 'foo'; var key = _1.Links.toKey(prefix, 'image.png'); var res = _1.Links.parseKey(prefix, " ".concat(key, " ")); (0, test_1.expect)(res.prefix).to.eql(prefix); (0, test_1.expect)(res.key).to.eql(key); (0, test_1.expect)(res.path).to.eql('image.png'); (0, test_1.expect)(res.name).to.eql('image.png'); (0, test_1.expect)(res.dir).to.eql(''); (0, test_1.expect)(res.ext).to.eql('png'); }); it('path: dir/name', function () { var prefix = 'foo'; var key = _1.Links.toKey(prefix, '///foo/bar/image.png'); var res = _1.Links.parseKey(prefix, " ".concat(key, " ")); (0, test_1.expect)(res.key).to.eql(key); (0, test_1.expect)(res.path).to.eql('foo/bar/image.png'); (0, test_1.expect)(res.name).to.eql('image.png'); (0, test_1.expect)(res.dir).to.eql('foo/bar'); (0, test_1.expect)(res.ext).to.eql('png'); }); it('path variants', function () { var test = function (prefix, input, path) { var res = _1.Links.create(prefix).parseKey(input); (0, test_1.expect)(res.key).to.eql(input.trim()); (0, test_1.expect)(res.path).to.eql(path); (0, test_1.expect)(res.name).to.eql(test_1.fs.basename(res.path)); (0, test_1.expect)(res.dir).to.eql(test_1.fs.dirname(res.path).replace(/^\./, '')); (0, test_1.expect)(res.ext).to.eql(test_1.fs.extname(res.path).replace(/^\./, '')); }; test('ref', 'ref:foo', 'foo'); test('fs', 'fs:foo:png', 'foo.png'); test('fs', 'fs:fs:foo:png', 'fs.foo.png'); test('fs', 'fs:foo::bar:png', 'foo/bar.png'); test('fs', 'fs:foo::bar::zoo:png', 'foo/bar/zoo.png'); test('fs', 'fs:[::]foo:png', '..foo.png'); test('fs', 'fs:foo[::]png', 'foo..png'); }); }); describe('parseValue', function () { it('uri', function () { var res = _1.Links.parseValue(' cell:foo:A1 ? '); (0, test_1.expect)(res.value).to.eql('cell:foo:A1'); (0, test_1.expect)(res.uri.type).to.eql('CELL'); (0, test_1.expect)(res.uri.ns).to.eql('foo'); (0, test_1.expect)(res.uri.key).to.eql('A1'); (0, test_1.expect)(res.query).to.eql({}); }); it('query', function () { var res1 = _1.Links.parseValue('cell:foo:A1'); (0, test_1.expect)(res1.query).to.eql({}); var res2 = _1.Links.parseValue(' cell:foo:A1 ? '); (0, test_1.expect)(res2.query).to.eql({}); var res3 = _1.Links.parseValue('cell:foo:A1?color=red&isEnabled=true'); var res4 = _1.Links.parseValue(' cell:foo:A1 ? color=red&isEnabled=true '); (0, test_1.expect)(res3.query).to.eql({ color: 'red', isEnabled: true }); (0, test_1.expect)(res4.query).to.eql(res3.query); }); }); describe('parse (key:value)', function () { it('combines [parseKey] and [parseValue]', function () { var prefix = 'p'; var linkKey = 'p:bar/baz.t'; var linkValue = 'cell:foo:A1?hash=abc'; var key = _1.Links.parseKey(prefix, linkKey); var value = _1.Links.parseValue(linkValue); var res1 = _1.Links.parseLink(prefix, linkKey, linkValue); var res2 = _1.Links.create(prefix).parse(linkKey, linkValue); (0, test_1.expect)(JSON.stringify(res1)).to.eql(JSON.stringify(res2)); (0, test_1.expect)(JSON.stringify(res1)).to.eql(JSON.stringify(tslib_1.__assign(tslib_1.__assign({}, key), value))); }); }); describe('toList', function () { it('empty', function () { var test = function (links) { var res1 = _1.Links.toList('prefix', links); var res2 = _1.Links.create('prefix').toList(links); (0, test_1.expect)(res1).to.eql([]); (0, test_1.expect)(res2).to.eql([]); }; test(); test({}); test({ 'SOMETHING:ELSE': 'foo' }); }); it('converts to list', function () { var keys = { 'fs:main:js': 'file:foo:abc123?status=uploading', 'fs:images/foo/kitten:png': 'file:foo:def456?hash=sha256-abc', 'NO:MATCH': '<something>', }; var list = _1.Links.create('fs').toList(keys); (0, test_1.expect)(list.length).to.eql(2); (0, test_1.expect)(list[0].key).to.eql('fs:main:js'); (0, test_1.expect)(list[0].value).to.eql('file:foo:abc123?status=uploading'); (0, test_1.expect)(list[1].key).to.eql('fs:images/foo/kitten:png'); (0, test_1.expect)(list[1].value).to.eql('file:foo:def456?hash=sha256-abc'); }); }); describe('find: byName', function () { var LINKS = { 'fs:main:js': 'file:foo:abc', 'ref:foo::bar::zoo:png': 'cell:foo:A1', 'ref:foo::bar::baz:jpg': 'cell:foo:A2', }; it('not found', function () { var test = function (prefix, path) { var res1 = _1.Links.find(prefix, LINKS).byName(path); var res2 = _1.Links.create(prefix).find(LINKS).byName(path); (0, test_1.expect)(res1).to.eql(undefined); (0, test_1.expect)(res2).to.eql(undefined); }; test('', ''); test('', undefined); test('fs', ''); test('fs', undefined); test('fs', 'main.js/'); test('ref', 'foo::bar::zoo:png'); }); it('found', function () { var test = function (prefix, path, expected) { var res1 = _1.Links.find(prefix, LINKS).byName(path); var res2 = _1.Links.create(prefix).find(LINKS).byName(path); (0, test_1.expect)(res1 === null || res1 === void 0 ? void 0 : res1.value).to.eql(expected, path); (0, test_1.expect)(res2 === null || res2 === void 0 ? void 0 : res2.value).to.eql(expected, path); }; test('fs', 'main.js', 'file:foo:abc'); test('fs', '/main.js', 'file:foo:abc'); test('ref', 'foo/bar/zoo.png', 'cell:foo:A1'); test('ref', '///foo/bar/zoo.png', 'cell:foo:A1'); test('fs', '*.js', 'file:foo:abc'); test('ref', 'foo/bar*', 'cell:foo:A1'); test('ref', 'foo/bar*.jpg', 'cell:foo:A2'); }); }); describe('error', function () { it('toKey: throw if contains ":"', function () { var fn = function () { return _1.Links.toKey('foo', 'foo:bar.png'); }; (0, test_1.expect)(fn).to.throw(/cannot contain ":" character/); }); it('encode: throw if contains ":"', function () { var fn = function () { return _1.Links.encodeKey('foo:bar.png'); }; (0, test_1.expect)(fn).to.throw(/cannot contain ":" character/); }); }); });