mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 13:35:42 +09:00
chore(repo): remove dead code and useless tests (#3375)
This commit is contained in:
1 parent
7e177a1c90
commit
1cd62eb661
1391 files changed
+783
-120415
No files matched your search
@@ -1,34 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {getConfig} from '@app/api/Config';
|
||||
import {resolveContactEmails} from '@app/api/instance/ContactEmails';
|
||||
import {afterEach, describe, expect, it} from 'vitest';
|
||||
|
||||
describe('resolveContactEmails', () => {
|
||||
const originalSelfHosted = getConfig().instance.selfHosted;
|
||||
|
||||
afterEach(() => {
|
||||
getConfig().instance.selfHosted = originalSelfHosted;
|
||||
});
|
||||
|
||||
it('gives a hosted instance the hosted mailboxes', () => {
|
||||
getConfig().instance.selfHosted = false;
|
||||
expect(resolveContactEmails()).toEqual({
|
||||
appealsEmail: '[email protected]',
|
||||
safetyEmail: '[email protected]',
|
||||
supportEmail: '[email protected]',
|
||||
});
|
||||
});
|
||||
|
||||
it('gives a self-hosted instance no mailbox at all', () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
expect(resolveContactEmails()).toEqual({appealsEmail: null, safetyEmail: null, supportEmail: null});
|
||||
});
|
||||
|
||||
it('follows the instance switch on every call', () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
expect(resolveContactEmails().supportEmail).toBeNull();
|
||||
getConfig().instance.selfHosted = false;
|
||||
expect(resolveContactEmails().supportEmail).toBe('[email protected]');
|
||||
});
|
||||
});
|
||||
@@ -1,84 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {
|
||||
GATEWAY_ROLLOUT_CONFIG_NATS_SUBJECT,
|
||||
GatewayRolloutConfigPublisher,
|
||||
} from '@app/api/instance/GatewayRolloutConfigPublisher';
|
||||
import type {GatewayRolloutConfig} from '@fluxer/schema/src/domains/admin/GatewayRolloutSchemas';
|
||||
import type {NatsConnection} from '@nats-io/transport-node';
|
||||
import type {INatsConnectionManager} from '@pkgs/nats/src/INatsConnectionManager';
|
||||
import {describe, expect, it} from 'vitest';
|
||||
|
||||
interface FakePublish {
|
||||
subject: string;
|
||||
body: Record<string, unknown>;
|
||||
}
|
||||
|
||||
class FakeNatsConnectionManager implements INatsConnectionManager {
|
||||
private closed = true;
|
||||
readonly publishes: Array<FakePublish> = [];
|
||||
connectCalls = 0;
|
||||
flushCalls = 0;
|
||||
|
||||
async connect(): Promise<void> {
|
||||
this.connectCalls += 1;
|
||||
this.closed = false;
|
||||
}
|
||||
|
||||
getConnection(): NatsConnection {
|
||||
if (this.closed) {
|
||||
throw new Error('not connected');
|
||||
}
|
||||
return {
|
||||
publish: (subject: string, data: Uint8Array) => {
|
||||
this.publishes.push({
|
||||
subject,
|
||||
body: JSON.parse(new TextDecoder().decode(data)) as Record<string, unknown>,
|
||||
});
|
||||
},
|
||||
flush: async () => {
|
||||
this.flushCalls += 1;
|
||||
},
|
||||
} as unknown as NatsConnection;
|
||||
}
|
||||
|
||||
async drain(): Promise<void> {
|
||||
this.closed = true;
|
||||
}
|
||||
|
||||
isClosed(): boolean {
|
||||
return this.closed;
|
||||
}
|
||||
}
|
||||
|
||||
describe('GatewayRolloutConfigPublisher', () => {
|
||||
it('publishes the validated rollout config on the gateway rollout subject', async () => {
|
||||
const manager = new FakeNatsConnectionManager();
|
||||
const publisher = new GatewayRolloutConfigPublisher(manager);
|
||||
const config: GatewayRolloutConfig = {
|
||||
session_rollout_percentage: 25,
|
||||
session_rollout_mode: 'modulo',
|
||||
guild_rollout_percentage: 50,
|
||||
rpc_request_timeout_ms: 10000,
|
||||
max_concurrent_session_starts: 512,
|
||||
max_concurrent_guild_starts: 256,
|
||||
gateway_dispatch_relay_shards: 32,
|
||||
gateway_dispatch_relay_max_queue: 50000,
|
||||
voice_e2ee_scope: 'guild_feature_only',
|
||||
};
|
||||
|
||||
await publisher.publish(config);
|
||||
|
||||
expect(manager.connectCalls).toBe(1);
|
||||
expect(manager.flushCalls).toBe(1);
|
||||
expect(manager.publishes).toEqual([
|
||||
{
|
||||
subject: GATEWAY_ROLLOUT_CONFIG_NATS_SUBJECT,
|
||||
body: {
|
||||
type: 'gateway_rollout_config',
|
||||
config,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import type {INatsConnectionManager} from '@pkgs/nats/src/INatsConnectionManager
|
||||
|
||||
const textEncoder = new TextEncoder();
|
||||
|
||||
export const GATEWAY_ROLLOUT_CONFIG_NATS_SUBJECT = 'config.gateway.rollout';
|
||||
const GATEWAY_ROLLOUT_CONFIG_NATS_SUBJECT = 'config.gateway.rollout';
|
||||
|
||||
interface GatewayRolloutConfigNatsMessage {
|
||||
type: 'gateway_rollout_config';
|
||||
|
||||
@@ -25,14 +25,8 @@ import {
|
||||
DEFAULT_CHANNEL_THREADS_CONFIG,
|
||||
everyoneChannelThreadsConfig,
|
||||
} from '@fluxer/schema/src/domains/admin/ChannelThreadsSchemas';
|
||||
import {
|
||||
DEFAULT_DOMAIN_MIGRATION_CONFIG,
|
||||
type DomainMigrationConfig,
|
||||
} from '@fluxer/schema/src/domains/admin/DomainMigrationSchemas';
|
||||
import {
|
||||
DEFAULT_EXPERIMENT_DELIVERY_CONFIG,
|
||||
type ExperimentDeliveryConfig,
|
||||
} from '@fluxer/schema/src/domains/experiment/ExperimentSchemas';
|
||||
import {DEFAULT_DOMAIN_MIGRATION_CONFIG} from '@fluxer/schema/src/domains/admin/DomainMigrationSchemas';
|
||||
import {DEFAULT_EXPERIMENT_DELIVERY_CONFIG} from '@fluxer/schema/src/domains/experiment/ExperimentSchemas';
|
||||
import {
|
||||
getDefaultPostgresClient,
|
||||
type IPostgresClient,
|
||||
@@ -498,47 +492,6 @@ describe('InstanceConfigRepository', () => {
|
||||
await expect(repository.getDomainMigrationConfig()).resolves.toEqual(DEFAULT_DOMAIN_MIGRATION_CONFIG);
|
||||
});
|
||||
|
||||
it('round-trips a stored domain migration config', async () => {
|
||||
const executor = new CountingInMemoryCassandraQueryExecutor();
|
||||
setCassandraQueryExecutorForTesting(executor);
|
||||
const kvProvider = new MockKVProvider();
|
||||
const repository = createRepository(kvProvider);
|
||||
|
||||
const config: DomainMigrationConfig = {
|
||||
...DEFAULT_DOMAIN_MIGRATION_CONFIG,
|
||||
enabled: true,
|
||||
config_version: 5,
|
||||
rollout_basis_points: 2500,
|
||||
rollout_salt: 'domain-migration-v2',
|
||||
included_user_ids: ['1400000000000000001'],
|
||||
excluded_user_ids: ['1400000000000000002'],
|
||||
anonymous_rollout_basis_points: 300,
|
||||
standalone_forwarding: true,
|
||||
};
|
||||
await repository.setDomainMigrationConfig(config);
|
||||
|
||||
await expect(repository.getDomainMigrationConfig()).resolves.toEqual(config);
|
||||
});
|
||||
|
||||
it('fills newly added domain migration fields from the schema defaults', async () => {
|
||||
const executor = new CountingInMemoryCassandraQueryExecutor();
|
||||
setCassandraQueryExecutorForTesting(executor);
|
||||
const kvProvider = new MockKVProvider();
|
||||
const repository = createRepository(kvProvider);
|
||||
|
||||
await repository.setConfig(
|
||||
DOMAIN_MIGRATION_CONFIG_KEY,
|
||||
JSON.stringify({enabled: true, config_version: 2, rollout_basis_points: 1000}),
|
||||
);
|
||||
|
||||
await expect(repository.getDomainMigrationConfig()).resolves.toEqual({
|
||||
...DEFAULT_DOMAIN_MIGRATION_CONFIG,
|
||||
enabled: true,
|
||||
config_version: 2,
|
||||
rollout_basis_points: 1000,
|
||||
});
|
||||
});
|
||||
|
||||
it('publishes a refresh so another repository observes the domain migration config', async () => {
|
||||
const executor = new CountingInMemoryCassandraQueryExecutor();
|
||||
setCassandraQueryExecutorForTesting(executor);
|
||||
@@ -585,32 +538,6 @@ describe('InstanceConfigRepository', () => {
|
||||
await expect(repository.getExperimentDeliveryConfig()).resolves.toEqual(DEFAULT_EXPERIMENT_DELIVERY_CONFIG);
|
||||
});
|
||||
|
||||
it('round-trips a stored experiment delivery config', async () => {
|
||||
const executor = new CountingInMemoryCassandraQueryExecutor();
|
||||
setCassandraQueryExecutorForTesting(executor);
|
||||
const kvProvider = new MockKVProvider();
|
||||
const repository = createRepository(kvProvider);
|
||||
|
||||
const config: ExperimentDeliveryConfig = {poll_interval_seconds: 900, poll_jitter_percent: 0};
|
||||
await repository.setExperimentDeliveryConfig(config);
|
||||
|
||||
await expect(repository.getExperimentDeliveryConfig()).resolves.toEqual(config);
|
||||
});
|
||||
|
||||
it('fills missing experiment delivery fields from the schema defaults', async () => {
|
||||
const executor = new CountingInMemoryCassandraQueryExecutor();
|
||||
setCassandraQueryExecutorForTesting(executor);
|
||||
const kvProvider = new MockKVProvider();
|
||||
const repository = createRepository(kvProvider);
|
||||
|
||||
await repository.setConfig(EXPERIMENT_DELIVERY_CONFIG_KEY, JSON.stringify({poll_interval_seconds: 3600}));
|
||||
|
||||
await expect(repository.getExperimentDeliveryConfig()).resolves.toEqual({
|
||||
...DEFAULT_EXPERIMENT_DELIVERY_CONFIG,
|
||||
poll_interval_seconds: 3600,
|
||||
});
|
||||
});
|
||||
|
||||
it('uses the registration URL id as the admin-visible registration code', async () => {
|
||||
const executor = new CountingInMemoryCassandraQueryExecutor();
|
||||
setCassandraQueryExecutorForTesting(executor);
|
||||
|
||||
@@ -14,7 +14,6 @@ import {MockKVProvider} from '@app/api/test/mocks/MockKVProvider';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
const INSTANCE_BILLING_CONFIG_KEY = 'instance_billing_config';
|
||||
const APP_PUBLIC_CONFIG_KEY = 'app_public_config';
|
||||
|
||||
const GBP_PRICES = {
|
||||
monthly: 'price_GbpMonthly',
|
||||
@@ -59,23 +58,6 @@ describe('InstanceConfigRepository billing and premium branding', () => {
|
||||
return raw === null ? null : JSON.parse(raw);
|
||||
}
|
||||
|
||||
it('returns an all-null billing config when nothing is stored', async () => {
|
||||
const repository = createRepository();
|
||||
expect(await repository.getInstanceBillingConfig()).toEqual({
|
||||
enabled: null,
|
||||
stripe_secret_key: null,
|
||||
stripe_webhook_secret: null,
|
||||
automatic_tax: null,
|
||||
tax_id_collection: null,
|
||||
terms_consent_required: null,
|
||||
default_currency: null,
|
||||
prices: null,
|
||||
country_currencies: null,
|
||||
legacy_prices: null,
|
||||
});
|
||||
expect(getEffectiveBillingConfig().catalogMode).toBe('env');
|
||||
});
|
||||
|
||||
it('merges patches, keeps secrets on undefined, clears them on null and replaces the catalog maps', async () => {
|
||||
const repository = createRepository();
|
||||
await repository.setInstanceBillingConfig({
|
||||
@@ -138,48 +120,6 @@ describe('InstanceConfigRepository billing and premium branding', () => {
|
||||
expect(await repository.getConfig(INSTANCE_BILLING_CONFIG_KEY)).toBeNull();
|
||||
});
|
||||
|
||||
it('redacts secrets in the admin view and reports the effective state', async () => {
|
||||
Config.instance.selfHosted = true;
|
||||
setCachedInstancePremiumMode('mirror');
|
||||
const repository = createRepository();
|
||||
await repository.setInstanceBillingConfig({
|
||||
enabled: true,
|
||||
stripe_secret_key: 'sk_test_do_not_leak',
|
||||
stripe_webhook_secret: 'whsec_do_not_leak',
|
||||
prices: {GBP: GBP_PRICES},
|
||||
});
|
||||
|
||||
const admin = await repository.getInstanceBillingAdminConfig();
|
||||
expect(JSON.stringify(admin)).not.toContain('do_not_leak');
|
||||
expect(admin).toEqual({
|
||||
enabled: true,
|
||||
effective_enabled: true,
|
||||
stripe_secret_key_set: true,
|
||||
stripe_webhook_secret_set: true,
|
||||
stripe_secret_key_stored: true,
|
||||
stripe_webhook_secret_stored: true,
|
||||
automatic_tax: null,
|
||||
tax_id_collection: null,
|
||||
terms_consent_required: null,
|
||||
effective_automatic_tax: false,
|
||||
effective_tax_id_collection: false,
|
||||
effective_terms_consent_required: false,
|
||||
default_currency: null,
|
||||
prices: {GBP: GBP_PRICES},
|
||||
country_currencies: null,
|
||||
legacy_prices: null,
|
||||
billing_active: true,
|
||||
stripe_serviceable: true,
|
||||
catalog_mode: 'operator',
|
||||
webhook_url: `${Config.endpoints.apiPublic.replace(/\/+$/, '')}/stripe/webhook`,
|
||||
});
|
||||
|
||||
setCachedInstancePremiumMode('everyone');
|
||||
const everyone = await repository.getInstanceBillingAdminConfig();
|
||||
expect(everyone.billing_active).toBe(false);
|
||||
expect(everyone.stripe_serviceable).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps, sets and clears the checkout flags like the other nullable fields', async () => {
|
||||
const repository = createRepository();
|
||||
const set = await repository.setInstanceBillingConfig({automatic_tax: true, terms_consent_required: false});
|
||||
@@ -287,44 +227,4 @@ describe('InstanceConfigRepository billing and premium branding', () => {
|
||||
});
|
||||
expect(getEffectiveBillingConfig().catalogMode).toBe('operator');
|
||||
});
|
||||
|
||||
it('defaults the premium product name to Plutonium on hosted and Premium on self-hosted', async () => {
|
||||
const repository = createRepository();
|
||||
Config.instance.selfHosted = false;
|
||||
const hosted = await repository.getAppPublicConfig();
|
||||
expect(hosted.branding.premium_product_name).toBe('Plutonium');
|
||||
expect(hosted.branding.premium_info_url).toBeNull();
|
||||
Config.instance.selfHosted = true;
|
||||
expect((await repository.getAppPublicConfig()).branding.premium_product_name).toBe('Premium');
|
||||
});
|
||||
|
||||
it('stores the premium name nullable so a reset and unrelated branding saves keep the default', async () => {
|
||||
Config.instance.selfHosted = true;
|
||||
const repository = createRepository();
|
||||
|
||||
await repository.setAppPublicConfig({branding: {theme_color: '#123456'}});
|
||||
expect(await readRaw(repository, APP_PUBLIC_CONFIG_KEY)).toMatchObject({
|
||||
branding: {theme_color: '#123456', premium_product_name: null},
|
||||
});
|
||||
|
||||
const named = await repository.setAppPublicConfig({
|
||||
branding: {premium_product_name: 'Gold', premium_info_url: 'https://example.com/gold'},
|
||||
});
|
||||
expect(named.branding.premium_product_name).toBe('Gold');
|
||||
expect(named.branding.premium_info_url).toBe('https://example.com/gold');
|
||||
|
||||
const unrelated = await repository.setAppPublicConfig({branding: {product_name: 'Example'}});
|
||||
expect(unrelated.branding.premium_product_name).toBe('Gold');
|
||||
expect(unrelated.branding.premium_info_url).toBe('https://example.com/gold');
|
||||
|
||||
const reset = await repository.setAppPublicConfig({branding: {premium_product_name: null, premium_info_url: null}});
|
||||
expect(reset.branding.premium_product_name).toBe('Premium');
|
||||
expect(reset.branding.premium_info_url).toBeNull();
|
||||
expect(await readRaw(repository, APP_PUBLIC_CONFIG_KEY)).toMatchObject({
|
||||
branding: {product_name: 'Example', premium_product_name: null, premium_info_url: null},
|
||||
});
|
||||
|
||||
Config.instance.selfHosted = false;
|
||||
expect((await repository.getAppPublicConfig()).branding.premium_product_name).toBe('Plutonium');
|
||||
});
|
||||
});
|
||||
@@ -1,59 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {getConfig} from '@app/api/Config';
|
||||
import {getLegalUrls, resolveLegalUrls, setCachedConfiguredLegalUrls} from '@app/api/instance/LegalUrls';
|
||||
import {afterEach, describe, expect, it} from 'vitest';
|
||||
|
||||
const UNSET = {terms_url: null, guidelines_url: null};
|
||||
const CONFIGURED = {terms_url: 'https://example.org/tos', guidelines_url: 'https://example.org/rules'};
|
||||
|
||||
describe('resolveLegalUrls', () => {
|
||||
const originalSelfHosted = getConfig().instance.selfHosted;
|
||||
|
||||
afterEach(() => {
|
||||
getConfig().instance.selfHosted = originalSelfHosted;
|
||||
setCachedConfiguredLegalUrls(UNSET);
|
||||
});
|
||||
|
||||
it('points a hosted instance at the marketing pages by default', () => {
|
||||
getConfig().instance.selfHosted = false;
|
||||
const marketing = getConfig().endpoints.marketing;
|
||||
expect(resolveLegalUrls(UNSET)).toEqual({
|
||||
termsUrl: `${marketing}/terms`,
|
||||
guidelinesUrl: `${marketing}/guidelines`,
|
||||
});
|
||||
});
|
||||
|
||||
it('lets a hosted instance override each page', () => {
|
||||
getConfig().instance.selfHosted = false;
|
||||
expect(resolveLegalUrls(CONFIGURED)).toEqual({
|
||||
termsUrl: 'https://example.org/tos',
|
||||
guidelinesUrl: 'https://example.org/rules',
|
||||
});
|
||||
expect(resolveLegalUrls({terms_url: null, guidelines_url: 'https://example.org/rules'}).termsUrl).toBe(
|
||||
`${getConfig().endpoints.marketing}/terms`,
|
||||
);
|
||||
});
|
||||
|
||||
it('gives a self-hosted instance no links until they are configured', () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
expect(resolveLegalUrls(UNSET)).toEqual({termsUrl: null, guidelinesUrl: null});
|
||||
expect(resolveLegalUrls({terms_url: null, guidelines_url: 'https://example.org/rules'})).toEqual({
|
||||
termsUrl: null,
|
||||
guidelinesUrl: 'https://example.org/rules',
|
||||
});
|
||||
expect(resolveLegalUrls(CONFIGURED)).toEqual({
|
||||
termsUrl: 'https://example.org/tos',
|
||||
guidelinesUrl: 'https://example.org/rules',
|
||||
});
|
||||
});
|
||||
|
||||
it('resolves the cached instance config', () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
expect(getLegalUrls()).toEqual({termsUrl: null, guidelinesUrl: null});
|
||||
setCachedConfiguredLegalUrls(CONFIGURED);
|
||||
expect(getLegalUrls()).toEqual({termsUrl: 'https://example.org/tos', guidelinesUrl: 'https://example.org/rules'});
|
||||
getConfig().instance.selfHosted = false;
|
||||
expect(getLegalUrls()).toEqual({termsUrl: 'https://example.org/tos', guidelinesUrl: 'https://example.org/rules'});
|
||||
});
|
||||
});
|
||||
@@ -1,35 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {getConfig} from '@app/api/Config';
|
||||
import {getDefaultProductName, getInstanceProductName, setCachedProductName} from '@app/api/instance/ProductName';
|
||||
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
||||
|
||||
describe('getInstanceProductName', () => {
|
||||
const originalProductName = getConfig().instance.branding.productName;
|
||||
|
||||
beforeEach(() => {
|
||||
setCachedProductName(null);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
getConfig().instance.branding.productName = originalProductName;
|
||||
setCachedProductName(null);
|
||||
});
|
||||
|
||||
it('uses the configured name until a stored name is known', () => {
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
expect(getDefaultProductName()).toBe('Configured Chat');
|
||||
expect(getInstanceProductName()).toBe('Configured Chat');
|
||||
});
|
||||
|
||||
it('falls back to Fluxer when no name is configured', () => {
|
||||
getConfig().instance.branding.productName = '';
|
||||
expect(getInstanceProductName()).toBe('Fluxer');
|
||||
});
|
||||
|
||||
it('prefers the stored name', () => {
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
setCachedProductName('Example Chat');
|
||||
expect(getInstanceProductName()).toBe('Example Chat');
|
||||
});
|
||||
});
|
||||
@@ -1,164 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createApplicationID} from '@app/api/BrandedTypes';
|
||||
import {getConfig} from '@app/api/Config';
|
||||
import {setCachedProductName} from '@app/api/instance/ProductName';
|
||||
import {createRuntimeEmailService, getInstanceConfigRepository} from '@app/api/middleware/ServiceSingletons';
|
||||
import {ApplicationRepository, resetAdminSecretHashForTesting} from '@app/api/oauth/repositories/ApplicationRepository';
|
||||
import {ApnsPushServiceTestHooks} from '@app/api/push/ApnsPushService';
|
||||
import type {ApiTestHarness} from '@app/api/test/ApiTestHarness';
|
||||
import {createApiTestHarness} from '@app/api/test/ApiTestHarness';
|
||||
import {ADMIN_OAUTH2_APPLICATION_ID} from '@fluxer/constants/src/Core';
|
||||
import {EmailI18nService} from '@pkgs/email/src/EmailI18nService';
|
||||
import type {IEmailService} from '@pkgs/email/src/IEmailService';
|
||||
import {SmtpEmailProvider} from '@pkgs/email/src/SmtpEmailProvider';
|
||||
import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
describe('instance product name in server-rendered text', () => {
|
||||
let harness: ApiTestHarness;
|
||||
const original = {
|
||||
productName: getConfig().instance.branding.productName,
|
||||
selfHosted: getConfig().instance.selfHosted,
|
||||
adminSecret: getConfig().admin.oauthClientSecret,
|
||||
emailEnabled: getConfig().email.enabled,
|
||||
emailFromName: getConfig().email.fromName,
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
harness = await createApiTestHarness();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await harness.reset();
|
||||
getInstanceConfigRepository().clearCacheForTesting();
|
||||
setCachedProductName(null);
|
||||
getConfig().email.enabled = false;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
getConfig().instance.branding.productName = original.productName;
|
||||
getConfig().instance.selfHosted = original.selfHosted;
|
||||
getConfig().admin.oauthClientSecret = original.adminSecret;
|
||||
getConfig().email.enabled = original.emailEnabled;
|
||||
getConfig().email.fromName = original.emailFromName;
|
||||
resetAdminSecretHashForTesting();
|
||||
setCachedProductName(null);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await harness.shutdown();
|
||||
});
|
||||
|
||||
function createRecordedEmailService(): {service: IEmailService; subjects: Array<string>; names: Array<string>} {
|
||||
const subjects: Array<string> = [];
|
||||
const names: Array<string> = [];
|
||||
const getTemplate = EmailI18nService.prototype.getTemplate;
|
||||
vi.spyOn(EmailI18nService.prototype, 'getTemplate').mockImplementation(function (
|
||||
this: EmailI18nService,
|
||||
key,
|
||||
locale,
|
||||
variables,
|
||||
productName,
|
||||
) {
|
||||
const result = getTemplate.call(this, key, locale, variables, productName);
|
||||
names.push(productName);
|
||||
if (result.ok) subjects.push(result.value.subject);
|
||||
return result;
|
||||
});
|
||||
return {service: createRuntimeEmailService({isEmailBounced: async () => false}), subjects, names};
|
||||
}
|
||||
|
||||
it('sends hosted emails as Fluxer', async () => {
|
||||
getConfig().instance.branding.productName = '';
|
||||
const {service, subjects, names} = createRecordedEmailService();
|
||||
await expect(service.sendPasswordResetEmail('[email protected]', 'testuser', 'token', 'en-US')).resolves.toBe(true);
|
||||
expect(names).toEqual(['Fluxer']);
|
||||
expect(subjects).toEqual(['Reset your Fluxer password']);
|
||||
});
|
||||
|
||||
it('sends self-hosted emails under the configured name', async () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
const {service, subjects} = createRecordedEmailService();
|
||||
await service.sendPasswordResetEmail('[email protected]', 'testuser', 'token', 'en-US');
|
||||
await service.sendEmailVerification('[email protected]', 'testuser', 'token', 'de');
|
||||
expect(subjects[0]).toBe('Reset your Configured Chat password');
|
||||
expect(subjects[1]).toContain('Configured Chat');
|
||||
expect(subjects.join('\n')).not.toContain('Fluxer');
|
||||
});
|
||||
|
||||
it('picks up a name saved in the dashboard on the next email without a restart', async () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
const {service, subjects} = createRecordedEmailService();
|
||||
await service.sendPasswordResetEmail('[email protected]', 'testuser', 'token', 'en-US');
|
||||
await getInstanceConfigRepository().setAppPublicConfig({branding: {product_name: 'Renamed Chat'}});
|
||||
await service.sendPasswordResetEmail('[email protected]', 'testuser', 'token', 'en-US');
|
||||
setCachedProductName(null);
|
||||
await service.sendPasswordResetEmail('[email protected]', 'testuser', 'token', 'en-US');
|
||||
expect(subjects).toEqual([
|
||||
'Reset your Configured Chat password',
|
||||
'Reset your Renamed Chat password',
|
||||
'Reset your Renamed Chat password',
|
||||
]);
|
||||
});
|
||||
|
||||
async function sendAndReadSenderName(): Promise<string | undefined> {
|
||||
getConfig().email.enabled = true;
|
||||
const sendEmail = vi.spyOn(SmtpEmailProvider.prototype, 'sendEmail').mockResolvedValue(true);
|
||||
const service = createRuntimeEmailService({isEmailBounced: async () => false});
|
||||
await expect(service.sendPasswordResetEmail('[email protected]', 'testuser', 'token', 'en-US')).resolves.toBe(true);
|
||||
return sendEmail.mock.calls.at(-1)?.[0].from.name;
|
||||
}
|
||||
|
||||
it('sends hosted emails from Fluxer when no sender name is set', async () => {
|
||||
getConfig().instance.branding.productName = '';
|
||||
getConfig().email.fromName = '';
|
||||
await expect(sendAndReadSenderName()).resolves.toBe('Fluxer');
|
||||
});
|
||||
|
||||
it('sends self-hosted emails from the product name when no sender name is set', async () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
getConfig().email.fromName = '';
|
||||
await expect(sendAndReadSenderName()).resolves.toBe('Configured Chat');
|
||||
await getInstanceConfigRepository().setAppPublicConfig({branding: {product_name: 'Renamed Chat'}});
|
||||
await expect(sendAndReadSenderName()).resolves.toBe('Renamed Chat');
|
||||
});
|
||||
|
||||
it('keeps a configured sender name over the product name', async () => {
|
||||
getConfig().instance.selfHosted = true;
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
getConfig().email.fromName = 'Configured Mailer';
|
||||
await expect(sendAndReadSenderName()).resolves.toBe('Configured Mailer');
|
||||
getConfig().instance.selfHosted = false;
|
||||
getConfig().instance.branding.productName = '';
|
||||
await expect(sendAndReadSenderName()).resolves.toBe('Configured Mailer');
|
||||
});
|
||||
|
||||
it('titles an APNs alert that has no title with the instance name', () => {
|
||||
const titleOf = (): unknown => {
|
||||
const {aps} = ApnsPushServiceTestHooks.buildApnsPayload({notification: {body: 'Hello'}});
|
||||
return (aps as {alert: {title: string}}).alert.title;
|
||||
};
|
||||
getConfig().instance.branding.productName = '';
|
||||
expect(titleOf()).toBe('Fluxer');
|
||||
getConfig().instance.branding.productName = 'Configured Chat';
|
||||
expect(titleOf()).toBe('Configured Chat');
|
||||
setCachedProductName('Renamed Chat');
|
||||
expect(titleOf()).toBe('Renamed Chat');
|
||||
});
|
||||
|
||||
it('names the built-in admin application after the instance', async () => {
|
||||
getConfig().admin.oauthClientSecret = 'product-name-test-secret';
|
||||
resetAdminSecretHashForTesting();
|
||||
const repository = new ApplicationRepository();
|
||||
const nameOf = async (): Promise<string | undefined> =>
|
||||
(await repository.getApplication(createApplicationID(ADMIN_OAUTH2_APPLICATION_ID)))?.name;
|
||||
getConfig().instance.branding.productName = '';
|
||||
await expect(nameOf()).resolves.toBe('Fluxer Admin');
|
||||
setCachedProductName('Renamed Chat');
|
||||
await expect(nameOf()).resolves.toBe('Renamed Chat Admin');
|
||||
});
|
||||
});
|
||||
@@ -1,79 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import type {InstancePolicyConfig} from '@app/api/instance/InstanceConfigRepository';
|
||||
import {SingleCommunityService} from '@app/api/instance/SingleCommunityService';
|
||||
import type {User} from '@app/api/models/User';
|
||||
import {UnknownGuildError} from '@fluxer/errors/src/domains/guild/UnknownGuildError';
|
||||
import {describe, expect, it} from 'vitest';
|
||||
|
||||
const EXISTING_GUILD_ID = '1234567890123456789';
|
||||
const OWNER = {id: 42n} as unknown as User;
|
||||
|
||||
interface Harness {
|
||||
service: SingleCommunityService;
|
||||
written: Array<Partial<InstancePolicyConfig>>;
|
||||
createdNames: Array<string>;
|
||||
}
|
||||
|
||||
function createHarness(params: {designatedGuildId: string | null; designatedGuildExists: boolean}): Harness {
|
||||
const written: Array<Partial<InstancePolicyConfig>> = [];
|
||||
const createdNames: Array<string> = [];
|
||||
let nextCreatedGuildId = 999n;
|
||||
const instanceConfigRepository = {
|
||||
getInstancePolicyConfig: async () => ({
|
||||
single_community_enabled: false,
|
||||
single_community_guild_id: params.designatedGuildId,
|
||||
}),
|
||||
setInstancePolicyConfig: async (patch: Partial<InstancePolicyConfig>) => {
|
||||
written.push(patch);
|
||||
},
|
||||
};
|
||||
const guildDataService = {
|
||||
getGuildSystem: async () => {
|
||||
if (!params.designatedGuildExists) {
|
||||
throw new UnknownGuildError();
|
||||
}
|
||||
return {} as never;
|
||||
},
|
||||
createGuild: async ({data}: {data: {name: string}}) => {
|
||||
createdNames.push(data.name);
|
||||
nextCreatedGuildId += 1n;
|
||||
return {id: nextCreatedGuildId.toString()} as never;
|
||||
},
|
||||
};
|
||||
const service = new SingleCommunityService(
|
||||
instanceConfigRepository as never,
|
||||
guildDataService as never,
|
||||
null as never,
|
||||
);
|
||||
return {service, written, createdNames};
|
||||
}
|
||||
|
||||
describe('SingleCommunityService.ensureStockCommunity', () => {
|
||||
it('reuses the designated community when it still exists', async () => {
|
||||
const harness = createHarness({designatedGuildId: EXISTING_GUILD_ID, designatedGuildExists: true});
|
||||
const guildId = await harness.service.ensureStockCommunity({owner: OWNER, name: 'Fluxer'});
|
||||
expect(guildId.toString()).toBe(EXISTING_GUILD_ID);
|
||||
expect(harness.createdNames).toEqual([]);
|
||||
expect(harness.written).toEqual([{single_community_enabled: true, single_community_guild_id: EXISTING_GUILD_ID}]);
|
||||
});
|
||||
|
||||
it('creates a fresh community when the designated one was deleted', async () => {
|
||||
const harness = createHarness({designatedGuildId: EXISTING_GUILD_ID, designatedGuildExists: false});
|
||||
const guildId = await harness.service.ensureStockCommunity({owner: OWNER, name: 'Fluxer'});
|
||||
expect(guildId.toString()).not.toBe(EXISTING_GUILD_ID);
|
||||
expect(harness.createdNames).toEqual(['Fluxer']);
|
||||
});
|
||||
|
||||
it('creates a fresh community when the instance never designated one', async () => {
|
||||
const harness = createHarness({designatedGuildId: null, designatedGuildExists: false});
|
||||
await harness.service.ensureStockCommunity({owner: OWNER, name: 'Fluxer'});
|
||||
expect(harness.createdNames).toEqual(['Fluxer']);
|
||||
});
|
||||
|
||||
it('rejects an invalid stored guild id without creating a community', async () => {
|
||||
const harness = createHarness({designatedGuildId: 'not-a-snowflake', designatedGuildExists: true});
|
||||
await expect(harness.service.ensureStockCommunity({owner: OWNER, name: 'Fluxer'})).rejects.toThrow(SyntaxError);
|
||||
expect(harness.createdNames).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user