fix(cr2-frontend): 代理归属回退、换号继承确认与请求竞态收口
- CR-04:全局代理加 proxy_uid 归属,跨账号不再静默回退/补报 - CR-12:取消静默继承,设置页显式确认;同 OS 作用域、locked 代理排除、无效快照清除、uid+epoch 双校验、登出 capture 超时 - CR-26/27:关键词分页初始值与档位一致;两页面请求序号闸丢弃旧响应 vue-tsc 两前端 rc=0;定向 35 + 8 + 2 例全绿。
This commit is contained in:
@@ -155,6 +155,22 @@ test('登出先留继承快照再清缓存,且清缓存必须早于移除 uid'
|
||||
)
|
||||
})
|
||||
|
||||
test('登出清理期间禁用提交,且清理结束才恢复(CR-12)', () => {
|
||||
assert.match(loginPageSource, /logoutCleanupPending = ref\(false\)/, '缺清理中状态')
|
||||
assert.match(
|
||||
loginPageSource,
|
||||
/:disabled="loggingIn \|\| logoutCleanupPending"/,
|
||||
'清理期间登录按钮必须禁用',
|
||||
)
|
||||
assert.match(
|
||||
loginPageSource,
|
||||
/if \(loggingIn\.value \|\| logoutCleanupPending\.value\) return/,
|
||||
'回车/表单提交也要挡住,不能只靠按钮 disabled',
|
||||
)
|
||||
assert.match(loginPageSource, /logoutCleanupPending\.value = true/, '登出时应进入清理中状态')
|
||||
assert.match(loginPageSource, /logoutCleanupPending\.value = false/, '清理结束(含超时)必须恢复')
|
||||
})
|
||||
|
||||
test('停用/删除文案必须互不冒充:停用句不说删除、删除句不说停用', () => {
|
||||
const disabledLine = loginPageSource
|
||||
.split('\n')
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { PromiseTimeoutError, withTimeout } from '../src/shared/utils/promise-timeout.ts'
|
||||
|
||||
/**
|
||||
* 登出 capture 的超时底座(CR-12):原生桥卡死时必须让调用方继续清 token/uid。
|
||||
* 这里只验超时原语本身;capture 走超时后仍写快照/继续清理的分支在 secret-store.test.ts。
|
||||
*/
|
||||
|
||||
test('withTimeout 在期限内原样返回结果', async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
const value = await withTimeout(Promise.resolve('ok'), 50)
|
||||
assert.equal(value, 'ok')
|
||||
})
|
||||
})
|
||||
|
||||
test('withTimeout 对永不返回的 promise 按时抛 PromiseTimeoutError', async () => {
|
||||
const start = Date.now()
|
||||
await assert.rejects(
|
||||
() => withTimeout(new Promise(() => {}), 20),
|
||||
(error: unknown) => error instanceof PromiseTimeoutError && /超过 20ms 未完成/.test(error.message),
|
||||
)
|
||||
assert.ok(Date.now() - start < 1000, '超时必须及时触发,不能等底层 promise')
|
||||
})
|
||||
|
||||
test('withTimeout 透传底层拒绝(吞掉会伪造成功)', async () => {
|
||||
await assert.rejects(
|
||||
() => withTimeout(Promise.reject(new Error('bridge down')), 50),
|
||||
/bridge down/,
|
||||
)
|
||||
})
|
||||
@@ -4,7 +4,10 @@ import { http } from '../src/shared/api/http.ts'
|
||||
import {
|
||||
captureApiSecretsForInheritance,
|
||||
clearApiSecretCache,
|
||||
confirmApiSecretInheritance,
|
||||
declineApiSecretInheritance,
|
||||
ensureBrandCheckProxy,
|
||||
getPendingApiSecretInheritance,
|
||||
getServerProxySnapshot,
|
||||
getStoredApiSecret,
|
||||
getStoredApiSecretSnapshot,
|
||||
@@ -199,9 +202,18 @@ test('test_secret_store_backfills_legacy_global_proxy_to_server', async (t) => {
|
||||
clearApiSecretCache()
|
||||
const globalUrl = 'https://api.jikip.com/ip-get?num=2&key=legacy'
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
const saveCalls: Array<Record<string, unknown>> = []
|
||||
let config: Record<string, unknown> = { proxy_url: globalUrl, proxy_mode: 2 }
|
||||
let bundleCalls = 0
|
||||
// 老版本只把代理存在全局 proxy_url,proxy_users 里没有该用户
|
||||
attachDesktopBridge(() => ({ proxy_url: globalUrl, proxy_mode: 2 }), (patch) => patch)
|
||||
attachDesktopBridge(
|
||||
() => config,
|
||||
(patch) => {
|
||||
saveCalls.push(patch)
|
||||
config = mergeDesktopConfig(config, patch)
|
||||
return config
|
||||
},
|
||||
)
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
@@ -226,6 +238,15 @@ test('test_secret_store_backfills_legacy_global_proxy_to_server', async (t) => {
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, globalUrl, '旧版全局代理应补报到服务端')
|
||||
assert.equal(getServerProxySnapshot()?.exists, true, '补报后应重拉服务端代理快照')
|
||||
// CR-04:升级时一次性归属迁移——全局 proxy 归给第一个读到它的 uid,并清空全局
|
||||
assert.equal(config.proxy_uid, '42', '全局代理应被当前 uid 认领')
|
||||
assert.equal(config.proxy_url, '', '认领后应清空无归属的全局 proxy_url')
|
||||
assert.equal(
|
||||
(config.proxy_users as Record<string, { proxy_url: string }>)['42'].proxy_url,
|
||||
globalUrl,
|
||||
'认领结果应写入 proxy_users[uid]',
|
||||
)
|
||||
assert.ok(saveCalls.some((patch) => patch.proxy_uid === '42'), '归属迁移必须落盘,不能只在内存里认领')
|
||||
})
|
||||
|
||||
test('test_secret_store_user_proxy_wins_over_legacy_global', async (t) => {
|
||||
@@ -315,6 +336,45 @@ function mergeDesktopConfig(current: Record<string, unknown>, patch: Record<stri
|
||||
return next
|
||||
}
|
||||
|
||||
/** 可变的桌面 app.json:读当前值、写回按原生 save_config 口径合并,并记录每次 patch。 */
|
||||
function attachConfigStore(initial: Record<string, unknown>) {
|
||||
let config = initial
|
||||
const saveCalls: Array<Record<string, unknown>> = []
|
||||
attachDesktopBridge(
|
||||
() => config,
|
||||
(patch) => {
|
||||
saveCalls.push(patch)
|
||||
config = mergeDesktopConfig(config, patch)
|
||||
return config
|
||||
},
|
||||
)
|
||||
return {
|
||||
get config() {
|
||||
return config
|
||||
},
|
||||
saveCalls,
|
||||
}
|
||||
}
|
||||
|
||||
/** 合法的本机继承快照(schema 2 + 同作用域),测试按需覆盖字段。 */
|
||||
function validInheritanceConfig(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
current_uid: '43',
|
||||
api_secret_os_scope: 'scope-1',
|
||||
api_secret_inherit: {
|
||||
schema: 2,
|
||||
fromUid: '42',
|
||||
fromUsername: 'alice',
|
||||
savedAt: Date.now(),
|
||||
osUserScope: 'scope-1',
|
||||
secrets: { 'appearance-patent': 'sk-alice-appearance' },
|
||||
proxyUrl: '',
|
||||
proxyMode: 1,
|
||||
...overrides,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test('test_secret_store_inherits_previous_account_config_on_desktop', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
clearApiSecretCache()
|
||||
@@ -337,6 +397,8 @@ test('test_secret_store_inherits_previous_account_config_on_desktop', async (t)
|
||||
await captureApiSecretsForInheritance()
|
||||
const stash = config.api_secret_inherit as Record<string, unknown> | null
|
||||
assert.equal(stash?.fromUid, '42', '登出时应把当前账号配置留成本机继承快照')
|
||||
assert.equal(stash?.schema, 2, '快照必须带当前 schema 与 OS 作用域')
|
||||
assert.equal(stash?.osUserScope, config.api_secret_os_scope, '快照作用域必须与本机 app.json 一致')
|
||||
|
||||
// 真实登出顺序:先清缓存(此时 uid 仍是 42),再移除 uid;登录 B 后 uid=43
|
||||
clearApiSecretCache()
|
||||
@@ -370,33 +432,44 @@ test('test_secret_store_inherits_previous_account_config_on_desktop', async (t)
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
// CR-12:加载只登记待确认,不再自动继承
|
||||
assert.deepEqual(migratePayloads, [], '用户未确认前不得调用 migrate')
|
||||
const pending = getPendingApiSecretInheritance()
|
||||
assert.equal(pending?.fromUid, '42')
|
||||
assert.equal(pending?.fromUsername, 'alice')
|
||||
assert.equal(pending?.proxy, true)
|
||||
assert.deepEqual(pending?.moduleKeys.slice().sort(), ['appearance-patent', 'similar-asin'])
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), '', '确认前不得把上一个账号的明文写进本地镜像')
|
||||
|
||||
const notice = await confirmApiSecretInheritance()
|
||||
const migrated = migratePayloads.flatMap((payload) => payload.items || [])
|
||||
assert.equal(
|
||||
migrated.find((item) => item.moduleKey === 'appearance-patent')?.value,
|
||||
'sk-alice-appearance',
|
||||
'上一个账号的外观专利密钥应继承给当前账号',
|
||||
'确认后上一个账号的外观专利密钥应继承给当前账号',
|
||||
)
|
||||
assert.equal(
|
||||
migrated.find((item) => item.moduleKey === 'similar-asin')?.value,
|
||||
'sk-alice-similar',
|
||||
'上一个账号的货源查询密钥应继承给当前账号',
|
||||
'确认后上一个账号的货源查询密钥应继承给当前账号',
|
||||
)
|
||||
assert.equal(
|
||||
migrated.find((item) => item.moduleKey === 'proxy')?.value,
|
||||
aliceProxy,
|
||||
'服务端缺代理时应继承上一个账号的代理',
|
||||
'服务端缺代理且未锁定时应继承上一个账号的代理',
|
||||
)
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), 'sk-alice-appearance', '继承后当前账号本地应能直接取到明文')
|
||||
const users = config.proxy_users as Record<string, { proxy_url: string; proxy_mode: number }>
|
||||
assert.equal(users['43'].proxy_url, aliceProxy, '继承的代理应写进当前 uid 的本地配置')
|
||||
assert.equal(users['43'].proxy_mode, 2, '代理模式应一并继承')
|
||||
assert.equal(config.api_secret_inherit, null, '继承成功后快照应消费掉,避免被更后面的账号重复使用')
|
||||
assert.equal(config.api_secret_inherit, null, '确认继承后快照应消费掉,避免被更后面的账号重复使用')
|
||||
assert.equal(getPendingApiSecretInheritance(), null, '确认后不应再有待确认摘要')
|
||||
|
||||
const notice = takeApiSecretInheritanceNotice()
|
||||
assert.equal(notice?.fromUid, '42')
|
||||
assert.equal(notice?.fromUsername, 'alice')
|
||||
assert.equal(notice?.proxy, true)
|
||||
assert.deepEqual(notice?.moduleKeys.slice().sort(), ['appearance-patent', 'similar-asin'])
|
||||
assert.equal(takeApiSecretInheritanceNotice()?.fromUid, '42', '设置页应能消费到本次继承提示')
|
||||
})
|
||||
|
||||
test('test_secret_store_inheritance_never_overwrites_existing_account_config', async (t) => {
|
||||
@@ -405,11 +478,13 @@ test('test_secret_store_inheritance_never_overwrites_existing_account_config', a
|
||||
localStorage.setItem('uid', '43')
|
||||
const config: Record<string, unknown> = {
|
||||
current_uid: '43',
|
||||
api_secret_os_scope: 'scope-1',
|
||||
api_secret_inherit: {
|
||||
schema: 1,
|
||||
schema: 2,
|
||||
fromUid: '42',
|
||||
fromUsername: 'alice',
|
||||
savedAt: Date.now(),
|
||||
osUserScope: 'scope-1',
|
||||
secrets: { 'appearance-patent': 'sk-alice-appearance', 'similar-asin': 'sk-alice-similar' },
|
||||
proxyUrl: 'https://api.jikip.com/ip-get?num=1&key=alice',
|
||||
proxyMode: 2,
|
||||
@@ -441,6 +516,10 @@ test('test_secret_store_inheritance_never_overwrites_existing_account_config', a
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.deepEqual(migratePayloads, [], '用户未确认前不得自动继承')
|
||||
assert.equal(getPendingApiSecretInheritance()?.fromUid, '42', '应登记待确认快照')
|
||||
await confirmApiSecretInheritance()
|
||||
|
||||
const migrated = migratePayloads.flatMap((payload) => payload.items || [])
|
||||
assert.deepEqual(
|
||||
migrated.map((item) => item.moduleKey),
|
||||
@@ -448,7 +527,7 @@ test('test_secret_store_inheritance_never_overwrites_existing_account_config', a
|
||||
'只补当前账号空缺的模块,已有密钥和已有代理都不能被上一个账号覆盖',
|
||||
)
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), '', '当前账号已有密钥时不得把上一个账号的明文塞进本地')
|
||||
assert.equal(config.api_secret_inherit, null, '没有需要补的项时也应消费快照')
|
||||
assert.equal(config.api_secret_inherit, null, '继承完成后应消费快照')
|
||||
assert.equal(
|
||||
(config.proxy_users as Record<string, unknown> | undefined)?.['43'],
|
||||
undefined,
|
||||
@@ -456,6 +535,264 @@ test('test_secret_store_inheritance_never_overwrites_existing_account_config', a
|
||||
)
|
||||
})
|
||||
|
||||
test('test_secret_store_declines_inheritance_and_clears_stash', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig())
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
assert.equal(getPendingApiSecretInheritance()?.fromUid, '42', '应先登记待确认快照')
|
||||
await declineApiSecretInheritance()
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '用户选择不继承后快照必须立即清掉')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
assert.deepEqual(migratePayloads, [], '拒绝继承不得调用 migrate')
|
||||
})
|
||||
|
||||
test('test_secret_store_expired_inheritance_snapshot_is_cleared', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig({
|
||||
savedAt: Date.now() - 31 * 24 * 60 * 60 * 1000,
|
||||
}))
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '过期快照必须立即从 app.json 清掉(明文不许长期留存)')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
assert.deepEqual(migratePayloads, [])
|
||||
})
|
||||
|
||||
test('test_secret_store_malformed_inheritance_snapshot_is_cleared', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig({ schema: 1 }))
|
||||
mockRequest(t, () => bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')]))
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '结构不符(旧 schema/缺作用域)必须立即清掉')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
})
|
||||
|
||||
test('test_secret_store_inheritance_scope_mismatch_is_cleared', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
// 别的 OS 用户 app.json 里拷来的快照:作用域标记对不上
|
||||
const store = attachConfigStore(validInheritanceConfig({ osUserScope: 'scope-from-another-os-user' }))
|
||||
mockRequest(t, () => bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')]))
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '跨 OS 用户作用域的快照必须清掉')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
})
|
||||
|
||||
test('test_secret_store_locked_server_proxy_is_not_inherited', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const stashProxy = 'https://api.jikip.com/ip-get?num=1&key=alice'
|
||||
const store = attachConfigStore(validInheritanceConfig({ proxyUrl: stashProxy, proxyMode: 2 }))
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
const payload = req.data as { items?: Array<{ moduleKey?: string; value?: string }> }
|
||||
migratePayloads.push(payload)
|
||||
return okResponse({ migrated: payload.items?.length || 0 })
|
||||
}
|
||||
return okResponse({
|
||||
items: [serverItem('appearance-patent'), serverItem('similar-asin')],
|
||||
requiredModules: ['appearance-patent', 'similar-asin'],
|
||||
complete: false,
|
||||
// 后台锁定但 exists 仍为 false:只靠 exists 判断会漏掉 locked 这一条
|
||||
proxy: { ...serverItem('proxy'), exists: false, locked: true },
|
||||
})
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
const notice = await confirmApiSecretInheritance()
|
||||
|
||||
const migrated = migratePayloads.flatMap((payload) => payload.items || [])
|
||||
assert.equal(
|
||||
migrated.some((item) => item.moduleKey === 'proxy'),
|
||||
false,
|
||||
'后台锁定的代理不得被上一个账号的快照覆盖',
|
||||
)
|
||||
assert.equal(notice?.proxy, false, '继承摘要要如实说明代理没有继承')
|
||||
assert.equal(
|
||||
(store.config.proxy_users as Record<string, unknown> | undefined)?.['43'],
|
||||
undefined,
|
||||
'锁定代理场景不得把快照代理写进本地配置',
|
||||
)
|
||||
})
|
||||
|
||||
test('test_secret_store_inheritance_write_aborts_when_uid_changes', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig())
|
||||
let releaseMigrate: ((value: unknown) => void) | null = null
|
||||
let markStarted: (() => void) | null = null
|
||||
const migrateStarted = new Promise<void>((resolve) => {
|
||||
markStarted = resolve
|
||||
})
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
markStarted?.()
|
||||
return new Promise((resolve) => {
|
||||
releaseMigrate = resolve
|
||||
})
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
assert.equal(getPendingApiSecretInheritance()?.fromUid, '42')
|
||||
const confirmPromise = confirmApiSecretInheritance()
|
||||
await migrateStarted
|
||||
// migrate 在飞时切号:晚到的成功响应不得把 A 的明文写进 B 的本地镜像
|
||||
localStorage.setItem('uid', '44')
|
||||
releaseMigrate?.({ data: { success: true, message: 'ok', data: { migrated: 1 } } })
|
||||
|
||||
const notice = await confirmPromise
|
||||
assert.equal(notice, null, '会话已变时确认流程必须返回空')
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), '', '切号后不得写入上一个账号的明文')
|
||||
assert.equal(
|
||||
(store.config.proxy_users as Record<string, unknown> | undefined)?.['43'],
|
||||
undefined,
|
||||
'切号后不得写回上一个账号的代理',
|
||||
)
|
||||
assert.ok(store.config.api_secret_inherit, '会话已变时快照应保留给正确账号重试,而不是被误清')
|
||||
})
|
||||
|
||||
test('test_secret_store_does_not_backfill_foreign_global_proxy', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const aliceProxy = 'https://api.jikip.com/ip-get?num=1&key=alice'
|
||||
const store = attachConfigStore({
|
||||
proxy_users: { '43': {} },
|
||||
proxy_url: aliceProxy,
|
||||
proxy_uid: '42',
|
||||
})
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(
|
||||
migratePayloads.flatMap((payload) => payload.items || []).some((item) => item.moduleKey === 'proxy'),
|
||||
false,
|
||||
'全局代理归属 uid=42 时,uid=43 不得静默补报',
|
||||
)
|
||||
assert.equal(store.config.proxy_uid, '42', '不得把别人的全局代理改归属到当前账号')
|
||||
assert.deepEqual(store.config.proxy_users, { '43': {} }, '也不得写进当前 uid 的 proxy_users')
|
||||
assert.deepEqual(store.saveCalls, [], '归属不符时不应发生任何配置写回')
|
||||
})
|
||||
|
||||
test('test_secret_store_backfills_global_proxy_owned_by_current_uid', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const ownProxy = 'https://api.jikip.com/ip-get?num=1&key=owned'
|
||||
const store = attachConfigStore({ proxy_url: ownProxy, proxy_uid: '43' })
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
const proxyMigrate = migratePayloads
|
||||
.flatMap((payload) => payload.items || [])
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, ownProxy, 'proxy_uid 等于当前 uid 时应允许回退全局代理')
|
||||
assert.deepEqual(store.saveCalls, [], '已有归属时无需再做一次性迁移')
|
||||
})
|
||||
|
||||
test('test_secret_store_backfills_shared_global_proxy_whitelist', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const sharedProxy = 'https://api.jikip.com/ip-get?num=1&key=shared'
|
||||
attachConfigStore({
|
||||
proxy_url: sharedProxy,
|
||||
proxy_uid: '42',
|
||||
proxy_shared_uids: ['43'],
|
||||
})
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
const proxyMigrate = migratePayloads
|
||||
.flatMap((payload) => payload.items || [])
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, sharedProxy, '显式共享白名单内的账号可以回退全局代理')
|
||||
})
|
||||
|
||||
test('test_secret_store_capture_times_out_instead_of_blocking_logout', async () => {
|
||||
const { localStorage } = setupWindow()
|
||||
clearApiSecretCache()
|
||||
localStorage.setItem('username', 'alice')
|
||||
seedDesktopMirror(localStorage, '42', 'appearance-patent', 'sk-alice-appearance')
|
||||
const saveCalls: Array<Record<string, unknown>> = []
|
||||
// 原生桥卡死:read_config 永不返回。capture 自身必须超时,否则登录页会一直停在清理中
|
||||
attachDesktopBridge(
|
||||
(() => new Promise(() => {})) as unknown as () => Record<string, unknown>,
|
||||
(patch) => {
|
||||
saveCalls.push(patch)
|
||||
return patch
|
||||
},
|
||||
)
|
||||
const start = Date.now()
|
||||
|
||||
await captureApiSecretsForInheritance()
|
||||
|
||||
assert.ok(Date.now() - start < 5000, 'capture 超时必须及时返回,不能拖住登出清理')
|
||||
const saved = saveCalls[0]?.api_secret_inherit as Record<string, unknown> | undefined
|
||||
assert.equal(saved?.fromUid, '42', '读配置超时也不影响把已取到的密钥留成本机快照')
|
||||
assert.equal(saved?.proxyUrl, '', '读不到配置时不写代理,避免把不确定的出口留给下一个账号')
|
||||
})
|
||||
|
||||
|
||||
test('test_secret_store_locked_proxy_not_in_module_cards', async (t) => {
|
||||
setupWindow()
|
||||
clearApiSecretCache()
|
||||
|
||||
Reference in New Issue
Block a user