* feat(balancers): tabbed Observatory/Burst form replacing raw JSON Replace the raw JSON editor for the Observatory / Burst Observatory sections with a proper Ant Design form, and split the Balancers page into two sub-tabs: "Balancer Settings" (the existing table) and "Observatory". Observers stay fully auto-managed by balancer strategy through the existing syncObservatories logic: users edit only the tunable probe fields, the subjectSelector is shown read-only since it is derived from the balancers, and deleting the last balancer that needs an observer now warns in the confirm dialog that the observer will be removed too. Overlapping selectors keep an observer alive while any balancer still references it. Also add the previously missing pingConfig.httpMethod field (HEAD/GET) and translations for the new strings across all 13 locales. * refactor(balancers): tighten httpMethod typing and align connectivity default Address automated review feedback on the Observatory form: - Use the ObservatoryHttpMethodSchema enum for pingConfig.httpMethod instead of a free-form z.string(), and drive the HTTP method Select from its options. Removes the previously dead enum export and the duplicate local list, and types the field as 'HEAD' | 'GET'. - Align the schema's connectivity default with DEFAULT_BURST_OBSERVATORY (the hicloud URL) so it matches what burst observers are actually created with. No behavior change.
180 lines
7.9 KiB
TypeScript
180 lines
7.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { observersRemovedByDeletingBalancer, syncObservatories } from '@/pages/xray/balancers/balancer-helpers';
|
|
import type { XraySettingsValue } from '@/hooks/useXraySetting';
|
|
|
|
function tpl(routing: Record<string, unknown>, extra: Record<string, unknown> = {}): XraySettingsValue {
|
|
return { routing, ...extra } as unknown as XraySettingsValue;
|
|
}
|
|
|
|
// Observatory sections have no reload API in xray-core, so creating one turns
|
|
// a balancer save from a live (hot-applied) routing change into a full
|
|
// restart. These tests pin the rule: only strategies that genuinely need an
|
|
// observer may create one — which, for random/roundRobin, means a fallbackTag
|
|
// is set (xray-core then requires the Observatory feature; see #5605).
|
|
describe('syncObservatories', () => {
|
|
it('does not create burstObservatory for a fresh random balancer (stays hot-appliable)', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['direct'] }] });
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeUndefined();
|
|
expect(t.observatory).toBeUndefined();
|
|
});
|
|
|
|
it('does not create burstObservatory for roundRobin without fallback', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'roundRobin' } }] });
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeUndefined();
|
|
});
|
|
|
|
it('creates burstObservatory for a random balancer with a fallbackTag (#5605)', () => {
|
|
const t = tpl({ balancers: [{ tag: 'OverProxy', selector: ['opera-proxy'], fallbackTag: 'warp' }] });
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeDefined();
|
|
expect((t.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['opera-proxy']);
|
|
});
|
|
|
|
it('creates burstObservatory for roundRobin with a fallbackTag', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], fallbackTag: 'warp', strategy: { type: 'roundRobin' } }] });
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeDefined();
|
|
expect((t.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['a']);
|
|
});
|
|
|
|
it('treats an empty-string fallbackTag as no fallback (stays hot-appliable)', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], fallbackTag: '' }] });
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeUndefined();
|
|
});
|
|
|
|
it('removes burstObservatory when a random balancer drops its fallbackTag', () => {
|
|
const t = tpl(
|
|
{ balancers: [{ tag: 'OverProxy', selector: ['opera-proxy'], fallbackTag: '' }] },
|
|
{ burstObservatory: { subjectSelector: ['opera-proxy'] } },
|
|
);
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeUndefined();
|
|
});
|
|
|
|
it('removes burstObservatory when a roundRobin balancer drops its fallbackTag', () => {
|
|
const t = tpl(
|
|
{ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'roundRobin' } }] },
|
|
{ burstObservatory: { subjectSelector: ['a'] } },
|
|
);
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeUndefined();
|
|
});
|
|
|
|
it('keeps burstObservatory while another fallback balancer still needs it', () => {
|
|
const t = tpl(
|
|
{
|
|
balancers: [
|
|
{ tag: 'b1', selector: ['a'] },
|
|
{ tag: 'b2', selector: ['b'], fallbackTag: 'warp', strategy: { type: 'roundRobin' } },
|
|
],
|
|
},
|
|
{ burstObservatory: { subjectSelector: ['a', 'b'] } },
|
|
);
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeDefined();
|
|
expect((t.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['b', 'a']);
|
|
});
|
|
|
|
it('creates burstObservatory for leastLoad (required by the strategy)', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastLoad' } }] });
|
|
syncObservatories(t);
|
|
expect(t.burstObservatory).toBeDefined();
|
|
expect((t.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['a']);
|
|
});
|
|
|
|
it('creates observatory for leastPing (required by the strategy)', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastPing' } }] });
|
|
syncObservatories(t);
|
|
expect(t.observatory).toBeDefined();
|
|
expect((t.observatory as { subjectSelector: string[] }).subjectSelector).toEqual(['a']);
|
|
});
|
|
|
|
it('keeps an existing burstObservatory in sync for random balancers (legacy setups)', () => {
|
|
const t = tpl(
|
|
{ balancers: [{ tag: 'b1', selector: ['a'] }, { tag: 'b2', selector: ['b'], strategy: { type: 'leastLoad' } }] },
|
|
{ burstObservatory: { subjectSelector: ['stale'] } },
|
|
);
|
|
syncObservatories(t);
|
|
expect((t.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['b', 'a']);
|
|
});
|
|
|
|
it('removes observatories when no balancer can use them', () => {
|
|
const t = tpl({ balancers: [] }, {
|
|
observatory: { subjectSelector: ['a'] },
|
|
burstObservatory: { subjectSelector: ['a'] },
|
|
});
|
|
syncObservatories(t);
|
|
expect(t.observatory).toBeUndefined();
|
|
expect(t.burstObservatory).toBeUndefined();
|
|
});
|
|
|
|
it('creates burstObservatory with the HEAD httpMethod default for leastLoad', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastLoad' } }] });
|
|
syncObservatories(t);
|
|
const burst = t.burstObservatory as { pingConfig: { httpMethod: string; sampling: number } };
|
|
expect(burst.pingConfig.httpMethod).toBe('HEAD');
|
|
expect(burst.pingConfig.sampling).toBe(2);
|
|
});
|
|
|
|
it('drops only the prefixes no remaining balancer uses (note #2)', () => {
|
|
const t = tpl({
|
|
balancers: [
|
|
{ tag: 'a', selector: ['prefixA', 'prefixB'], strategy: { type: 'leastLoad' } },
|
|
{ tag: 'b', selector: ['prefixC', 'prefixB'], strategy: { type: 'leastLoad' } },
|
|
],
|
|
});
|
|
syncObservatories(t);
|
|
expect(new Set((t.burstObservatory as { subjectSelector: string[] }).subjectSelector)).toEqual(
|
|
new Set(['prefixA', 'prefixB', 'prefixC']),
|
|
);
|
|
(t.routing as { balancers: unknown[] }).balancers.splice(0, 1);
|
|
syncObservatories(t);
|
|
expect(new Set((t.burstObservatory as { subjectSelector: string[] }).subjectSelector)).toEqual(
|
|
new Set(['prefixC', 'prefixB']),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('observersRemovedByDeletingBalancer', () => {
|
|
it('reports the burst observer as removed when deleting the last leastLoad balancer', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastLoad' } }] });
|
|
syncObservatories(t);
|
|
expect(observersRemovedByDeletingBalancer(t, 0)).toEqual({ observatory: false, burst: true });
|
|
});
|
|
|
|
it('keeps the burst observer when another balancer still needs it (overlap)', () => {
|
|
const t = tpl({
|
|
balancers: [
|
|
{ tag: 'a', selector: ['prefixA', 'prefixB'], strategy: { type: 'leastLoad' } },
|
|
{ tag: 'b', selector: ['prefixC', 'prefixB'], strategy: { type: 'leastLoad' } },
|
|
],
|
|
});
|
|
syncObservatories(t);
|
|
expect(observersRemovedByDeletingBalancer(t, 0)).toEqual({ observatory: false, burst: false });
|
|
});
|
|
|
|
it('reports the regular observer as removed when deleting the last leastPing balancer', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastPing' } }] });
|
|
syncObservatories(t);
|
|
expect(observersRemovedByDeletingBalancer(t, 0)).toEqual({ observatory: true, burst: false });
|
|
});
|
|
|
|
it('reports nothing removed when the balancer never had an observer', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'] }] });
|
|
syncObservatories(t);
|
|
expect(observersRemovedByDeletingBalancer(t, 0)).toEqual({ observatory: false, burst: false });
|
|
});
|
|
|
|
it('does not mutate the template it inspects', () => {
|
|
const t = tpl({ balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastLoad' } }] });
|
|
syncObservatories(t);
|
|
const before = JSON.stringify(t);
|
|
observersRemovedByDeletingBalancer(t, 0);
|
|
expect(JSON.stringify(t)).toBe(before);
|
|
});
|
|
});
|