写配置的纯逻辑在 shared/providers.ts。删除前 subscriptionReferences 同时查 直接 use 和经订阅副本(共用缓存文件 path)的间接引用,有被引用的就拦下并列出 策略组。订阅不允许改名:名字被 use: 和副本引用,同步所有引用不如删了重建。 页面还没挂进 App.vue 的导航,由集成者统一加。
162 lines
5.1 KiB
TypeScript
162 lines
5.1 KiB
TypeScript
import { describe, expect, it } from 'bun:test'
|
|
import {
|
|
DEFAULT_INTERVAL,
|
|
addSubscription,
|
|
listSubscriptions,
|
|
providerNames,
|
|
removeSubscription,
|
|
subscriptionReferences,
|
|
updateSubscription,
|
|
} from './providers.ts'
|
|
import { applySetup, chainProviderName } from './inbound-groups.ts'
|
|
import type { OutboundSetup } from './inbound-groups.ts'
|
|
import type { ConfigObject } from '../vendor/config-editor/index.ts'
|
|
|
|
const URL = 'http://127.0.0.1:3001/token/download/V2House'
|
|
|
|
/** 一份带一个订阅的配置,形状仿照目标机器上那份。 */
|
|
function base(): ConfigObject {
|
|
return {
|
|
'proxy-providers': {
|
|
subs: {
|
|
type: 'http',
|
|
url: URL,
|
|
path: './providers/subs.yaml',
|
|
interval: 86400,
|
|
'health-check': { enable: true, url: 'https://www.gstatic.com/generate_204', interval: 300 },
|
|
},
|
|
},
|
|
'proxy-groups': [{ name: 'PROXY', type: 'select', proxies: ['DIRECT'], use: ['subs'] }],
|
|
}
|
|
}
|
|
|
|
const chainedSetup: OutboundSetup = {
|
|
proxy: { sources: ['subs'], filter: null },
|
|
chain: { sources: ['subs'], filter: null },
|
|
}
|
|
|
|
describe('读出订阅列表', () => {
|
|
it('读出地址、间隔和健康检查', () => {
|
|
expect(listSubscriptions(base())).toEqual([
|
|
{
|
|
name: 'subs',
|
|
url: URL,
|
|
interval: 86400,
|
|
healthCheck: { url: 'https://www.gstatic.com/generate_204', interval: 300 },
|
|
},
|
|
])
|
|
})
|
|
|
|
it('健康检查没写或没开都算未开启', () => {
|
|
const data = base()
|
|
const subs = (data['proxy-providers'] as ConfigObject).subs as ConfigObject
|
|
delete subs['health-check']
|
|
expect(listSubscriptions(data)[0]?.healthCheck).toBeNull()
|
|
subs['health-check'] = { enable: false, url: 'https://example.com', interval: 60 }
|
|
expect(listSubscriptions(data)[0]?.healthCheck).toBeNull()
|
|
})
|
|
|
|
it('不含 Patchbay 生成的订阅副本', () => {
|
|
const data = applySetup(
|
|
{ ...base(), listeners: [{ name: 'test1', type: 'mixed', port: 7778 }] },
|
|
'test1',
|
|
chainedSetup,
|
|
)
|
|
expect(listSubscriptions(data).map((s) => s.name)).toEqual(['subs'])
|
|
// 副本还在,只是不列出来
|
|
expect(providerNames(data)).toContain(chainProviderName('test1', 'subs'))
|
|
})
|
|
|
|
it('没有 proxy-providers 段时是空列表', () => {
|
|
expect(listSubscriptions({ mode: 'rule' })).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('新增订阅', () => {
|
|
it('自动生成缓存路径和默认间隔', () => {
|
|
const next = addSubscription({}, 'subs2', URL)
|
|
expect(next['proxy-providers']).toEqual({
|
|
subs2: {
|
|
type: 'http',
|
|
url: URL,
|
|
path: './providers/subs2.yaml',
|
|
interval: DEFAULT_INTERVAL,
|
|
},
|
|
})
|
|
})
|
|
|
|
it('不动已有的订阅', () => {
|
|
const data = base()
|
|
const next = addSubscription(data, 'subs2', URL)
|
|
expect((next['proxy-providers'] as ConfigObject).subs).toEqual(
|
|
(data['proxy-providers'] as ConfigObject).subs,
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('编辑订阅', () => {
|
|
it('只改地址和间隔,其余字段原样保留', () => {
|
|
const next = updateSubscription(base(), 'subs', 'http://example.com/new', 3600)
|
|
const entry = (next['proxy-providers'] as ConfigObject).subs as ConfigObject
|
|
expect(entry.url).toBe('http://example.com/new')
|
|
expect(entry.interval).toBe(3600)
|
|
expect(entry.path).toBe('./providers/subs.yaml')
|
|
expect(entry['health-check']).toEqual({
|
|
enable: true,
|
|
url: 'https://www.gstatic.com/generate_204',
|
|
interval: 300,
|
|
})
|
|
})
|
|
|
|
it('订阅不存在时不做改动', () => {
|
|
const data = base()
|
|
expect(updateSubscription(data, 'ghost', URL, 1)).toBe(data)
|
|
})
|
|
})
|
|
|
|
describe('删除订阅', () => {
|
|
it('从映射里去掉', () => {
|
|
const data = addSubscription(base(), 'subs2', URL)
|
|
const next = removeSubscription(data, 'subs2')
|
|
expect(providerNames(next)).toEqual(['subs'])
|
|
})
|
|
|
|
it('删到最后一个时整个键去掉,不留空映射', () => {
|
|
const next = removeSubscription(base(), 'subs')
|
|
expect('proxy-providers' in next).toBe(false)
|
|
})
|
|
|
|
it('订阅不存在时不做改动', () => {
|
|
const data = base()
|
|
expect(removeSubscription(data, 'ghost')).toBe(data)
|
|
})
|
|
})
|
|
|
|
describe('删除前的引用检查', () => {
|
|
it('找出 use 它的策略组', () => {
|
|
expect(subscriptionReferences(base(), 'subs')).toEqual(['PROXY'])
|
|
})
|
|
|
|
it('没人在用时是空列表', () => {
|
|
const data = base()
|
|
;(data['proxy-groups'] as ConfigObject[])[0] = { name: 'PROXY', type: 'select', proxies: ['DIRECT'] }
|
|
expect(subscriptionReferences(data, 'subs')).toEqual([])
|
|
})
|
|
|
|
it('链式代理的专属组 use 的是副本,也算引用原订阅', () => {
|
|
const data = applySetup(
|
|
{ ...base(), listeners: [{ name: 'test1', type: 'mixed', port: 7778 }] },
|
|
'test1',
|
|
chainedSetup,
|
|
)
|
|
const refs = subscriptionReferences(data, 'subs')
|
|
// 入口组直接 use 原订阅,三个专属组 use 副本,两边都要认出来
|
|
expect(refs).toContain('patchbay:test1 链式')
|
|
expect(refs).toContain('patchbay:test1')
|
|
})
|
|
|
|
it('订阅不存在时是空列表', () => {
|
|
expect(subscriptionReferences(base(), 'ghost')).toEqual([])
|
|
})
|
|
})
|