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.0.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";910// This mostly tests the trivial minimal edge cases.11describe("create a blank minimal string SyncDoc and call public methods on it", () => {12const { client_id, project_id, path, string_id, init_queries } = a_txt();13const client = new Client(init_queries, client_id);14let syncstring: SyncString;1516it("creates the syncstring and wait for it to be ready", async () => {17syncstring = new SyncString({ project_id, path, client });18expect(syncstring.get_state()).toBe("init");19await once(syncstring, "ready");20expect(syncstring.get_state()).toBe("ready");21});2223it("call set_cursor_locs, an error since cursors aren't enabled", () => {24expect(async () => {25await syncstring.set_cursor_locs([]);26}).rejects.toThrow("cursors are not enabled");27});2829it("calls each public get method", () => {30expect(syncstring.get_state()).toBe("ready");31expect(syncstring.get_project_id()).toBe(project_id);32expect(syncstring.get_path()).toBe(path);33expect(syncstring.get_string_id()).toBe(string_id);34expect(syncstring.get_my_user_id()).toBe(1);35});3637it("the db-style get methods all fail on a string", () => {38expect(() => syncstring.get()).toThrow(39"queries on strings don't have meaning",40);41expect(() => syncstring.get_one()).toThrow(42"queries on strings don't have meaning",43);44expect(() => syncstring.delete()).toThrow(45"delete on strings doesn't have meaning",46);47});4849it("get the underlying doc", () => {50// via Document51expect(syncstring.get_doc().to_str()).toBe("");52// directly53expect(syncstring.to_str()).toBe("");54});5556it("get the size via count", () => {57expect(syncstring.count()).toBe(0);58});5960it("get current version", () => {61expect(syncstring.version().to_str()).toBe("");62});6364it("get version without (removing nothing though)", () => {65expect(syncstring.version_without([]).to_str()).toBe("");66expect(syncstring.version_without([new Date()]).to_str()).toBe("");67});6869it("revert to version now (does nothing - no error)", () => {70syncstring.revert(new Date());71});7273it("undo/redo -- nothing to undo yet...", () => {74expect(syncstring.in_undo_mode()).toBe(false);75syncstring.undo();76expect(syncstring.in_undo_mode()).toBe(true);77syncstring.exit_undo_mode();78expect(syncstring.in_undo_mode()).toBe(false);79syncstring.redo(); // no error80});8182it("account_id of change at given point in time gives error", () => {83expect(() => syncstring.account_id(new Date())).toThrow("no patch at");84});8586it("time sent of change at given point in time gives error", () => {87expect(() => syncstring.time_sent(new Date())).toThrow("no patch at");88});8990it("user_id of change at given point in time gives error", () => {91expect(() => syncstring.user_id(new Date())).toThrow("no patch at");92});9394it("get list of versions (should be empty)", () => {95expect(syncstring.versions()).toEqual([]);96});9798it("get all known versions (also empty)", () => {99expect(syncstring.all_versions()).toEqual([]);100});101102it("last changed when time began", () => {103expect(syncstring.last_changed()).toEqual(new Date(0));104});105106it("check ready state", async () => {107syncstring.assert_is_ready("check ready state");108await syncstring.wait_until_ready(); // trivial since already ready109});110111it("wait for an already true condition", async () => {112await syncstring.wait(() => true);113});114115it("get cursors (error, since cursors not enabled)", async () => {116expect(() => syncstring.get_cursors()).toThrow("cursors are not enabled");117});118119it("set, then get, something from the settings field", async () => {120await syncstring.set_settings({ foo: { bar: "none" } });121expect(syncstring.get_settings().get("foo").toJS()).toEqual({122bar: "none",123});124});125126it("verifies it has the full history already", () => {127expect(syncstring.has_full_history()).toBe(true);128});129130it("loads full history (which does basically nothing)", async () => {131await syncstring.load_full_history();132});133134it("do a save (no-op, since haven't done anything yet)", async () => {135await syncstring.save();136});137138it("change the snapshot interval", async () => {139await syncstring.set_snapshot_interval(17);140expect((syncstring as any).snapshot_interval).toBe(17);141});142143it("read only checks", async () => {144await syncstring.wait_until_read_only_known(); // no-op145expect(syncstring.is_read_only()).toBe(false);146});147148it("hashes of versions", () => {149expect(syncstring.hash_of_saved_version()).toBe(0);150expect(syncstring.hash_of_live_version()).toBe(0);151expect(syncstring.has_uncommitted_changes()).toBe(false);152});153154it("saves to disk (no-op, since nothing changed)", async () => {155await syncstring.save_to_disk();156});157158it("close and clean up", async () => {159expect(syncstring.get_state()).toBe("ready");160await syncstring.close();161expect(syncstring.get_state()).toBe("closed");162});163});164165166