Path: blob/master/src/packages/sync/editor/string/test/sync.1.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/*678DEVELOPMENT:910pnpm test sync.1.test.ts1112*/1314import { Client } from "./client-test";15import { SyncString } from "../sync";16import { once } from "@cocalc/util/async-utils";17import { a_txt } from "./data";1819describe("create syncstring and test doing some edits", () => {20const { client_id, project_id, path, init_queries } = a_txt();21const client = new Client(init_queries, client_id);22let syncstring: SyncString;23const v = [24"SageMathCloud",25"SageMathCloud -- Collaborative Calculation",26"CoCalc -- Collaborative Calculation",27];2829it("creates the syncstring and wait until ready", async () => {30syncstring = new SyncString({ project_id, path, client, cursors: true });31expect(syncstring.get_state()).toBe("init");32await once(syncstring, "ready");33});3435it("sets the value and commit change", async () => {36for (let i = 0; i < v.length; i++) {37syncstring.from_str(v[i]);38expect(syncstring.to_str()).toBe(v[i]);39expect(syncstring.count()).toBe(v[i].length);40expect(syncstring.versions().length).toBe(i);41await syncstring.commit();42await syncstring.save();43expect(syncstring.versions().length).toBe(i + 1);44}45});4647it("gets the value at each past version", () => {48const vers = syncstring.versions();49for (let i = 0; i < v.length; i++) {50expect(syncstring.version(vers[i]).to_str()).toEqual(v[i]);51}52});5354it("undo it all", () => {55for (let i = 0; i < v.length; i++) {56syncstring.undo();57let e = v[v.length - i - 2];58if (e === undefined) {59e = "";60}61expect(syncstring.to_str()).toEqual(e);62expect(syncstring.in_undo_mode()).toBe(true);63}64// we made lots of changes with undo/redo, and these65// automatically result in new commits.66expect(syncstring.versions().length).toBe(2 * v.length);67});6869it("redo it all (undo mode state continues from above)", () => {70expect(syncstring.in_undo_mode()).toBe(true);71for (let i = 0; i < v.length; i++) {72syncstring.redo();73expect(syncstring.to_str()).toEqual(v[i]);74}75syncstring.exit_undo_mode();76expect(syncstring.in_undo_mode()).toBe(false);77expect(syncstring.versions().length).toBe(3 * v.length);78});7980it("revert to each past point in time", () => {81const vers = syncstring.versions();82for (let i = 0; i < v.length; i++) {83syncstring.revert(vers[i]);84expect(syncstring.to_str()).toEqual(v[i]);85}86// correct, since no commits.87expect(syncstring.versions().length).toBe(3 * v.length);88});8990it("last_changed is the time of the last version", () => {91const vers = syncstring.versions();92expect(syncstring.last_changed()).toEqual(vers[vers.length - 1]);93});9495it("is not read only", () => {96expect(syncstring.is_read_only()).toBe(false);97});9899it("save to disk", async () => {100expect(syncstring.has_unsaved_changes()).toBe(true);101const promise = syncstring.save_to_disk();102// Mock: we set save to done in the syncstring103// table, otherwise the promise will never resolve.104(syncstring as any).set_save({105state: "done",106error: "",107hash: syncstring.hash_of_live_version(),108});109(syncstring as any).syncstring_table.emit("change-no-throttle");110await promise;111expect(syncstring.has_unsaved_changes()).toBe(false);112});113114it("close and clean up", async () => {115await syncstring.close();116expect(syncstring.get_state()).toBe("closed");117});118});119120121