Something went wrong. Try again.
A free and open source instant messaging and VoIP platform built for friends, groups, and communities.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485/* * Copyright (C) 2026 Fluxer Contributors * * This file is part of Fluxer. * * Fluxer is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Fluxer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Fluxer. If not, see <https://www.gnu.org/licenses/>. */
import {createTestAccount as authCreateTestAccount, type TestAccount} from '@fluxer/api/src/auth/tests/AuthTestUtils';import type {ApiTestHarness} from '@fluxer/api/src/test/ApiTestHarness';import {createBuilder} from '@fluxer/api/src/test/TestRequestBuilder';import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';import type {MessageResponse} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
export interface AckResponse { token: string;}
export async function createTestAccount(harness: ApiTestHarness): Promise<TestAccount> { return authCreateTestAccount(harness);}
export async function createMessageHarness(): Promise<ApiTestHarness> { const {createApiTestHarness} = await import('@fluxer/api/src/test/ApiTestHarness'); return await createApiTestHarness();}
export async function sendMessage( harness: ApiTestHarness, token: string, channelId: string, content: string,): Promise<MessageResponse> { await ensureSessionStarted(harness, token);
const msg = await createBuilder<MessageResponse>(harness, token) .post(`/channels/${channelId}/messages`) .body({ content, }) .execute();
if (!msg.id) { throw new Error('Message response missing id'); } return msg;}
export async function getMessage( harness: ApiTestHarness, token: string, channelId: string, messageId: string,): Promise<MessageResponse> { const msg = await createBuilder<MessageResponse>(harness, token) .get(`/channels/${channelId}/messages/${messageId}`) .execute();
if (!msg.id) { throw new Error('Message response missing id'); } return msg;}
export async function getMessages( harness: ApiTestHarness, token: string, channelId: string, queryParams?: Record<string, string>,): Promise<Array<MessageResponse>> { const queryString = queryParams ? `?${new URLSearchParams(Object.entries(queryParams).map(([k, v]) => [k, v] as [string, string])).toString()}` : '';
return createBuilder<Array<MessageResponse>>(harness, token) .get(`/channels/${channelId}/messages${queryString}`) .execute();}
export async function editMessage( harness: ApiTestHarness, token: string, channelId: string, messageId: string, content: string,): Promise<MessageResponse> { const msg = await createBuilder<MessageResponse>(harness, token) .patch(`/channels/${channelId}/messages/${messageId}`) .body({ content, }) .execute();
if (!msg.id) { throw new Error('Message response missing id'); } return msg;}
export async function deleteMessage( harness: ApiTestHarness, token: string, channelId: string, messageId: string,): Promise<void> { await createBuilder<void>(harness, token) .delete(`/channels/${channelId}/messages/${messageId}`) .expect(204) .execute();}
export async function ackMessage( harness: ApiTestHarness, token: string, channelId: string, messageId: string, mentionCount = 0,): Promise<void> { await createBuilder<void>(harness, token) .post(`/channels/${channelId}/messages/${messageId}/ack`) .body({ mention_count: mentionCount, }) .expect(204) .execute();}
export async function pinMessage( harness: ApiTestHarness, token: string, channelId: string, messageId: string,): Promise<void> { await createBuilder<void>(harness, token) .put(`/channels/${channelId}/pins/${messageId}`) .body(null) .expect(204) .execute();}
export async function unpinMessage( harness: ApiTestHarness, token: string, channelId: string, messageId: string,): Promise<void> { await createBuilder<void>(harness, token).delete(`/channels/${channelId}/pins/${messageId}`).expect(204).execute();}
export async function getPins( harness: ApiTestHarness, token: string, channelId: string,): Promise<Array<MessageResponse>> { const result = await createBuilder<{items: Array<{message: MessageResponse; pinned_at: string}>; has_more: boolean}>( harness, token, ) .get(`/channels/${channelId}/messages/pins`) .execute(); return result.items.map((item) => item.message);}
export async function createGuild(harness: ApiTestHarness, token: string, name: string): Promise<GuildResponse> { const guild = await createBuilder<GuildResponse>(harness, token) .post('/guilds') .body({ name, }) .execute();
if (!guild.id) { throw new Error('Guild response missing id'); } return guild;}
export async function createDMChannel( harness: ApiTestHarness, token: string, recipientUserId: string,): Promise<MessageResponse> { const channel = await createBuilder<MessageResponse>(harness, token) .post('/users/@me/channels') .body({ recipient_id: recipientUserId, }) .execute();
if (!channel.id) { throw new Error('Channel response missing id'); } return channel;}
export async function createChannelInvite( harness: ApiTestHarness, token: string, channelId: string,): Promise<{code: string}> { return createBuilder<{code: string}>(harness, token).post(`/channels/${channelId}/invites`).body({}).execute();}
export async function acceptInvite(harness: ApiTestHarness, token: string, inviteCode: string): Promise<void> { await createBuilder<void>(harness, token).post(`/invites/${inviteCode}`).body({}).expect(200).execute();}
export async function addReaction( harness: ApiTestHarness, token: string, channelId: string, messageId: string, emoji: string,): Promise<void> { await createBuilder<void>(harness, token) .put(`/channels/${channelId}/messages/${messageId}/reactions/${emoji}/@me`) .body(null) .expect(204) .execute();}
export async function removeReaction( harness: ApiTestHarness, token: string, channelId: string, messageId: string, emoji: string, userId?: string,): Promise<void> { const user = userId ?? '@me'; await createBuilder<void>(harness, token) .delete(`/channels/${channelId}/messages/${messageId}/reactions/${emoji}/${user}`) .expect(204) .execute();}
export async function removeAllReactions( harness: ApiTestHarness, token: string, channelId: string, messageId: string,): Promise<void> { await createBuilder<void>(harness, token) .delete(`/channels/${channelId}/messages/${messageId}/reactions`) .expect(204) .execute();}
export async function removeAllReactionsForEmoji( harness: ApiTestHarness, token: string, channelId: string, messageId: string, emoji: string,): Promise<void> { await createBuilder<void>(harness, token) .delete(`/channels/${channelId}/messages/${messageId}/reactions/${emoji}`) .expect(204) .execute();}
export async function deleteAttachment( harness: ApiTestHarness, token: string, channelId: string, messageId: string, attachmentId: string,): Promise<void> { await createBuilder<void>(harness, token) .delete(`/channels/${channelId}/messages/${messageId}/attachments/${attachmentId}`) .body(null) .expect(204) .execute();}
export async function updateChannelPermissions( harness: ApiTestHarness, token: string, channelId: string, overwriteId: string, overwrite: { type: number; allow?: string; deny?: string; },): Promise<void> { await createBuilder<void>(harness, token) .put(`/channels/${channelId}/permissions/${overwriteId}`) .body(overwrite) .expect(204) .execute();}
export async function editMessageWithAttachments( harness: ApiTestHarness, token: string, channelId: string, messageId: string, updates: { content?: string; attachments?: Array<Record<string, unknown>>; embeds?: Array<Record<string, unknown>>; flags?: number; },): Promise<MessageResponse> { const msg = await createBuilder<MessageResponse>(harness, token) .patch(`/channels/${channelId}/messages/${messageId}`) .body(updates) .execute();
if (!msg.id) { throw new Error('Message response missing id'); } return msg;}
export async function createFriendship(harness: ApiTestHarness, user1: TestAccount, user2: TestAccount): Promise<void> { await createBuilder<unknown>(harness, user1.token) .post(`/users/@me/relationships/${user2.userId}`) .body({}) .execute();
await createBuilder<unknown>(harness, user2.token).put(`/users/@me/relationships/${user1.userId}`).body({}).execute();}
export async function ensureSessionStarted(harness: ApiTestHarness, token: string): Promise<void> { const me = await createBuilder<{id: string; flags?: string | number}>(harness, token).get('/users/@me').execute();
const HAS_SESSION_STARTED = BigInt(1) << BigInt(39); const currentFlags = BigInt(me.flags ?? 0);
if ((currentFlags & HAS_SESSION_STARTED) !== BigInt(0)) { return; }
await createBuilder<unknown>(harness, token) .patch(`/test/users/${me.id}/flags`) .body({ flags: (currentFlags | HAS_SESSION_STARTED).toString(), }) .execute();}
export interface SeedMessageInput { message_id?: string; timestamp?: string; content?: string; author_id?: string;}
export interface SeededMessage { message_id: string; bucket: number; timestamp: string;}
export interface SeedMessagesResult { messages: Array<SeededMessage>; buckets_populated: Array<number>; channel_state_updated: boolean;}
export async function seedMessages( harness: ApiTestHarness, channelId: string, messages: Array<SeedMessageInput>, options?: { authorId?: string; clearExisting?: boolean; skipBucketIndex?: boolean; },): Promise<SeedMessagesResult> { return createBuilder<SeedMessagesResult>(harness, '') .post('/test/messages/seed') .body({ channel_id: channelId, messages, author_id: options?.authorId, clear_existing: options?.clearExisting ?? false, skip_bucket_index: options?.skipBucketIndex ?? false, }) .execute();}
export async function seedMessagesWithContent( harness: ApiTestHarness, channelId: string, count: number, authorId: string,): Promise<SeedMessagesResult> { const messages: Array<SeedMessageInput> = []; const baseTime = Date.now() - 3600000; for (let i = 0; i < count; i++) { messages.push({ timestamp: new Date(baseTime + i * 1000).toISOString(), content: `Test message ${i + 1}`, }); } return seedMessages(harness, channelId, messages, {authorId, clearExisting: true});}
export async function seedMessagesAtTimestamps( harness: ApiTestHarness, channelId: string, timestamps: Array<Date>, authorId: string,): Promise<SeedMessagesResult> { const messages: Array<SeedMessageInput> = timestamps.map((ts, i) => ({ timestamp: ts.toISOString(), content: `Test message ${i + 1}`, })); return seedMessages(harness, channelId, messages, {authorId, clearExisting: true});}
export interface ChannelStateResponse { channel_id: string; exists: boolean; created_bucket?: number; has_messages?: boolean; last_message_id?: string | null; last_message_bucket?: number | null; updated_at?: string;}
export async function getChannelState(harness: ApiTestHarness, channelId: string): Promise<ChannelStateResponse> { return createBuilder<ChannelStateResponse>(harness, '').get(`/test/channels/${channelId}/state`).execute();}
export interface ChannelBucket { bucket: number; updated_at: string;}
export interface ChannelBucketsResponse { channel_id: string; buckets: Array<ChannelBucket>; count: number;}
export async function getChannelBuckets(harness: ApiTestHarness, channelId: string): Promise<ChannelBucketsResponse> { return createBuilder<ChannelBucketsResponse>(harness, '').get(`/test/channels/${channelId}/buckets`).execute();}
export async function clearChannelMessages(harness: ApiTestHarness, channelId: string): Promise<void> { await createBuilder<{channel_id: string; cleared: boolean}>(harness, '') .delete(`/test/channels/${channelId}/messages`) .execute();}
export async function markChannelAsIndexed(harness: ApiTestHarness, channelId: string): Promise<void> { await createBuilder<{channel_id: string; indexed_at: string}>(harness, '') .post(`/test/channels/${channelId}/mark-indexed`) .body({}) .execute();}
export async function markGuildChannelsAsIndexed( harness: ApiTestHarness, token: string, guildId: string,): Promise<void> { const channels = await createBuilder<Array<{id: string}>>(harness, token) .get(`/guilds/${guildId}/channels`) .execute();
await Promise.all(channels.map((channel) => markChannelAsIndexed(harness, channel.id)));}
export async function markUserDmChannelsAsIndexed(harness: ApiTestHarness, token: string): Promise<void> { const channels = await createBuilder<Array<{id: string}>>(harness, token).get('/users/@me/channels').execute();
await Promise.all(channels.map((channel) => markChannelAsIndexed(harness, channel.id)));}