CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/generic/test/patch-value-cache.test.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { PatchValueCache } from "../patch-value-cache";
7
import { StringDocument } from "../../string/doc";
8
9
describe("Test out an empty cache", () => {
10
let cache: PatchValueCache;
11
12
it("creates the empty cache", () => {
13
cache = new PatchValueCache();
14
expect(typeof cache).toBe("object"); // trivial check
15
});
16
17
it("invalidates the empty cache (no op)", () => {
18
cache.invalidate(new Date());
19
});
20
21
it("prunes the empty cache (no op)", () => {
22
cache.prune(0);
23
});
24
25
it("gets newest value after some point -- nothing there, so undefined", () => {
26
expect(cache.newest_value_at_most()).toBe(undefined);
27
});
28
29
it("gets cached entry at given point in time, so undefined", () => {
30
expect(cache.get(new Date())).toBe(undefined);
31
});
32
33
it("gets oldest cached time, so undefined since there are none", () => {
34
expect(cache.oldest_time()).toBe(undefined);
35
});
36
37
it("there are no cached values", () => {
38
expect(cache.size()).toBe(0);
39
});
40
});
41
42
describe("Test a cache with some contents", () => {
43
let cache: PatchValueCache;
44
45
const times: Date[] = [
46
new Date("2019-01-03T20:34"),
47
new Date("2019-01-03T20:40"),
48
new Date("2019-01-03T22:00"),
49
];
50
51
const values: StringDocument[] = [
52
new StringDocument("CoCalc"),
53
new StringDocument("SageMath"),
54
new StringDocument("SageMathCloud"),
55
];
56
57
const starts = [3, 10, 20];
58
59
it("creates a cache and populates it", () => {
60
cache = new PatchValueCache();
61
for (let n = 0; n <= 2; n++) {
62
cache.include(times[n], values[n], starts[n]);
63
}
64
});
65
66
it("gets oldest cached time", () => {
67
expect(cache.oldest_time()).toEqual(times[0]);
68
});
69
70
it("there are some cached values", () => {
71
expect(cache.size()).toEqual(times.length);
72
});
73
74
it("gets newest value after some point", () => {
75
for (let n = 0; n <= 2; n++) {
76
const v = cache.newest_value_at_most(times[n]);
77
expect(v != null).toBe(true);
78
if (v != null) {
79
expect(v.value.to_str()).toBe(values[n].to_str());
80
}
81
}
82
83
const v = cache.newest_value_at_most(new Date("2019-01-03T20:35"));
84
expect(v != null).toBe(true);
85
if (v != null) {
86
expect(v.value.to_str()).toBe(values[0].to_str());
87
}
88
});
89
90
it("gets cached entry at given point in time", () => {
91
for (let n = 0; n <= 2; n++) {
92
const e = cache.get(times[n]);
93
expect(e != null).toBe(true);
94
if (e != null) {
95
expect(e.value.to_str()).toBe(values[n].to_str());
96
}
97
}
98
});
99
100
it("invalidates a cached value", () => {
101
// this also invalidates cache for times[2] (which is later),
102
// hence the 1 below.
103
cache.invalidate(times[1]);
104
expect(cache.size()).toBe(1);
105
});
106
107
it("prunes the cache down to size 2 (from size 3)...", () => {
108
// put the two that gote removed above back:
109
for (let n = 1; n <= 2; n++) {
110
cache.include(times[n], values[n], starts[n]);
111
}
112
cache.prune(2);
113
expect(cache.size()).toBe(2);
114
});
115
});
116
117