-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
238 lines (198 loc) · 6.15 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
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 10px;
}
.test {
margin-bottom: 3px;
padding: 3px;
}
.running {
background-color: #fffde7;
}
.passed {
background-color: #e8f5e9;
}
.failed {
background-color: #ffebee;
}
</style>
<script src="https://unpkg.com/braid-http@~1.3/braid-http-client.js"></script>
<div id="testContainer"></div>
<script type=module>
let delay = 0
function createTestDiv(testName) {
const div = document.createElement("div")
div.className = "test running"
div.innerHTML = `<span style="font-weight:bold">${testName}: </span><span class="result">Running...</span>`
testContainer.appendChild(div)
return div
}
function updateTestResult(div, passed, message, got, expected) {
div.className = `test ${passed ? "passed" : "failed"}`
if (passed) {
div.querySelector(".result").textContent = message
div.querySelector(".result").style.fontSize = message.length > 400 ? 'xx-small' : message.length > 100 ? 'small' : ''
} else {
div.querySelector(".result").innerHTML = `${message}<br><strong>Got:</strong> ${got}<br><strong>Expected:</strong> ${expected}`
}
}
async function runTest(testName, testFunction, expectedResult) {
delay += 70
await new Promise(done => setTimeout(done, delay))
const div = createTestDiv(testName)
try {
let x = await testFunction()
if (x == expectedResult) {
updateTestResult(div, true, x)
} else {
updateTestResult(div, false, "Mismatch:", x, expectedResult)
}
} catch (error) {
updateTestResult(div, false, "Error:", error.message || error, expectedResult)
}
}
runTest(
"test subscribing starting at a version using simpleton",
async () => {
let key = 'test-' + Math.random().toString(36).slice(2)
let r = await braid_fetch(`/${key}`, {
method: 'PUT',
version: ['hi-1'],
parents: [],
body: 'xx'
})
if (!r.ok) throw 'got: ' + r.statusCode
let r2 = await braid_fetch(`/${key}`, {
version: ['hi-0'],
subscribe: true
})
return await new Promise(async (done, fail) => {
r2.subscribe(update => {
done(JSON.stringify(update.parents))
}, fail)
})
},
JSON.stringify([ "hi-0" ])
)
runTest(
"test dt_create_bytes with big agent name",
async () => {
let x = await (await fetch(`/test.html?dt_create_bytes_big_name`)).json()
return JSON.stringify(x)
},
JSON.stringify({ok: true})
)
runTest(
"test dt_create_bytes with many agent names",
async () => {
let x = await (await fetch(`/test.html?dt_create_bytes_many_names`)).json()
return JSON.stringify(x)
},
JSON.stringify({ok: true})
)
runTest(
"test deleting a resource",
async () => {
let key = 'test-' + Math.random().toString(36).slice(2)
let r0 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
await fetch(`/${key}`, {
method: 'PUT',
body: JSON.stringify({a: 5, b: 6}, null, 4)
})
let r1 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
await fetch(`/${key}`, {
method: 'DELETE'
})
let r2 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
await fetch(`/${key}`, {
method: 'PUT',
body: JSON.stringify({a: 5, b: 7}, null, 4)
})
let r3 = (await (await fetch(`/test.html?check=/${key}`)).json()).result
return JSON.stringify([r0, r1, r2, r3])
},
JSON.stringify([false, true, false, true])
)
runTest(
"test getting a binary update from a subscription",
async () => {
return await new Promise(async (done, fail) => {
let key = 'test-' + Math.random().toString(36).slice(2)
await fetch(`/${key}`, {
method: 'PUT',
body: JSON.stringify({a: 5, b: 6}, null, 4)
})
let r = await braid_fetch(`/${key}`, {
subscribe: true
})
r.subscribe(update => done(update.body_text), fail)
})
},
JSON.stringify({a: 5, b: 6}, null, 4)
)
runTest(
"test sending a json patch to some json-text",
async () => {
let key = 'test-' + Math.random().toString(36).slice(2)
await fetch(`/${key}`, {
method: 'PUT',
body: JSON.stringify({a: 5, b: 6})
})
await fetch(`/${key}`, {
method: 'PUT',
headers: { 'Content-Range': 'json a' },
body: '67'
})
let r = await fetch(`/${key}`)
return await r.text()
},
JSON.stringify({a: 67, b: 6}, null, 4)
)
runTest(
"test sending multiple json patches to some json-text",
async () => {
let key = 'test-' + Math.random().toString(36).slice(2)
await fetch(`/${key}`, {
method: 'PUT',
body: JSON.stringify({a: 5, b: 6, c: 7})
})
await braid_fetch(`/${key}`, {
method: 'PUT',
headers: { 'Content-Range': 'json a' },
patches: [{
unit: 'json',
range: 'a',
content: '55',
}, {
unit: 'json',
range: 'b',
content: '66',
}]
})
let r = await fetch(`/${key}`)
return await r.text()
},
JSON.stringify({a: 55, b: 66, c: 7}, null, 4)
)
runTest(
"test deleting something using a json patch",
async () => {
let key = 'test-' + Math.random().toString(36).slice(2)
await fetch(`/${key}`, {
method: 'PUT',
body: JSON.stringify({a: 5, b: 6}, null, 4)
})
await fetch(`/${key}`, {
method: 'PUT',
headers: { 'Content-Range': 'json a' },
body: ''
})
let r = await fetch(`/${key}`)
return await r.text()
},
JSON.stringify({b: 6}, null, 4)
)
</script>