-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPerspective.test.ts
217 lines (171 loc) · 6.95 KB
/
Perspective.test.ts
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
import Perspective from './Perspective'
import type PerspectiveContext from './PerspectiveContext'
import type PerspectiveID from './PerspectiveID'
import { PerspectivismDb } from './db'
import { v4 as uuidv4 } from 'uuid'
import faker from 'faker'
import type Link from '../ad4m/Links'
import type { LinkQuery } from '../ad4m/Links'
import Memory from 'lowdb/adapters/Memory'
import type LanguageRef from '../ad4m/LanguageRef'
function createLink(): Link {
return {
source: faker.internet.url(),
target: faker.internet.url(),
predicate: faker.internet.url(),
} as Link
}
const did = 'did:local-test-agent'
const agentService = {
did,
createSignedExpression: jest.fn(data => {
return {
author: { did },
timestamp: "now",
data,
proof: {
signature: "abcdefgh",
key: `${did}#primary`
}
}
}),
agent: { did }
}
const sharingLanguage = {
address: "sharing-language-address",
name: "sharing-lanugage Name"
} as LanguageRef
function LinksAdapter() {
this.getLinks = jest.fn(args=>{return []})
this.addLink = jest.fn(args=>{return []})
this.updateLink = jest.fn(args=>{return []})
this.removeLink = jest.fn(args=>{return []})
}
let linksAdapter = new LinksAdapter()
const languageController = {
getLinksAdapter: jest.fn(langRef => {
if(langRef.address === sharingLanguage.address)
return linksAdapter
else
return null
})
}
describe('Perspective', () => {
let perspective
let allLinks
beforeEach(() => {
const db = new PerspectivismDb(new Memory())
perspective = new Perspective(
{
uuid: uuidv4(),
name: "Test Perspective"
} as PerspectiveID,
// @ts-ignore
{
agentService,
db,
languageController
} as PerspectiveContext)
allLinks = []
})
it('wraps links in expressions on addLink', () => {
const link = createLink()
const expression = perspective.addLink(link)
expect(expression.author).toEqual(agentService.agent)
expect(expression.data).toEqual(link)
expect(agentService.createSignedExpression.mock.calls.length).toBe(1)
expect(agentService.createSignedExpression.mock.calls[0][0]).toEqual(link)
})
describe('after adding 5 links', () => {
beforeEach(() => {
for(let i=0; i<5; i++) {
const link = createLink()
if(i%2 === 0) {
link.source = 'root'
}
allLinks.push(link)
perspective.addLink(link)
}
})
it('has asked agent service for 5 signatures', () => {
expect(agentService.createSignedExpression.mock.calls.length).toBe(6)
})
it('can get all links', async () => {
const result = await perspective.getLinks({} as LinkQuery)
expect(result.length).toEqual(5)
for(let i=0; i<5; i++) {
expect(result).toEqual(
expect.arrayContaining(
[expect.objectContaining({data: allLinks[i]})]
)
)
}
})
it('can get links by source', async () => {
const result = await perspective.getLinks({source: 'root'} as LinkQuery)
expect(result.length).toEqual(3)
})
})
describe('with link sharing language', () => {
beforeEach(() => {
perspective.sharedPerspective = {
linkLanguages: [sharingLanguage]
}
linksAdapter = new LinksAdapter()
})
it('calls link language on getLinks() with the query once', async () => {
const query = {source: 'root'}
await perspective.getLinks(query)
expect(linksAdapter.getLinks.mock.calls.length).toBe(1)
expect(linksAdapter.getLinks.mock.calls[0][0]).toEqual(query)
expect(linksAdapter.addLink.mock.calls.length).toBe(0)
expect(linksAdapter.updateLink.mock.calls.length).toBe(0)
expect(linksAdapter.removeLink.mock.calls.length).toBe(0)
})
it('calls link language on addLink() with link expression once', async () => {
const link = createLink()
const linkExpression = await perspective.addLink(link)
expect(linksAdapter.addLink.mock.calls.length).toBe(1)
expect(linksAdapter.addLink.mock.calls[0][0]).toEqual(linkExpression)
expect(linksAdapter.getLinks.mock.calls.length).toBe(0)
expect(linksAdapter.updateLink.mock.calls.length).toBe(0)
expect(linksAdapter.removeLink.mock.calls.length).toBe(0)
})
it('calls link language on updateLink() with link expression once', async () => {
const link1 = createLink()
const link2 = createLink()
const link1Expression = await perspective.addLink(link1)
const link2Expression = perspective.ensureLinkExpression(link2)
await perspective.updateLink(link1Expression, link2Expression)
expect(linksAdapter.updateLink.mock.calls.length).toBe(1)
expect(linksAdapter.updateLink.mock.calls[0][0]).toEqual(link1Expression)
expect(linksAdapter.updateLink.mock.calls[0][1]).toEqual(link2Expression)
expect(linksAdapter.getLinks.mock.calls.length).toBe(0)
expect(linksAdapter.addLink.mock.calls.length).toBe(1)
expect(linksAdapter.removeLink.mock.calls.length).toBe(0)
})
it('calls link language on removeLink() with link expression once', async () => {
const link = createLink()
const linkExpression = await perspective.addLink(link)
await perspective.removeLink(linkExpression)
expect(linksAdapter.removeLink.mock.calls.length).toBe(1)
expect(linksAdapter.removeLink.mock.calls[0][0]).toEqual(linkExpression)
expect(linksAdapter.getLinks.mock.calls.length).toBe(0)
expect(linksAdapter.updateLink.mock.calls.length).toBe(0)
expect(linksAdapter.addLink.mock.calls.length).toBe(1)
})
describe('syncWithSharingAdpater', () => {
it('adds all missing links from local DB to linksAdapter', async () => {
perspective.sharedPerspective = null
const link = createLink()
const linkExpression = await perspective.addLink(link)
perspective.sharedPerspective = {
linkLanguages: [sharingLanguage]
}
await perspective.syncWithSharingAdapter()
expect(linksAdapter.addLink.mock.calls.length).toBe(1)
expect(linksAdapter.addLink.mock.calls[0][0]).toEqual(linkExpression)
})
})
})
})