feature: events polling
This commit is contained in:
parent
61a51017a3
commit
b700251278
19 changed files with 353 additions and 77 deletions
|
@ -2,6 +2,7 @@ import { api, API_CLIENT } from '../../../utils/api.ts';
|
|||
import * as asserts from 'jsr:@std/assert';
|
||||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../helpers.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - Create',
|
||||
|
@ -72,6 +73,7 @@ Deno.test({
|
|||
|
||||
asserts.assert(new_room);
|
||||
} finally {
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import { api, API_CLIENT } from '../../../utils/api.ts';
|
|||
import * as asserts from 'jsr:@std/assert';
|
||||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../helpers.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - Delete',
|
||||
|
@ -48,6 +49,7 @@ Deno.test({
|
|||
|
||||
asserts.assert(deleted_room);
|
||||
} finally {
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import * as asserts from 'jsr:@std/assert';
|
|||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../../helpers.ts';
|
||||
import { api, API_CLIENT } from '../../../../utils/api.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - EVENTS - Create',
|
||||
|
@ -113,6 +114,7 @@ Deno.test({
|
|||
|
||||
asserts.assert(event_from_other_user);
|
||||
} finally {
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
|
|
121
tests/api/rooms/events/get_events.test.ts
Normal file
121
tests/api/rooms/events/get_events.test.ts
Normal file
|
@ -0,0 +1,121 @@
|
|||
import * as asserts from 'jsr:@std/assert';
|
||||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../../helpers.ts';
|
||||
import { api, API_CLIENT } from '../../../../utils/api.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - EVENTS - Get',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
const owner_info = await get_new_user(client);
|
||||
|
||||
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'rooms.create']);
|
||||
|
||||
const room = await client.fetch('/rooms', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test get events room'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(room);
|
||||
|
||||
const NUM_INITIAL_EVENTS = 5;
|
||||
const events_initial_batch: any[] = [];
|
||||
for (let i = 0; i < NUM_INITIAL_EVENTS; ++i) {
|
||||
const event = await client.fetch(`/rooms/${room.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
i
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event);
|
||||
events_initial_batch.push(event);
|
||||
}
|
||||
|
||||
asserts.assertEquals(events_initial_batch.length, NUM_INITIAL_EVENTS);
|
||||
|
||||
const other_user_info = await get_new_user(client);
|
||||
|
||||
const events_from_server = await client.fetch(`/rooms/${room.id}/events`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(events_from_server.length, NUM_INITIAL_EVENTS);
|
||||
|
||||
const newest_event = events_from_server[0];
|
||||
asserts.assert(newest_event);
|
||||
|
||||
const long_poll_request_promise = client.fetch(`/rooms/${room.id}/events?wait=true&after_id=${newest_event.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
const wait_and_then_create_an_event = new Promise((resolve) => {
|
||||
setTimeout(async () => {
|
||||
await client.fetch(`/rooms/${room.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
i: 12345
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
resolve(undefined);
|
||||
}, 2_000);
|
||||
});
|
||||
|
||||
await Promise.all([long_poll_request_promise, wait_and_then_create_an_event]).then((values) => {
|
||||
const long_polled_events = values.shift();
|
||||
asserts.assert(Array.isArray(long_polled_events));
|
||||
asserts.assertEquals(long_polled_events.length, 1);
|
||||
asserts.assertEquals(long_polled_events[0].data?.i, 12345);
|
||||
});
|
||||
} finally {
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
|
@ -2,6 +2,7 @@ import * as asserts from 'jsr:@std/assert';
|
|||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../../helpers.ts';
|
||||
import { api, API_CLIENT } from '../../../../utils/api.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - EVENTS - Update',
|
||||
|
@ -235,6 +236,7 @@ Deno.test({
|
|||
|
||||
asserts.assertEquals(delete_owner_event_response.deleted, true);
|
||||
} finally {
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import * as asserts from 'jsr:@std/assert';
|
|||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../../helpers.ts';
|
||||
import { api, API_CLIENT } from '../../../../utils/api.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - EVENTS - Update (APPEND_ONLY_EVENTS)',
|
||||
|
@ -159,6 +160,7 @@ Deno.test({
|
|||
} finally {
|
||||
Deno.env.delete('APPEND_ONLY_EVENTS');
|
||||
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import { api, API_CLIENT } from '../../../utils/api.ts';
|
|||
import * as asserts from 'jsr:@std/assert';
|
||||
import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../helpers.ts';
|
||||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
import { clear_room_events_cache } from '../../../models/event.ts';
|
||||
|
||||
Deno.test({
|
||||
name: 'API - ROOMS - Update',
|
||||
|
@ -91,6 +92,7 @@ Deno.test({
|
|||
asserts.assertEquals(updated_by_other_user_room.topic, 'this is a newer topic');
|
||||
asserts.assertEquals(updated_by_other_user_room.permissions.write, [user_info.user.id, other_user_info.user.id]);
|
||||
} finally {
|
||||
clear_room_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue