From d4bda0b1d51af7bb3e94aa234b7b5c3418339f64 Mon Sep 17 00:00:00 2001 From: Stanimir Date: Wed, 19 Jun 2024 17:47:10 +0300 Subject: [PATCH] add unit test byDateModified --- __tests__/common/unit/misc.test.js | 132 ++++++++++++++++------------- packages/common/misc.js | 12 ++- packages/common/package.json | 4 +- 3 files changed, 85 insertions(+), 63 deletions(-) diff --git a/__tests__/common/unit/misc.test.js b/__tests__/common/unit/misc.test.js index ba4f4c69..3a2c42e4 100644 --- a/__tests__/common/unit/misc.test.js +++ b/__tests__/common/unit/misc.test.js @@ -132,88 +132,102 @@ describe("Common misc unit tests", () => { }); test("misc.sortByCriteria", () => { const items = [ - { name: "b", isFile: true, size: 30 }, - { name: "a", isFile: true, size: 20 }, - { name: "c", isFile: true, size: 10 }, + { name: "b", isFile: true, size: 30, lmdt: new Date('2024-06-30T00:00:00Z').getTime() }, + { name: "a", isFile: true, size: 20, lmdt: new Date('2024-06-20T00:00:00Z').getTime() }, + { name: "c", isFile: true, size: 10, lmdt: new Date('2024-06-10T00:00:00Z').getTime() }, { name: "folder", isFile: false }, - { name: "0", isFile: true, size: 11 }, - { name: "2", isFile: true, size: 12 }, - { name: "1", isFile: true, size: 13 }, - { name: "02", isFile: true, size: 14 }, - { name: "01", isFile: true, size: 15 }, - { name: "10", isFile: true, size: 16 }, + { name: "0", isFile: true, size: 11, lmdt: new Date('2024-06-11T00:00:00Z').getTime() }, + { name: "2", isFile: true, size: 12, lmdt: new Date('2024-06-12T00:00:00Z').getTime() }, + { name: "1", isFile: true, size: 13, lmdt: new Date('2024-06-13T00:00:00Z').getTime() }, + { name: "02", isFile: true, size: 14, lmdt: new Date('2024-06-14T00:00:00Z').getTime() }, + { name: "01", isFile: true, size: 15, lmdt: new Date('2024-06-15T00:00:00Z').getTime() }, + { name: "10", isFile: true, size: 16, lmdt: new Date('2024-06-16T00:00:00Z').getTime() }, ]; const reversedItems = [...items].reverse(); //asc let output = misc.sortByCriteria(items, "byName", true); expect(output).toEqual([ - { name: "folder", isFile: false }, - { name: "a", isFile: true, size: 20 }, - { name: "b", isFile: true, size: 30 }, - { name: "c", isFile: true, size: 10 }, - { name: "0", isFile: true, size: 11 }, - { name: "1", isFile: true, size: 13 }, - { name: "01", isFile: true, size: 15 }, - { name: "2", isFile: true, size: 12 }, - { name: "02", isFile: true, size: 14 }, - { name: "10", isFile: true, size: 16 }, + items.find(i => i.name === 'folder'), + items.find(i => i.name === 'a'), + items.find(i => i.name === 'b'), + items.find(i => i.name === 'c'), + items.find(i => i.name === '0'), + items.find(i => i.name === '1'), + items.find(i => i.name === '01'), + items.find(i => i.name === '2'), + items.find(i => i.name === '02'), + items.find(i => i.name === '10'), ]); //asc reverse output = misc.sortByCriteria(reversedItems, "byName", true); expect(output).toEqual([ - { name: "folder", isFile: false }, - { name: "0", isFile: true, size: 11 }, - { name: "01", isFile: true, size: 15 }, - { name: "1", isFile: true, size: 13 }, - { name: "02", isFile: true, size: 14 }, - { name: "2", isFile: true, size: 12 }, - { name: "10", isFile: true, size: 16 }, - { name: "a", isFile: true, size: 20 }, - { name: "b", isFile: true, size: 30 }, - { name: "c", isFile: true, size: 10 }, + items.find(i => i.name === 'folder'), + items.find(i => i.name === '0'), + items.find(i => i.name === '01'), + items.find(i => i.name === '1'), + items.find(i => i.name === '02'), + items.find(i => i.name === '2'), + items.find(i => i.name === '10'), + items.find(i => i.name === 'a'), + items.find(i => i.name === 'b'), + items.find(i => i.name === 'c'), ]); //desc output = misc.sortByCriteria(items, "byName", false); expect(output).toEqual([ - { name: "10", isFile: true, size: 16 }, - { name: "02", isFile: true, size: 14 }, - { name: "2", isFile: true, size: 12 }, - { name: "01", isFile: true, size: 15 }, - { name: "1", isFile: true, size: 13 }, - { name: "0", isFile: true, size: 11 }, - { name: "c", isFile: true, size: 10 }, - { name: "b", isFile: true, size: 30 }, - { name: "a", isFile: true, size: 20 }, - { name: "folder", isFile: false }, + items.find(i => i.name === '10'), + items.find(i => i.name === '02'), + items.find(i => i.name === '2'), + items.find(i => i.name === '01'), + items.find(i => i.name === '1'), + items.find(i => i.name === '0'), + items.find(i => i.name === 'c'), + items.find(i => i.name === 'b'), + items.find(i => i.name === 'a'), + items.find(i => i.name === 'folder'), ]); //desc reversed output = misc.sortByCriteria(reversedItems, "byName", false); expect(output).toEqual([ - { name: "c", isFile: true, size: 10 }, - { name: "b", isFile: true, size: 30 }, - { name: "a", isFile: true, size: 20 }, - { name: "10", isFile: true, size: 16 }, - { name: "2", isFile: true, size: 12 }, - { name: "02", isFile: true, size: 14 }, - { name: "1", isFile: true, size: 13 }, - { name: "01", isFile: true, size: 15 }, - { name: "0", isFile: true, size: 11 }, - { name: "folder", isFile: false }, + items.find(i => i.name === 'c'), + items.find(i => i.name === 'b'), + items.find(i => i.name === 'a'), + items.find(i => i.name === '10'), + items.find(i => i.name === '2'), + items.find(i => i.name === '02'), + items.find(i => i.name === '1'), + items.find(i => i.name === '01'), + items.find(i => i.name === '0'), + items.find(i => i.name === 'folder'), ]); output = misc.sortByCriteria(items, "byFileSize", true); expect(output).toEqual([ - { name: "folder", isFile: false }, - { name: "c", isFile: true, size: 10 }, - { name: "0", isFile: true, size: 11 }, - { name: "2", isFile: true, size: 12 }, - { name: "1", isFile: true, size: 13 }, - { name: "02", isFile: true, size: 14 }, - { name: "01", isFile: true, size: 15 }, - { name: "10", isFile: true, size: 16 }, - { name: "a", isFile: true, size: 20 }, - { name: "b", isFile: true, size: 30 }, + items.find(i => i.name === 'folder'), + items.find(i => i.name === 'c'), + items.find(i => i.name === '0'), + items.find(i => i.name === '2'), + items.find(i => i.name === '1'), + items.find(i => i.name === '02'), + items.find(i => i.name === '01'), + items.find(i => i.name === '10'), + items.find(i => i.name === 'a'), + items.find(i => i.name === 'b'), + ]); + + output = misc.sortByCriteria(items, "byDateModified", true); + expect(output).toEqual([ + items.find(i => i.name === 'folder'), + items.find(i => i.name === 'c'), + items.find(i => i.name === '0'), + items.find(i => i.name === '2'), + items.find(i => i.name === '1'), + items.find(i => i.name === '02'), + items.find(i => i.name === '01'), + items.find(i => i.name === '10'), + items.find(i => i.name === 'a'), + items.find(i => i.name === 'b'), ]); }); }); diff --git a/packages/common/misc.js b/packages/common/misc.js index 95ca350b..8463b3bc 100644 --- a/packages/common/misc.js +++ b/packages/common/misc.js @@ -514,14 +514,22 @@ function sortBySize(a, b) { return aSize - bSize; } +function getTimestamp(value) { + if (value === undefined) { + return 0; // Default to epoch start if undefined + } + return value; // Already a timestamp +} + /** * @param a: TS.FileSystemEntry * @param b: TS.FileSystemEntry * @returns {number} */ function sortByDateModified(a, b) { - const aLmdt = a.lmdt | 0; - const bLmdt = b.lmdt | 0; + + const aLmdt = getTimestamp(a.lmdt); + const bLmdt = getTimestamp(b.lmdt); return aLmdt - bLmdt; } diff --git a/packages/common/package.json b/packages/common/package.json index 7cb9ce16..f37a2644 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -6,8 +6,8 @@ "license": "MIT", "scripts": { "clean-install": "npx rimraf package-lock.json && npx rimraf node_modules && npm install", - "npmpack-ts": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.3.tgz && npx shx cp -rL ./package/** ../../../tagspaces/node_modules/@tagspaces/tagspaces-common && npx rimraf package", - "npmpack-ts-main": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.3.tgz && npx shx cp -rL ./package/** ../../../tagspaces/release/app/node_modules/@tagspaces/tagspaces-common && npx rimraf package", + "npmpack-ts": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.23.tgz && npx shx cp -rL ./package/** ../../../tagspaces/node_modules/@tagspaces/tagspaces-common && npx rimraf package", + "npmpack-ts-main": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.23.tgz && npx shx cp -rL ./package/** ../../../tagspaces/release/app/node_modules/@tagspaces/tagspaces-common && npx rimraf package", "npmpack-webdav": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-1.3.13.tgz && npx shx cp -rL ./package/** ../common-webdav/node_modules/@tagspaces/tagspaces-common && npx rimraf package", "pkg-publish": "npm run clean-install && npm publish --access public" },