Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/sync/editor/string/test/sync.1.test.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Client } from "./client-test";6import { SyncString } from "../sync";7import { once } from "@cocalc/util/async-utils";8import { a_txt } from "./data";910describe("create syncstring and test doing some edits", () => {11const { client_id, project_id, path, init_queries } = a_txt();12const client = new Client(init_queries, client_id);13let syncstring: SyncString;14const v = [15"SageMathCloud",16"SageMathCloud -- Collaborative Calculation",17"CoCalc -- Collaborative Calculation",18];1920it("creates the syncstring and wait until ready", async () => {21syncstring = new SyncString({ project_id, path, client, cursors: true });22expect(syncstring.get_state()).toBe("init");23await once(syncstring, "ready");24});2526it("sets the value and commit change", async () => {27for (let i = 0; i < v.length; i++) {28syncstring.from_str(v[i]);29expect(syncstring.to_str()).toBe(v[i]);30expect(syncstring.count()).toBe(v[i].length);31expect(syncstring.versions().length).toBe(i);32await syncstring.save();33expect(syncstring.versions().length).toBe(i + 1);34}35});3637it("gets the value at each past version", () => {38const vers = syncstring.versions();39for (let i = 0; i < v.length; i++) {40expect(syncstring.version(vers[i]).to_str()).toEqual(v[i]);41}42});4344it("undo it all", () => {45for (let i = 0; i < v.length; i++) {46syncstring.undo();47let e = v[v.length - i - 2];48if (e === undefined) {49e = "";50}51expect(syncstring.to_str()).toEqual(e);52expect(syncstring.in_undo_mode()).toBe(true);53}54// we made lots of changes with undo/redo, and these55// automatically result in new commits.56expect(syncstring.versions().length).toBe(2 * v.length);57});5859it("redo it all (undo mode state continues from above)", () => {60expect(syncstring.in_undo_mode()).toBe(true);61for (let i = 0; i < v.length; i++) {62syncstring.redo();63expect(syncstring.to_str()).toEqual(v[i]);64}65syncstring.exit_undo_mode();66expect(syncstring.in_undo_mode()).toBe(false);67expect(syncstring.versions().length).toBe(3 * v.length);68});6970it("revert to each past point in time", () => {71const vers = syncstring.versions();72for (let i = 0; i < v.length; i++) {73syncstring.revert(vers[i]);74expect(syncstring.to_str()).toEqual(v[i]);75}76// correct, since no commits.77expect(syncstring.versions().length).toBe(3 * v.length);78});7980it("gets info about patch at a given point in time", () => {81const t = syncstring.versions()[1];82expect(syncstring.account_id(t)).toEqual(client_id);8384// time sent is not set since patch wasn't made offline.85expect(syncstring.time_sent(t)).toBe(undefined);8687expect(syncstring.user_id(t)).toEqual(1);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