CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/test/client-test.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Minimal client class that we use for testing.
8
*/
9
10
import { EventEmitter } from "events";
11
import { bind_methods, keys } from "@cocalc/util/misc";
12
import { once } from "@cocalc/util/async-utils";
13
import {
14
Client as Client0,
15
FileWatcher as FileWatcher0,
16
} from "../../generic/types";
17
import { SyncTable } from "@cocalc/sync/table/synctable";
18
import { ExecuteCodeOptionsWithCallback } from "@cocalc/util/types/execute-code";
19
20
export class FileWatcher extends EventEmitter implements FileWatcher0 {
21
private path: string;
22
constructor(path: string) {
23
super();
24
this.path = path;
25
console.log("FileWatcher", this.path);
26
}
27
public close(): void {}
28
}
29
30
export class Client extends EventEmitter implements Client0 {
31
private _client_id: string;
32
private initial_get_query: { [table: string]: any[] };
33
public set_queries: any[] = [];
34
35
constructor(
36
initial_get_query: { [table: string]: any[] },
37
client_id: string,
38
) {
39
super();
40
this._client_id = client_id;
41
this.initial_get_query = initial_get_query;
42
bind_methods(this, ["query", "dbg", "query_cancel"]);
43
}
44
45
public server_time(): Date {
46
return new Date();
47
}
48
49
public is_project(): boolean {
50
return false;
51
}
52
53
public is_browser(): boolean {
54
return true;
55
}
56
57
public is_compute_server(): boolean {
58
return false;
59
}
60
61
public dbg(_f: string): Function {
62
// return (...args) => {
63
// console.log(_f, ...args);
64
// };
65
return (..._) => {};
66
}
67
68
public mark_file(_opts: {
69
project_id: string;
70
path: string;
71
action: string;
72
ttl: number;
73
}): void {
74
//console.log("mark_file", opts);
75
}
76
77
public log_error(opts: {
78
project_id: string;
79
path: string;
80
string_id: string;
81
error: any;
82
}): void {
83
console.log("log_error", opts);
84
}
85
86
public query(opts): void {
87
if (opts.options && opts.options.length === 1 && opts.options[0].set) {
88
// set query
89
this.set_queries.push(opts);
90
opts.cb();
91
} else {
92
// get query -- returns predetermined result
93
const table = keys(opts.query)[0];
94
let result = this.initial_get_query[table];
95
if (result == null) {
96
result = [];
97
}
98
//console.log("GET QUERY ", table, result);
99
opts.cb(undefined, { query: { [table]: result } });
100
}
101
}
102
103
path_access(opts: { path: string; mode: string; cb: Function }): void {
104
console.log("path_access", opts.path, opts.mode);
105
opts.cb(true);
106
}
107
path_exists(opts: { path: string; cb: Function }): void {
108
console.log("path_access", opts.path);
109
opts.cb(true);
110
}
111
path_stat(opts: { path: string; cb: Function }): void {
112
console.log("path_state", opts.path);
113
opts.cb(true);
114
}
115
async path_read(opts: {
116
path: string;
117
maxsize_MB?: number;
118
cb: Function;
119
}): Promise<void> {
120
console.log("path_ready", opts.path);
121
opts.cb(true);
122
}
123
async write_file(opts: {
124
path: string;
125
data: string;
126
cb: Function;
127
}): Promise<void> {
128
console.log("write_file", opts.path, opts.data);
129
opts.cb(true);
130
}
131
watch_file(opts: { path: string }): FileWatcher {
132
return new FileWatcher(opts.path);
133
}
134
135
public is_connected(): boolean {
136
return true;
137
}
138
139
public is_signed_in(): boolean {
140
return true;
141
}
142
143
public touch_project(_): void {}
144
145
public query_cancel(_): void {}
146
147
public alert_message(_): void {}
148
149
public is_deleted(_filename: string, _project_id?: string): boolean {
150
return false;
151
}
152
153
public set_deleted(_filename: string, _project_id?: string): void {}
154
155
async synctable_project(
156
_project_id: string,
157
query: any,
158
options: any,
159
throttle_changes?: number,
160
): Promise<SyncTable> {
161
const s = new SyncTable(query, options, this, throttle_changes);
162
await once(s, "connected");
163
return s;
164
}
165
166
async synctable_database(
167
_query: any,
168
_options: any,
169
_throttle_changes?: number,
170
): Promise<SyncTable> {
171
throw Error("not implemented");
172
}
173
174
// account_id or project_id
175
public client_id(): string {
176
return this._client_id;
177
}
178
179
public sage_session({ path }): void {
180
console.log(`sage_session: path=${path}`);
181
}
182
183
public shell(opts: ExecuteCodeOptionsWithCallback): void {
184
console.log(`shell: opts=${JSON.stringify(opts)}`);
185
}
186
}
187
188