UNPKG

@platform/cell.schema

Version:

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

396 lines (395 loc) 21.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var test_1 = require("../test"); var _1 = require("."); var Uri_1 = require("../Uri"); describe('Urls', function () { describe('static', function () { it('Url.uri', function () { (0, test_1.expect)(_1.Urls.Uri).to.equal(Uri_1.Uri); }); }); describe('fields', function () { it('parse (protocol, host, port => origin)', function () { var test = function (input, host, port, protocol, origin) { var res1 = _1.Urls.parse(input); var res2 = _1.Urls.create(input); var hostname = host.replace(/:\d*$/, ''); (0, test_1.expect)(res1.origin.protocol).to.eql(protocol); (0, test_1.expect)(res1.origin.hostname).to.eql(hostname); (0, test_1.expect)(res1.origin.host).to.eql(host); (0, test_1.expect)(res1.origin.port).to.eql(port); (0, test_1.expect)(res1.origin.toString()).to.eql(origin); (0, test_1.expect)(res1.origin.protocol).to.eql(res2.protocol); (0, test_1.expect)(res1.origin.hostname).to.eql(res2.hostname); (0, test_1.expect)(res1.origin.host).to.eql(res2.host); (0, test_1.expect)(res1.origin.port).to.eql(res2.port); (0, test_1.expect)(res1.origin.toString()).to.eql(res2.origin); }; test('foo.com:1234', 'foo.com:1234', 1234, 'https', 'https://foo.com:1234'); test('foo.com', 'foo.com', 80, 'https', 'https://foo.com'); test('foo.com///', 'foo.com', 80, 'https', 'https://foo.com'); test('http://foo.com', 'foo.com', 80, 'https', 'https://foo.com'); test('https://foo.com/', 'foo.com', 80, 'https', 'https://foo.com'); test('foo.com:8080', 'foo.com:8080', 8080, 'https', 'https://foo.com:8080'); test('localhost.foo.com', 'localhost.foo.com', 80, 'https', 'https://localhost.foo.com'); test(undefined, 'localhost', 80, 'http', 'http://localhost'); test('', 'localhost', 80, 'http', 'http://localhost'); test(' ', 'localhost', 80, 'http', 'http://localhost'); test('1234', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test(1234, 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test(80, 'localhost', 80, 'http', 'http://localhost'); test('80', 'localhost', 80, 'http', 'http://localhost'); test('localhost', 'localhost', 80, 'http', 'http://localhost'); test('localhost:1234', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test('localhost/', 'localhost', 80, 'http', 'http://localhost'); test('http://localhost', 'localhost', 80, 'http', 'http://localhost'); test('https://localhost', 'localhost', 80, 'http', 'http://localhost'); test('https://localhost//', 'localhost', 80, 'http', 'http://localhost'); test('https://localhost:1234', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test('https://localhost:1234//', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); }); }); describe('sys', function () { var url = _1.Urls.create(); it('info', function () { var res = url.sys.info; (0, test_1.expect)(res.toString()).to.eql('http://localhost/.sys'); }); it('uid', function () { var res = url.sys.uid; (0, test_1.expect)(res.toString()).to.eql('http://localhost/.uid'); (0, test_1.expect)(res.query({ total: 2 }).toString()).to.eql('http://localhost/.uid?total=2'); }); }); describe('local', function () { var url = _1.Urls.create(); it('fs', function () { var res = url.local.fs; (0, test_1.expect)(res.toString()).to.eql('http://localhost/local/fs'); }); }); describe('namespace (ns)', function () { var URI = 'ns:foo'; var url = _1.Urls.create(); it('uri', function () { var res = url.ns(URI); (0, test_1.expect)(res.uri).to.eql(URI); }); it('uri (from raw namespace id)', function () { var res = url.ns('foo'); (0, test_1.expect)(res.uri).to.eql('ns:foo'); }); it('throw if non-namespace URI passed', function () { (0, test_1.expect)(function () { return url.ns('foo:bar'); }).to.throw(); (0, test_1.expect)(function () { return url.ns('cell:foo'); }).to.throw(); }); it('info', function () { var res1 = url.ns('foo').info; var res2 = url.ns('ns:foo').info; var res3 = url.ns('cell:foo:A1').info; var res4 = url.ns({ id: 'foo', type: 'NS' }).info; var URL = 'http://localhost/ns:foo'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); (0, test_1.expect)(res3.toString()).to.eql(URL); (0, test_1.expect)(res4.toString()).to.eql(URL); }); it('info (with query)', function () { var ns = url.ns('foo'); var res1 = ns.info.query({ cells: true }); (0, test_1.expect)(res1.toString()).to.eql('http://localhost/ns:foo?cells=true'); var res2 = ns.info.query({ cells: ['A1', 'B2:Z9'] }); (0, test_1.expect)(res2.toString()).to.eql('http://localhost/ns:foo?cells=A1&cells=B2:Z9'); var res3 = ns.info.query({ rows: '1:9' }); (0, test_1.expect)(res3.toString()).to.eql('http://localhost/ns:foo?rows=1:9'); var res4 = ns.info.query({ columns: 'A:Z' }); (0, test_1.expect)(res4.toString()).to.eql('http://localhost/ns:foo?columns=A:Z'); var res5 = ns.info.query({ files: true }); (0, test_1.expect)(res5.toString()).to.eql('http://localhost/ns:foo?files=true'); var res6 = ns.info.query({ total: true }); (0, test_1.expect)(res6.toString()).to.eql('http://localhost/ns:foo?total=true'); var res7 = ns.info.query({ total: 'rows' }); (0, test_1.expect)(res7.toString()).to.eql('http://localhost/ns:foo?total=rows'); var res8 = ns.info.query({ total: ['rows', 'columns', 'rows'] }); (0, test_1.expect)(res8.toString()).to.eql('http://localhost/ns:foo?total=rows&total=columns'); var res9 = ns.info.query({ total: ['rows', 'files'] }); (0, test_1.expect)(res9.toString()).to.eql('http://localhost/ns:foo?total=rows&total=files'); }); }); describe('cell', function () { var URI = 'cell:foo:A1'; var url = _1.Urls.create(); it('uri', function () { var res = url.cell(URI); (0, test_1.expect)(res.uri).to.eql(URI); }); it('throw if non-cell URI passed', function () { (0, test_1.expect)(function () { return url.cell('foo:bar'); }).to.throw(); (0, test_1.expect)(function () { return url.cell('ns:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.cell('cell:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.cell('cell:foo:A'); }).to.throw(); (0, test_1.expect)(function () { return url.cell('cell:foo:1'); }).to.throw(); }); it('info', function () { var res1 = url.cell(URI).info; var res2 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).info; var URL = 'http://localhost/cell:foo:A1'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); describe('fs (files)', function () { it('fs.list', function () { var res1 = url.cell(URI).files.list; var res2 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).files.list; var URL = 'http://localhost/cell:foo:A1/fs'; (0, test_1.expect)(res1.query({}).toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('fs.list (filter)', function () { var URL = 'http://localhost/cell:foo:A1/fs'; var list = url.cell(URI).files.list; (0, test_1.expect)(list.toString()).to.eql(URL); var res = list.query({ filter: 'foo/bar' }); (0, test_1.expect)(res.query({ filter: 'foo/bar' }).toString()).to.eql(URL + '?filter=foo/bar'); (0, test_1.expect)(res.query({ filter: '/foo/bar' }).toString()).to.eql(URL + '?filter=/foo/bar'); }); it('fs.delete', function () { var res1 = url.cell(URI).files.delete; var res2 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).files.delete; var URL = 'http://localhost/cell:foo:A1/fs'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('fs.copy', function () { var res1 = url.cell(URI).files.copy; var res2 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).files.copy; var URL = 'http://localhost/cell:foo:A1/fs:copy'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('fs.upload (start)', function () { var res1 = url.cell(URI).files.upload; var res2 = res1.query({ changes: true }); var res3 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).files.upload; var URL = 'http://localhost/cell:foo:A1/fs:upload'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql("".concat(URL, "?changes=true")); (0, test_1.expect)(res3.toString()).to.eql(URL); }); it('fs.uploaded (complete)', function () { var res1 = url.cell(URI).files.uploaded; var res2 = res1.query({ changes: true }); var res3 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).files.uploaded; var URL = 'http://localhost/cell:foo:A1/fs:uploaded'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql("".concat(URL, "?changes=true")); (0, test_1.expect)(res3.toString()).to.eql(URL); }); }); describe('fs (file)', function () { it('file.toString()', function () { var file = url.cell(URI).file; (0, test_1.expect)(file.toString()).to.eql('/cell:foo:A1/fs/'); }); it('file.byName', function () { var res1 = url.cell(URI).file.byName(' kitten.png '); var res2 = url.cell(Uri_1.Uri.cell('cell:foo:A1')).file.byName('kitten.png'); var URL = 'http://localhost/cell:foo:A1/fs/kitten.png'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('file.byName (throws)', function () { var fn = function () { return url.cell(URI).file.byName(' '); }; (0, test_1.expect)(fn).to.throw(); }); it('file.byFileUri', function () { var test = function (fileUri, fileExtension, expected) { var res = url.cell(URI).file.byFileUri(fileUri, fileExtension); (0, test_1.expect)(res.toString()).to.eql(expected); }; test('file:foo:123', 'png', 'http://localhost/cell:foo:A1/file:123.png'); test('file:foo:123', '.png', 'http://localhost/cell:foo:A1/file:123.png'); test('file:foo:123', ' ...png ', 'http://localhost/cell:foo:A1/file:123.png'); test(' file:foo:123 ', ' png ', 'http://localhost/cell:foo:A1/file:123.png'); test('file:foo:123', '', 'http://localhost/cell:foo:A1/file:123'); test('file:foo:123', ' ', 'http://localhost/cell:foo:A1/file:123'); test('file:foo:123', undefined, 'http://localhost/cell:foo:A1/file:123'); }); it('file.byFileUri (throws)', function () { (0, test_1.expect)(function () { return url.cell(URI).file.byFileUri('cell:foo:A1'); }).to.throw(); (0, test_1.expect)(function () { return url.cell(URI).file.byFileUri('foo:123'); }).to.throw(); (0, test_1.expect)(function () { return url.cell(URI).file.byFileUri(''); }).to.throw(); (0, test_1.expect)(function () { return url.cell(URI).file.byFileUri(' '); }).to.throw(); }); }); }); describe('row', function () { var URI = 'cell:foo:1'; var url = _1.Urls.create(); it('uri', function () { var res = url.row(URI); (0, test_1.expect)(res.uri).to.eql(URI); }); it('throw if non-row URI passed', function () { (0, test_1.expect)(function () { return url.row('foo:bar'); }).to.throw(); (0, test_1.expect)(function () { return url.row('ns:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.row('cell:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.row('cell:foo:A1'); }).to.throw(); (0, test_1.expect)(function () { return url.row('cell:foo:A'); }).to.throw(); }); it('info', function () { var res1 = url.row(URI).info; var res2 = url.row(Uri_1.Uri.row('cell:foo:1')).info; var URL = 'http://localhost/cell:foo:1'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); }); describe('column', function () { var URI = 'cell:foo:A'; var url = _1.Urls.create(); it('uri', function () { var res = url.column(URI); (0, test_1.expect)(res.uri).to.eql(URI); }); it('throw if non-row URI passed', function () { (0, test_1.expect)(function () { return url.column('foo:bar'); }).to.throw(); (0, test_1.expect)(function () { return url.column('ns:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.column('cell:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.column('cell:foo:A1'); }).to.throw(); (0, test_1.expect)(function () { return url.column('cell:foo:1'); }).to.throw(); }); it('info', function () { var res1 = url.column(URI).info; var res2 = url.column(Uri_1.Uri.column('cell:foo:A')).info; var URL = 'http://localhost/cell:foo:A'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); }); describe('file', function () { var URI = 'file:foo:123'; var url = _1.Urls.create(); it('uri', function () { var res = url.file(URI); (0, test_1.expect)(res.uri).to.eql(URI); }); it('throw if non-cell URI passed', function () { (0, test_1.expect)(function () { return url.file('foo:bar'); }).to.throw(); (0, test_1.expect)(function () { return url.file('ns:foo'); }).to.throw(); (0, test_1.expect)(function () { return url.file('cell:foo:A1'); }).to.throw(); (0, test_1.expect)(function () { return url.file('file:boo'); }).to.throw(); }); it('info', function () { var res1 = url.file(URI).info; var res2 = url.file(Uri_1.Uri.file('file:foo:123')).info; var URL = 'http://localhost/file:foo:123/info'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('download', function () { var res1 = url.file(URI).download; var res2 = url.file(Uri_1.Uri.file('file:foo:123')).download; var URL = 'http://localhost/file:foo:123'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('delete', function () { var res1 = url.file(URI).delete; var res2 = url.file(Uri_1.Uri.file('file:foo:123')).delete; var URL = 'http://localhost/file:foo:123'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); it('uploaded', function () { var res1 = url.file(URI).uploaded; var res2 = url.file(Uri_1.Uri.file('file:foo:123')).uploaded; var URL = 'http://localhost/file:foo:123/uploaded'; (0, test_1.expect)(res1.toString()).to.eql(URL); (0, test_1.expect)(res2.toString()).to.eql(URL); }); }); describe('fn (function runtime)', function () { it('fn:run', function () { var urls = _1.Urls.create(); (0, test_1.expect)(urls.fn.run.toString()).to.eql('http://localhost/fn:run'); }); describe('bundle.manifest (file)', function () { it('dir variants', function () { var test = function (dir, expected) { var bundle = { host: 'localhost', uri: 'cell:foo:A1', dir: dir, }; var urls = _1.Urls.create(); var res = urls.fn.bundle.manifest(bundle); (0, test_1.expect)(res.toString()).to.eql(expected); }; test(undefined, 'http://localhost/cell:foo:A1/fs/index.json'); test(' v1.2.3 ', 'http://localhost/cell:foo:A1/fs/v1.2.3/index.json'); test(' //foo/v1.2.3// ', 'http://localhost/cell:foo:A1/fs/foo/v1.2.3/index.json'); }); it('strips HTTP on host mismatch check', function () { var urls = _1.Urls.create('domain.com'); var bundle = { host: 'https://domain.com', uri: 'cell:foo:A1', }; var res = urls.fn.bundle.manifest(bundle); (0, test_1.expect)(urls.host).to.eql('domain.com'); (0, test_1.expect)(res.toString()).to.eql('https://domain.com/cell:foo:A1/fs/index.json'); }); it('throw if host mismatch', function () { var test = function (host1, host2) { var urls = _1.Urls.create(host1); var bundle = { host: host2, uri: 'cell:foo:A1' }; var fn = function () { return urls.fn.bundle.manifest(bundle); }; (0, test_1.expect)(fn).to.throw(/Host mismatch/); }; test('localhost', 'localhost:1234'); test('domain.com', 'foo.com'); test('domain.com:8080', 'domain.com:1234'); }); }); describe('bundle.files', function () { it('dir variants', function () { var test = function (dir, expected) { var bundle = { host: 'localhost', uri: 'cell:foo:A1', dir: dir, }; var urls = _1.Urls.create(); var res = urls.fn.bundle.files(bundle); (0, test_1.expect)(res.toString()).to.eql(expected); }; test(undefined, 'http://localhost/cell:foo:A1/fs'); test(' v1.2.3 ', 'http://localhost/cell:foo:A1/fs?filter=v1.2.3/**'); test(' //foo/v1.2.3// ', 'http://localhost/cell:foo:A1/fs?filter=foo/v1.2.3/**'); }); it('strips HTTP on host mismatch check', function () { var urls = _1.Urls.create('domain.com'); var bundle = { host: 'https://domain.com', uri: 'cell:foo:A1', }; var res = urls.fn.bundle.files(bundle); (0, test_1.expect)(urls.host).to.eql('domain.com'); (0, test_1.expect)(res.toString()).to.eql('https://domain.com/cell:foo:A1/fs'); }); it('throw if host mismatch', function () { var test = function (host1, host2) { var urls = _1.Urls.create(host1); var bundle = { host: host2, uri: 'cell:foo:A1' }; var fn = function () { return urls.fn.bundle.files(bundle); }; (0, test_1.expect)(fn).to.throw(/Host mismatch/); }; test('localhost', 'localhost:1234'); test('domain.com', 'foo.com'); test('domain.com:8080', 'domain.com:1234'); }); }); }); });