aboutsummaryrefslogtreecommitdiff
path: root/tests/history.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/history.test.ts')
-rw-r--r--tests/history.test.ts77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/history.test.ts b/tests/history.test.ts
new file mode 100644
index 0000000..ecd7785
--- /dev/null
+++ b/tests/history.test.ts
@@ -0,0 +1,77 @@
+
+
+
+/*** IMPORT ------------------------------------------- ***/
+
+import { assertEquals } from "jsr:@std/assert@^1.0.0";
+
+/*** UTILITY ------------------------------------------ ***/
+
+import { evict } from "../source/library/state/history-logic.ts";
+
+type Entry = {
+ favorite: boolean;
+ id: string;
+ timestamp: number;
+};
+
+function entry(id: string, timestamp: number, favorite = false): Entry {
+ return { favorite, id, timestamp };
+}
+
+/*** TESTS -------------------------------------------- ***/
+
+Deno.test("evict keeps everything when under cap", () => {
+ const entries = [entry("a", 3), entry("b", 2), entry("c", 1)];
+ assertEquals(evict(entries, 5), entries);
+});
+
+Deno.test("evict drops the oldest non-favorites above cap", () => {
+ const entries = [
+ entry("a", 5),
+ entry("b", 4),
+ entry("c", 3),
+ entry("d", 2),
+ entry("e", 1)
+ ];
+ const kept = evict(entries, 3);
+ assertEquals(kept.map((e) => e.id), ["a", "b", "c"]);
+});
+
+Deno.test("evict never drops favorites", () => {
+ const entries = [
+ entry("a", 10),
+ entry("b", 9),
+ entry("fav-old", 1, true),
+ entry("c", 8),
+ entry("d", 7)
+ ];
+ const kept = evict(entries, 3);
+
+ assertEquals(kept.some((e) => e.id === "fav-old"), true);
+ assertEquals(kept.length, 3);
+});
+
+Deno.test("evict can exceed cap when favorites alone do so", () => {
+ const entries = [
+ entry("fav-1", 5, true),
+ entry("fav-2", 4, true),
+ entry("fav-3", 3, true),
+ entry("regular", 2)
+ ];
+ const kept = evict(entries, 2);
+
+ assertEquals(kept.length, 3);
+ assertEquals(kept.every((e) => e.favorite), true);
+});
+
+Deno.test("evict sorts by timestamp descending", () => {
+ const entries = [
+ entry("c", 1),
+ entry("a", 3),
+ entry("b", 2),
+ entry("d", 0)
+ ];
+ const kept = evict(entries, 3);
+ assertEquals(kept.map((e) => e.id), ["a", "b", "c"]);
+});