import test from 'node:test' import assert from 'node:assert/strict' import { DEFAULT_PROXY_PROVIDER, UNKNOWN_SOURCE_LABEL, buildProxyEntryPayload, emptyProxyEntryDraft, llmSourceLabel, proxyDisplayText, proxySourceLabel, validateProxyEntry, } from '../src/pages/account/user-secret-model.ts' import { QINGGUO_EXTRACT_URL, composeQingguoLink } from '../src/shared/utils/qingguo-link.ts' // 密钥管理页(后管)两条收口的判据(2026-11-02): // ① 货源标签按模块分字典——代理列只认供应商,LLM 渠道名不许出现在代理行上; // ② 代理列只显示后端脱敏值(masked),界面既不回落原文、也没有可预填的整条链接。 // 夹具里的 key/pwd 全是假值(fakekey/fakepwd),本仓有尺子按长度+高熵认凭据。 test('代理列只认供应商:LLM 渠道值在代理列认不出', () => { assert.equal(proxySourceLabel('qingguo'), '青果') assert.equal(proxySourceLabel('JIKIP'), '极客', '后端值大小写不一时应归一') assert.equal(proxySourceLabel(' qingguo '), '青果') // 这一条就是截图上「代理行贴了龙抬头」的形状:合表按值匹配会认下来,分表必须认不出 assert.equal(proxySourceLabel('longtaitou'), UNKNOWN_SOURCE_LABEL) assert.equal(proxySourceLabel('t8star'), UNKNOWN_SOURCE_LABEL) assert.equal(proxySourceLabel(''), UNKNOWN_SOURCE_LABEL) assert.equal(proxySourceLabel(null), UNKNOWN_SOURCE_LABEL) assert.equal(proxySourceLabel(undefined), UNKNOWN_SOURCE_LABEL) }) test('LLM 列只认渠道:代理供应商名不许贴到密钥列上', () => { assert.equal(llmSourceLabel('longtaitou'), '龙抬头') assert.equal(llmSourceLabel('t8star'), '贞贞') assert.equal(llmSourceLabel('qingguo'), UNKNOWN_SOURCE_LABEL) assert.equal(llmSourceLabel('jikip'), UNKNOWN_SOURCE_LABEL) assert.equal(llmSourceLabel('nonsense'), UNKNOWN_SOURCE_LABEL) // 两张表除占位符外不得有任何共同输出:合表就是从这个性质破掉的 const proxyLabels = ['qingguo', 'jikip'].map(proxySourceLabel) assert.deepEqual(proxyLabels.filter((label) => label !== UNKNOWN_SOURCE_LABEL), ['青果', '极客']) }) test('代理列文本只取 masked,绝不回落到原文字段', () => { const link = composeQingguoLink('fakekey', 'fakepwd') const masked = 'https://overseas.proxy.qg.net' // 后端 SecretMasking.maskProxy 的输出形状 assert.equal(proxyDisplayText({ masked, full: '' }), masked) // 万一某个旧回包还带着 full:界面也不许把它渲染出来(回落原文=脱敏白做) assert.equal(proxyDisplayText({ masked, full: link }), masked) const shown = proxyDisplayText({ masked, full: link }) assert.ok(!shown.includes('fakekey'), `显示文本里漏出了 key:${shown}`) assert.ok(!shown.includes('fakepwd'), `显示文本里漏出了 pwd:${shown}`) assert.ok(!shown.includes('get?'), `显示文本里漏出了提取链接的查询段:${shown}`) }) test('masked 为空时显示占位符,不猜原文', () => { assert.equal(proxyDisplayText({ masked: '', full: composeQingguoLink('fakekey', 'fakepwd') }), UNKNOWN_SOURCE_LABEL) assert.equal(proxyDisplayText({ masked: null }), UNKNOWN_SOURCE_LABEL) assert.equal(proxyDisplayText(undefined), UNKNOWN_SOURCE_LABEL) }) test('青果链接拼装口径:overseas 域+key+pwd,特殊字符按 URL 组件编码', () => { assert.equal(composeQingguoLink('fakekey', 'fakepwd'), `${QINGGUO_EXTRACT_URL}?key=fakekey&pwd=fakepwd`) assert.equal(composeQingguoLink(' fakekey ', ' fakepwd '), `${QINGGUO_EXTRACT_URL}?key=fakekey&pwd=fakepwd`) // pwd 里的 @ 与 : 不编码会把 host 切歪(AGENTS 铁律 25 实录) const encoded = composeQingguoLink('fakekey', 'p@ss:1') assert.ok(encoded.includes('pwd=p%40ss%3A1'), encoded) }) test('改代理每次从空表开始:没有原文可预填', () => { const draft = emptyProxyEntryDraft('qingguo') assert.equal(draft.provider, 'qingguo') assert.deepEqual([draft.authkey, draft.authpwd, draft.link], ['', '', '']) // 供应商值认不出时不猜成凭据,只回落到后端默认供应商(极客) assert.equal(emptyProxyEntryDraft('longtaitou').provider, DEFAULT_PROXY_PROVIDER) assert.equal(emptyProxyEntryDraft(null).provider, DEFAULT_PROXY_PROVIDER) }) test('青果要两把都填:缺哪把就点名哪把', () => { assert.equal(validateProxyEntry({ provider: 'qingguo', authkey: '', authpwd: 'fakepwd', link: '' }), '请填写青果 Authkey') assert.match(validateProxyEntry({ provider: 'qingguo', authkey: 'fakekey', authpwd: ' ', link: '' }), /Authpwd/) assert.equal(validateProxyEntry({ provider: 'qingguo', authkey: 'fakekey', authpwd: 'fakepwd', link: '' }), '') assert.equal(buildProxyEntryPayload({ provider: 'qingguo', authkey: 'fakekey', authpwd: 'fakepwd', link: '' })?.value, composeQingguoLink('fakekey', 'fakepwd')) assert.equal(buildProxyEntryPayload({ provider: 'qingguo', authkey: 'fakekey', authpwd: '', link: '' }), null, '不合法时不得产出载荷(拿空串顶上去等于把配置清空)') }) test('极客填自家提取链接:形状与后端 validateProxyValue 同口径、不扩大判死', () => { const draft = { provider: 'jikip' as const, authkey: '', authpwd: '', link: 'https://api.example.test/ip-get?appkey=fakekey' } assert.equal(validateProxyEntry(draft), '') assert.deepEqual(buildProxyEntryPayload(draft), { value: 'https://api.example.test/ip-get?appkey=fakekey', channel: 'jikip' }) assert.match(validateProxyEntry({ ...draft, link: '' }), /极客提取链接/) assert.match(validateProxyEntry({ ...draft, link: 'ftp://api.example.test/ip-get' }), /格式不正确/) assert.match(validateProxyEntry({ ...draft, link: 'not a url' }), /格式不正确/) // 静态代理地址(http://host:port)同样是后端认的形态,前端不许判死 assert.equal(validateProxyEntry({ ...draft, link: 'http://1.2.3.4:8080' }), '') }) test('载荷带上这一单的供应商:channel 缺失会被后端按域名识别,识别不出就静默换供应商', () => { assert.equal(buildProxyEntryPayload({ provider: 'qingguo', authkey: 'fakekey', authpwd: 'fakepwd', link: '' })?.channel, 'qingguo') assert.equal(buildProxyEntryPayload({ provider: 'jikip', authkey: '', authpwd: '', link: 'https://api.example.test/ip-get' })?.channel, 'jikip') })