From e007f2592c75f0d218e8840ec999960ee76665fe Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 26 Mar 2024 07:55:23 +0500 Subject: [PATCH] core: add extensive grouping/sorting tests --- .../__snapshots__/notes.test.ts.snap | 1067 +++++++++++++++++ packages/core/__tests__/notes.test.ts | 362 +++--- packages/core/src/database/sql-collection.ts | 3 +- 3 files changed, 1290 insertions(+), 142 deletions(-) create mode 100644 packages/core/__tests__/__snapshots__/notes.test.ts.snap diff --git a/packages/core/__tests__/__snapshots__/notes.test.ts.snap b/packages/core/__tests__/__snapshots__/notes.test.ts.snap new file mode 100644 index 000000000..a9c5da9f9 --- /dev/null +++ b/packages/core/__tests__/__snapshots__/notes.test.ts.snap @@ -0,0 +1,1067 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`group notes by abc, sort by title, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: -", + "-7", + "GROUP: .", + ".9", + "GROUP: /", + "/A", + "GROUP: 0", + "0O", + "GROUP: 1", + "1N", + "GROUP: 2", + "2M", + "GROUP: 3", + "3L", + "GROUP: 4", + "4K", + "GROUP: 5", + "5J", + "GROUP: 6", + "6I", + "6undefined", + "GROUP: 7", + "7-", + "7H", + "GROUP: 8", + "8=", + "8G", + "GROUP: 9", + "9.", + "9Q", + "GROUP: =", + "=8", + "GROUP: A", + "A/", + "AP", + "GROUP: B", + "BO", + "BZ", + "GROUP: C", + "CN", + "CY", + "GROUP: D", + "DM", + "DX", + "GROUP: E", + "EW", + "EW", + "GROUP: F", + "FV", + "FV", + "GROUP: G", + "G8", + "GU", + "GU", + "GROUP: H", + "H7", + "HT", + "HT", + "GROUP: I", + "I6", + "IS", + "IS", + "GROUP: J", + "J5", + "JR", + "JR", + "GROUP: K", + "K4", + "KQ", + "KQ", + "GROUP: L", + "L3", + "LP", + "LP", + "GROUP: M", + "M2", + "MD", + "MO", + "MO", + "GROUP: N", + "N1", + "NC", + "NN", + "NN", + "GROUP: O", + "O0", + "OB", + "OM", + "OM", + "GROUP: P", + "PA", + "PL", + "PL", + "PS", + "GROUP: Q", + "Q9", + "QK", + "QK", + "QR", + "GROUP: R", + "RJ", + "RJ", + "RQ", + "GROUP: S", + "SI", + "SI", + "SP", + "GROUP: T", + "TH", + "TH", + "GROUP: U", + "UG", + "UG", + "GROUP: V", + "VF", + "VF", + "GROUP: W", + "WE", + "WE", + "GROUP: X", + "XD", + "GROUP: Y", + "YC", + "GROUP: Z", + "ZB", +] +`; + +exports[`group notes by abc, sort by title, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: Z", + "ZB", + "GROUP: Y", + "YC", + "GROUP: X", + "XD", + "GROUP: W", + "WE", + "WE", + "GROUP: V", + "VF", + "VF", + "GROUP: U", + "UG", + "UG", + "GROUP: T", + "TH", + "TH", + "GROUP: S", + "SP", + "SI", + "SI", + "GROUP: R", + "RQ", + "RJ", + "RJ", + "GROUP: Q", + "QR", + "QK", + "QK", + "Q9", + "GROUP: P", + "PS", + "PL", + "PL", + "PA", + "GROUP: O", + "OM", + "OM", + "OB", + "O0", + "GROUP: N", + "NN", + "NN", + "NC", + "N1", + "GROUP: M", + "MO", + "MO", + "MD", + "M2", + "GROUP: L", + "LP", + "LP", + "L3", + "GROUP: K", + "KQ", + "KQ", + "K4", + "GROUP: J", + "JR", + "JR", + "J5", + "GROUP: I", + "IS", + "IS", + "I6", + "GROUP: H", + "HT", + "HT", + "H7", + "GROUP: G", + "GU", + "GU", + "G8", + "GROUP: F", + "FV", + "FV", + "GROUP: E", + "EW", + "EW", + "GROUP: D", + "DX", + "DM", + "GROUP: C", + "CY", + "CN", + "GROUP: B", + "BZ", + "BO", + "GROUP: A", + "AP", + "A/", + "GROUP: =", + "=8", + "GROUP: 9", + "9Q", + "9.", + "GROUP: 8", + "8G", + "8=", + "GROUP: 7", + "7H", + "7-", + "GROUP: 6", + "6undefined", + "6I", + "GROUP: 5", + "5J", + "GROUP: 4", + "4K", + "GROUP: 3", + "3L", + "GROUP: 2", + "2M", + "GROUP: 1", + "1N", + "GROUP: 0", + "0O", + "GROUP: /", + "/A", + "GROUP: .", + ".9", + "GROUP: -", + "-7", +] +`; + +exports[`group notes by default, sort by dateCreated, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: Older", + "Note in Older - 27", + "Note in Older - 26", + "Note in Older - 25", + "Note in Older - 24", + "Note in Older - 23", + "Note in Older - 22", + "Note in Older - 21", + "Note in Older - 20", + "Note in Older - 19", + "Note in Older - 18", + "Note in Older - 17", + "Note in Older - 16", + "Note in Older - 15", + "Note in Older - 14", + "GROUP: Last week", + "Note in Last week - 13", + "Note in Last week - 12", + "Note in Last week - 11", + "Note in Last week - 10", + "Note in Last week - 9", + "Note in Last week - 8", + "Note in Last week - 7", + "GROUP: Recent", + "Note in Recent - 6", + "Note in Recent - 5", + "Note in Recent - 4", + "Note in Recent - 3", + "Note in Recent - 2", + "Note in Recent - 1", + "Note in Recent - 0", +] +`; + +exports[`group notes by default, sort by dateCreated, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: Recent", + "Note in Recent - 0", + "Note in Recent - 1", + "Note in Recent - 2", + "Note in Recent - 3", + "Note in Recent - 4", + "Note in Recent - 5", + "Note in Recent - 6", + "GROUP: Last week", + "Note in Last week - 7", + "Note in Last week - 8", + "Note in Last week - 9", + "Note in Last week - 10", + "Note in Last week - 11", + "Note in Last week - 12", + "Note in Last week - 13", + "GROUP: Older", + "Note in Older - 14", + "Note in Older - 15", + "Note in Older - 16", + "Note in Older - 17", + "Note in Older - 18", + "Note in Older - 19", + "Note in Older - 20", + "Note in Older - 21", + "Note in Older - 22", + "Note in Older - 23", + "Note in Older - 24", + "Note in Older - 25", + "Note in Older - 26", + "Note in Older - 27", +] +`; + +exports[`group notes by default, sort by dateEdited, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: Older", + "Note in Older - 27", + "Note in Older - 26", + "Note in Older - 25", + "Note in Older - 24", + "Note in Older - 23", + "Note in Older - 22", + "Note in Older - 21", + "Note in Older - 20", + "Note in Older - 19", + "Note in Older - 18", + "Note in Older - 17", + "Note in Older - 16", + "Note in Older - 15", + "Note in Older - 14", + "GROUP: Last week", + "Note in Last week - 13", + "Note in Last week - 12", + "Note in Last week - 11", + "Note in Last week - 10", + "Note in Last week - 9", + "Note in Last week - 8", + "Note in Last week - 7", + "GROUP: Recent", + "Note in Recent - 6", + "Note in Recent - 5", + "Note in Recent - 4", + "Note in Recent - 3", + "Note in Recent - 2", + "Note in Recent - 1", + "Note in Recent - 0", +] +`; + +exports[`group notes by default, sort by dateEdited, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: Recent", + "Note in Recent - 0", + "Note in Recent - 1", + "Note in Recent - 2", + "Note in Recent - 3", + "Note in Recent - 4", + "Note in Recent - 5", + "Note in Recent - 6", + "GROUP: Last week", + "Note in Last week - 7", + "Note in Last week - 8", + "Note in Last week - 9", + "Note in Last week - 10", + "Note in Last week - 11", + "Note in Last week - 12", + "Note in Last week - 13", + "GROUP: Older", + "Note in Older - 14", + "Note in Older - 15", + "Note in Older - 16", + "Note in Older - 17", + "Note in Older - 18", + "Note in Older - 19", + "Note in Older - 20", + "Note in Older - 21", + "Note in Older - 22", + "Note in Older - 23", + "Note in Older - 24", + "Note in Older - 25", + "Note in Older - 26", + "Note in Older - 27", +] +`; + +exports[`group notes by month, sort by dateCreated, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: April 2024", + "Note in 3 - 1", + "GROUP: June 2024", + "Note in 5 - 3", + "GROUP: July 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: September 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: October 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: November 2024", + "Note in 10 - 8", + "GROUP: December 2024", + "Note in 11 - 9", + "GROUP: January 2025", + "Note in 0 - 10", + "GROUP: February 2025", + "Note in 1 - 11", +] +`; + +exports[`group notes by month, sort by dateCreated, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: February 2025", + "Note in 1 - 11", + "GROUP: January 2025", + "Note in 0 - 10", + "GROUP: December 2024", + "Note in 11 - 9", + "GROUP: November 2024", + "Note in 10 - 8", + "GROUP: October 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: September 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: July 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: June 2024", + "Note in 5 - 3", + "GROUP: April 2024", + "Note in 3 - 1", +] +`; + +exports[`group notes by month, sort by dateEdited, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: April 2024", + "Note in 3 - 1", + "GROUP: June 2024", + "Note in 5 - 3", + "GROUP: July 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: September 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: October 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: November 2024", + "Note in 10 - 8", + "GROUP: December 2024", + "Note in 11 - 9", + "GROUP: January 2025", + "Note in 0 - 10", + "GROUP: February 2025", + "Note in 1 - 11", +] +`; + +exports[`group notes by month, sort by dateEdited, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: February 2025", + "Note in 1 - 11", + "GROUP: January 2025", + "Note in 0 - 10", + "GROUP: December 2024", + "Note in 11 - 9", + "GROUP: November 2024", + "Note in 10 - 8", + "GROUP: October 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: September 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: July 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: June 2024", + "Note in 5 - 3", + "GROUP: April 2024", + "Note in 3 - 1", +] +`; + +exports[`group notes by none, sort by dateCreated, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: All", + "Note in 3 - 1", + "Note in 5 - 3", + "Note in 6 - 4", + "Note in 6 - 4", + "Note in 8 - 6", + "Note in 8 - 6", + "Note in 9 - 7", + "Note in 9 - 7", + "Note in 10 - 8", + "Note in 11 - 9", + "Note in 0 - 10", + "Note in 1 - 11", +] +`; + +exports[`group notes by none, sort by dateCreated, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: All", + "Note in 1 - 11", + "Note in 0 - 10", + "Note in 11 - 9", + "Note in 10 - 8", + "Note in 9 - 7", + "Note in 9 - 7", + "Note in 8 - 6", + "Note in 8 - 6", + "Note in 6 - 4", + "Note in 6 - 4", + "Note in 5 - 3", + "Note in 3 - 1", +] +`; + +exports[`group notes by none, sort by dateEdited, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: All", + "Note in 3 - 1", + "Note in 5 - 3", + "Note in 6 - 4", + "Note in 6 - 4", + "Note in 8 - 6", + "Note in 8 - 6", + "Note in 9 - 7", + "Note in 9 - 7", + "Note in 10 - 8", + "Note in 11 - 9", + "Note in 0 - 10", + "Note in 1 - 11", +] +`; + +exports[`group notes by none, sort by dateEdited, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: All", + "Note in 1 - 11", + "Note in 0 - 10", + "Note in 11 - 9", + "Note in 10 - 8", + "Note in 9 - 7", + "Note in 9 - 7", + "Note in 8 - 6", + "Note in 8 - 6", + "Note in 6 - 4", + "Note in 6 - 4", + "Note in 5 - 3", + "Note in 3 - 1", +] +`; + +exports[`group notes by none, sort by title, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: All", + "-7", + ".9", + "/A", + "0O", + "1N", + "2M", + "3L", + "4K", + "5J", + "6I", + "6undefined", + "7-", + "7H", + "8=", + "8G", + "9.", + "9Q", + "=8", + "A/", + "AP", + "BO", + "BZ", + "CN", + "CY", + "DM", + "DX", + "EW", + "EW", + "FV", + "FV", + "G8", + "GU", + "GU", + "H7", + "HT", + "HT", + "I6", + "IS", + "IS", + "J5", + "JR", + "JR", + "K4", + "KQ", + "KQ", + "L3", + "LP", + "LP", + "M2", + "MD", + "MO", + "MO", + "N1", + "NC", + "NN", + "NN", + "O0", + "OB", + "OM", + "OM", + "PA", + "PL", + "PL", + "PS", + "Q9", + "QK", + "QK", + "QR", + "RJ", + "RJ", + "RQ", + "SI", + "SI", + "SP", + "TH", + "TH", + "UG", + "UG", + "VF", + "VF", + "WE", + "WE", + "XD", + "YC", + "ZB", +] +`; + +exports[`group notes by none, sort by title, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: All", + "ZB", + "YC", + "XD", + "WE", + "WE", + "VF", + "VF", + "UG", + "UG", + "TH", + "TH", + "SP", + "SI", + "SI", + "RQ", + "RJ", + "RJ", + "QR", + "QK", + "QK", + "Q9", + "PS", + "PL", + "PL", + "PA", + "OM", + "OM", + "OB", + "O0", + "NN", + "NN", + "NC", + "N1", + "MO", + "MO", + "MD", + "M2", + "LP", + "LP", + "L3", + "KQ", + "KQ", + "K4", + "JR", + "JR", + "J5", + "IS", + "IS", + "I6", + "HT", + "HT", + "H7", + "GU", + "GU", + "G8", + "FV", + "FV", + "EW", + "EW", + "DX", + "DM", + "CY", + "CN", + "BZ", + "BO", + "AP", + "A/", + "=8", + "9Q", + "9.", + "8G", + "8=", + "7H", + "7-", + "6undefined", + "6I", + "5J", + "4K", + "3L", + "2M", + "1N", + "0O", + "/A", + ".9", + "-7", +] +`; + +exports[`group notes by week, sort by dateCreated, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 22 - 28 Apr, 2024", + "Note in 3 - 1", + "GROUP: 24 - 30 Jun, 2024", + "Note in 5 - 3", + "GROUP: 22 - 28 Jul, 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: 23 - 29 Sep, 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: 21 - 27 Oct, 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: 25 Nov - 1 Dec, 2024", + "Note in 10 - 8", + "GROUP: 23 - 29 Dec, 2024", + "Note in 11 - 9", + "GROUP: 20 - 26 Jan, 2025", + "Note in 0 - 10", + "GROUP: 24 Feb - 2 Mar, 2025", + "Note in 1 - 11", +] +`; + +exports[`group notes by week, sort by dateCreated, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 24 Feb - 2 Mar, 2025", + "Note in 1 - 11", + "GROUP: 20 - 26 Jan, 2025", + "Note in 0 - 10", + "GROUP: 23 - 29 Dec, 2024", + "Note in 11 - 9", + "GROUP: 25 Nov - 1 Dec, 2024", + "Note in 10 - 8", + "GROUP: 21 - 27 Oct, 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: 23 - 29 Sep, 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: 22 - 28 Jul, 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: 24 - 30 Jun, 2024", + "Note in 5 - 3", + "GROUP: 22 - 28 Apr, 2024", + "Note in 3 - 1", +] +`; + +exports[`group notes by week, sort by dateEdited, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 22 - 28 Apr, 2024", + "Note in 3 - 1", + "GROUP: 24 - 30 Jun, 2024", + "Note in 5 - 3", + "GROUP: 22 - 28 Jul, 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: 23 - 29 Sep, 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: 21 - 27 Oct, 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: 25 Nov - 1 Dec, 2024", + "Note in 10 - 8", + "GROUP: 23 - 29 Dec, 2024", + "Note in 11 - 9", + "GROUP: 20 - 26 Jan, 2025", + "Note in 0 - 10", + "GROUP: 24 Feb - 2 Mar, 2025", + "Note in 1 - 11", +] +`; + +exports[`group notes by week, sort by dateEdited, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 24 Feb - 2 Mar, 2025", + "Note in 1 - 11", + "GROUP: 20 - 26 Jan, 2025", + "Note in 0 - 10", + "GROUP: 23 - 29 Dec, 2024", + "Note in 11 - 9", + "GROUP: 25 Nov - 1 Dec, 2024", + "Note in 10 - 8", + "GROUP: 21 - 27 Oct, 2024", + "Note in 9 - 7", + "Note in 9 - 7", + "GROUP: 23 - 29 Sep, 2024", + "Note in 8 - 6", + "Note in 8 - 6", + "GROUP: 22 - 28 Jul, 2024", + "Note in 6 - 4", + "Note in 6 - 4", + "GROUP: 24 - 30 Jun, 2024", + "Note in 5 - 3", + "GROUP: 22 - 28 Apr, 2024", + "Note in 3 - 1", +] +`; + +exports[`group notes by year, sort by dateCreated, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 2025", + "Note in 2025 - 1", + "GROUP: 2027", + "Note in 2027 - 3", + "GROUP: 2028", + "Note in 2028 - 4", + "Note in 2028 - 4", + "GROUP: 2030", + "Note in 2030 - 6", + "Note in 2030 - 6", + "GROUP: 2031", + "Note in 2031 - 7", + "Note in 2031 - 7", + "GROUP: 2032", + "Note in 2032 - 8", + "GROUP: 2033", + "Note in 2033 - 9", + "GROUP: 2034", + "Note in 2034 - 10", + "GROUP: 2035", + "Note in 2035 - 11", +] +`; + +exports[`group notes by year, sort by dateCreated, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 2035", + "Note in 2035 - 11", + "GROUP: 2034", + "Note in 2034 - 10", + "GROUP: 2033", + "Note in 2033 - 9", + "GROUP: 2032", + "Note in 2032 - 8", + "GROUP: 2031", + "Note in 2031 - 7", + "Note in 2031 - 7", + "GROUP: 2030", + "Note in 2030 - 6", + "Note in 2030 - 6", + "GROUP: 2028", + "Note in 2028 - 4", + "Note in 2028 - 4", + "GROUP: 2027", + "Note in 2027 - 3", + "GROUP: 2025", + "Note in 2025 - 1", +] +`; + +exports[`group notes by year, sort by dateEdited, direction asc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 2025", + "Note in 2025 - 1", + "GROUP: 2027", + "Note in 2027 - 3", + "GROUP: 2028", + "Note in 2028 - 4", + "Note in 2028 - 4", + "GROUP: 2030", + "Note in 2030 - 6", + "Note in 2030 - 6", + "GROUP: 2031", + "Note in 2031 - 7", + "Note in 2031 - 7", + "GROUP: 2032", + "Note in 2032 - 8", + "GROUP: 2033", + "Note in 2033 - 9", + "GROUP: 2034", + "Note in 2034 - 10", + "GROUP: 2035", + "Note in 2035 - 11", +] +`; + +exports[`group notes by year, sort by dateEdited, direction desc 1`] = ` +[ + "GROUP: Conflicted", + "Conflicted", + "GROUP: Pinned", + "Pinned note", + "GROUP: 2035", + "Note in 2035 - 11", + "GROUP: 2034", + "Note in 2034 - 10", + "GROUP: 2033", + "Note in 2033 - 9", + "GROUP: 2032", + "Note in 2032 - 8", + "GROUP: 2031", + "Note in 2031 - 7", + "Note in 2031 - 7", + "GROUP: 2030", + "Note in 2030 - 6", + "Note in 2030 - 6", + "GROUP: 2028", + "Note in 2028 - 4", + "Note in 2028 - 4", + "GROUP: 2027", + "Note in 2027 - 3", + "GROUP: 2025", + "Note in 2025 - 1", +] +`; diff --git a/packages/core/__tests__/notes.test.ts b/packages/core/__tests__/notes.test.ts index f6b5624ea..3b3dc0040 100644 --- a/packages/core/__tests__/notes.test.ts +++ b/packages/core/__tests__/notes.test.ts @@ -30,6 +30,7 @@ import { } from "./utils"; import { test, expect } from "vitest"; import { MONTHS_FULL } from "../src/utils/date"; +import { GroupOptions, Note } from "../src/types"; async function createAndAddNoteToNotebook( db: Database, @@ -380,148 +381,227 @@ test("adding a note with an invalid tag should clean the tag array", () => ).toBe(0); })); -test("get grouped notes by abc", () => - databaseTest().then(async (db) => { - const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - for (const letter of alphabet) { - for (const letter2 of alphabet) { - await db.notes.add({ title: `${letter}${letter2}` }); - } - } - await db.notes.add({ title: `Pinned note`, pinned: true }); - await db.notes.add({ - title: `Conflicted`, - conflicted: true - }); - const grouping = await db.notes.all.grouped({ - groupBy: "abc", - sortDirection: "asc", - sortBy: "title" - }); +const groups: { notes: () => Partial[]; groupOptions: GroupOptions[] }[] = + [ + { + notes: () => { + const alphabet = + "6789ABCDEFGHIJKLMNOPQRSTUVWMNOPQGHIJKLMNOPQRS0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/.=-"; + const notes: Partial[] = []; + for (let i = 0; i < alphabet.length; ++i) { + const letter = alphabet[i]; + const letter2 = alphabet[alphabet.length - i]; + notes.push({ title: `${letter}${letter2}` }); + } + return notes; + }, + groupOptions: [ + { + groupBy: "abc", + sortDirection: "asc", + sortBy: "title" + }, + { + groupBy: "abc", + sortDirection: "desc", + sortBy: "title" + }, + { + groupBy: "none", + sortDirection: "asc", + sortBy: "title" + }, + { + groupBy: "none", + sortDirection: "desc", + sortBy: "title" + } + ] + }, + { + notes: () => { + const notes: Partial[] = []; + const now = dayjs(1711420693667); + const months = [3, 8, 6, 4, 7, 9, 1, 4, 6, 7, 10, 11]; + for (const month of months) { + const date = now.add(month, "month").valueOf(); + notes.push({ + title: `Note in ${now.add(month, "month").month()} - ${month}`, + dateCreated: date, + dateEdited: date + }); + } + return notes; + }, + groupOptions: [ + { + groupBy: "month", + sortDirection: "desc", + sortBy: "dateCreated" + }, + { + groupBy: "month", + sortDirection: "asc", + sortBy: "dateCreated" + }, + { + groupBy: "month", + sortDirection: "asc", + sortBy: "dateEdited" + }, + { + groupBy: "month", + sortDirection: "desc", + sortBy: "dateEdited" + }, + { + groupBy: "none", + sortDirection: "desc", + sortBy: "dateEdited" + }, + { + groupBy: "none", + sortDirection: "asc", + sortBy: "dateEdited" + }, + { + groupBy: "none", + sortDirection: "asc", + sortBy: "dateCreated" + }, + { + groupBy: "none", + sortDirection: "desc", + sortBy: "dateCreated" + }, + { + groupBy: "week", + sortDirection: "asc", + sortBy: "dateCreated" + }, + { + groupBy: "week", + sortDirection: "desc", + sortBy: "dateCreated" + }, + { + groupBy: "week", + sortDirection: "asc", + sortBy: "dateEdited" + }, + { + groupBy: "week", + sortDirection: "desc", + sortBy: "dateEdited" + } + ] + }, + { + notes: () => { + const notes: Partial[] = []; + const now = dayjs(1711420693667); + const years = [3, 8, 6, 4, 7, 9, 1, 4, 6, 7, 10, 11]; + for (const year of years) { + const date = now.add(year, "year").valueOf(); + notes.push({ + title: `Note in ${now.add(year, "year").year()} - ${year}`, + dateCreated: date, + dateEdited: date + }); + } + return notes; + }, + groupOptions: [ + { + groupBy: "year", + sortDirection: "desc", + sortBy: "dateCreated" + }, + { + groupBy: "year", + sortDirection: "asc", + sortBy: "dateCreated" + }, + { + groupBy: "year", + sortDirection: "asc", + sortBy: "dateEdited" + }, + { + groupBy: "year", + sortDirection: "desc", + sortBy: "dateEdited" + } + ] + }, + { + notes: () => { + const notes: Partial[] = []; + const ranges = { + Recent: [0, 7], + "Last week": [7, 14], + Older: [14, 28] + }; + for (const key in ranges) { + const range = ranges[key]; + for (let i = range[0]; i < range[1]; i++) { + const date = dayjs().subtract(i, "days").toDate().getTime(); - expect((await grouping.item(0))?.group?.title).toBe("Conflicted"); - expect((await grouping.item(1))?.group?.title).toBe("Pinned"); - for (let i = 0; i < alphabet.length; ++i) { - expect((await grouping.item(i * alphabet.length + 2))?.group?.title).toBe( - alphabet[i] - ); - } - })); + notes.push({ + title: `Note in ${key} - ${i}`, + dateCreated: date, + dateEdited: date + }); + } + } -test("get grouped notes by month", () => - databaseTest().then(async (db) => { - for (let month = 0; month <= 11; ++month) { - for (let i = 0; i < 5; ++i) { - const date = dayjs().month(month).toDate().getTime(); + return notes; + }, + groupOptions: [ + { + groupBy: "default", + sortDirection: "desc", + sortBy: "dateCreated" + }, + { + groupBy: "default", + sortDirection: "asc", + sortBy: "dateCreated" + }, + { + groupBy: "default", + sortDirection: "asc", + sortBy: "dateEdited" + }, + { + groupBy: "default", + sortDirection: "desc", + sortBy: "dateEdited" + } + ] + } + ]; + +for (const group of groups) { + const notes = group.notes(); + for (const groupOptions of group.groupOptions) { + const title = `group notes by ${groupOptions.groupBy}, sort by ${groupOptions.sortBy}, direction ${groupOptions.sortDirection}`; + test(`${title}`, () => + databaseTest().then(async (db) => { + await Promise.all(notes.map((note) => db.notes.add(note))); + await db.notes.add({ title: `Pinned note`, pinned: true }); await db.notes.add({ - title: `Note in ${month} - ${i}`, - dateCreated: date + title: `Conflicted`, + conflicted: true }); - } - } - - const grouping = await db.notes.all.grouped({ - groupBy: "month", - sortDirection: "desc", - sortBy: "dateCreated" - }); - - for (let month = 11; month >= 0; --month) { - expect((await grouping.item((11 - month) * 5))?.group?.title).toContain( - MONTHS_FULL[month] - ); - } - })); - -test("get grouped notes by year", () => - databaseTest().then(async (db) => { - for (let year = 2020; year <= 2025; ++year) { - for (let i = 0; i < 5; ++i) { - const date = dayjs().year(year).toDate().getTime(); - await db.notes.add({ - title: `Note in ${year} - ${i}`, - dateCreated: date - }); - } - } - - const grouping = await db.notes.all.grouped({ - groupBy: "year", - sortDirection: "desc", - sortBy: "dateCreated" - }); - - for (let year = 2020; year <= 2025; ++year) { - expect((await grouping.item((2025 - year) * 5))?.group?.title).toBe( - year.toString() - ); - } - })); - -test("get grouped notes by week", () => - databaseTest().then(async (db) => { - for (let i = 1; i <= 6; ++i) { - for (let week = 1; week <= 4; ++week) { - const date = dayjs() - .month(i) - .date(week * 7) - .year(2023) - .startOf("week") - .toDate() - .getTime(); - await db.notes.add({ - title: `Note in ${week} - ${i}`, - dateCreated: date - }); - } - } - - const grouping = await db.notes.all.grouped({ - groupBy: "week", - sortDirection: "desc", - sortBy: "dateCreated" - }); - - const weeks = [ - "19 - 25 Jun, 2023", - "22 - 28 May, 2023", - "17 - 23 Apr, 2023", - "20 - 26 Mar, 2023", - "20 - 26 Feb, 2023" - ]; - for (let i = 1; i <= 5; ++i) { - expect((await grouping.item(i * 4))?.group?.title).toBe(weeks[i - 1]); - } - })); - -test("get grouped notes default", () => - databaseTest().then(async (db) => { - const ranges = { - Recent: [0, 7], - "Last week": [7, 14], - Older: [14, 28] - }; - for (const key in ranges) { - const range = ranges[key]; - for (let i = range[0]; i < range[1]; i++) { - const date = dayjs().subtract(i, "days").toDate().getTime(); - - await db.notes.add({ - title: `Note in ${key} - ${i}`, - dateCreated: date - }); - } - } - - const grouping = await db.notes.all.grouped({ - groupBy: "default", - sortDirection: "desc", - sortBy: "dateCreated" - }); - - let i = 0; - for (const key in ranges) { - expect((await grouping.item(i * 7))?.group?.title).toBe(key); - ++i; - } - })); + const grouping = await db.notes.all.grouped(groupOptions); + const items: string[] = []; + for (let i = 0; i < grouping.length; i++) { + const item = await grouping.item(i); + if (item.group) items.push(`GROUP: ${item.group.title}`); + if (item.item) items.push(item.item.title); + } + expect(items).toMatchSnapshot(); + })); + } +} diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.ts index f8aa257fd..6f63a65bd 100644 --- a/packages/core/src/database/sql-collection.ts +++ b/packages/core/src/database/sql-collection.ts @@ -527,7 +527,8 @@ export class FilteredSelector { const sortBy: Set = new Set(); if (isGroupOptions(options)) { if (options.groupBy === "abc") sortBy.add("title"); - else if (options.sortBy === "title") sortBy.add("dateCreated"); + else if (options.sortBy === "title" && options.groupBy !== "none") + sortBy.add("dateCreated"); } sortBy.add(options.sortBy);