Path: blob/master/src/packages/sync/editor/string/test/sync.0.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/*67DEVELOPMENT:89pnpm test sync.0.test.ts1011*/1213import { Client } from "./client-test";14import { SyncString } from "../sync";15import { a_txt } from "./data";16import { once } from "@cocalc/util/async-utils";1718// This mostly tests the trivial minimal edge cases.19describe("create a blank minimal string SyncDoc and call public methods on it", () => {20const { client_id, project_id, path, string_id, init_queries } = a_txt();21const client = new Client(init_queries, client_id);22let syncstring: SyncString;2324it("creates the syncstring and wait for it to be ready", async () => {25syncstring = new SyncString({ project_id, path, client });26expect(syncstring.get_state()).toBe("init");27await once(syncstring, "ready");28expect(syncstring.get_state()).toBe("ready");29});3031it("call set_cursor_locs, an error since cursors aren't enabled", () => {32expect(async () => {33await syncstring.set_cursor_locs([]);34}).rejects.toThrow("cursors are not enabled");35});3637it("calls each public get method", () => {38expect(syncstring.get_state()).toBe("ready");39expect(syncstring.get_project_id()).toBe(project_id);40expect(syncstring.get_path()).toBe(path);41expect(syncstring.get_string_id()).toBe(string_id);42expect(syncstring.get_my_user_id()).toBe(1);43});4445it("the db-style get methods all fail on a string", () => {46expect(() => syncstring.get()).toThrow(47"queries on strings don't have meaning",48);49expect(() => syncstring.get_one()).toThrow(50"queries on strings don't have meaning",51);52expect(() => syncstring.delete()).toThrow(53"delete on strings doesn't have meaning",54);55});5657it("get the underlying doc", () => {58// via Document59expect(syncstring.get_doc().to_str()).toBe("");60// directly61expect(syncstring.to_str()).toBe("");62});6364it("get the size via count", () => {65expect(syncstring.count()).toBe(0);66});6768it("get current version", () => {69expect(syncstring.version().to_str()).toBe("");70});7172it("get version without (removing nothing though)", () => {73expect(syncstring.version_without([]).to_str()).toBe("");74expect(syncstring.version_without([Date.now()]).to_str()).toBe("");75});7677it("revert to version now (error since no version with this time)", () => {78expect(() => syncstring.revert(Date.now())).toThrow("unknown time");79});8081it("undo/redo -- nothing to undo yet...", () => {82expect(syncstring.in_undo_mode()).toBe(false);83syncstring.undo();84expect(syncstring.in_undo_mode()).toBe(true);85syncstring.exit_undo_mode();86expect(syncstring.in_undo_mode()).toBe(false);87syncstring.redo(); // no error88});8990it("account_id of change at given point in time gives error", () => {91expect(() => syncstring.account_id(Date.now())).toThrow("no patch at");92});9394it("user_id of change at given point in time gives error", () => {95expect(() => syncstring.user_id(Date.now())).toThrow("no patch at");96});9798it("get list of versions (should be empty)", () => {99expect(syncstring.versions()).toEqual([]);100});101102it("last changed when time began", () => {103expect(syncstring.last_changed()).toEqual(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.hasFullHistory()).toBe(true);128});129130it("loads more history (which does basically nothing)", async () => {131await syncstring.loadMoreHistory();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