mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 21:15:32 +09:00
test(backend): remove duplicate and useless tests (#2820)
This commit is contained in:
1 parent
deac653a9e
commit
56e04e7b53
102 files changed
+10
-15831
No files matched your search
@@ -1,70 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {getConfig} from '@fluxer/config/src/ConfigLoader';
|
||||
|
||||
type ReleaseChannel = 'stable' | 'canary';
|
||||
|
||||
interface BuildMetadata {
|
||||
buildVersion: string;
|
||||
releaseChannel: ReleaseChannel;
|
||||
}
|
||||
|
||||
const FALLBACK_VALUES = {
|
||||
BUILD_VERSION: 'dev',
|
||||
RELEASE_CHANNEL: 'stable',
|
||||
} as const;
|
||||
|
||||
let fallbackLogged = false;
|
||||
|
||||
function isDevEnvironment(): boolean {
|
||||
try {
|
||||
const config = getConfig();
|
||||
return config.env === 'development' || config.env === 'test';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function logFallbackWarning(usedFallbacks: Array<string>): void {
|
||||
if (fallbackLogged || usedFallbacks.length === 0 || isDevEnvironment()) {
|
||||
return;
|
||||
}
|
||||
fallbackLogged = true;
|
||||
process.stdout.write(
|
||||
`[build-metadata] Using fallback values for: ${usedFallbacks.join(', ')}. ` +
|
||||
`This indicates missing env vars in CI/production.\n`,
|
||||
);
|
||||
}
|
||||
|
||||
function getEnvOrDefault(name: string, defaultValue: string, usedFallbacks: Array<string>): string {
|
||||
const value = process.env[name];
|
||||
if (value !== undefined && value.trim() !== '') {
|
||||
return value.trim();
|
||||
}
|
||||
usedFallbacks.push(name);
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
function resolveReleaseChannel(usedFallbacks: Array<string>): ReleaseChannel {
|
||||
const raw = getEnvOrDefault('RELEASE_CHANNEL', FALLBACK_VALUES.RELEASE_CHANNEL, usedFallbacks).toLowerCase();
|
||||
switch (raw) {
|
||||
case 'canary':
|
||||
return 'canary';
|
||||
default:
|
||||
return 'stable';
|
||||
}
|
||||
}
|
||||
|
||||
let cachedMetadata: BuildMetadata | null = null;
|
||||
|
||||
export function getBuildMetadata(): BuildMetadata {
|
||||
if (cachedMetadata === null) {
|
||||
const usedFallbacks: Array<string> = [];
|
||||
cachedMetadata = {
|
||||
buildVersion: getEnvOrDefault('BUILD_VERSION', FALLBACK_VALUES.BUILD_VERSION, usedFallbacks),
|
||||
releaseChannel: resolveReleaseChannel(usedFallbacks),
|
||||
};
|
||||
logFallbackWarning(usedFallbacks);
|
||||
}
|
||||
return cachedMetadata;
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {getBuildMetadata} from '@fluxer/config/src/BuildMetadata';
|
||||
import type {MasterConfig} from '@fluxer/config/src/MasterConfig';
|
||||
|
||||
export function extractBaseServiceConfig(master: MasterConfig) {
|
||||
return {
|
||||
env: master.env,
|
||||
};
|
||||
}
|
||||
|
||||
export function extractKVClientConfig(master: MasterConfig) {
|
||||
if (!master.internal) {
|
||||
throw new Error('internal configuration is required for KV client access');
|
||||
}
|
||||
return {
|
||||
kvUrl: master.internal.kv,
|
||||
kvMode: master.internal.kv_mode,
|
||||
kvClusterNodes: master.internal.kv_cluster_nodes,
|
||||
kvClusterNatMap: master.internal.kv_cluster_nat_map,
|
||||
};
|
||||
}
|
||||
|
||||
export function extractBuildInfoConfig() {
|
||||
const metadata = getBuildMetadata();
|
||||
return {
|
||||
releaseChannel: metadata.releaseChannel,
|
||||
buildVersion: metadata.buildVersion,
|
||||
};
|
||||
}
|
||||
|
||||
export function extractRateLimit(
|
||||
rawRateLimit:
|
||||
| {
|
||||
limit?: number | null;
|
||||
window_ms?: number | null;
|
||||
}
|
||||
| null
|
||||
| undefined,
|
||||
):
|
||||
| {
|
||||
limit: number;
|
||||
windowMs: number;
|
||||
}
|
||||
| undefined {
|
||||
if (!rawRateLimit || rawRateLimit.limit == null || rawRateLimit.window_ms == null) {
|
||||
return undefined;
|
||||
}
|
||||
return {
|
||||
limit: rawRateLimit.limit,
|
||||
windowMs: rawRateLimit.window_ms,
|
||||
};
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import type {MasterConfig} from '@fluxer/config/src/MasterConfig';
|
||||
import {
|
||||
extractBaseServiceConfig,
|
||||
extractBuildInfoConfig,
|
||||
extractKVClientConfig,
|
||||
extractRateLimit,
|
||||
} from '@fluxer/config/src/ServiceConfigSlices';
|
||||
import {describe, expect, test, vi} from 'vitest';
|
||||
|
||||
function createMasterStub(overrides: Partial<MasterConfig> = {}): MasterConfig {
|
||||
return {
|
||||
env: 'development',
|
||||
internal: {
|
||||
kv: 'redis://127.0.0.1:6379/0',
|
||||
kv_provider: 'redis',
|
||||
kv_mode: 'standalone',
|
||||
kv_cluster_nodes: [],
|
||||
kv_cluster_nat_map: {},
|
||||
api: 'http://127.0.0.1:8080',
|
||||
media_proxy: 'http://localhost:8088/media',
|
||||
},
|
||||
services: {} as MasterConfig['services'],
|
||||
...overrides,
|
||||
} as MasterConfig;
|
||||
}
|
||||
|
||||
describe('extractBaseServiceConfig', () => {
|
||||
test('returns env from master config', () => {
|
||||
const master = createMasterStub({env: 'production'});
|
||||
const result = extractBaseServiceConfig(master);
|
||||
expect(result).toEqual({
|
||||
env: 'production',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractKVClientConfig', () => {
|
||||
test('returns kvUrl', () => {
|
||||
const master = createMasterStub();
|
||||
const result = extractKVClientConfig(master);
|
||||
expect(result).toEqual({
|
||||
kvUrl: 'redis://127.0.0.1:6379/0',
|
||||
kvMode: 'standalone',
|
||||
kvClusterNodes: [],
|
||||
kvClusterNatMap: {},
|
||||
});
|
||||
});
|
||||
test('throws when internal is missing', () => {
|
||||
const master = createMasterStub();
|
||||
(master as unknown as Record<string, unknown>).internal = undefined;
|
||||
expect(() => extractKVClientConfig(master)).toThrow('internal configuration is required');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractBuildInfoConfig', () => {
|
||||
test('returns fallback build metadata and reports the missing environment variables', () => {
|
||||
vi.stubEnv('BUILD_VERSION', undefined);
|
||||
vi.stubEnv('RELEASE_CHANNEL', undefined);
|
||||
const stdout = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
|
||||
try {
|
||||
const result = extractBuildInfoConfig();
|
||||
expect(result).toEqual({
|
||||
releaseChannel: 'stable',
|
||||
buildVersion: 'dev',
|
||||
});
|
||||
expect(stdout).toHaveBeenCalledExactlyOnceWith(
|
||||
'[build-metadata] Using fallback values for: BUILD_VERSION, RELEASE_CHANNEL. This indicates missing env vars in CI/production.\n',
|
||||
);
|
||||
} finally {
|
||||
stdout.mockRestore();
|
||||
vi.unstubAllEnvs();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractRateLimit', () => {
|
||||
test('returns undefined for null input', () => {
|
||||
expect(extractRateLimit(null)).toBeUndefined();
|
||||
});
|
||||
test('returns undefined for undefined input', () => {
|
||||
expect(extractRateLimit(undefined)).toBeUndefined();
|
||||
});
|
||||
test('returns undefined when limit is missing', () => {
|
||||
expect(extractRateLimit({window_ms: 60000})).toBeUndefined();
|
||||
});
|
||||
test('returns undefined when window_ms is missing', () => {
|
||||
expect(extractRateLimit({limit: 100})).toBeUndefined();
|
||||
});
|
||||
test('returns normalised object for valid input', () => {
|
||||
const result = extractRateLimit({limit: 100, window_ms: 60000});
|
||||
expect(result).toEqual({limit: 100, windowMs: 60000});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user