UNPKG

@platform/cell.schema

Version:

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

277 lines (276 loc) 12.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var test_1 = require("../test"); var _1 = require("."); describe('FileLinks', function () { it('prefix', function () { (0, test_1.expect)(_1.FileLinks.prefix).to.eql('fs'); }); it('total', function () { var test = function (links, expected) { var res = _1.FileLinks.total(links); (0, test_1.expect)(res).to.eql(expected); }; test(undefined, 0); test({}, 0); test({ foo: 'bar' }, 0); test({ 'fs:foo:png': '...' }, 1); test({ foo: 'bar', 'fs:foo:png': '...' }, 1); test({ foo: 'bar', 'fs:file1:png': '...', 'fs:file2:jpg': '...', }, 2); }); describe('is', function () { it('fileKey', function () { var test = function (input, expected) { var res = _1.FileLinks.is.fileKey(input); (0, test_1.expect)(res).to.eql(expected); }; test(undefined, false); test('', false); test(' ', false); test('ref:ns:foo', false); test('ref:cell:foo:A1', false); test('fs:func:wasm', true); test(' fs:func:wasm ', true); }); it('fileValue', function () { var test = function (input, expected) { var res = _1.FileLinks.is.fileValue(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', true); }); it('fileUploading', function () { var test = function (key, expected) { var res = _1.FileLinks.is.fileUploading(key); (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('file:foo:abc?status=', false); test('file:foo:abc?status=derp', false); test(' file:foo:abc?status=uploading ', true); test('file:foo:abc?something=true&status=uploading ', true); }); }); describe('encoding', function () { it('toKey (encoded)', function () { var test = function (input, output) { var res = _1.FileLinks.toKey(input); (0, test_1.expect)(res).to.eql(output); }; test('foo', 'fs:foo'); test('foo.png', 'fs:foo:png'); test('/foo.png', 'fs:foo:png'); test('//foo.png', 'fs:foo:png'); test('fs.foo.png', 'fs:fs:foo:png'); test('cat&bird.png', 'fs:cat&bird:png'); test('foo/bar.png', 'fs:foo::bar:png'); test('foo/bar/zoo.png', 'fs:foo::bar::zoo:png'); test('/foo/bar.png', 'fs:foo::bar:png'); test('///foo/bar.png', 'fs:foo::bar:png'); test('foo/bar/', 'fs:foo::bar'); test('foo/bar.png/', 'fs:foo::bar:png'); }); }); describe('parse (key:value)', function () { it('throw: file URI not provided', function () { (0, test_1.expect)(function () { return _1.FileLinks.parseValue('cell:foo:A1'); }).to.throw(); (0, test_1.expect)(function () { return _1.FileLinks.parseValue('ns:foo'); }).to.throw(); (0, test_1.expect)(function () { return _1.FileLinks.parseValue('DERP'); }).to.throw(); }); it('uri', function () { var test = function (input, expected) { var res = _1.FileLinks.parseValue(input); (0, test_1.expect)(res.uri.toString()).to.eql(expected); }; test('file:foo:123', 'file:foo:123'); test('file:foo:123?hash=abc', 'file:foo:123'); test(' file:foo:123?hash=abc ', 'file:foo:123'); test('file:foo:123?bam=boo&hash=abc ', 'file:foo:123'); }); it('hash', function () { var test = function (input, expected) { var res = _1.FileLinks.parseValue(input); (0, test_1.expect)(res.query.hash).to.eql(expected); }; test('file:foo:123', undefined); test('file:foo:123?hash=abc', 'abc'); test(' file:foo:123?hash=abc ', 'abc'); test('file:foo:123?bam=boo', undefined); test('file:foo:123?bam=boo&hash=abc ', 'abc'); }); it('status', function () { var test = function (input, expected) { var res = _1.FileLinks.parseValue(input); (0, test_1.expect)(res.query.status).to.eql(expected); }; test('file:foo:123', undefined); test('file:foo:123?hash=abc', undefined); test(' file:foo:123?hash=abc&status=uploading ', 'uploading'); test('file:foo:123?hash=abc&status=foo', 'foo'); }); it('toString', function () { var test = function (input, expected) { var res = _1.FileLinks.parseValue(input); (0, test_1.expect)(res.toString()).to.eql(expected); }; test('file:foo:123', 'file:foo:123'); test(' file:foo:123 ', 'file:foo:123'); test('file:foo:123?', 'file:foo:123'); test(' file:foo:123?hash=abc ', 'file:foo:123?hash=abc'); test(' file:foo:123?status=uploading ', 'file:foo:123?status=uploading'); test(' file:foo:123?hash=abc&status=uploading ', 'file:foo:123?status=uploading&hash=abc'); }); it('toString: modify query-string values', function () { var test = function (args, expected) { (0, test_1.expect)(_1.FileLinks.parseValue('file:foo:123').toString(args)).to.eql(expected); (0, test_1.expect)(_1.FileLinks.parseValue(' file:foo:123 ').toString(args)).to.eql(expected); }; test({}, 'file:foo:123'); test({ hash: 'abc' }, 'file:foo:123?hash=abc'); test({ status: 'uploading' }, 'file:foo:123?status=uploading'); test({ hash: 'abc', status: 'uploading' }, 'file:foo:123?status=uploading&hash=abc'); }); it('toString: remove query-string values', function () { var test = function (args, expected) { var res = _1.FileLinks.parseValue('file:foo:123?status=uploading&hash=abc').toString(args); (0, test_1.expect)(res).to.eql(expected); }; test({}, 'file:foo:123?status=uploading&hash=abc'); test({ status: null }, 'file:foo:123?hash=abc'); test({ hash: null }, 'file:foo:123?status=uploading'); test({ hash: null, status: null }, 'file:foo:123'); }); }); describe('parseKey', function () { it('name', function () { var key = _1.FileLinks.toKey('image.png'); var res = _1.FileLinks.parseKey(" ".concat(key, " ")); (0, test_1.expect)(res.prefix).to.eql('fs'); (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 key = _1.FileLinks.toKey('///foo/bar/image.png'); var res = _1.FileLinks.parseKey(" ".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 (input, path) { var res = _1.FileLinks.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('fs:foo', 'foo'); test('fs:foo:png', 'foo.png'); test('fs:fs:foo:png', 'fs.foo.png'); test('fs:foo::bar:png', 'foo/bar.png'); test('fs:foo::bar::zoo:png', 'foo/bar/zoo.png'); test('fs:[::]foo:png', '..foo.png'); test('fs:foo[::]png', 'foo..png'); }); }); describe('toList', function () { it('empty', function () { var test = function (keys) { var res = _1.FileLinks.toList(keys); (0, test_1.expect)(res).to.eql([]); }; test(); test({}); test({ 'SOMETHING:ELSE': 'foo' }); }); it('converts to list', function () { var LINKS = { 'fs:main:js': 'file:foo:abc123?status=uploading', 'fs:images/foo/kitten:png': 'file:foo:def456?hash=sha256-abc', }; var list = _1.FileLinks.toList(LINKS); (0, test_1.expect)(list.length).to.eql(2); (0, test_1.expect)(list[0].uri.toString()).to.eql('file:foo:abc123'); (0, test_1.expect)(list[0].query.hash).to.eql(undefined); (0, test_1.expect)(list[0].query.status).to.eql('uploading'); (0, test_1.expect)(list[0].path).to.eql('main.js'); (0, test_1.expect)(list[0].dir).to.eql(''); (0, test_1.expect)(list[0].name).to.eql('main.js'); (0, test_1.expect)(list[0].ext).to.eql('js'); (0, test_1.expect)(list[1].uri.toString()).to.eql('file:foo:def456'); (0, test_1.expect)(list[1].query.hash).to.eql('sha256-abc'); (0, test_1.expect)(list[1].query.status).to.eql(undefined); (0, test_1.expect)(list[1].path).to.eql('images/foo/kitten.png'); (0, test_1.expect)(list[1].dir).to.eql('images/foo'); (0, test_1.expect)(list[1].name).to.eql('kitten.png'); (0, test_1.expect)(list[1].ext).to.eql('png'); }); }); describe('find: byName', function () { var LINKS = { 'fs:main:js': 'file:foo:abc123?status=uploading', 'fs:images/foo/kitten:png': 'file:foo:def456?hash=sha256-abc', 'ref:foo': 'cell:foo:A1', }; it('no match', function () { var test = function (keys, path) { var res = _1.FileLinks.find(keys).byName(path); (0, test_1.expect)(res).to.eql(undefined); }; test(undefined, 'main.js'); test(undefined, undefined); test({}, 'main.js'); test(LINKS, 'no-match.pdf'); test(LINKS, ''); test(LINKS, ' '); test(LINKS, undefined); test(LINKS, 'main.js/'); }); it('match: main.js', function () { var test = function (path) { var res = _1.FileLinks.find(LINKS).byName(path); (0, test_1.expect)(res === null || res === void 0 ? void 0 : res.uri.toString()).to.eql('file:foo:abc123'); (0, test_1.expect)(res === null || res === void 0 ? void 0 : res.path).to.eql('main.js'); }; test('main.js'); test(' main.js '); test('/main.js'); test(' //main.js '); }); it('match: images/foo/kitten.png', function () { var test = function (path) { var res = _1.FileLinks.find(LINKS).byName(path); (0, test_1.expect)(res === null || res === void 0 ? void 0 : res.uri.toString()).to.eql('file:foo:def456'); (0, test_1.expect)(res === null || res === void 0 ? void 0 : res.path).to.eql('images/foo/kitten.png'); }; test('images/foo/kitten.png'); test(' images/foo/kitten.png '); test('/images/foo/kitten.png'); test('///images/foo/kitten.png'); }); }); describe('error', function () { it('toKey: throw if contains ":"', function () { var fn = function () { return _1.FileLinks.toKey('foo:bar.png'); }; (0, test_1.expect)(fn).to.throw(/cannot contain ":" character/); }); }); });