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