A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {createTestAccount} from '@app/api/auth/tests/AuthTestUtils';
import {createGuild} from '@app/api/guild/tests/GuildTestUtils';
import {getMessages} from '@app/api/message/tests/MessageTestUtils';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
import {HTTP_STATUS} from '@app/api/test/TestConstants';
import {createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
import {createWebhook, deleteWebhook} from '@app/api/webhook/tests/WebhookTestUtils';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
function createInstatusPage() {
return {
id: 'pg_123',
status_indicator: 'HASISSUES',
status_description: 'Partially degraded service',
url: 'https://status.fluxer.app',
};
}
function createInstatusMeta() {
// `documentation` seems to always be an empty string.
// If only we had documentation about it.
return {unsubscribe: 'https://status.fluxer.app/unsubscribe?id=1&token=abc', documentation: ''};
}
function createInstatusIncident(overrides: Record<string, unknown> = {}) {
return {
id: 'inc_1',
name: 'Elevated API latency',
url: 'https://status.fluxer.app/incident/inc_1',
status: 'Investigating',
backfilled: false,
resolved_at: null,
created_at: '2026-07-06T10:00:00.000Z',
updated_at: '2026-07-06T10:00:00.000Z',
incident_updates: [
{
id: 'u_1',
incident_id: 'inc_1',
markdown: 'We are currently investigating this incident.',
status: 'Investigating',
created_at: '2026-07-06T10:00:00.000Z',
updated_at: '2026-07-06T10:00:00.000Z',
},
],
affected_components: [],
...overrides,
};
}
async function countWebhookMessages(
harness: ApiTestHarness,
token: string,
channelId: string,
webhookId: string,
): Promise<number> {
const messages = await getMessages(harness, token, channelId);
return messages.filter((message) => message.webhook_id === webhookId).length;
}
describe('Webhook Instatus integration', () => {
let harness: ApiTestHarness;
beforeEach(async () => {
harness = await createApiTestHarness();
});
afterEach(async () => {
await harness?.shutdown();
});
describe('POST /webhooks/:webhook_id/:token/instatus', () => {
it('suppresses a repeated incident callback within the deduplication window', async () => {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Instatus Duplicate Guild');
const channelId = guild.system_channel_id!;
const webhook = await createWebhook(harness, channelId, owner.token, 'Instatus Duplicate Webhook');
const body = {meta: createInstatusMeta(), page: createInstatusPage(), incident: createInstatusIncident()};
for (let attempt = 0; attempt < 2; attempt++) {
await createBuilderWithoutAuth(harness)
.post(`/webhooks/${webhook.id}/${webhook.token}/instatus`)
.body(body)
.expect(HTTP_STATUS.NO_CONTENT)
.execute();
}
expect(await countWebhookMessages(harness, owner.token, channelId, webhook.id)).toBe(1);
await deleteWebhook(harness, webhook.id, owner.token);
});
it('processes a callback with no identifier every time', async () => {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Instatus Unidentified Guild');
const channelId = guild.system_channel_id!;
const webhook = await createWebhook(harness, channelId, owner.token, 'Instatus Unidentified Webhook');
const body = {
meta: createInstatusMeta(),
page: createInstatusPage(),
incident: createInstatusIncident({id: null}),
};
for (let attempt = 0; attempt < 2; attempt++) {
await createBuilderWithoutAuth(harness)
.post(`/webhooks/${webhook.id}/${webhook.token}/instatus`)
.body(body)
.expect(HTTP_STATUS.NO_CONTENT)
.execute();
}
expect(await countWebhookMessages(harness, owner.token, channelId, webhook.id)).toBe(2);
await deleteWebhook(harness, webhook.id, owner.token);
});
it('rejects an instatus webhook with an invalid token', async () => {
const owner = await createTestAccount(harness);
const guild = await createGuild(harness, owner.token, 'Instatus Invalid Token Guild');
const channelId = guild.system_channel_id!;
const webhook = await createWebhook(harness, channelId, owner.token, 'Instatus Invalid Webhook');
await createBuilderWithoutAuth(harness)
.post(`/webhooks/${webhook.id}/invalid_token/instatus`)
.body({
meta: createInstatusMeta(),
page: createInstatusPage(),
incident: {name: 'Outage', status: 'INVESTIGATING'},
})
.expect(HTTP_STATUS.NOT_FOUND)
.execute();
await deleteWebhook(harness, webhook.id, owner.token);
});
});
});