Path: blob/master/src/packages/sync/editor/generic/test/util.test.ts
1451 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6DEVELOPMENT:78pnpm test `pwd`/util.test.ts9*/1011import {12make_patch,13apply_patch,14patch_cmp,15three_way_merge,16time_cmp,17} from "../util";1819describe("test making and applying some patches on strings", () => {20const s0 = "This is CoCalc! Open source software. And a website.";21const s1 = "This is SageMath! Open source software.";22it("creates and applies a patch that applies cleanly", () => {23const patch = make_patch(s0, s1);24expect(patch).toEqual([25[26[27[0, " is "],28[-1, "CoCalc"],29[1, "SageMath"],30[0, "! Op"],31],324,334,3414,3516,36],37[38[39[0, "are."],40[-1, " And a website."],41],4235,4335,4420,454,46],47]);48expect(apply_patch(patch, s0)).toEqual([s1, true]); // true=clean49});5051it("creates a patch that does NOT apply cleanly", () => {52const patch = make_patch(s0, s1);53expect(apply_patch(patch, "This is CoCalc!")).toEqual([54"This is SageMath!",55false, // not clean56]);57});58});5960describe("test doing a 3-way merge", () => {61const base = "SageMathCloud is software for using Sage in the cloud.";6263// we change base in two different ways: first name64const local = "CoCalc is software for using Sage in the cloud.";65// .... then (independently) the purpose.66const remote =67"SageMathCloud is software for collaborative calculation in the cloud.";6869it("does a three-way merge", () => {70const merge = three_way_merge({ base, local, remote });71// Merging captures both changes.72expect(merge).toBe(73"CoCalc is software for collaborative calculation in the cloud."74);75});76});7778describe("Test comparison of patch log entries (compares time and user)", () => {79const p0 = {80time: new Date("2019-01-01T22:15:31.539Z").valueOf(),81patch: [],82user_id: 0,83size: 2,84};85const p1 = {86time: new Date("2019-01-01T22:15:40Z").valueOf(),87patch: [],88user_id: 1,89size: 2,90};91const p2 = {92time: new Date("2019-01-01T22:15:31.539Z").valueOf(),93patch: [],94user_id: 1,95size: 2,96};9798it("compares some patch log entries", () => {99expect(patch_cmp(p0, p0)).toBe(0);100expect(patch_cmp(p1, p1)).toBe(0);101expect(patch_cmp(p0, p1)).toBe(-1);102expect(patch_cmp(p1, p0)).toBe(1);103expect(patch_cmp(p0, p2)).toBe(-1);104expect(patch_cmp(p2, p0)).toBe(1);105});106});107108describe("Test comparing times", () => {109const t0 = new Date("2019-01-01T22:15:31.539Z");110const t1 = new Date("2019-01-01T22:15:40Z");111const t2 = new Date("2019-01-01T22:15:31.539Z");112113it("compares some times", () => {114expect(time_cmp(t0, t1)).toBe(-1);115expect(time_cmp(t1, t0)).toBe(1);116expect(time_cmp(t0, t0)).toBe(0);117expect(time_cmp(t0, t2)).toBe(0);118expect(time_cmp(t2, t0)).toBe(0);119});120});121122123