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/util.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 {
7
make_patch,
8
apply_patch,
9
patch_cmp,
10
three_way_merge,
11
time_cmp,
12
} from "../util";
13
14
describe("test making and applying some patches on strings", () => {
15
const s0 = "This is CoCalc! Open source software. And a website.";
16
const s1 = "This is SageMath! Open source software.";
17
it("creates and applies a patch that applies cleanly", () => {
18
const patch = make_patch(s0, s1);
19
expect(patch).toEqual([
20
[
21
[
22
[0, " is "],
23
[-1, "CoCalc"],
24
[1, "SageMath"],
25
[0, "! Op"],
26
],
27
4,
28
4,
29
14,
30
16,
31
],
32
[
33
[
34
[0, "are."],
35
[-1, " And a website."],
36
],
37
35,
38
35,
39
20,
40
4,
41
],
42
]);
43
expect(apply_patch(patch, s0)).toEqual([s1, true]); // true=clean
44
});
45
46
it("creates a patch that does NOT apply cleanly", () => {
47
const patch = make_patch(s0, s1);
48
expect(apply_patch(patch, "This is CoCalc!")).toEqual([
49
"This is SageMath!",
50
false, // not clean
51
]);
52
});
53
});
54
55
describe("test doing a 3-way merge", () => {
56
const base = "SageMathCloud is software for using Sage in the cloud.";
57
58
// we change base in two different ways: first name
59
const local = "CoCalc is software for using Sage in the cloud.";
60
// .... then (independently) the purpose.
61
const remote =
62
"SageMathCloud is software for collaborative calculation in the cloud.";
63
64
it("does a three-way merge", () => {
65
const merge = three_way_merge({ base, local, remote });
66
// Merging captures both changes.
67
expect(merge).toBe(
68
"CoCalc is software for collaborative calculation in the cloud."
69
);
70
});
71
});
72
73
describe("Test comparison of patch log entries (compares time and user)", () => {
74
const p0 = {
75
time: new Date("2019-01-01T22:15:31.539Z"),
76
patch: [],
77
user_id: 0,
78
size: 2,
79
};
80
const p1 = {
81
time: new Date("2019-01-01T22:15:40Z"),
82
patch: [],
83
user_id: 1,
84
size: 2,
85
};
86
const p2 = {
87
time: new Date("2019-01-01T22:15:31.539Z"),
88
patch: [],
89
user_id: 1,
90
size: 2,
91
};
92
93
it("compares some patch log entries", () => {
94
expect(patch_cmp(p0, p0)).toBe(0);
95
expect(patch_cmp(p1, p1)).toBe(0);
96
expect(patch_cmp(p0, p1)).toBe(-1);
97
expect(patch_cmp(p1, p0)).toBe(1);
98
expect(patch_cmp(p0, p2)).toBe(-1);
99
expect(patch_cmp(p2, p0)).toBe(1);
100
});
101
});
102
103
describe("Test comparing times", () => {
104
const t0 = new Date("2019-01-01T22:15:31.539Z");
105
const t1 = new Date("2019-01-01T22:15:40Z");
106
const t2 = new Date("2019-01-01T22:15:31.539Z");
107
108
it("compares some times", () => {
109
expect(time_cmp(t0, t1)).toBe(-1);
110
expect(time_cmp(t1, t0)).toBe(1);
111
expect(time_cmp(t0, t0)).toBe(0);
112
expect(time_cmp(t0, t2)).toBe(0);
113
expect(time_cmp(t2, t0)).toBe(0);
114
});
115
});
116
117