-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
296 lines (253 loc) · 8.51 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
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
// This file is intended for examples and testing
import fs from 'fs';
import Akun from './index.js';
const credentials = JSON.parse(fs.readFileSync('credentials.json'));
function setTimeoutPromise(duration) {
return new Promise(res => setTimeout(res, duration));
}
function printObject(object) {
return JSON.stringify(object, null, '\t');
}
async function onChat(akun, client, chatNode) {
console.log(`New ChatNode:\n${chatNode}`);
// console.log('historyChat', client.historyChat.nodes);
if (chatNode.body === 'peng') {
console.log('attempting response');
const res = await client.postChat('pung');
console.log(`post response: ${res}`);
}
if (chatNode.body === 'login') {
const res = await akun.login(credentials['username'], credentials['password']);
console.log(`Logged in as ${res['username']}!`);
}
if (chatNode.body === 'reply') {
const res = await client.reply('rooply', chatNode.id);
console.log(`reply response: ${res}`);
}
}
async function onChatUpdated(akun, client, chatNode) {
console.log(`Updated ChatNode:\n${chatNode}`);
}
function onUsersCount(akun, client, usersCount) {
console.log(`Updated usersCount: ${usersCount}`);
}
async function onChapter(akun, client, chapterNode) {
console.log(`New ChapterNode:\n${chapterNode}`);
// console.log('historyStory', client.historyStory.nodes);
if (chapterNode.body === '<p>ping</p>') {
console.log('Trying to chapter');
const res = await client.postChat('chapong');
console.log(`post response: ${res}`);
}
}
async function onChapterUpdated(akun, client, chapterNode) {
console.log(`Updated ChapterNode:\n${chapterNode}`);
}
async function onChoice(akun, client, choiceNode) {
console.log(`New ChoiceNode:\n${choiceNode}`);
// console.log('historyStory', client.historyStory.nodes);
}
async function onChoiceUpdated(akun, client, choiceNode) {
console.log(`Updated ChoiceNode:\n${choiceNode}`);
}
async function onReaderPost(akun, client, readerPostNode) {
console.log(`New ReaderPostNode:\n${readerPostNode}`);
// console.log('historyStory', client.historyStory.nodes);
}
async function onReaderPostUpdated(akun, client, readerPostNode) {
console.log(`Updated ReaderPostNode:\n${readerPostNode}`);
}
async function onSubscriptionSucceeded(data) {
console.log('subscriptionSucceeded', data);
}
async function testStory(akun, storyId) {
const client = await akun.join(storyId);
client.chatThread.on('chat', (node) => {
onChat(akun, client, node);
});
client.chatThread.on('chatUpdated', (node) => {
onChatUpdated(akun, client, node);
});
client.chatThread.on('usersCount', (usersCount) => {
onUsersCount(akun, client, usersCount);
});
client.storyThread.on('chapter', (node) => {
onChapter(akun, client, node);
});
client.storyThread.on('chapterUpdated', (node) => {
onChapterUpdated(akun, client, node);
});
client.storyThread.on('choice', (node) => {
onChoice(akun, client, node);
});
client.storyThread.on('choiceUpdated', (node) => {
onChoiceUpdated(akun, client, node);
});
client.storyThread.on('readerPost', (node) => {
onReaderPost(akun, client, node);
});
client.storyThread.on('readerPostUpdated', (node) => {
onReaderPostUpdated(akun, client, node);
});
client.storyThread.on('subscriptionSucceeded', onSubscriptionSucceeded);
// const resTestPost1 = await client.postChat('Test post 1');
// console.log(`post response: ${resTestPost1}`);
const resLogin = await akun.login(credentials['username'], credentials['password']);
console.log(`Logged in as ${resLogin['username']}!`);
// const resTestPost2 = await client.postChat('Test post 2');
// console.log(`post response: ${resTestPost2}`);
// console.log(client.latestChapter());
// client.historyChat.nodes.forEach((node) => {
// console.log(node.toString())
// });
// client.historyStory.nodes.forEach((node) => {
// console.log(node.toString())
// });
return new Promise(res => {
client.chatThread.on('chat', (node) => {
if (node.body === 'exit') {
res();
}
});
});
}
async function testChat(akun, chatId) {
const client = await akun.join(chatId);
client.chatThread.on('chat', (node) => {
onChat(akun, client, node);
});
client.chatThread.on('chatUpdated', (node) => {
onChatUpdated(akun, client, node);
});
client.chatThread.on('choice', (node) => {
onChoice(akun, client, node);
});
client.chatThread.on('choiceUpdated', (node) => {
onChoiceUpdated(akun, client, node);
});
client.chatThread.on('subscriptionSucceeded', onSubscriptionSucceeded);
// const resTestPost1 = await client.postChat('Test post 1');
// console.log(`post response: ${resTestPost1}`);
const resLogin = await akun.login(credentials['username'], credentials['password']);
console.log(`Logged in as ${resLogin['username']}!`);
// const resTestPost2 = await client.postChat('Test post 2');
// console.log(`post response: ${JSON.stringify(resTestPost2, null, '\t')}`);
return new Promise(res => {
client.chatThread.on('chat', (node) => {
if (node.body === 'exit') {
res();
}
});
});
}
async function testPut(akun, chatId) {
await akun.login(credentials['username'], credentials['password']);
const client = await akun.join(chatId);
const postData = await client.post('Test post');
const data = await akun.getNode(postData['_id']);
data.b = 'edited text';
console.log(await akun.put('/api/node', {data}));
}
async function testAnonToggle(akun, chatId) {
await akun.login(credentials['username'], credentials['password']);
const client = await akun.join(chatId);
await akun.setAnon(true);
await client.postChat('Test anon post');
await akun.setAnon(false);
await client.postChat('Test non-anon post');
}
async function testVote(akun, pollId) {
await akun.vote(pollId, 0);
await akun.removeVote(pollId, 0);
}
async function testPost(akun, chatId) {
const client = await akun.join(chatId);
await client.postChat('Test post 1');
// console.log(await client.reply('rooply', 't2KXjRztofE5Z58uE'));
}
async function testBan(akun) {
let res;
res = await akun.login(credentials['username'], credentials['password']);
console.log(res);
res = await akun.ban('iRoYFFCDCZnB2QvEq', '53zAELYaC8RkkMpcn');
console.log(res);
res = await akun.unban('iRoYFFCDCZnB2QvEq', 'B5cqvTk3kMgRNPesr');
console.log(res);
}
async function testPollOpenClose(akun) {
await akun.login(credentials['username'], credentials['password']);
while (true) {
console.log(await akun.closeChoice('58BfMER3GZg48LbYm'));
await setTimeoutPromise(1000);
console.log(await akun.openChoice('58BfMER3GZg48LbYm'));
await setTimeoutPromise(1000);
}
}
async function testPollChoiceRemoval(akun) {
let res;
res = await akun.login(credentials['username'], credentials['password']);
console.log(res);
const client = await akun.join('vhHhMfskRnNDbxwzo');
client.storyThread.on('choice', (node) => {
onChoice(akun, client, node);
for (let choiceId = 0; choiceId < node.choiceValues.length; choiceId++) {
if (node.choiceValues[choiceId] === 'test') {
akun.removeChoiceNodeChoice(node.id, choiceId).then(res => console.log(res));
}
}
});
client.storyThread.on('choiceUpdated', (node) => {
onChoiceUpdated(akun, client, node);
for (let choiceId = 0; choiceId < node.choiceValues.length; choiceId++) {
if (node.choiceValues[choiceId] === 'test') {
akun.removeChoiceNodeChoice(node.id, choiceId, 'sample reason').then(res => console.log(res));
}
}
});
return new Promise(res => {
client.chatThread.on('chat', (node) => {
if (node.body === 'exit') {
res();
}
});
});
}
async function testGetStories(akun) {
const {stories} = await akun.getStories('stories', 1, {sort: 'new'});
stories.forEach((story, i) => {
console.log(i, `${story.t} by ${story.u[0].n}`);
});
}
async function runTests(akun) {
// await testAnonToggle(akun, 'vhHhMfskRnNDbxwzo');
// await testPost(akun, 'vhHhMfskRnNDbxwzo');
await testStory(akun, 'vhHhMfskRnNDbxwzo');
// await testChat(akun, 'oQ2fkvRS4nxjLfSmA');
// await testChat(akun, 'oWC3WhFDMXqZkAG69');
// await testPut(akun, 'vhHhMfskRnNDbxwzo');
// await testVote(akun, 'TziTddJsppEfr82nh');
// await testPollOpenClose(akun);
// await testBan(akun);
// await testPollChoiceRemoval(akun);
// await testGetStories(akun);
}
async function setup(withRealtime = true) {
const settings = {
hostname: 'fiction.live'
// silent: true
};
if (withRealtime) {
settings.connection = {
hostname: 'rt.fiction.live'
};
}
return new Akun(settings);
}
async function teardown(akun) {
await akun.destroy();
}
(async function run() {
const akun = await setup(true);
await runTests(akun).catch(console.error);
await teardown(akun);
})().catch(console.error);