This repository was archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathlinks.js
More file actions
407 lines (335 loc) · 14.7 KB
/
links.js
File metadata and controls
407 lines (335 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const { one, two } = require('../config')
const sleep = require('sleep')
module.exports = scenario => {
scenario('delete_post', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
// creates a simple link with alice as author with initial chain header
await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Posty' }
)
// creates a simple link with bob as author with different chain header
await bob.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Posty' }
)
// get all created links so far alice
const alice_posts = await bob.call('app', 'simple', 'get_my_links',
{ base: alice.info('app').agentAddress, status_request: 'Live' }
)
// expect two links from alice
t.ok(alice_posts.Ok)
t.equal(alice_posts.Ok.links.length, 2)
// get all created links so far for bob
const bob_posts = await bob.call('app', 'simple', 'get_my_links',
{ base: alice.info('app').agentAddress, status_request: 'Live' }
)
// expected two links from bob
t.ok(bob_posts.Ok)
t.equal(bob_posts.Ok.links.length, 2)
// alice removes both links
await alice.callSync('app', 'simple', 'delete_link', { base: alice.info('app').agentAddress, target: 'Posty' })
// get links from bob
const bob_agent_posts_expect_empty = await bob.call('app', 'simple', 'get_my_links', { base: alice.info('app').agentAddress, status_request: 'Live' })
// get links from alice
const alice_agent_posts_expect_empty = await alice.call('app', 'simple', 'get_my_links', { base: alice.info('app').agentAddress, status_request: 'Live' })
// bob expects zero links
t.ok(bob_agent_posts_expect_empty.Ok)
t.equal(bob_agent_posts_expect_empty.Ok.links.length, 0) // #!# fails with expected: 0 actual: 2
// alice expects zero links
t.ok(alice_agent_posts_expect_empty.Ok)
t.equal(alice_agent_posts_expect_empty.Ok.links.length, 0)
// different chain hash up to this point so we should be able to create a link with the same data
await alice.callSync('app', 'simple', 'create_link', { base: alice.info('app').agentAddress, target: 'Posty' })
// get posts as Alice and as Bob
const alice_posts_not_empty = await alice.call('app', 'simple', 'get_my_links', { base: alice.info('app').agentAddress, status_request: 'Live' })
const bob_posts_not_empty = await bob.call('app', 'simple', 'get_my_links', { base: alice.info('app').agentAddress, status_request: 'Live' })
// expect 1 post
t.ok(alice_posts_not_empty.Ok)
t.equal(alice_posts_not_empty.Ok.links.length, 1)
t.ok(bob_posts_not_empty.Ok)
t.equal(bob_posts_not_empty.Ok.links.length, 1) // #!# fails with expected: 1 actual: 2
})
scenario('delete_post_with_bad_link', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
const result_bob_delete = await bob.callSync('app', 'blog', 'delete_post', {
content: 'Bad'
})
// bad in_reply_to is an error condition
t.ok(result_bob_delete.Err)
t.notOk(result_bob_delete.Ok)
const error = JSON.parse(result_bob_delete.Err.Internal)
t.deepEqual(error.kind, { ErrorGeneric: 'Target for link not found' })
t.ok(error.file)
t.ok(error.line)
})
scenario('get_links_paginate', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
// commits an entry and creates two links for alice
await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Holo world' }
)
await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Holo world 2' }
)
await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Holo world 3' }
)
await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Holo world 4' }
)
// get posts for alice from bob
const bob_posts_live = await bob.call('app', 'simple', 'get_my_links_with_pagination',
{
base: alice.info('app').agentAddress,
pagesize: 3,
pagenumber:0
})
const alice_posts_live = await alice.call('app', 'simple', 'get_my_links_with_pagination',
{
base: alice.info('app').agentAddress,
pagesize: 3,
pagenumber:0
})
// make sure all our links are live and they are two of them
console.log("alice posts live : " + JSON.stringify(alice_posts_live));
t.equal(3, alice_posts_live.Ok.links.length)
t.equal(3, bob_posts_live.Ok.links.length)
const bob_posts_live_2 = await bob.call('app', 'simple', 'get_my_links_with_pagination',
{
base: alice.info('app').agentAddress,
pagesize: 3,
pagenumber:1
})
const alice_posts_live_2 = await alice.call('app', 'simple', 'get_my_links_with_pagination',
{
base: alice.info('app').agentAddress,
pagesize: 3,
pagenumber:1
})
t.equal(1, bob_posts_live_2.Ok.links.length)
t.equal(1, alice_posts_live_2.Ok.links.length)
sleep.sleep(5);
const bob_posts_live_time = await bob.call('app', 'simple', 'get_my_links_with_time_pagination',
{
base: alice.info('app').agentAddress,
from_seconds: Math.floor(new Date() / 1000),//last ever second
limit:3
})
const alice_posts_live_time = await bob.call('app', 'simple', 'get_my_links_with_time_pagination',
{
base: alice.info('app').agentAddress,
from_seconds: Math.floor(new Date() / 1000),//last ever second
limit:3
})
t.equal(3, bob_posts_live_time.Ok.links.length)
t.equal(3, alice_posts_live_time.Ok.links.length)
const bob_posts_time_2 = await bob.call('app', 'simple', 'get_my_links_with_time_pagination',
{
base: alice.info('app').agentAddress,
from_seconds: 0,//first ever second
limit:3
})
const alice_posts_time_2 = await bob.call('app', 'simple', 'get_my_links_with_time_pagination',
{
base: alice.info('app').agentAddress,
from_seconds: 0,//first ever second
limit:3
})
t.equal(0, bob_posts_time_2.Ok.links.length)
t.equal(0, alice_posts_time_2.Ok.links.length)
})
scenario('get_links_crud', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
// commits an entry and creates two links for alice
await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Holo world' }
)
const alice_result = await alice.callSync('app', 'simple', 'create_link',
{ base: alice.info('app').agentAddress, target: 'Holo world 2' }
)
// get posts for alice from alice
const alice_posts_live = await alice.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress, status_request: 'Live'
})
console.log('alice posts' + JSON.stringify(alice_posts_live))
// get posts for alice from bob
const bob_posts_live = await bob.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'Live'
})
// make sure all our links are live and they are two of them
t.equal(2, alice_posts_live.Ok.links.length)
t.equal('live', alice_posts_live.Ok.links[0].status)
t.equal('live', alice_posts_live.Ok.links[1].status)
t.equal(2, bob_posts_live.Ok.links.length)
t.equal('live', bob_posts_live.Ok.links[0].status)
t.equal('live', bob_posts_live.Ok.links[1].status)
/// /delete the holo world post from the links alice created
await alice.callSync('app', 'simple', 'delete_link',
{
base: alice.info('app').agentAddress,
target: 'Holo world'
})
// get all posts with a deleted status from bob
const bob_posts_deleted = await bob.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'Deleted'
})
// get all posts with a deleted status from alice
const alice_posts_deleted = await alice.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'Deleted'
})
// make sure only 1 is returned and it has a status of deleted
t.equal(1, alice_posts_deleted.Ok.links.length)
t.equal(1, bob_posts_deleted.Ok.links.length)
t.equal('deleted', alice_posts_deleted.Ok.links[0].status)
t.equal('deleted', bob_posts_deleted.Ok.links[0].status)
// get all posts from the agent
const bob_posts_all = await bob.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'All'
})
const alice_posts_all = await alice.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'All'
})
// make sure we get two links with the first one being a deleted link and the second one being a live link since they are now sorted backwards
t.equal(2, alice_posts_all.Ok.links.length)
t.equal('deleted', alice_posts_all.Ok.links[0].status)
t.equal('live', alice_posts_all.Ok.links[1].status)
t.equal(2, bob_posts_all.Ok.links.length)
t.equal('deleted', bob_posts_all.Ok.links[0].status)
t.equal('live', bob_posts_all.Ok.links[1].status)
const bob_posts_ascending = await bob.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'All',
sort_order : "Ascending"
})
const alice_posts_ascennding = await alice.call('app', 'simple', 'get_my_links',
{
base: alice.info('app').agentAddress,
status_request: 'All',
sort_order : "Ascending"
})
// make sure we get two links with the first one being a deleted link and the second one being a live link since they are now sorted backwards
t.equal(2, alice_posts_ascennding.Ok.links.length)
t.equal('live', alice_posts_ascennding.Ok.links[0].status)
t.equal('deleted', alice_posts_ascennding.Ok.links[1].status)
t.equal(2, bob_posts_ascending.Ok.links.length)
t.equal('live', bob_posts_ascending.Ok.links[0].status)
t.equal('deleted', bob_posts_ascending.Ok.links[1].status)
})
scenario('get_links_crud_count', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
// commits an entry and creates two links for alice
await alice.callSync('app', 'simple', 'create_link_with_tag',
{ base: alice.info('app').agentAddress, target: 'Holo world', tag: 'tag' }
)
// commit an entry with other tag
await alice.callSync('app', 'simple', 'create_link_with_tag',
{ base: alice.info('app').agentAddress, target: 'Holo world', tag: 'differen' }
)
await alice.callSync('app', 'simple', 'create_link_with_tag',
{ base: alice.info('app').agentAddress, target: 'Holo world 2', tag: 'tag' })
// get posts for alice from alice
const alice_posts_live = await alice.call('app', 'simple', 'get_my_links_count',
{
base: alice.info('app').agentAddress,
status_request: 'Live',
tag: 'tag'
})
// get posts for alice from bob
const bob_posts_live = await bob.call('app', 'simple', 'get_my_links_count',
{
base: alice.info('app').agentAddress,
status_request: 'Live',
tag: 'tag'
})
// make sure count equals to 2
t.equal(2, alice_posts_live.Ok.count)
t.equal(2, bob_posts_live.Ok.count)
const bob_posts_live_diff_tag = await bob.call('app', 'simple', 'get_my_links_count',
{
base: alice.info('app').agentAddress,
status_request: 'Live',
tag: 'differen'
})
t.equal(1, bob_posts_live_diff_tag.Ok.count)
/// /delete the holo world post from the links alice created
await alice.callSync('app', 'simple', 'delete_link_with_tag',
{
base: alice.info('app').agentAddress,
target: 'Holo world',
tag: 'tag'
})
// get all bob posts
const bob_posts_deleted = await bob.call('app', 'simple', 'get_my_links_count',
{
base: alice.info('app').agentAddress,
status_request: 'Deleted',
tag: 'tag'
})
// get all posts with a deleted status from alice
const alice_posts_deleted = await alice.call('app', 'simple', 'get_my_links_count',
{
base: alice.info('app').agentAddress,
status_request: 'Deleted',
tag: 'tag'
})
// make sure count is equal to 1
t.equal(1, alice_posts_deleted.Ok.count)
t.equal(1, bob_posts_deleted.Ok.count)
const bob_posts_deleted_diff_tag = await bob.call('app', 'simple', 'get_my_links_count',
{
base: alice.info('app').agentAddress,
status_request: 'Live',
tag: 'differen'
})
t.equal(1, bob_posts_deleted_diff_tag.Ok.count)
})
scenario('get_sources_after_same_link', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
await bob.callSync('app', 'blog', 'create_post_with_agent',
{ agent_id: alice.info('app').agentAddress, content: 'Holo world', in_reply_to: null }
)
await bob.callSync('app', 'blog', 'create_post_with_agent',
{ agent_id: alice.info('app').agentAddress, content: 'Holo world', in_reply_to: null }
)
const alice_posts = await bob.call('app', 'blog', 'authored_posts_with_sources',
{
agent: alice.info('app').agentAddress
})
const bob_posts = await alice.call('app', 'blog', 'authored_posts_with_sources',
{
agent: alice.info('app').agentAddress
})
t.equal(bob.info('app').agentAddress, alice_posts.Ok.links[0].headers[0].provenances[0][0])
t.equal(bob.info('app').agentAddress, bob_posts.Ok.links[0].headers[0].provenances[0][0])
})
scenario('get_sources_from_link', async (s, t) => {
const { alice, bob } = await s.players({ alice: one, bob: one }, true)
await alice.callSync('app', 'blog', 'create_post', {
content: 'Holo world', in_reply_to: null
})
await bob.callSync('app', 'blog', 'create_post', {
content: 'Another one', in_reply_to: null
})
const alice_posts = await bob.call('app', 'blog', 'authored_posts_with_sources', {
agent: alice.info('app').agentAddress
})
const bob_posts = await alice.call('app', 'blog', 'authored_posts_with_sources', {
agent: bob.info('app').agentAddress
})
t.equal(bob_posts.Ok.links.length, 1)
t.equal(bob.info('app').agentAddress, bob_posts.Ok.links[0].headers[0].provenances[0][0])
t.equal(alice_posts.Ok.links.length, 1)
t.equal(alice.info('app').agentAddress, alice_posts.Ok.links[0].headers[0].provenances[0][0])
})
}