Path: blob/master/src/packages/sync/editor/string/test/client-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/*6Minimal client class that we use for testing.7*/89import { EventEmitter } from "events";10import { bind_methods, keys } from "@cocalc/util/misc";11import {12Client as Client0,13FileWatcher as FileWatcher0,14} from "../../generic/types";15import { SyncTable } from "@cocalc/sync/table/synctable";16import { ExecuteCodeOptionsWithCallback } from "@cocalc/util/types/execute-code";17import { once } from "@cocalc/util/async-utils";1819export class FileWatcher extends EventEmitter implements FileWatcher0 {20private path: string;21constructor(path: string) {22super();23this.path = path;24console.log("FileWatcher", this.path);25}26public close(): void {}27}2829export class Client extends EventEmitter implements Client0 {30private _client_id: string;31private initial_get_query: { [table: string]: any[] };32public set_queries: any[] = [];3334constructor(35initial_get_query: { [table: string]: any[] },36client_id: string,37) {38super();39this._client_id = client_id;40this.initial_get_query = initial_get_query;41bind_methods(this, ["query", "dbg", "query_cancel"]);42}4344public server_time(): Date {45return new Date();46}4748isTestClient = () => {49return true;50};5152public is_project(): boolean {53return false;54}5556public is_browser(): boolean {57return true;58}5960public is_compute_server(): boolean {61return false;62}6364public dbg(_f: string): Function {65// return (...args) => {66// console.log(_f, ...args);67// };68return (..._) => {};69}7071public mark_file(_opts: {72project_id: string;73path: string;74action: string;75ttl: number;76}): void {77//console.log("mark_file", opts);78}7980public log_error(opts: {81project_id: string;82path: string;83string_id: string;84error: any;85}): void {86console.log("log_error", opts);87}8889public query(opts): void {90if (opts.options && opts.options.length === 1 && opts.options[0].set) {91// set query92this.set_queries.push(opts);93opts.cb();94} else {95// get query -- returns predetermined result96const table = keys(opts.query)[0];97let result = this.initial_get_query[table];98if (result == null) {99result = [];100}101//console.log("GET QUERY ", table, result);102opts.cb(undefined, { query: { [table]: result } });103}104}105106path_access(opts: { path: string; mode: string; cb: Function }): void {107console.log("path_access", opts.path, opts.mode);108opts.cb(true);109}110path_exists(opts: { path: string; cb: Function }): void {111console.log("path_access", opts.path);112opts.cb(true);113}114path_stat(opts: { path: string; cb: Function }): void {115console.log("path_state", opts.path);116opts.cb(true);117}118async path_read(opts: {119path: string;120maxsize_MB?: number;121cb: Function;122}): Promise<void> {123console.log("path_ready", opts.path);124opts.cb(true);125}126async write_file(opts: {127path: string;128data: string;129cb: Function;130}): Promise<void> {131console.log("write_file", opts.path, opts.data);132opts.cb(true);133}134watch_file(opts: { path: string }): FileWatcher {135return new FileWatcher(opts.path);136}137138public is_connected(): boolean {139return true;140}141142public is_signed_in(): boolean {143return true;144}145146public touch_project(_): void {}147148public query_cancel(_): void {}149150public alert_message(_): void {}151152public is_deleted(_filename: string, _project_id?: string): boolean {153return false;154}155156public set_deleted(_filename: string, _project_id?: string): void {}157158async synctable_ephemeral(159_project_id: string,160query: any,161options: any,162throttle_changes?: number,163): Promise<SyncTable> {164const s = new SyncTable(query, options, this, throttle_changes);165await once(s, "connected");166return s;167}168169async synctable_conat(_query: any): Promise<SyncTable> {170throw Error("synctable_conat: not implemented");171}172async pubsub_conat(_query: any): Promise<SyncTable> {173throw Error("pubsub_conat: not implemented");174}175176// account_id or project_id177public client_id(): string {178return this._client_id;179}180181public sage_session({ path }): void {182console.log(`sage_session: path=${path}`);183}184185public shell(opts: ExecuteCodeOptionsWithCallback): void {186console.log(`shell: opts=${JSON.stringify(opts)}`);187}188}189190191