@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
103 lines (102 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var test_1 = require("../test");
var FilePath_1 = require("./FilePath");
var Url_1 = require("../Url");
describe('FilePath', function () {
describe('fromUrl', function () {
it('ok (with or without domain)', function () {
var test = function (input) {
var res = FilePath_1.FilePath.fromUrl(input);
(0, test_1.expect)(res.ok).to.eql(true);
(0, test_1.expect)(res.error).to.eql(undefined);
};
test('http://localhost:5000/cell:foo:A1/fs/sample/index.html');
test('/cell:foo:A1/fs/sample/index.html');
});
it('toString (full path)', function () {
var test = function (input) {
var res = FilePath_1.FilePath.fromUrl(input);
(0, test_1.expect)(res.toString()).to.eql(Url_1.Url.parse(input).path);
};
test('http://localhost:5000/cell:foo:A1/fs/sample/index.html');
test('/cell:foo:A1/fs/sample/index.html');
});
it('path', function () {
var test = function (input, path) {
var res = FilePath_1.FilePath.fromUrl(input);
(0, test_1.expect)(res.path).to.eql(path);
};
test('http://localhost:5000/cell:foo:A1/fs/sample/index.html', 'sample/index.html');
test('/cell:foo:A1/fs/sample/index.html', 'sample/index.html');
test(' /cell:foo:A1/fs/foo/bar/ ', 'foo/bar/');
});
it('dir/filename', function () {
var test = function (input, dir, filename) {
var res = FilePath_1.FilePath.fromUrl(input);
(0, test_1.expect)(res.dir).to.eql(dir);
(0, test_1.expect)(res.filename).to.eql(filename);
};
test(' /cell:foo:A1/fs/sample/index.html ', 'sample', 'index.html');
test(' /cell:foo:A1/fs/foo/bar/bird.png ', 'foo/bar', 'bird.png');
test(' /cell:foo:A1/fs/foo/bar/ ', 'foo/bar', '');
});
it('error: not a cell URI path', function () {
var test = function (url) {
var res = FilePath_1.FilePath.fromUrl(url);
(0, test_1.expect)(res.ok).to.eql(false);
(0, test_1.expect)(res.error).to.include('path does not start with a cell URI');
};
test('http://localhost:5000/ns:foo/bar');
test('cell:foo:A1/foo/file.html');
});
it('error: not within "/fs" range', function () {
var test = function (url) {
var res = FilePath_1.FilePath.fromUrl(url);
(0, test_1.expect)(res.ok).to.eql(false);
(0, test_1.expect)(res.error).to.include('not a file-system path (eg "/cell:foo:A1/fs/filename")');
};
test('http://localhost:5000/cell:foo:A1/bar');
test(' /cell:foo:A1/bar ');
test('http://localhost:5000/cell:foo:A1');
test('http://localhost:5000/cell:foo:A1/');
test('http://localhost:5000/cell:foo:A1///');
});
});
describe('local', function () {
describe('Path / Location', function () {
it('toAbsolute', function () {
var test = function (path, root, expected) {
var res1 = FilePath_1.FilePath.Local.toAbsolutePath({ path: path, root: root });
var res2 = FilePath_1.FilePath.Local.toAbsoluteLocation({ path: path, root: root });
(0, test_1.expect)(res1).to.eql(expected);
(0, test_1.expect)(res2).to.eql("file://".concat(expected));
};
test(' ', ' /Users/bob/ ', '/Users/bob/');
test(' /foo/bar.png ', ' /Users/bob/ ', '/Users/bob/foo/bar.png');
test('foo/bar.png', '/Users/bob', '/Users/bob/foo/bar.png');
test('foo/bar.png', '/Users/bob///', '/Users/bob/foo/bar.png');
test('/Users/bob/foo/bar.png', '/Users/bob', '/Users/bob/foo/bar.png');
test('/Users/bob/foo/bar.png', '/Users/bob/', '/Users/bob/foo/bar.png');
test(' ~/foo/bar.png', '/Users/bob', '/Users/bob/foo/bar.png');
test(' ~foo/bar.png ', '/Users/bob', '/Users/bob/~foo/bar.png');
test('/~foo/bar.png', '/Users/bob', '/Users/bob/~foo/bar.png');
});
it('toRelative', function () {
var test = function (path, root, expected) {
var res1 = FilePath_1.FilePath.Local.toRelativePath({ path: path, root: root });
var res2 = FilePath_1.FilePath.Local.toRelativeLocation({ path: path, root: root });
(0, test_1.expect)(res1).to.eql(expected);
(0, test_1.expect)(res2).to.eql("file://".concat(expected));
};
test(' /foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
test('///foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
test(' foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
test(' ~/foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
test(' ~//foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
test(' /Users/bob/foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
test('file:///Users/bob/foo/bar.png ', ' /Users/bob/ ', '~/foo/bar.png');
});
});
});
});