UNPKG

@platform/cell.schema

Version:

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

202 lines (201 loc) 8.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _1 = require("."); var test_1 = require("../test"); var Uri_1 = require("../Uri"); describe('RefLinks', function () { it('prefix', function () { (0, test_1.expect)(_1.RefLinks.prefix).to.eql('ref'); }); describe('is', function () { it('refKey', function () { var test = function (input, expected) { var res = _1.RefLinks.is.refKey(input); (0, test_1.expect)(res).to.eql(expected); }; test(undefined, false); test('', false); test(' ', false); test('fs:func:wasm', false); test(' ref:ns:foo ', true); test('ref:ns:foo', true); test('ref:cell:foo:A1', true); test('ref:cell:foo:A', true); test('ref:cell:foo:1', true); }); it('refValue', function () { var test = function (input, expected) { var res = _1.RefLinks.is.refValue(input); (0, test_1.expect)(res).to.eql(expected); }; test(undefined, false); test('', false); test(' ', false); test('fs:func:wasm', false); test('file:foo:abc', false); test('ns:foo', true); test(' ns:foo ', true); test('cell:foo:A1', true); test('cell:foo:A', true); test('cell:foo:1', true); test('cell:foo:A1?hash=abc', true); test('cell:foo:A?hash=abc', true); test('cell:foo:1?hash=abc', true); }); }); it('total', function () { var test = function (links, expected) { var res = _1.RefLinks.total(links); (0, test_1.expect)(res).to.eql(expected); }; test(undefined, 0); test({}, 0); test({ foo: 'bar' }, 0); test({ ref: '...' }, 1); test({ foo: 'bar', 'ref:foo': '...' }, 1); test({ foo: 'bar', ref: '...', 'ref:foo': '...', }, 2); }); it('toKey', function () { var test = function (linkName, expected) { var res = _1.RefLinks.toKey(linkName); (0, test_1.expect)(res).to.eql(expected); }; test('foo', 'ref:foo'); test('foo/bar', 'ref:foo::bar'); test('foo.bar', 'ref:foo:bar'); }); it('parseKey', function () { var key = _1.RefLinks.toKey('foo.bar/thing.t'); var res = _1.RefLinks.parseKey(key); (0, test_1.expect)(res.prefix).to.eql('ref'); (0, test_1.expect)(res.key).to.eql('ref:foo:bar::thing:t'); (0, test_1.expect)(res.path).to.eql('foo.bar/thing.t'); (0, test_1.expect)(res.name).to.eql('thing.t'); (0, test_1.expect)(res.dir).to.eql('foo.bar'); (0, test_1.expect)(res.ext).to.eql('t'); }); it('parseKey (no extension)', function () { var key = _1.RefLinks.toKey('foo.bar/thing'); var res = _1.RefLinks.parseKey(key); (0, test_1.expect)(res.prefix).to.eql('ref'); (0, test_1.expect)(res.path).to.eql('foo.bar/thing'); (0, test_1.expect)(res.name).to.eql('thing'); (0, test_1.expect)(res.dir).to.eql('foo.bar'); (0, test_1.expect)(res.ext).to.eql(''); }); it('toValue', function () { var uri = Uri_1.Uri.parse('cell:foo:A1').parts; var res1 = _1.RefLinks.toValue(uri); (0, test_1.expect)(res1).to.eql('cell:foo:A1'); var res2 = _1.RefLinks.toValue(uri, { hash: 'abc' }); (0, test_1.expect)(res2).to.eql('cell:foo:A1?hash=abc'); }); describe('parse (key:value)', function () { it('throw: file URI not provided', function () { var fn = function () { return _1.RefLinks.parse('ref:foo', 'file:foo:123'); }; (0, test_1.expect)(fn).to.throw(); }); it('uri', function () { var test = function (input, expected) { var res = _1.RefLinks.parse('ref:foo', input); (0, test_1.expect)(res.uri.toString()).to.eql(expected); }; test('cell:foo:A1', 'cell:foo:A1'); test('cell:foo:A1?hash=abc', 'cell:foo:A1'); test(' cell:foo:A1?hash=abc ', 'cell:foo:A1'); test('cell:foo:A1?hash=abc&foo=123', 'cell:foo:A1'); }); it('hash', function () { var test = function (input, expected) { var res = _1.RefLinks.parse('ref:foo', input); (0, test_1.expect)(res.query.hash).to.eql(expected); }; test('cell:foo:A1', undefined); test('cell:foo:A1?hash=abc', 'abc'); test(' cell:foo:A1?hash=abc ', 'abc'); test('cell:foo:A1?bam=boo', undefined); test('cell:foo:A1?bam=boo&hash=abc ', 'abc'); }); it('toString', function () { var test = function (input, expected) { var res = _1.RefLinks.parse('ref:foo', input); (0, test_1.expect)(res.toString()).to.eql(expected); }; test('cell:foo:A1', 'cell:foo:A1'); test(' cell:foo:A1 ', 'cell:foo:A1'); test('cell:foo:A1?', 'cell:foo:A1'); test(' cell:foo:A1?hash=abc ', 'cell:foo:A1?hash=abc'); }); it('toString: modify query-string values', function () { var test = function (args, expected) { var key = 'ref:foo'; (0, test_1.expect)(_1.RefLinks.parse(key, 'cell:foo:A1').toString(args)).to.eql(expected); (0, test_1.expect)(_1.RefLinks.parse(key, ' cell:foo:A1 ').toString(args)).to.eql(expected); }; test({}, 'cell:foo:A1'); test({ hash: 'abc' }, 'cell:foo:A1?hash=abc'); }); it('toString: remove query-string values', function () { var test = function (args, expected) { var res = _1.RefLinks.parse('ref:foo', 'cell:foo:A1?hash=abc').toString(args); (0, test_1.expect)(res).to.eql(expected); }; test({}, 'cell:foo:A1?hash=abc'); test({ hash: null }, 'cell:foo:A1'); }); }); describe('find: byName', function () { var LINKS = { 'fs:main:js': 'file:foo:abc123', 'ref:foo::bar::zoo:png': 'cell:foo:A1', 'ref:type': 'ns:foo', }; it('not found', function () { var test = function (path) { var res = _1.RefLinks.find(LINKS).byName(path); (0, test_1.expect)(res).to.eql(undefined); }; test(''); test(undefined); test(''); test(undefined); test('main.js'); test('foo::bar::zoo'); }); it('found', function () { var test = function (path, expected) { var res = _1.RefLinks.find(LINKS).byName(path); (0, test_1.expect)(res === null || res === void 0 ? void 0 : res.value).to.eql(expected, path); }; test('type', 'ns:foo'); test('foo/bar/zoo.png', 'cell:foo:A1'); test('///foo/bar/zoo.png', 'cell:foo:A1'); test('foo/bar*', 'cell:foo:A1'); }); }); describe('list', function () { it('empty', function () { var LINKS = { 'fs:main:js': 'file:foo:abc123', 'foo:bar': 'cell:foo:A1', }; (0, test_1.expect)(_1.RefLinks.toList({})).to.eql([]); (0, test_1.expect)(_1.RefLinks.toList(LINKS)).to.eql([]); }); it('items', function () { var LINKS = { 'fs:main:js': 'file:foo:abc123', 'ref:foo::bar::zoo:png': 'cell:foo:A1', 'ref:type': 'ns:foo', }; var res = _1.RefLinks.toList(LINKS); (0, test_1.expect)(res.length).to.eql(2); (0, test_1.expect)(res[0].path).to.eql('foo/bar/zoo.png'); (0, test_1.expect)(res[1].path).to.eql('type'); }); }); });