forked from judell/hlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
516 lines (464 loc) · 19.8 KB
/
test.html
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<html>
<head>
<script src="tinytest.js"></script>
<link rel="stylesheet" href="hlib.css">
<style>
body {
background-color: #99ff99;
}
.icon-floppy, .icon-pencil {
font-size: x-small;
width: 1em;
height: 1em;
margin-left: -22px;
padding-right: 2px;
cursor: pointer;
}
.icon-delete {
font-size: x-small;
cursor: pointer;
display: inline;
}
</style>
</head>
<body>
<div id="svgDefs"></div>
<div class="formField" id="tokenContainer"></div>
<div class="formField" id="anyContainer"></div>
<div class="formField" id="exactTagSearchContainer"></div>
<div class="formField" id="wildcard_uriContainer"></div>
<div class="formField" id="groupContainer"></div>
<div id="annotationViewer"></div>
<div id="annotationEditor"></div>
<script>
localStorage.removeItem('h_settings')
</script>
<!-- This module syntax is so convenient... -->
<script type="module">
// Because with this import method...
import * as hlib from './hlib.js'
// ... I can debug the library, which uses export statements, directly.
// For deployed apps based on this library, the pattern so far is:
// - bundle as hlib2.bundle.js
// - run webpack
// - use devtool to produce a source map
// - use source-map-loader to load the source map
// It will be nice when all that nonsense goes away.
localStorage.removeItem('h_settings')
hlib.getById('svgDefs').outerHTML = hlib.svgIcons
const scratchUri = 'https://jonudell.net/h/scratch.html'
const scratchQuote = 'First para'
const scratchText = 'hlib test'
const testAnnotationId = 'e7j5ss0vEemSmqt5QQ2I0w'
function appendBody(element) {
document.body.appendChild(element)
}
function checkAndDeleteTestAnnotation(data, token) {
return new Promise(resolve => {
async function checkAndDelete() {
const response = JSON.parse(data.response)
if (response.target[0].selector) {
const selectors = hlib.parseSelectors(response.target)
eq(scratchQuote, selectors.TextQuote.exact)
const annotation = hlib.parseAnnotation(response)
assert(annotation.quote.indexOf(`class="quoteExact">${scratchQuote}</span>`))
}
assert(response.id)
assert(response.updated)
data = await hlib.deleteAnnotation(response.id, token)
console.log('deleted test annotation', data)
}
resolve(checkAndDelete())
})
}
function initializeSettings() {
localStorage.removeItem('h_settings')
hlib.settingsToLocalStorage(hlib.getSettings())
}
function clearUrlLine() {
history.pushState(null, '', location.href.replace(/\?.+/, ''))
}
function createExternalLink(anno) {
const standaloneAnnotationUrl = `${hlib.getSettings().service}/a/${anno.id}`
return `
<a title="view/edit/reply" target="_standalone" href="${standaloneAnnotationUrl}">
<img class="externalLinkImage" src="https://jonudell.info/hlib/externalLink.png">
</a>`
}
tests({
'gets token': function () {
return new Promise(resolve => {
let token = hlib.getToken()
let slice = token.slice(0, 9)
eq('6879-358c', slice)
resolve()
})
},
'initial settings as expected': function () {
return new Promise(resolve => {
initializeSettings()
clearUrlLine()
const settings = hlib.getSettings()
eq(hlib.getDefaultSettings().max, settings.max)
eq('false', settings.exactTagSearch)
eq('https://hypothes.is', settings.service)
resolve()
})
},
'input form change updates url and localStorage': function () {
return new Promise(resolve => {
const container = hlib.getById('anyContainer')
hlib.createAnyInputForm(container)
const inputElement = container.querySelector('input')
const newAny = 'any'
inputElement.value = newAny
inputElement.onchange()
assert(location.href.indexOf(`any=${newAny}` != -1))
assert(hlib.getSettings().any == newAny)
container.remove()
resolve()
})
},
'checkbox change updates url and localStorage': function () {
return new Promise(resolve => {
const container = hlib.getById('exactTagSearchContainer')
hlib.createExactTagSearchCheckbox(hlib.getById('exactTagSearchContainer'));
const inputElement = container.querySelector('input')
inputElement.checked = true
inputElement.onchange()
assert(location.href.indexOf('exactTagSearch=true') != -1)
assert(hlib.getSettings().exactTagSearch === 'true')
container.outerHTML = `<div class="formField" id="exactTagSearchContainer"></div>`
hlib.updateSetting('exactTagSearch', '')
resolve()
})
},
'remembered facets and settings appear on url line': function () {
return new Promise(resolve => {
initializeSettings()
hlib.updateSetting('wildcard_uri', 'https://www.nytimes.com/*')
hlib.updateSetting('exactTagSearch', 'true')
hlib.settingsToUrl(hlib.getSettings())
assert(location.href.indexOf('exactTagSearch=true') != -1)
assert(hlib.getSettings().exactTagSearch === 'true')
assert(location.href.indexOf('wildcard_uri=https%3A%2F%2Fwww.nytimes.com%2F*') != -1)
assert(hlib.getSettings().wildcard_uri === 'https://www.nytimes.com/*')
initializeSettings()
clearUrlLine()
resolve()
})
},
'remembered facets and settings appear on forms': function () {
return new Promise(resolve => {
initializeSettings()
const wildcard_uriContainer = hlib.getById('wildcard_uriContainer')
const wildcard_uriSetting = 'https://www.nytimes.com/*'
const exactTagSearchContainer = hlib.getById('exactTagSearchContainer')
const exactTagSearchSetting = 'true'
hlib.updateSetting('wildcard_uri', wildcard_uriSetting)
hlib.updateSetting('exactTagSearch', exactTagSearchSetting)
hlib.createWildcardUriInputForm(wildcard_uriContainer)
assert(wildcard_uriContainer.querySelector('input').value === wildcard_uriSetting)
hlib.createExactTagSearchCheckbox(exactTagSearchContainer)
assert(exactTagSearchContainer.querySelector('input').checked)
wildcard_uriContainer.outerHTML = `<div class="formField" id="wildcard_uriContainer"></div>`
exactTagSearchContainer.outerHTML = `<div class="formField" id="exactTagSearchContainer"></div>`
resolve()
})
},
'gets "" when no user': function () {
return new Promise(resolve => {
initializeSettings()
eq("", hlib.getSettings().user)
resolve()
})
},
'creates default group picklist with > 1 groups when token': function () {
return new Promise(resolve => {
async function runTest() {
let defaultSelectId = 'groupsList'
await hlib.createGroupInputForm(groupContainer)
const groupList = hlib.getById(defaultSelectId)
assert(groupList)
const options = groupList.querySelectorAll('option')
assert(groupList.length > 1)
eq('__world__', hlib.getSelectedGroup(defaultSelectId))
hlib.getById('groupContainer').outerHTML = `<div class="formField" id="groupContainer"></div>`
}
resolve(runTest())
})
},
'creates group picklist with 1 group when no token': function () {
return new Promise(resolve => {
async function runTest() {
let token = hlib.getToken()
delete localStorage['h_token']
const groupContainer = hlib.getById('groupContainer')
await hlib.createGroupInputForm(groupContainer)
localStorage['h_token'] = token
const groupList = hlib.getById('groupsList')
assert(groupList)
const options = groupList.querySelectorAll('option')
assert(groupList.length == 1, `expected 1, got ${groupList.length}`)
eq('__world__', hlib.getSelectedGroup())
hlib.getById('groupContainer').outerHTML = `<div class="formField" id="groupContainer"></div>`
}
resolve(runTest())
})
},
'creates a pagenote': function () {
return new Promise(resolve => {
async function runTest() {
const token = hlib.getToken()
const params = {
uri: scratchUri,
group: '__world__',
text: scratchText,
}
const payload = hlib.createAnnotationPayload(params)
const data = await hlib.postAnnotation(payload, token)
await checkAndDeleteTestAnnotation(data, token)
}
resolve(runTest())
})
},
'creates an annotation': function () {
return new Promise(resolve => {
async function runTest() {
const token = hlib.getToken()
const params = {
uri: scratchUri,
group: '__world__',
text: scratchText,
exact: scratchQuote,
}
const payload = hlib.createAnnotationPayload(params)
const data = await hlib.postAnnotation(payload, token)
await checkAndDeleteTestAnnotation(data, token)
}
resolve(runTest())
})
},
'creates a reply': function () {
return new Promise(resolve => {
async function runTest() {
const token = hlib.getToken()
const annoParams = {
uri: scratchUri,
text: scratchText,
}
const annoPayload = hlib.createAnnotationPayload(annoParams)
const annoData = await hlib.postAnnotation(annoPayload, token)
const annoId = JSON.parse(annoData.response).id
const replyParams = {
uri: scratchUri,
text: scratchText,
references: [annoId]
}
const replyPayload = hlib.createAnnotationPayload(replyParams)
const replyData = await hlib.postAnnotation(replyPayload, token)
const newAnnoId = JSON.parse(annoData.response).id
const newReplyReference = JSON.parse(replyData.response).references[0]
assertEquals(
newAnnoId,
newReplyReference
)
await checkAndDeleteTestAnnotation(annoData, token)
await checkAndDeleteTestAnnotation(replyData, token)
}
resolve(runTest())
})
},
'finds a test annotation': function () {
return new Promise(resolve => {
async function runTest() {
const token = hlib.getToken()
const params = {
user: 'judell',
tag: 'hlibtest',
}
const data = await hlib.search(params)
const [annos, replies] = data
assert(annos.length)
eq(testAnnotationId, hlib.parseAnnotation(annos[0]).id)
}
resolve(runTest())
})
},
'fails to retrieve from wrong service': function () {
return new Promise(resolve => {
async function runTest() {
hlib.updateSetting('service', 'https://WrongService')
const params = {}
try {
const data = await hlib.search(params) // the api request should fail
} catch (e) {
hlib.updateSetting('service', 'https://hypothes.is')
}
}
resolve(runTest())
})
},
'retrieves 400 annotations': function () {
const expected = 400
return new Promise(resolve => {
async function runTest() {
let params = { max: expected }
const data = await hlib.search(params)
eq(expected, data[0].length + data[1].length)
}
resolve(runTest())
})
},
'uses wildcard uris': function () {
return new Promise(resolve => {
async function runTest() {
const token = hlib.getToken()
const params = {
wildcard_uri: 'https://web.hypothes.is/*',
max: 400
}
const data = await hlib.search(params)
const annos = data[0]
const uris = annos.map(a => { return a.uri })
const blogUris = uris.map(u => u.startsWith('https://web.hypothes.is/blog')).filter(x => { return x })
assert(blogUris.length > 1)
const faqUris = uris.map(u => u.startsWith('https://web.hypothes.is/faq')).filter(x => { return x })
assert(faqUris.length > 1)
}
resolve(runTest())
})
},
'displays annotation for viewing': function () {
return new Promise(resolve => {
async function runTest() {
const data = await hlib.getAnnotation(testAnnotationId)
const anno = hlib.parseAnnotation(JSON.parse(data.response))
const userCanEdit = false
const standaloneAnnotationUrl = `${hlib.getSettings().service}/a/${anno.id}`
const externalLink = createExternalLink(anno)
eq(testAnnotationId, anno.id)
const annotationCard = hlib.getById('annotationViewer')
annotationCard.innerHTML = `
<div class="annotationCard annotation" id="_${anno.id}">
<annotation-editor state="viewing">
${userCanEdit ? '<span is="edit-or-save-icon"></span>' : ''}
<div class="annotationHeader">
<span class="user">
<a title="search user" target="_user" href="./?user=${anno.user}">${anno.user}</a>
</span>
<span> </span>
<span class="dateTime">${anno.updated}</span>
<span> </span>
<span class="groupSlug">in ${anno.group}</span>
<span> </span>
<span class="externalLink">${externalLink}</span>
</div>
<div class="annotationQuote">
${anno.quote}
</div>
<div class="annotationText">
${anno.text}
</div>
<div is="annotation-tags-editor" state="viewing" class="annotationTags" user-can-edit="false"
tags="${encodeURIComponent(JSON.stringify(anno.tags))}">
</div>
</annotation-editor>
</div>`
const annotationEditor = document.querySelector('annotation-editor')
assert(annotationEditor.querySelector('.quoteExact').innerHTML.includes("This domain is established"))
eq('Text of the test annotation.', annotationEditor.querySelector('.annotationText').innerText)
eq(1, annotationEditor.querySelectorAll('.annotationTag').length)
eq('<a target="_tag" href="./?tag=hlibtest">hlibtest</a>',
annotationEditor.querySelector('.annotationTag').innerHTML)
const tagEditor = annotationEditor.querySelector('div[is="annotation-tags-editor"]')
eq('viewing', tagEditor.getAttribute('state'))
eq('false', tagEditor.getAttribute('user-can-edit'))
await hlib.delaySeconds(2)
annotationCard.remove()
}
resolve(runTest())
})
},
'displays annotation for editing': function () {
return new Promise(resolve => {
async function runTest() {
let data = await hlib.getAnnotation(testAnnotationId)
let anno = hlib.parseAnnotation(JSON.parse(data.response))
const userCanEdit = true
const externalLink = createExternalLink(anno)
eq(testAnnotationId, anno.id)
const annotationCard = hlib.getById('annotationEditor')
annotationCard.innerHTML = `
<div class="annotationCard annotation" id="_${anno.id}">
<annotation-editor state="viewing">
${userCanEdit ? '<span is="edit-or-save-icon"></span>' : ''}
<div class="annotationHeader">
<span class="user">
<a title="search user" target="_user" href="./?user=${anno.user}">${anno.user}</a>
</span>
<span> </span>
<span class="dateTime">${anno.updated}</span>
<span> </span>
<span class="groupSlug">in ${anno.group}</span>
<span> </span>
<span class="externalLink">${externalLink}</span>
</div>
<div class="annotationQuote">
${anno.quote}
</div>
<div class="annotationText">
${anno.text}
</div>
<div is="annotation-tags-editor" state="viewing" class="annotationTags" user-can-edit="${userCanEdit}"
tags="${encodeURIComponent(JSON.stringify(anno.tags))}">
</div>
</annotation-editor>
</div>`
const annotationEditor = document.querySelector('annotation-editor')
assert(annotationEditor.querySelector('.quoteExact').innerHTML.includes("This domain is established"))
const annotationText = annotationEditor.querySelector('.annotationText')
eq('Text of the test annotation.', annotationText.innerText)
eq(1, annotationEditor.querySelectorAll('.annotationTag').length)
eq('<a target="_tag" href="./?tag=hlibtest">hlibtest</a>',
annotationEditor.querySelector('.annotationTag').innerHTML)
const tagEditor = annotationEditor.querySelector('div[is="annotation-tags-editor"]')
eq('viewing', tagEditor.getAttribute('state'))
eq('true', tagEditor.getAttribute('user-can-edit'))
await hlib.delaySeconds(1)
const editOrSaveIcon = document.querySelector('span[is="edit-or-save-icon"]')
editOrSaveIcon.click() // switch to editing
eq('true', annotationText.getAttribute('contentEditable'))
eq('editing', tagEditor.getAttribute('state'))
annotationText.innerText = "Temporarily changed"
const tagInput = tagEditor.querySelector('input')
tagInput.value = 'changed'
const tagChangeEvent = new Event('change')
tagInput.dispatchEvent(tagChangeEvent)
editOrSaveIcon.click() // save and switch back to viewing
await hlib.delaySeconds(1)
data = await hlib.getAnnotation(testAnnotationId)
anno = hlib.parseAnnotation(JSON.parse(data.response))
eq('Temporarily changed', anno.text)
eq('changed', anno.tags[0])
editOrSaveIcon.click() // switch back to editing to restore original values
eq('true', annotationText.getAttribute('contentEditable'))
eq('editing', tagEditor.getAttribute('state'))
annotationText.innerText = "Text of the test annotation."
tagInput.value = 'hlibtest'
tagInput.dispatchEvent(tagChangeEvent)
editOrSaveIcon.click() // switch back to viewing
await hlib.delaySeconds(1)
data = await hlib.getAnnotation(testAnnotationId)
anno = hlib.parseAnnotation(JSON.parse(data.response))
eq('Text of the test annotation.', anno.text)
eq('hlibtest', anno.tags[0])
}
resolve(runTest())
})
}
})
</script>
<div id="log"></div>
</body>
</html>