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/util.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 {6make_patch,7apply_patch,8patch_cmp,9three_way_merge,10time_cmp,11} from "../util";1213describe("test making and applying some patches on strings", () => {14const s0 = "This is CoCalc! Open source software. And a website.";15const s1 = "This is SageMath! Open source software.";16it("creates and applies a patch that applies cleanly", () => {17const patch = make_patch(s0, s1);18expect(patch).toEqual([19[20[21[0, " is "],22[-1, "CoCalc"],23[1, "SageMath"],24[0, "! Op"],25],264,274,2814,2916,30],31[32[33[0, "are."],34[-1, " And a website."],35],3635,3735,3820,394,40],41]);42expect(apply_patch(patch, s0)).toEqual([s1, true]); // true=clean43});4445it("creates a patch that does NOT apply cleanly", () => {46const patch = make_patch(s0, s1);47expect(apply_patch(patch, "This is CoCalc!")).toEqual([48"This is SageMath!",49false, // not clean50]);51});52});5354describe("test doing a 3-way merge", () => {55const base = "SageMathCloud is software for using Sage in the cloud.";5657// we change base in two different ways: first name58const local = "CoCalc is software for using Sage in the cloud.";59// .... then (independently) the purpose.60const remote =61"SageMathCloud is software for collaborative calculation in the cloud.";6263it("does a three-way merge", () => {64const merge = three_way_merge({ base, local, remote });65// Merging captures both changes.66expect(merge).toBe(67"CoCalc is software for collaborative calculation in the cloud."68);69});70});7172describe("Test comparison of patch log entries (compares time and user)", () => {73const p0 = {74time: new Date("2019-01-01T22:15:31.539Z"),75patch: [],76user_id: 0,77size: 2,78};79const p1 = {80time: new Date("2019-01-01T22:15:40Z"),81patch: [],82user_id: 1,83size: 2,84};85const p2 = {86time: new Date("2019-01-01T22:15:31.539Z"),87patch: [],88user_id: 1,89size: 2,90};9192it("compares some patch log entries", () => {93expect(patch_cmp(p0, p0)).toBe(0);94expect(patch_cmp(p1, p1)).toBe(0);95expect(patch_cmp(p0, p1)).toBe(-1);96expect(patch_cmp(p1, p0)).toBe(1);97expect(patch_cmp(p0, p2)).toBe(-1);98expect(patch_cmp(p2, p0)).toBe(1);99});100});101102describe("Test comparing times", () => {103const t0 = new Date("2019-01-01T22:15:31.539Z");104const t1 = new Date("2019-01-01T22:15:40Z");105const t2 = new Date("2019-01-01T22:15:31.539Z");106107it("compares some times", () => {108expect(time_cmp(t0, t1)).toBe(-1);109expect(time_cmp(t1, t0)).toBe(1);110expect(time_cmp(t0, t0)).toBe(0);111expect(time_cmp(t0, t2)).toBe(0);112expect(time_cmp(t2, t0)).toBe(0);113});114});115116117