-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
test.js
108 lines (103 loc) · 3.36 KB
/
test.js
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
/* global it, describe, mocha, chai, Asciidoctor, AsciidoctorKroki, mochaOpts, pako */
const httpGet = (uri, encoding = 'utf8') => {
let data = ''
let status = -1
try {
const xhr = new XMLHttpRequest()
xhr.open('GET', uri, false)
if (encoding === 'binary') {
xhr.responseType = 'arraybuffer'
}
xhr.addEventListener('load', function () {
status = this.status
if (status === 200 || status === 0) {
if (encoding === 'binary') {
const arrayBuffer = xhr.response
const byteArray = new Uint8Array(arrayBuffer)
for (let i = 0; i < byteArray.byteLength; i++) {
data += String.fromCharCode(byteArray[i])
}
} else {
data = this.responseText
}
}
})
xhr.send()
} catch (e) {
throw new Error(`Error reading file: ${uri}; reason: ${e.message}`)
}
// assume that no data means it doesn't exist
if (status === 404 || !data) {
throw new Error(`No such file: ${uri}`)
}
return data
}
(async () => {
let reporter
if (typeof mochaOpts === 'function') {
reporter = await mochaOpts().reporter
} else {
reporter = 'html'
}
mocha.setup({
ui: 'bdd',
checkLeaks: false,
reporter: reporter
})
const expect = chai.expect
const asciidoctor = Asciidoctor({ runtime: { platform: 'browser' } })
const parts = window.location.href.split('/') // break the string into an array
parts.pop()
parts.pop()
parts.pop()
const baseDir = parts.join('/')
function encodeText (text) {
const compressed = pako.deflate(text, { to: 'string', level: 9 })
return window.btoa(compressed)
.replace(/\+/g, '-').replace(/\//g, '_')
}
describe('Conversion', () => {
describe('When extension is registered', () => {
it('should convert a diagram to an image', () => {
const input = `
[plantuml,alice-bob,png,role=sequence]
....
alice -> bob
....
`
const registry = asciidoctor.Extensions.create()
AsciidoctorKroki.register(registry)
const html = asciidoctor.convert(input, { extension_registry: registry })
expect(html).to.contain('https://kroki.io/plantuml/png/eNpLzMlMTlXQtVNIyk8CABoDA90=')
expect(html).to.contain('<div class="imageblock sequence kroki-format-png kroki">')
})
it('should convert a diagram with a relative path to an image', () => {
const input = `plantuml::${baseDir}/test/fixtures/alice.puml[svg,role=sequence]`
const registry = asciidoctor.Extensions.create()
AsciidoctorKroki.register(registry, {
vfs: {
read: (path, encoding = 'utf8') => {
return httpGet(path, encoding)
},
exists: (_) => {
return false
},
add: (_) => {
// no-op
}
}
})
const text = httpGet(`${baseDir}/test/fixtures/alice.puml`, 'utf8')
const html = asciidoctor.convert(input, { extension_registry: registry, safe: 'safe', attributes: { 'allow-uri-read': true } })
expect(html).to.contain(`<img src="https://kroki.io/plantuml/svg/${encodeText(text)}" alt="diagram">`)
}).timeout(5000)
})
})
mocha.run(function (failures) {
if (failures > 0) {
console.error('%d failures', failures)
}
})
})().catch(err => {
console.error('Unable to start the browser tests suite: ' + err)
})