Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/test/sync.0.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
8
DEVELOPMENT:
9
10
pnpm test sync.0.test.ts
11
12
*/
13
14
import { Client } from "./client-test";
15
import { SyncString } from "../sync";
16
import { a_txt } from "./data";
17
import { once } from "@cocalc/util/async-utils";
18
19
// This mostly tests the trivial minimal edge cases.
20
describe("create a blank minimal string SyncDoc and call public methods on it", () => {
21
const { client_id, project_id, path, string_id, init_queries } = a_txt();
22
const client = new Client(init_queries, client_id);
23
let syncstring: SyncString;
24
25
it("creates the syncstring and wait for it to be ready", async () => {
26
syncstring = new SyncString({ project_id, path, client });
27
expect(syncstring.get_state()).toBe("init");
28
await once(syncstring, "ready");
29
expect(syncstring.get_state()).toBe("ready");
30
});
31
32
it("call set_cursor_locs, an error since cursors aren't enabled", () => {
33
expect(async () => {
34
await syncstring.set_cursor_locs([]);
35
}).rejects.toThrow("cursors are not enabled");
36
});
37
38
it("calls each public get method", () => {
39
expect(syncstring.get_state()).toBe("ready");
40
expect(syncstring.get_project_id()).toBe(project_id);
41
expect(syncstring.get_path()).toBe(path);
42
expect(syncstring.get_string_id()).toBe(string_id);
43
expect(syncstring.get_my_user_id()).toBe(1);
44
});
45
46
it("the db-style get methods all fail on a string", () => {
47
expect(() => syncstring.get()).toThrow(
48
"queries on strings don't have meaning",
49
);
50
expect(() => syncstring.get_one()).toThrow(
51
"queries on strings don't have meaning",
52
);
53
expect(() => syncstring.delete()).toThrow(
54
"delete on strings doesn't have meaning",
55
);
56
});
57
58
it("get the underlying doc", () => {
59
// via Document
60
expect(syncstring.get_doc().to_str()).toBe("");
61
// directly
62
expect(syncstring.to_str()).toBe("");
63
});
64
65
it("get the size via count", () => {
66
expect(syncstring.count()).toBe(0);
67
});
68
69
it("get current version", () => {
70
expect(syncstring.version().to_str()).toBe("");
71
});
72
73
it("get version without (removing nothing though)", () => {
74
expect(syncstring.version_without([]).to_str()).toBe("");
75
expect(syncstring.version_without([Date.now()]).to_str()).toBe("");
76
});
77
78
it("revert to version now (error since no version with this time)", () => {
79
expect(() => syncstring.revert(Date.now())).toThrow("unknown time");
80
});
81
82
it("undo/redo -- nothing to undo yet...", () => {
83
expect(syncstring.in_undo_mode()).toBe(false);
84
syncstring.undo();
85
expect(syncstring.in_undo_mode()).toBe(true);
86
syncstring.exit_undo_mode();
87
expect(syncstring.in_undo_mode()).toBe(false);
88
syncstring.redo(); // no error
89
});
90
91
it("account_id of change at given point in time gives error", () => {
92
expect(() => syncstring.account_id(Date.now())).toThrow("no patch at");
93
});
94
95
it("user_id of change at given point in time gives error", () => {
96
expect(() => syncstring.user_id(Date.now())).toThrow("no patch at");
97
});
98
99
it("get list of versions (should be empty)", () => {
100
expect(syncstring.versions()).toEqual([]);
101
});
102
103
it("last changed when time began", () => {
104
expect(syncstring.last_changed()).toEqual(0);
105
});
106
107
it("check ready state", async () => {
108
syncstring.assert_is_ready("check ready state");
109
await syncstring.wait_until_ready(); // trivial since already ready
110
});
111
112
it("wait for an already true condition", async () => {
113
await syncstring.wait(() => true);
114
});
115
116
it("get cursors (error, since cursors not enabled)", async () => {
117
expect(() => syncstring.get_cursors()).toThrow("cursors are not enabled");
118
});
119
120
it("set, then get, something from the settings field", async () => {
121
await syncstring.set_settings({ foo: { bar: "none" } });
122
expect(syncstring.get_settings().get("foo").toJS()).toEqual({
123
bar: "none",
124
});
125
});
126
127
it("verifies it has the full history already", () => {
128
expect(syncstring.hasFullHistory()).toBe(true);
129
});
130
131
it("loads more history (which does basically nothing)", async () => {
132
await syncstring.loadMoreHistory();
133
});
134
135
it("do a save (no-op, since haven't done anything yet)", async () => {
136
await syncstring.save();
137
});
138
139
it("change the snapshot interval", async () => {
140
await syncstring.set_snapshot_interval(17);
141
expect((syncstring as any).snapshot_interval).toBe(17);
142
});
143
144
it("read only checks", async () => {
145
await syncstring.wait_until_read_only_known(); // no-op
146
expect(syncstring.is_read_only()).toBe(false);
147
});
148
149
it("hashes of versions", () => {
150
expect(syncstring.hash_of_saved_version()).toBe(0);
151
expect(syncstring.hash_of_live_version()).toBe(0);
152
expect(syncstring.has_uncommitted_changes()).toBe(false);
153
});
154
155
it("saves to disk (no-op, since nothing changed)", async () => {
156
await syncstring.save_to_disk();
157
});
158
159
it("close and clean up", async () => {
160
expect(syncstring.get_state()).toBe("ready");
161
await syncstring.close();
162
expect(syncstring.get_state()).toBe("closed");
163
});
164
});
165
166