2021-11-07 04:14:14 +00:00
|
|
|
import { expect } from "chai";
|
2021-12-11 14:45:21 +00:00
|
|
|
import { JSDOM } from "jsdom";
|
2021-11-07 05:58:51 +00:00
|
|
|
import * as misc from "../src/misc";
|
2021-11-07 04:14:14 +00:00
|
|
|
|
2021-11-07 05:13:40 +00:00
|
|
|
describe("Misc: hidden file", () => {
|
2021-11-07 04:14:14 +00:00
|
|
|
it("should find hidden file correctly", () => {
|
2021-11-07 05:58:51 +00:00
|
|
|
let item = "";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = ".";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "..";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "/x/y/z/../././../a/b/c";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = ".hidden";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "_hidden_loose";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
2022-03-13 14:42:16 +00:00
|
|
|
expect(misc.isHiddenPath(item, true, false)).to.be.false;
|
2021-11-07 04:14:14 +00:00
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "/sdd/_hidden_loose";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "what/../_hidden_loose/what/what/what";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "what/../_hidden_loose/what/what/what";
|
2022-03-13 14:42:16 +00:00
|
|
|
expect(misc.isHiddenPath(item, true, false)).to.be.false;
|
2021-11-07 04:14:14 +00:00
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "what/../_hidden_loose/../.hidden/what/what/what";
|
2022-03-13 14:42:16 +00:00
|
|
|
expect(misc.isHiddenPath(item, true, false)).to.be.true;
|
|
|
|
|
|
|
|
item = "what/../_hidden_loose/../.hidden/what/what/what";
|
|
|
|
expect(misc.isHiddenPath(item, false, true)).to.be.false;
|
|
|
|
|
|
|
|
item = "what/_hidden_loose/what/what/what";
|
|
|
|
expect(misc.isHiddenPath(item, false, true)).to.be.true;
|
|
|
|
expect(misc.isHiddenPath(item, true, false)).to.be.false;
|
|
|
|
|
|
|
|
item = "what/.hidden/what/what/what";
|
|
|
|
expect(misc.isHiddenPath(item, false, true)).to.be.false;
|
|
|
|
expect(misc.isHiddenPath(item, true, false)).to.be.true;
|
2021-11-07 04:14:14 +00:00
|
|
|
});
|
|
|
|
});
|
2021-11-07 05:58:51 +00:00
|
|
|
|
|
|
|
describe("Misc: get folder levels", () => {
|
|
|
|
it("should ignore empty path", () => {
|
|
|
|
const item = "";
|
|
|
|
expect(misc.getFolderLevels(item)).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should ignore single file", () => {
|
|
|
|
const item = "xxx";
|
|
|
|
expect(misc.getFolderLevels(item)).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should detect path ending with /", () => {
|
|
|
|
const item = "xxx/";
|
|
|
|
const res = ["xxx"];
|
|
|
|
expect(misc.getFolderLevels(item)).to.deep.equal(res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should correctly split folders and files", () => {
|
|
|
|
const item = "xxx/yyy/zzz.md";
|
|
|
|
const res = ["xxx", "xxx/yyy"];
|
|
|
|
expect(misc.getFolderLevels(item)).to.deep.equal(res);
|
|
|
|
|
|
|
|
const item2 = "xxx/yyy/zzz";
|
|
|
|
const res2 = ["xxx", "xxx/yyy"];
|
|
|
|
expect(misc.getFolderLevels(item2)).to.deep.equal(res2);
|
|
|
|
|
|
|
|
const item3 = "xxx/yyy/zzz/";
|
|
|
|
const res3 = ["xxx", "xxx/yyy", "xxx/yyy/zzz"];
|
|
|
|
expect(misc.getFolderLevels(item3)).to.deep.equal(res3);
|
|
|
|
});
|
2021-11-28 02:45:46 +00:00
|
|
|
|
2022-02-27 09:54:31 +00:00
|
|
|
it("should correctly add ending slash if required", () => {
|
|
|
|
const item = "xxx/yyy/zzz.md";
|
|
|
|
const res = ["xxx/", "xxx/yyy/"];
|
|
|
|
expect(misc.getFolderLevels(item, true)).to.deep.equal(res);
|
|
|
|
|
|
|
|
const item2 = "xxx/yyy/zzz";
|
|
|
|
const res2 = ["xxx/", "xxx/yyy/"];
|
|
|
|
expect(misc.getFolderLevels(item2, true)).to.deep.equal(res2);
|
|
|
|
|
|
|
|
const item3 = "xxx/yyy/zzz/";
|
|
|
|
const res3 = ["xxx/", "xxx/yyy/", "xxx/yyy/zzz/"];
|
|
|
|
expect(misc.getFolderLevels(item3, true)).to.deep.equal(res3);
|
|
|
|
});
|
|
|
|
|
2021-11-28 04:18:44 +00:00
|
|
|
it("should treat path starting with / correctly", () => {
|
2021-11-28 02:45:46 +00:00
|
|
|
const item = "/xxx/yyy/zzz.md";
|
|
|
|
const res = ["/xxx", "/xxx/yyy"];
|
|
|
|
expect(misc.getFolderLevels(item)).to.deep.equal(res);
|
|
|
|
|
|
|
|
const item2 = "/xxx/yyy/zzz";
|
|
|
|
const res2 = ["/xxx", "/xxx/yyy"];
|
|
|
|
expect(misc.getFolderLevels(item2)).to.deep.equal(res2);
|
|
|
|
|
|
|
|
const item3 = "/xxx/yyy/zzz/";
|
|
|
|
const res3 = ["/xxx", "/xxx/yyy", "/xxx/yyy/zzz"];
|
|
|
|
expect(misc.getFolderLevels(item3)).to.deep.equal(res3);
|
|
|
|
|
|
|
|
const item4 = "/xxx";
|
|
|
|
const res4 = [] as string[];
|
|
|
|
expect(misc.getFolderLevels(item4)).to.deep.equal(res4);
|
|
|
|
|
|
|
|
const item5 = "/";
|
|
|
|
const res5 = [] as string[];
|
|
|
|
expect(misc.getFolderLevels(item5)).to.deep.equal(res5);
|
|
|
|
});
|
2021-11-07 05:58:51 +00:00
|
|
|
});
|
2021-11-09 02:00:44 +00:00
|
|
|
|
2022-02-27 09:54:31 +00:00
|
|
|
describe("Misc: get parent folder", () => {
|
|
|
|
it("should treat empty path correctly", () => {
|
|
|
|
const item = "";
|
|
|
|
expect(misc.getParentFolder(item)).equals("/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should treat one level path correctly", () => {
|
|
|
|
let item = "abc/";
|
|
|
|
expect(misc.getParentFolder(item)).equals("/");
|
|
|
|
item = "/efg/";
|
|
|
|
expect(misc.getParentFolder(item)).equals("/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should treat more levels path correctly", () => {
|
|
|
|
let item = "abc/efg";
|
|
|
|
expect(misc.getParentFolder(item)).equals("abc/");
|
|
|
|
item = "/hij/klm/";
|
|
|
|
expect(misc.getParentFolder(item)).equals("/hij/");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-09 02:00:44 +00:00
|
|
|
describe("Misc: vaild file name tests", () => {
|
|
|
|
it("should treat no ascii correctly", async () => {
|
|
|
|
const x = misc.isVaildText("😄🍎 apple 苹果");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find not-printable chars correctly", async () => {
|
|
|
|
const x = misc.isVaildText("😄🍎 apple 苹果\u0000");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow spaces/slashes/...", async () => {
|
|
|
|
const x = misc.isVaildText("😄🍎 apple 苹果/-_=/\\*%^&@#$`");
|
|
|
|
expect(x).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2021-11-21 07:31:20 +00:00
|
|
|
|
|
|
|
describe("Misc: get dirname", () => {
|
|
|
|
it("should return itself for folder", async () => {
|
|
|
|
const x = misc.getPathFolder("ssss/");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.equal("ssss/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return folder for file", async () => {
|
|
|
|
const x = misc.getPathFolder("sss/yyy");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.equal("sss/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should treat / specially", async () => {
|
|
|
|
const x = misc.getPathFolder("/");
|
|
|
|
expect(x).to.equal("/");
|
|
|
|
|
|
|
|
const y = misc.getPathFolder("/abc");
|
|
|
|
expect(y).to.equal("/");
|
|
|
|
});
|
|
|
|
});
|
2021-12-11 14:45:21 +00:00
|
|
|
|
|
|
|
describe("Misc: extract svg", () => {
|
|
|
|
beforeEach(function () {
|
|
|
|
const fakeBrowser = new JSDOM("");
|
|
|
|
global.window = fakeBrowser.window as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should extract rect from svg correctly", () => {
|
|
|
|
const x = "<svg><rect/><g/></svg>";
|
|
|
|
const y = misc.extractSvgSub(x);
|
|
|
|
// console.log(x)
|
|
|
|
expect(y).to.equal("<rect/><g/>");
|
|
|
|
});
|
|
|
|
});
|
2022-03-10 15:54:35 +00:00
|
|
|
|
|
|
|
describe("Misc: get split ranges", () => {
|
|
|
|
it("should deal with big parts", () => {
|
|
|
|
const k = misc.getSplitRanges(10, 20);
|
|
|
|
const k2: misc.SplitRange[] = [
|
|
|
|
{
|
|
|
|
partNum: 1,
|
|
|
|
start: 0,
|
|
|
|
end: 10,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
expect(k).to.deep.equal(k2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should deal with 0 remainder", () => {
|
|
|
|
const k = misc.getSplitRanges(20, 10);
|
|
|
|
const k2: misc.SplitRange[] = [
|
|
|
|
{
|
|
|
|
partNum: 1,
|
|
|
|
start: 0,
|
|
|
|
end: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
partNum: 2,
|
|
|
|
start: 10,
|
|
|
|
end: 20,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
expect(k).to.deep.equal(k2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should deal with not-0 remainder", () => {
|
|
|
|
const k = misc.getSplitRanges(25, 10);
|
|
|
|
const k2: misc.SplitRange[] = [
|
|
|
|
{
|
|
|
|
partNum: 1,
|
|
|
|
start: 0,
|
|
|
|
end: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
partNum: 2,
|
|
|
|
start: 10,
|
|
|
|
end: 20,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
partNum: 3,
|
|
|
|
start: 20,
|
|
|
|
end: 25,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
expect(k).to.deep.equal(k2);
|
|
|
|
});
|
|
|
|
});
|
2022-03-18 17:21:22 +00:00
|
|
|
|
|
|
|
describe("Misc: at which level", () => {
|
|
|
|
it("should throw error on some parameters", () => {
|
|
|
|
expect(() => misc.atWhichLevel(undefined)).to.throw();
|
|
|
|
expect(() => misc.atWhichLevel("")).to.throw();
|
|
|
|
expect(() => misc.atWhichLevel("..")).to.throw();
|
|
|
|
expect(() => misc.atWhichLevel(".")).to.throw();
|
|
|
|
expect(() => misc.atWhichLevel("/")).to.throw();
|
|
|
|
expect(() => misc.atWhichLevel("/xxyy")).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should treat folders correctly", () => {
|
|
|
|
expect(misc.atWhichLevel("x/")).to.be.equal(1);
|
|
|
|
expect(misc.atWhichLevel("x/y/")).to.be.equal(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should treat files correctly", () => {
|
|
|
|
expect(misc.atWhichLevel("x.md")).to.be.equal(1);
|
|
|
|
expect(misc.atWhichLevel("x/y.md")).to.be.equal(2);
|
|
|
|
expect(misc.atWhichLevel("x/y/z.md")).to.be.equal(3);
|
|
|
|
});
|
|
|
|
});
|