aboutsummaryrefslogtreecommitdiff
path: root/tests/history.test.ts
blob: ecd77859b9cd45d3f613379619a316e8d9fcd056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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"]);
});