文件导入走两步:内容先经新端点 POST /api/provider-files 写进内核工作 目录的 providers/ 下(路径白名单校验,只认两个路径助手生成的形状), 成了再写 file 类型的声明——顺序反了内核重载会去读不存在的文件。 file 类型没有地址和自动更新(内核靠文件监视跟进),详情页照此展示, 编辑按钮禁用。ini 导入的取文件按钮换成同一个拖放模块。 订阅的更新间隔改成开关语义:内核只在 interval > 0 时才起定时拉取 (component/resource/fetcher.go:226-228),关掉开关就是不写这个键。 配置页加「更新所有订阅」,详情的「立刻更新」改叫「更新订阅」。 规则表从零开始写入时自动垫上内网直连——保留地址不直连的话第一条 代理规则一生效,访问局域网的流量就全进了代理;按钮改成「补全内网 直连」,只在有缺口时出现。 所有表单对话框点遮罩关闭;失败横幅右侧加关闭按钮。
184 lines
5.9 KiB
TypeScript
184 lines
5.9 KiB
TypeScript
import { describe, expect, it } from 'bun:test'
|
|
import {
|
|
DEFAULT_INTERVAL,
|
|
addFileSubscription,
|
|
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',
|
|
type: 'http',
|
|
path: './providers/subs.yaml',
|
|
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,
|
|
)
|
|
})
|
|
|
|
it('关掉自动更新时不写 interval 键', () => {
|
|
const next = addSubscription({}, 'subs2', URL, null)
|
|
const entry = (next['proxy-providers'] as ConfigObject).subs2 as ConfigObject
|
|
expect('interval' in entry).toBe(false)
|
|
})
|
|
|
|
it('从文件导入的订阅是 file 类型,没有地址和间隔', () => {
|
|
const next = addFileSubscription({}, 'subs2')
|
|
expect(next['proxy-providers']).toEqual({
|
|
subs2: { type: 'file', path: './providers/subs2.yaml' },
|
|
})
|
|
})
|
|
})
|
|
|
|
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)
|
|
})
|
|
|
|
it('关掉自动更新时把已有的 interval 键去掉', () => {
|
|
const next = updateSubscription(base(), 'subs', URL, null)
|
|
const entry = (next['proxy-providers'] as ConfigObject).subs as ConfigObject
|
|
expect('interval' in entry).toBe(false)
|
|
})
|
|
})
|
|
|
|
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([])
|
|
})
|
|
})
|