mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
212 lines
5.2 KiB
JavaScript
212 lines
5.2 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs');
|
|
|
|
const DIR = __dirname + '/../../';
|
|
const COMMONDIR = DIR + 'common/';
|
|
const TMPLDIR = DIR + 'tmpl/';
|
|
|
|
const {
|
|
time,
|
|
timeEnd,
|
|
} = require(COMMONDIR + 'util');
|
|
|
|
const CloudFuncPath = COMMONDIR + 'cloudfunc';
|
|
|
|
const CloudFunc = require(CloudFuncPath);
|
|
|
|
const currify = require('currify');
|
|
const readFilesSync = require('@cloudcmd/read-files-sync');
|
|
|
|
const swap = currify((fn, a, b) => fn(b, a));
|
|
|
|
const test = require('tape');
|
|
const fresh = swap(require('fresh-require'), require);
|
|
|
|
const htmlLooksLike = require('html-looks-like');
|
|
|
|
const FS_DIR = TMPLDIR + 'fs/';
|
|
const EXPECT_PATH = __dirname + '/cloudfunc.html';
|
|
|
|
const addHBS = (a) => `${a}.hbs`;
|
|
const TMPL = [
|
|
'file',
|
|
'path',
|
|
'pathLink',
|
|
'link',
|
|
].map(addHBS);
|
|
|
|
const JSON_FILES = {
|
|
path : '/etc/X11/',
|
|
files : [{
|
|
name: 'applnk',
|
|
size: 'dir',
|
|
date: '21.02.2016',
|
|
uid : 0,
|
|
mode: 'rwx r-x r-x'
|
|
}, {
|
|
name: 'ай',
|
|
size: '1.30kb',
|
|
date: 0,
|
|
uid : 0,
|
|
mode: 'rwx r-x r-x'
|
|
}]
|
|
};
|
|
|
|
let Expect =
|
|
'<div data-name="js-path" class="reduce-text" title="/etc/X11/">' +
|
|
'<span data-name="js-clear-storage" class="path-icon icon-clear" ' +
|
|
'title="clear storage (Ctrl+D)">' +
|
|
'</span>' +
|
|
'<a data-name="js-refresh" href="/fs/etc/X11/" ' +
|
|
'class="path-icon icon-refresh" title="refresh (Ctrl+R)"></a>' +
|
|
'<span data-name="js-links" class=links>' +
|
|
'<a data-name="js-path-link" href="/fs/" title="/">/</a>' +
|
|
'<a data-name="js-path-link" href="/fs/etc/" title="/etc/">' +
|
|
'etc' +
|
|
'</a>/X11/' +
|
|
'</span>' +
|
|
'</div>';
|
|
|
|
test('cloudfunc: render', (t) => {
|
|
const template = readFilesSync(FS_DIR, TMPL, 'utf8');
|
|
const expect = fs.readFileSync(EXPECT_PATH, 'utf8');
|
|
|
|
time('CloudFunc.buildFromJSON');
|
|
const result = CloudFunc.buildFromJSON({
|
|
prefix : '',
|
|
data : JSON_FILES,
|
|
template: template
|
|
});
|
|
|
|
Expect += expect;
|
|
|
|
let i;
|
|
const isNotOk = Expect
|
|
.split('')
|
|
.some((item, number) => {
|
|
const ret = result[number] !== item;
|
|
|
|
if (ret) {
|
|
i = number;
|
|
}
|
|
|
|
return ret;
|
|
});
|
|
|
|
timeEnd('CloudFunc.buildFromJSON');
|
|
|
|
if (isNotOk) {
|
|
console.log(
|
|
`Error in char number: ${i}\n`,
|
|
`Expect: ${Expect.substr(i)}\n`,
|
|
`Result: ${result.substr(i)}`
|
|
);
|
|
|
|
console.log('buildFromJSON: Not OK');
|
|
}
|
|
|
|
t.equal(Expect, result, 'should be equal rendered json data');
|
|
|
|
htmlLooksLike(Expect, result);
|
|
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: formatMsg', (t) => {
|
|
const msg = 'hello';
|
|
const name = 'name';
|
|
const status = 'ok';
|
|
|
|
const result = CloudFunc.formatMsg(msg, name, status);
|
|
|
|
t.equal(result, 'hello: ok("name")');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: formatMsg', (t) => {
|
|
const msg = 'hello';
|
|
const name = null;
|
|
const status = 'ok';
|
|
|
|
const result = CloudFunc.formatMsg(msg, name, status);
|
|
|
|
t.equal(result, 'hello: ok');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getTitle', (t) => {
|
|
const CloudFunc = fresh(CloudFuncPath);
|
|
|
|
const result = CloudFunc.getTitle();
|
|
|
|
t.equal(result, 'Cloud Commander - /');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getTitle: no name', (t) => {
|
|
const CloudFunc = fresh(CloudFuncPath);
|
|
const path = '/hello/world';
|
|
|
|
const result = CloudFunc.getTitle({
|
|
path
|
|
});
|
|
|
|
t.equal(result, 'Cloud Commander - /hello/world');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getTitle: name, path', (t) => {
|
|
const CloudFunc = fresh(CloudFuncPath);
|
|
const name = 'hello';
|
|
const path = '/hello/world';
|
|
|
|
const result = CloudFunc.getTitle({
|
|
name,
|
|
path,
|
|
});
|
|
|
|
t.equal(result, 'hello - /hello/world');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getHeaderField', (t) => {
|
|
const sort = 'name';
|
|
const order = 'desc';
|
|
const name = 'name';
|
|
|
|
const result = CloudFunc.getHeaderField(sort, order, name);
|
|
const expected = 'name↓';
|
|
|
|
t.equal(result, expected, 'should set desc arrow');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getPathLink: no url', (t) => {
|
|
t.throws(CloudFunc.getPathLink, 'should throw when no url');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getPathLink: no template', (t) => {
|
|
const url = 'http://abc.com';
|
|
const prefix = '';
|
|
const fn = () => CloudFunc.getPathLink(url, prefix);
|
|
|
|
t.throws(fn, 'should throw when no template');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getDotDot', (t) => {
|
|
const dotDot = CloudFunc.getDotDot('/home');
|
|
|
|
t.equal(dotDot, '/', 'should return root');
|
|
t.end();
|
|
});
|
|
|
|
test('cloudfunc: getDotDot: two levels deep', (t) => {
|
|
const dotDot = CloudFunc.getDotDot('/home/coderaiser/');
|
|
|
|
t.equal(dotDot, '/home', 'should return up level');
|
|
t.end();
|
|
});
|
|
|