Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/sync/editor/generic/test/patch-value-cache.test.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { PatchValueCache } from "../patch-value-cache";6import { StringDocument } from "../../string/doc";78describe("Test out an empty cache", () => {9let cache: PatchValueCache;1011it("creates the empty cache", () => {12cache = new PatchValueCache();13expect(typeof cache).toBe("object"); // trivial check14});1516it("invalidates the empty cache (no op)", () => {17cache.invalidate(new Date());18});1920it("prunes the empty cache (no op)", () => {21cache.prune(0);22});2324it("gets newest value after some point -- nothing there, so undefined", () => {25expect(cache.newest_value_at_most()).toBe(undefined);26});2728it("gets cached entry at given point in time, so undefined", () => {29expect(cache.get(new Date())).toBe(undefined);30});3132it("gets oldest cached time, so undefined since there are none", () => {33expect(cache.oldest_time()).toBe(undefined);34});3536it("there are no cached values", () => {37expect(cache.size()).toBe(0);38});39});4041describe("Test a cache with some contents", () => {42let cache: PatchValueCache;4344const times: Date[] = [45new Date("2019-01-03T20:34"),46new Date("2019-01-03T20:40"),47new Date("2019-01-03T22:00"),48];4950const values: StringDocument[] = [51new StringDocument("CoCalc"),52new StringDocument("SageMath"),53new StringDocument("SageMathCloud"),54];5556const starts = [3, 10, 20];5758it("creates a cache and populates it", () => {59cache = new PatchValueCache();60for (let n = 0; n <= 2; n++) {61cache.include(times[n], values[n], starts[n]);62}63});6465it("gets oldest cached time", () => {66expect(cache.oldest_time()).toEqual(times[0]);67});6869it("there are some cached values", () => {70expect(cache.size()).toEqual(times.length);71});7273it("gets newest value after some point", () => {74for (let n = 0; n <= 2; n++) {75const v = cache.newest_value_at_most(times[n]);76expect(v != null).toBe(true);77if (v != null) {78expect(v.value.to_str()).toBe(values[n].to_str());79}80}8182const v = cache.newest_value_at_most(new Date("2019-01-03T20:35"));83expect(v != null).toBe(true);84if (v != null) {85expect(v.value.to_str()).toBe(values[0].to_str());86}87});8889it("gets cached entry at given point in time", () => {90for (let n = 0; n <= 2; n++) {91const e = cache.get(times[n]);92expect(e != null).toBe(true);93if (e != null) {94expect(e.value.to_str()).toBe(values[n].to_str());95}96}97});9899it("invalidates a cached value", () => {100// this also invalidates cache for times[2] (which is later),101// hence the 1 below.102cache.invalidate(times[1]);103expect(cache.size()).toBe(1);104});105106it("prunes the cache down to size 2 (from size 3)...", () => {107// put the two that gote removed above back:108for (let n = 1; n <= 2; n++) {109cache.include(times[n], values[n], starts[n]);110}111cache.prune(2);112expect(cache.size()).toBe(2);113});114});115116117