From a9126e5947082dab1e9630f00c93490dced5d185 Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sat, 27 Apr 2024 03:28:39 +0800 Subject: [PATCH] fix and optimize tests --- package.json | 2 - tests/configPersist.test.ts | 8 +- tests/encryptOpenSSL.test.ts | 50 ++++++------ tests/metadataOnRemote.test.ts | 23 +++--- tests/misc.test.ts | 144 ++++++++++++++++----------------- 5 files changed, 106 insertions(+), 121 deletions(-) diff --git a/package.json b/package.json index 1a114f7..8a3a07f 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,6 @@ "@types/node": "^20.12.7", "@types/qrcode": "^1.5.5", "builtin-modules": "^3.3.0", - "chai": "^4.4.1", - "chai-as-promised": "^7.1.1", "cross-env": "^7.0.3", "dotenv": "^16.4.5", "esbuild": "^0.20.2", diff --git a/tests/configPersist.test.ts b/tests/configPersist.test.ts index b5dabc5..8ee36e4 100644 --- a/tests/configPersist.test.ts +++ b/tests/configPersist.test.ts @@ -1,12 +1,8 @@ -import * as chai from "chai"; -import chaiAsPromised from "chai-as-promised"; +import { strict as assert } from "assert"; import { RemotelySavePluginSettings } from "../src/baseTypes"; import { messyConfigToNormal, normalConfigToMessy } from "../src/configPersist"; -chai.use(chaiAsPromised); -const expect = chai.expect; - const DEFAULT_SETTINGS: RemotelySavePluginSettings = { s3: { s3AccessKeyID: "acc", @@ -32,6 +28,6 @@ describe("Config Persist tests", () => { const k = DEFAULT_SETTINGS; const k2 = normalConfigToMessy(k); const k3 = messyConfigToNormal(k2); - expect(k3).to.deep.equal(k); + assert.deepEqual(k3, k); }); }); diff --git a/tests/encryptOpenSSL.test.ts b/tests/encryptOpenSSL.test.ts index 1ec579a..3249eec 100644 --- a/tests/encryptOpenSSL.test.ts +++ b/tests/encryptOpenSSL.test.ts @@ -1,5 +1,4 @@ -import * as chai from "chai"; -import chaiAsPromised from "chai-as-promised"; +import { strict as assert } from "assert"; import * as fs from "fs"; import * as path from "path"; import { @@ -13,9 +12,6 @@ import { } from "../src/encryptOpenSSL"; import { base64ToBase64url, bufferToArrayBuffer } from "../src/misc"; -chai.use(chaiAsPromised); -const expect = chai.expect; - describe("Encryption OpenSSL tests", () => { beforeEach(function () { global.window = { @@ -26,7 +22,7 @@ describe("Encryption OpenSSL tests", () => { it("should encrypt string", async () => { const k = "dkjdhkfhdkjgsdklxxd"; const password = "hey"; - expect(await encryptStringToBase32(k, password)).to.not.equal(k); + assert.notEqual(await encryptStringToBase32(k, password), k); }); it("should encrypt string and return different results each time", async () => { @@ -34,7 +30,7 @@ describe("Encryption OpenSSL tests", () => { const password = "hey"; const res1 = await encryptStringToBase32(k, password); const res2 = await encryptStringToBase32(k, password); - expect(res1).to.not.equal(res2); + assert.notEqual(res1, res2); }); it("should raise error using different password", async () => { @@ -42,7 +38,7 @@ describe("Encryption OpenSSL tests", () => { const password = "hey"; const password2 = "hey2"; const enc = await encryptStringToBase32(k, password); - await expect(decryptBase32ToString(enc, password2)).to.be.rejected; + await assert.rejects(decryptBase32ToString(enc, password2)); }); it("should encrypt and decrypt string and get the same result returned", async () => { @@ -52,7 +48,7 @@ describe("Encryption OpenSSL tests", () => { // console.log(enc); const dec = await decryptBase32ToString(enc, password); // console.log(dec); - expect(dec).equal(k); + assert.equal(dec, k); }); it("should encrypt text file and get the same result as openssl", async () => { @@ -78,7 +74,7 @@ describe("Encryption OpenSSL tests", () => { // we output base32, so we need some transformation const opensslBase64urlRes = base64ToBase64url(opensslBase64Res); - expect(enc).equal(opensslBase64urlRes); + assert.equal(enc, opensslBase64urlRes); }); it("should encrypt binary file and get the same result as openssl", async () => { @@ -102,7 +98,7 @@ describe("Encryption OpenSSL tests", () => { // openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -pass pass:somepassword -in mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg -out mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc - expect(Buffer.from(enc).equals(Buffer.from(opensslArrBuf))).to.be.true; + assert.ok(Buffer.from(enc).equals(Buffer.from(opensslArrBuf))); }); it("should encrypt binary file not deterministically", async () => { @@ -116,7 +112,7 @@ describe("Encryption OpenSSL tests", () => { const res1 = await encryptArrayBuffer(fileArrBuf, password); const res2 = await encryptArrayBuffer(fileArrBuf, password); - expect(Buffer.from(res1).equals(Buffer.from(res2))).to.be.false; + assert.ok(!Buffer.from(res1).equals(Buffer.from(res2))); }); it("should decrypt binary file and get the same result as openssl", async () => { @@ -132,36 +128,36 @@ describe("Encryption OpenSSL tests", () => { await fs.readFileSync(path.join(testFolder, testFileName)) ); - expect(Buffer.from(dec).equals(Buffer.from(opensslArrBuf))).to.be.true; + assert.deepEqual(Buffer.from(dec), Buffer.from(opensslArrBuf)); }); it("should get size from origin to encrypted correctly", () => { - expect(() => getSizeFromOrigToEnc(-1)).to.throw(); - expect(() => getSizeFromOrigToEnc(0.5)).to.throw(); - expect(getSizeFromOrigToEnc(0)).equals(32); - expect(getSizeFromOrigToEnc(15)).equals(32); - expect(getSizeFromOrigToEnc(16)).equals(48); - expect(getSizeFromOrigToEnc(31)).equals(48); - expect(getSizeFromOrigToEnc(32)).equals(64); - expect(getSizeFromOrigToEnc(14787203)).equals(14787232); + assert.throws(() => getSizeFromOrigToEnc(-1)); + assert.throws(() => getSizeFromOrigToEnc(0.5)); + assert.equal(getSizeFromOrigToEnc(0), 32); + assert.equal(getSizeFromOrigToEnc(15), 32); + assert.equal(getSizeFromOrigToEnc(16), 48); + assert.equal(getSizeFromOrigToEnc(31), 48); + assert.equal(getSizeFromOrigToEnc(32), 64); + assert.equal(getSizeFromOrigToEnc(14787203), 14787232); }); it("should get size from encrypted to origin correctly", () => { - expect(() => getSizeFromEncToOrig(-1)).to.throw(); - expect(() => getSizeFromEncToOrig(30)).to.throw(); + assert.throws(() => getSizeFromEncToOrig(-1)); + assert.throws(() => getSizeFromEncToOrig(30)); - expect(getSizeFromEncToOrig(32)).to.deep.equal({ + assert.deepEqual(getSizeFromEncToOrig(32), { minSize: 0, maxSize: 15, }); - expect(getSizeFromEncToOrig(48)).to.deep.equal({ + assert.deepEqual(getSizeFromEncToOrig(48), { minSize: 16, maxSize: 31, }); - expect(() => getSizeFromEncToOrig(14787231)).to.throw(); + assert.throws(() => getSizeFromEncToOrig(14787231)); let { minSize, maxSize } = getSizeFromEncToOrig(14787232); - expect(minSize <= 14787203 && 14787203 <= maxSize).to.be.true; + assert.ok(minSize <= 14787203 && 14787203 <= maxSize); }); }); diff --git a/tests/metadataOnRemote.test.ts b/tests/metadataOnRemote.test.ts index a72be89..e702b75 100644 --- a/tests/metadataOnRemote.test.ts +++ b/tests/metadataOnRemote.test.ts @@ -1,14 +1,9 @@ -import * as chai from "chai"; -import chaiAsPromised from "chai-as-promised"; - +import { strict as assert } from "assert"; import { isEqualMetadataOnRemote, MetadataOnRemote, } from "../src/metadataOnRemote"; -chai.use(chaiAsPromised); -const expect = chai.expect; - describe("Metadata operations tests", () => { it("should compare objects deeply", async () => { const a: MetadataOnRemote = { @@ -24,7 +19,7 @@ describe("Metadata operations tests", () => { ], }; - expect(isEqualMetadataOnRemote(a, b)); + assert.ok(isEqualMetadataOnRemote(a, b)); }); it("should find diff", async () => { @@ -41,7 +36,7 @@ describe("Metadata operations tests", () => { ], }; - expect(!isEqualMetadataOnRemote(a, b)); + assert.ok(!isEqualMetadataOnRemote(a, b)); }); it("should treat undefined correctly", async () => { @@ -53,22 +48,22 @@ describe("Metadata operations tests", () => { ], }; - expect(!isEqualMetadataOnRemote(a, b)); + assert.ok(!isEqualMetadataOnRemote(a, b)); b = { deletions: [] }; - expect(isEqualMetadataOnRemote(a, b)); + assert.ok(isEqualMetadataOnRemote(a, b)); b = { deletions: undefined }; - expect(isEqualMetadataOnRemote(a, b)); + assert.ok(isEqualMetadataOnRemote(a, b)); b = undefined; - expect(isEqualMetadataOnRemote(a, b)); + assert.ok(isEqualMetadataOnRemote(a, b)); }); it("should ignore generated at fields", async () => { const a: MetadataOnRemote = { deletions: [ - { key: "xxxx", actionWhen: 1 }, + { key: "xxx", actionWhen: 1 }, { key: "yyy", actionWhen: 2 }, ], generatedWhen: 1, @@ -81,6 +76,6 @@ describe("Metadata operations tests", () => { generatedWhen: 2, }; - expect(isEqualMetadataOnRemote(a, b)); + assert.ok(isEqualMetadataOnRemote(a, b)); }); }); diff --git a/tests/misc.test.ts b/tests/misc.test.ts index 326c8be..bbfc8b9 100644 --- a/tests/misc.test.ts +++ b/tests/misc.test.ts @@ -1,139 +1,139 @@ -import { expect } from "chai"; +import { strict as assert } from "assert"; 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; + assert.ok(!misc.isHiddenPath(item)); item = "."; - expect(misc.isHiddenPath(item)).to.be.false; + assert.ok(!misc.isHiddenPath(item)); item = ".."; - expect(misc.isHiddenPath(item)).to.be.false; + assert.ok(!misc.isHiddenPath(item)); item = "/x/y/z/../././../a/b/c"; - expect(misc.isHiddenPath(item)).to.be.false; + assert.ok(!misc.isHiddenPath(item)); item = ".hidden"; - expect(misc.isHiddenPath(item)).to.be.true; + assert.ok(misc.isHiddenPath(item)); item = "_hidden_loose"; - expect(misc.isHiddenPath(item)).to.be.true; - expect(misc.isHiddenPath(item, true, false)).to.be.false; + assert.ok(misc.isHiddenPath(item)); + assert.ok(!misc.isHiddenPath(item, true, false)); item = "/sdd/_hidden_loose"; - expect(misc.isHiddenPath(item)).to.be.true; + assert.ok(misc.isHiddenPath(item)); item = "what/../_hidden_loose/what/what/what"; - expect(misc.isHiddenPath(item)).to.be.true; + assert.ok(misc.isHiddenPath(item)); item = "what/../_hidden_loose/what/what/what"; - expect(misc.isHiddenPath(item, true, false)).to.be.false; + assert.ok(!misc.isHiddenPath(item, true, false)); item = "what/../_hidden_loose/../.hidden/what/what/what"; - expect(misc.isHiddenPath(item, true, false)).to.be.true; + assert.ok(misc.isHiddenPath(item, true, false)); item = "what/../_hidden_loose/../.hidden/what/what/what"; - expect(misc.isHiddenPath(item, false, true)).to.be.false; + assert.ok(!misc.isHiddenPath(item, false, true)); 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; + assert.ok(misc.isHiddenPath(item, false, true)); + assert.ok(!misc.isHiddenPath(item, true, 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; + assert.ok(!misc.isHiddenPath(item, false, true)); + assert.ok(misc.isHiddenPath(item, true, false)); }); }); describe("Misc: get folder levels", () => { it("should ignore empty path", () => { const item = ""; - expect(misc.getFolderLevels(item)).to.be.empty; + assert.equal(misc.getFolderLevels(item).length, 0); }); it("should ignore single file", () => { const item = "xxx"; - expect(misc.getFolderLevels(item)).to.be.empty; + assert.equal(misc.getFolderLevels(item).length, 0); }); it("should detect path ending with /", () => { const item = "xxx/"; const res = ["xxx"]; - expect(misc.getFolderLevels(item)).to.deep.equal(res); + assert.deepEqual(misc.getFolderLevels(item), 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); + assert.deepEqual(misc.getFolderLevels(item), res); const item2 = "xxx/yyy/zzz"; const res2 = ["xxx", "xxx/yyy"]; - expect(misc.getFolderLevels(item2)).to.deep.equal(res2); + assert.deepEqual(misc.getFolderLevels(item2), res2); const item3 = "xxx/yyy/zzz/"; const res3 = ["xxx", "xxx/yyy", "xxx/yyy/zzz"]; - expect(misc.getFolderLevels(item3)).to.deep.equal(res3); + assert.deepEqual(misc.getFolderLevels(item3), 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); + assert.deepEqual(misc.getFolderLevels(item, true), res); const item2 = "xxx/yyy/zzz"; const res2 = ["xxx/", "xxx/yyy/"]; - expect(misc.getFolderLevels(item2, true)).to.deep.equal(res2); + assert.deepEqual(misc.getFolderLevels(item2, true), res2); const item3 = "xxx/yyy/zzz/"; const res3 = ["xxx/", "xxx/yyy/", "xxx/yyy/zzz/"]; - expect(misc.getFolderLevels(item3, true)).to.deep.equal(res3); + assert.deepEqual(misc.getFolderLevels(item3, true), 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); + assert.deepEqual(misc.getFolderLevels(item), res); const item2 = "/xxx/yyy/zzz"; const res2 = ["/xxx", "/xxx/yyy"]; - expect(misc.getFolderLevels(item2)).to.deep.equal(res2); + assert.deepEqual(misc.getFolderLevels(item2), res2); const item3 = "/xxx/yyy/zzz/"; const res3 = ["/xxx", "/xxx/yyy", "/xxx/yyy/zzz"]; - expect(misc.getFolderLevels(item3)).to.deep.equal(res3); + assert.deepEqual(misc.getFolderLevels(item3), res3); const item4 = "/xxx"; const res4 = [] as string[]; - expect(misc.getFolderLevels(item4)).to.deep.equal(res4); + assert.deepEqual(misc.getFolderLevels(item4), res4); const item5 = "/"; const res5 = [] as string[]; - expect(misc.getFolderLevels(item5)).to.deep.equal(res5); + assert.deepEqual(misc.getFolderLevels(item5), res5); }); }); describe("Misc: get parent folder", () => { it("should treat empty path correctly", () => { const item = ""; - expect(misc.getParentFolder(item)).equals("/"); + assert.equal(misc.getParentFolder(item), "/"); }); it("should treat one level path correctly", () => { let item = "abc/"; - expect(misc.getParentFolder(item)).equals("/"); + assert.equal(misc.getParentFolder(item), "/"); item = "/efg/"; - expect(misc.getParentFolder(item)).equals("/"); + assert.equal(misc.getParentFolder(item), "/"); }); it("should treat more levels path correctly", () => { let item = "abc/efg"; - expect(misc.getParentFolder(item)).equals("abc/"); + assert.equal(misc.getParentFolder(item), "abc/"); item = "/hij/klm/"; - expect(misc.getParentFolder(item)).equals("/hij/"); + assert.equal(misc.getParentFolder(item), "/hij/"); }); }); @@ -141,18 +141,18 @@ 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; + assert.ok(x); }); it("should find not-printable chars correctly", async () => { const x = misc.isVaildText("😄🍎 apple 苹果\u0000"); // console.log(x) - expect(x).to.be.false; + assert.ok(!x); }); it("should allow spaces/slashes/...", async () => { const x = misc.isVaildText("😄🍎 apple 苹果/-_=/\\*%^&@#$`"); - expect(x).to.be.true; + assert.ok(x); }); }); @@ -160,21 +160,21 @@ describe("Misc: get dirname", () => { it("should return itself for folder", async () => { const x = misc.getPathFolder("ssss/"); // console.log(x) - expect(x).to.equal("ssss/"); + assert.equal(x, "ssss/"); }); it("should return folder for file", async () => { const x = misc.getPathFolder("sss/yyy"); // console.log(x) - expect(x).to.equal("sss/"); + assert.equal(x, "sss/"); }); it("should treat / specially", async () => { const x = misc.getPathFolder("/"); - expect(x).to.equal("/"); + assert.equal(x, "/"); const y = misc.getPathFolder("/abc"); - expect(y).to.equal("/"); + assert.equal(y, "/"); }); }); @@ -188,7 +188,7 @@ describe("Misc: extract svg", () => { const x = ""; const y = misc.extractSvgSub(x); // console.log(x) - expect(y).to.equal(""); + assert.equal(y, ""); }); }); @@ -202,7 +202,7 @@ describe("Misc: get split ranges", () => { end: 10, }, ]; - expect(k).to.deep.equal(k2); + assert.deepEqual(k, k2); }); it("should deal with 0 remainder", () => { @@ -219,7 +219,7 @@ describe("Misc: get split ranges", () => { end: 20, }, ]; - expect(k).to.deep.equal(k2); + assert.deepEqual(k, k2); }); it("should deal with not-0 remainder", () => { @@ -241,55 +241,55 @@ describe("Misc: get split ranges", () => { end: 25, }, ]; - expect(k).to.deep.equal(k2); + assert.deepEqual(k, k2); }); }); 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(); + assert.throws(() => misc.atWhichLevel(undefined)); + assert.throws(() => misc.atWhichLevel("")); + assert.throws(() => misc.atWhichLevel("..")); + assert.throws(() => misc.atWhichLevel(".")); + assert.throws(() => misc.atWhichLevel("/")); + assert.throws(() => misc.atWhichLevel("/xxyy")); }); it("should treat folders correctly", () => { - expect(misc.atWhichLevel("x/")).to.be.equal(1); - expect(misc.atWhichLevel("x/y/")).to.be.equal(2); + assert.equal(misc.atWhichLevel("x/"), 1); + assert.equal(misc.atWhichLevel("x/y/"), 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); + assert.equal(misc.atWhichLevel("x.md"), 1); + assert.equal(misc.atWhichLevel("x/y.md"), 2); + assert.equal(misc.atWhichLevel("x/y/z.md"), 3); }); }); describe("Misc: special char for dir", () => { it("should return false for normal string", () => { - expect(misc.checkHasSpecialCharForDir("")).to.be.false; - expect(misc.checkHasSpecialCharForDir("xxx")).to.be.false; - expect(misc.checkHasSpecialCharForDir("yyy_xxx")).to.be.false; - expect(misc.checkHasSpecialCharForDir("yyy.xxx")).to.be.false; - expect(misc.checkHasSpecialCharForDir("yyy?xxx")).to.be.false; + assert.ok(!misc.checkHasSpecialCharForDir("")); + assert.ok(!misc.checkHasSpecialCharForDir("xxx")); + assert.ok(!misc.checkHasSpecialCharForDir("yyy_xxx")); + assert.ok(!misc.checkHasSpecialCharForDir("yyy.xxx")); + assert.ok(!misc.checkHasSpecialCharForDir("yyy?xxx")); }); it("should return true for special cases", () => { - expect(misc.checkHasSpecialCharForDir("?")).to.be.true; - expect(misc.checkHasSpecialCharForDir("/")).to.be.true; - expect(misc.checkHasSpecialCharForDir("\\")).to.be.true; - expect(misc.checkHasSpecialCharForDir("xxx/yyy")).to.be.true; - expect(misc.checkHasSpecialCharForDir("xxx\\yyy")).to.be.true; - expect(misc.checkHasSpecialCharForDir("xxx?yyy")).to.be.true; + assert.ok(misc.checkHasSpecialCharForDir("?")); + assert.ok(misc.checkHasSpecialCharForDir("/")); + assert.ok(misc.checkHasSpecialCharForDir("\\")); + assert.ok(misc.checkHasSpecialCharForDir("xxx/yyy")); + assert.ok(misc.checkHasSpecialCharForDir("xxx\\yyy")); + assert.ok(misc.checkHasSpecialCharForDir("xxx?yyy")); }); }); describe("Misc: Dropbox: should fix the folder name cases", () => { it("should do nothing on empty folders", () => { const input: any[] = []; - expect(misc.fixEntityListCasesInplace(input)).to.be.empty; + assert.equal(misc.fixEntityListCasesInplace(input).length, 0); }); it("should sort folders by length by side effect", () => { @@ -306,7 +306,7 @@ describe("Misc: Dropbox: should fix the folder name cases", () => { { keyRaw: "bbb/" }, { keyRaw: "aaaa/" }, ]; - expect(misc.fixEntityListCasesInplace(input)).to.deep.equal(output); + assert.deepEqual(misc.fixEntityListCasesInplace(input), output); }); it("should fix folder names", () => { @@ -335,6 +335,6 @@ describe("Misc: Dropbox: should fix the folder name cases", () => { { keyRaw: "ddd/eee/fff.md" }, { keyRaw: "Ggg/Hhh你好/Fff世界.md" }, ]; - expect(misc.fixEntityListCasesInplace(input)).to.deep.equal(output); + assert.deepEqual(misc.fixEntityListCasesInplace(input), output); }); });