Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
###############################################################################
2
#
3
# CoCalc: Collaborative web-based calculation
4
# Copyright (C) 2017, Sagemath Inc.
5
# AGPLv3
6
#
7
###############################################################################
8
9
require('coffee-cache')
10
11
immutable = require('immutable')
12
13
synctable = require('../synctable')
14
misc = require('../misc')
15
16
expect = require('expect')
17
18
describe "create a simple synctable and do basic operations -- ", ->
19
client = new synctable.TestBrowserClient1()
20
query = {projects:[{project_id:null, title:null, last_active:null}]}
21
table = undefined
22
23
projects = [{project_id:'479453a0-4aa5-4249-8866-8b713088b919', title:'cocalC', last_active:misc.minutes_ago(0)},
24
{project_id:'56ad4e26-7fa6-4094-8070-1f37d003b3da', title:'SMC', last_active:misc.minutes_ago(1)},]
25
26
query_opts = undefined
27
28
it 'creates a SyncTable', (done) ->
29
table = synctable.sync_table(query, undefined, client, 0, 0)
30
# Wait for the query
31
client.once 'query', (opts) =>
32
query_opts = opts
33
opts.cb(undefined, {query : {projects : projects}})
34
35
table.on "connected", -> done()
36
37
it "it has the right data in it", ->
38
v = table.get().toJS()
39
for i in [0, 1]
40
expect(v[projects[0].project_id]).toEqual(projects[0])
41
42
it "add a project to the table via changefeed update from client", (done) ->
43
projects.push({project_id:'1f623948-615b-4a49-a865-a4616921d101', title:'CoCalc', last_active:misc.minutes_ago(2)})
44
45
# Confirm get the right change
46
table.once 'change', (keys) ->
47
expect(keys).toEqual([projects[2].project_id])
48
expect(table.get(projects[2].project_id).toJS()).toEqual(projects[2])
49
done()
50
51
# Now push the change to the table
52
query_opts.cb(undefined, {new_val:projects[2]})
53
54
it "change a project in the table via changefeed update from client", (done) ->
55
projects[0].title = 'CoCalc'
56
table.once 'change', (keys) ->
57
expect(keys).toEqual([projects[0].project_id])
58
expect(table.get(projects[0].project_id).toJS()).toEqual(projects[0])
59
expect(table.get().size).toEqual(3)
60
done()
61
query_opts.cb(undefined, {new_val:projects[0]})
62
63
it 'changes the synctable directly', (done) ->
64
client.once 'query', (opts) ->
65
opts.cb()
66
projects[0].title = 'COCALC'
67
table.set({project_id:projects[0].project_id, title:"COCALC"})
68
expect(table.get(projects[0].project_id).toJS()).toEqual(projects[0])
69
table.save(done)
70
71
it 'check the key method', ->
72
obj = projects[0]
73
expect(table.key(obj)).toEqual(obj.project_id)
74
expect(table.key(immutable.fromJS(obj))).toEqual(obj.project_id)
75
76
it 'checks waiting for a condition to be met', (done) ->
77
table.wait
78
until : (t) -> t.get(projects[0].project_id).get('title') == 'cocalc'
79
cb : done
80
table.set({project_id:projects[0].project_id, title:"coCalc"})
81
table.set({project_id:projects[0].project_id, title:"cocalc"})
82
83
it 'closes the table', ->
84
expect(table._state).toEqual('connected')
85
table.close()
86
expect(table._state).toEqual('closed')
87
88
89
describe "test changes are merged in object when modifying a record locally and remotely -- ", ->
90
client = new synctable.TestBrowserClient1()
91
query = {projects:[{project_id:null, title:null, desc:null}]}
92
table = undefined
93
94
projects = [{project_id:'479453a0-4aa5-4249-8866-8b713088b919', title:'cocalc', desc:'collaborative calculation'}]
95
96
query_opts = undefined
97
98
it 'creates a SyncTable', (done) ->
99
table = synctable.sync_table(query, undefined, client, 0, 0)
100
# Wait for the query
101
client.once 'query', (opts) ->
102
query_opts = opts
103
opts.cb(undefined, {query : {projects : projects}})
104
table.on "connected", -> done()
105
106
it 'makes a local change and a remote change to the same document', (done) ->
107
table.set({project_id:projects[0].project_id, title:"CoCalc"})
108
projects[0].desc = 'Collaborative Calculation'
109
table.once 'change', (keys) ->
110
expect(keys).toEqual([projects[0].project_id])
111
expect(table.get(projects[0].project_id).toJS()).toEqual({project_id:'479453a0-4aa5-4249-8866-8b713088b919', title:'CoCalc', desc:'Collaborative Calculation'})
112
done()
113
query_opts.cb(undefined, {new_val:projects[0]})
114
115
116
117
118