| 
									
										
										
										
											2025-07-24 12:09:24 -07:00
										 |  |  | import * as asserts from '@std/assert'; | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | import { EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from '../../../helpers.ts'; | 
					
						
							|  |  |  | import { api, API_CLIENT } from '../../../../utils/api.ts'; | 
					
						
							| 
									
										
										
										
											2025-07-24 12:09:24 -07:00
										 |  |  | import { generateTotp } from '../../../../utils/totp.ts'; | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | import { clear_topic_events_cache } from '../../../../models/event.ts'; | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | Deno.test({ | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 	name: 'API - TOPICS - EVENTS - Get', | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 	permissions: { | 
					
						
							|  |  |  | 		env: true, | 
					
						
							|  |  |  | 		read: true, | 
					
						
							|  |  |  | 		write: true, | 
					
						
							|  |  |  | 		net: true | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2025-07-24 12:09:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// TODO: see severus, but why do we need this?
 | 
					
						
							|  |  |  | 	sanitizeResources: false, | 
					
						
							|  |  |  | 	sanitizeOps: false, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 	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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 			await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'topics.create']); | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 			const topic = await client.fetch('/topics', { | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 				method: 'POST', | 
					
						
							|  |  |  | 				headers: { | 
					
						
							|  |  |  | 					'x-session_id': owner_info.session.id, | 
					
						
							|  |  |  | 					'x-totp': await generateTotp(owner_info.session.secret) | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: { | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 					name: 'test get events topic' | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 			asserts.assert(topic); | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			const NUM_INITIAL_EVENTS = 5; | 
					
						
							|  |  |  | 			const events_initial_batch: any[] = []; | 
					
						
							|  |  |  | 			for (let i = 0; i < NUM_INITIAL_EVENTS; ++i) { | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 				const event = await client.fetch(`/topics/${topic.id}/events`, { | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 					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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 			const events_from_server = await client.fetch(`/topics/${topic.id}/events`, { | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 				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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 			const long_poll_request_promise = client.fetch(`/topics/${topic.id}/events?wait=true&after_id=${newest_event.id}`, { | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 				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 () => { | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 					await client.fetch(`/topics/${topic.id}/events`, { | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 						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 { | 
					
						
							| 
									
										
										
										
											2025-09-10 12:51:27 -07:00
										 |  |  | 			clear_topic_events_cache(); | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 			if (test_server_info) { | 
					
						
							| 
									
										
										
										
											2025-07-24 12:09:24 -07:00
										 |  |  | 				await test_server_info.server.stop(); | 
					
						
							| 
									
										
										
										
											2025-07-02 21:28:07 -07:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }); |