remotely-save/tests/misc.test.ts
fyears af93af9047 Squashed commit of sync hidden files:
commit 73967756d51d246b3ca203aa3683cdfebf567000
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Mar 13 22:41:28 2022 +0800

    fix typo

commit 08e16faa9a9ace9bec71acb723e0d70ca361d49f
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Mar 13 22:41:01 2022 +0800

    add modal in settings

commit 9db7194fa28cb62b1fa4c01ac7f746d5ac3b86a8
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Mar 13 22:03:00 2022 +0800

    working sync for .obsidian and _

commit 4be24ba092181c1c3e1aabe5e9d4e9fcff28f987
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Mar 13 16:07:10 2022 +0800

    more logic for hidden path
2022-03-13 22:42:16 +08:00

247 lines
6.7 KiB
TypeScript

import { expect } from "chai";
import { JSDOM } from "jsdom";
import * as misc from "../src/misc";
describe("Misc: hidden file", () => {
it("should find hidden file correctly", () => {
let item = "";
expect(misc.isHiddenPath(item)).to.be.false;
item = ".";
expect(misc.isHiddenPath(item)).to.be.false;
item = "..";
expect(misc.isHiddenPath(item)).to.be.false;
item = "/x/y/z/../././../a/b/c";
expect(misc.isHiddenPath(item)).to.be.false;
item = ".hidden";
expect(misc.isHiddenPath(item)).to.be.true;
item = "_hidden_loose";
expect(misc.isHiddenPath(item)).to.be.true;
expect(misc.isHiddenPath(item, true, false)).to.be.false;
item = "/sdd/_hidden_loose";
expect(misc.isHiddenPath(item)).to.be.true;
item = "what/../_hidden_loose/what/what/what";
expect(misc.isHiddenPath(item)).to.be.true;
item = "what/../_hidden_loose/what/what/what";
expect(misc.isHiddenPath(item, true, false)).to.be.false;
item = "what/../_hidden_loose/../.hidden/what/what/what";
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;
});
});
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);
});
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);
});
it("should treat path starting with / correctly", () => {
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);
});
});
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/");
});
});
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;
});
});
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("/");
});
});
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/>");
});
});
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);
});
});