Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/test/client-test.ts
1451 views
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 {
13
Client as Client0,
14
FileWatcher as FileWatcher0,
15
} from "../../generic/types";
16
import { SyncTable } from "@cocalc/sync/table/synctable";
17
import { ExecuteCodeOptionsWithCallback } from "@cocalc/util/types/execute-code";
18
import { once } from "@cocalc/util/async-utils";
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
isTestClient = () => {
50
return true;
51
};
52
53
public is_project(): boolean {
54
return false;
55
}
56
57
public is_browser(): boolean {
58
return true;
59
}
60
61
public is_compute_server(): boolean {
62
return false;
63
}
64
65
public dbg(_f: string): Function {
66
// return (...args) => {
67
// console.log(_f, ...args);
68
// };
69
return (..._) => {};
70
}
71
72
public mark_file(_opts: {
73
project_id: string;
74
path: string;
75
action: string;
76
ttl: number;
77
}): void {
78
//console.log("mark_file", opts);
79
}
80
81
public log_error(opts: {
82
project_id: string;
83
path: string;
84
string_id: string;
85
error: any;
86
}): void {
87
console.log("log_error", opts);
88
}
89
90
public query(opts): void {
91
if (opts.options && opts.options.length === 1 && opts.options[0].set) {
92
// set query
93
this.set_queries.push(opts);
94
opts.cb();
95
} else {
96
// get query -- returns predetermined result
97
const table = keys(opts.query)[0];
98
let result = this.initial_get_query[table];
99
if (result == null) {
100
result = [];
101
}
102
//console.log("GET QUERY ", table, result);
103
opts.cb(undefined, { query: { [table]: result } });
104
}
105
}
106
107
path_access(opts: { path: string; mode: string; cb: Function }): void {
108
console.log("path_access", opts.path, opts.mode);
109
opts.cb(true);
110
}
111
path_exists(opts: { path: string; cb: Function }): void {
112
console.log("path_access", opts.path);
113
opts.cb(true);
114
}
115
path_stat(opts: { path: string; cb: Function }): void {
116
console.log("path_state", opts.path);
117
opts.cb(true);
118
}
119
async path_read(opts: {
120
path: string;
121
maxsize_MB?: number;
122
cb: Function;
123
}): Promise<void> {
124
console.log("path_ready", opts.path);
125
opts.cb(true);
126
}
127
async write_file(opts: {
128
path: string;
129
data: string;
130
cb: Function;
131
}): Promise<void> {
132
console.log("write_file", opts.path, opts.data);
133
opts.cb(true);
134
}
135
watch_file(opts: { path: string }): FileWatcher {
136
return new FileWatcher(opts.path);
137
}
138
139
public is_connected(): boolean {
140
return true;
141
}
142
143
public is_signed_in(): boolean {
144
return true;
145
}
146
147
public touch_project(_): void {}
148
149
public query_cancel(_): void {}
150
151
public alert_message(_): void {}
152
153
public is_deleted(_filename: string, _project_id?: string): boolean {
154
return false;
155
}
156
157
public set_deleted(_filename: string, _project_id?: string): void {}
158
159
async synctable_ephemeral(
160
_project_id: string,
161
query: any,
162
options: any,
163
throttle_changes?: number,
164
): Promise<SyncTable> {
165
const s = new SyncTable(query, options, this, throttle_changes);
166
await once(s, "connected");
167
return s;
168
}
169
170
async synctable_conat(_query: any): Promise<SyncTable> {
171
throw Error("synctable_conat: not implemented");
172
}
173
async pubsub_conat(_query: any): Promise<SyncTable> {
174
throw Error("pubsub_conat: not implemented");
175
}
176
177
// account_id or project_id
178
public client_id(): string {
179
return this._client_id;
180
}
181
182
public sage_session({ path }): void {
183
console.log(`sage_session: path=${path}`);
184
}
185
186
public shell(opts: ExecuteCodeOptionsWithCallback): void {
187
console.log(`shell: opts=${JSON.stringify(opts)}`);
188
}
189
}
190
191