179 lines
4.5 KiB
TypeScript
179 lines
4.5 KiB
TypeScript
import { beforeAll, describe, expect, it, vi } from 'vitest'
|
|
|
|
const probeAIAccountSession = vi.fn()
|
|
const probePublishAccountSession = vi.fn()
|
|
const silentRefreshAIAccountSession = vi.fn()
|
|
const silentRefreshPublishAccountSession = vi.fn()
|
|
|
|
vi.mock('./account-binder', () => ({
|
|
probeAIAccountSession,
|
|
probePublishAccountSession,
|
|
silentRefreshAIAccountSession,
|
|
silentRefreshPublishAccountSession,
|
|
}))
|
|
|
|
let getPlatformAdapter: typeof import('./platform-auth-adapters').getPlatformAdapter
|
|
|
|
beforeAll(async () => {
|
|
;({ getPlatformAdapter } = await import('./platform-auth-adapters'))
|
|
})
|
|
|
|
describe('platform auth adapters', () => {
|
|
it('routes AI account probes away from publish account probes', async () => {
|
|
probeAIAccountSession.mockResolvedValueOnce({
|
|
verdict: 'active',
|
|
reason: 'probe_success',
|
|
profile: null,
|
|
sessionFingerprint: 'ai-session',
|
|
})
|
|
const adapter = getPlatformAdapter('doubao')
|
|
|
|
await adapter.probe(
|
|
{
|
|
id: 'account-1',
|
|
platform: 'doubao',
|
|
platformUid: 'doubao-session:demo',
|
|
displayName: 'Geek',
|
|
},
|
|
'persist:test',
|
|
)
|
|
|
|
expect(probeAIAccountSession).toHaveBeenCalledTimes(1)
|
|
expect(probePublishAccountSession).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('routes AI silent refresh away from publish account refresh', async () => {
|
|
silentRefreshAIAccountSession.mockResolvedValueOnce(true)
|
|
const adapter = getPlatformAdapter('qwen')
|
|
|
|
await adapter.silentRefresh(
|
|
{
|
|
id: 'account-2',
|
|
platform: 'qwen',
|
|
platformUid: 'qwen-session:demo',
|
|
displayName: '通义千问账号',
|
|
},
|
|
'persist:test',
|
|
)
|
|
|
|
expect(silentRefreshAIAccountSession).toHaveBeenCalledTimes(1)
|
|
expect(silentRefreshPublishAccountSession).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('classifies Wenxin environment abnormal messages as risk control', () => {
|
|
const adapter = getPlatformAdapter('wenxin')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
message: '当前访问环境存在异常,请更换浏览器再尝试提问',
|
|
}),
|
|
).toBe('risk_control')
|
|
})
|
|
|
|
it('classifies Qiehao missing cookie as auth failure', () => {
|
|
const adapter = getPlatformAdapter('qiehao')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'qiehao_cookie_missing',
|
|
},
|
|
}),
|
|
).toBe('auth_failure')
|
|
})
|
|
|
|
it('classifies Bilibili missing csrf as auth failure', () => {
|
|
const adapter = getPlatformAdapter('bilibili')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'bilibili_csrf_missing',
|
|
},
|
|
}),
|
|
).toBe('auth_failure')
|
|
})
|
|
|
|
it('classifies Juejin missing login as auth failure', () => {
|
|
const adapter = getPlatformAdapter('juejin')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'juejin_not_logged_in',
|
|
},
|
|
}),
|
|
).toBe('auth_failure')
|
|
})
|
|
|
|
it('classifies Sohuhao missing login as auth failure', () => {
|
|
const adapter = getPlatformAdapter('sohuhao')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'sohuhao_not_logged_in',
|
|
},
|
|
}),
|
|
).toBe('auth_failure')
|
|
})
|
|
|
|
it('does not classify Wangyihao publish token misses as auth failure', () => {
|
|
const adapter = getPlatformAdapter('wangyihao')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'wangyihao_token_missing',
|
|
},
|
|
}),
|
|
).toBe('ok')
|
|
})
|
|
|
|
it('classifies Weixin GZH verification as challenge', () => {
|
|
const adapter = getPlatformAdapter('weixin_gzh')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'weixin_gzh_challenge_required',
|
|
},
|
|
}),
|
|
).toBe('challenge')
|
|
})
|
|
|
|
it('classifies Weixin GZH login timeout as auth failure', () => {
|
|
const adapter = getPlatformAdapter('weixin_gzh')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
message: '登录超时,请重新登录',
|
|
}),
|
|
).toBe('auth_failure')
|
|
})
|
|
|
|
it('classifies ZOL missing login as auth failure', () => {
|
|
const adapter = getPlatformAdapter('zol')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'zol_not_logged_in',
|
|
},
|
|
}),
|
|
).toBe('auth_failure')
|
|
})
|
|
|
|
it('classifies Dongchedi platform verification as challenge', () => {
|
|
const adapter = getPlatformAdapter('dongchedi')
|
|
|
|
expect(
|
|
adapter.classifyFailure({
|
|
error: {
|
|
code: 'dongchedi_challenge_required',
|
|
},
|
|
}),
|
|
).toBe('challenge')
|
|
})
|
|
})
|