Files
Genarrative/apps/ai-game-creator-shell/tests/clientAuthHost.test.ts
T
k88936 8b50db9893 修复登录失败分流把空文案的业务变体误判为需上报
- resolveClientAuthFailure 改用 clientAuthErrorKind 判分流,不再用 clientAuthErrorNotice 是否为 null 当判据
- 空文案的 input/session 变体改为给兜底提示、不进错误池,保持"预期拒绝永不进池"的约束
- clientAuthErrorNotice 文档注明 null 同时表示 fault 与空文案,不能当分流判据
- clientAuthHost.test.ts 增加空文案业务变体的分流用例
2026-10-01 16:05:47 +08:00

246 lines
7.7 KiB
TypeScript

// @vitest-environment jsdom
/**
* 登录态 façade 的边界。
*
* 凭据持久化、续期 HTTP、原生会话安装都在 Rust;这里只验证渲染层提交的结构化参数、三态
* 投影映射,以及没有原生宿主时的失败关闭。
*/
import { beforeEach, expect, test, vi } from 'vitest';
const invoke = vi.fn();
import {
getClientAuthErrorMessage,
loginClientWithPassword,
loginClientWithPhoneCode,
logoutClientAuthSession,
normalizeAuthPhoneInput,
readClientAuthState,
refreshClientAuthSession,
resolveClientAuthFailure,
sendClientPhoneLoginCode,
} from '../src/services/clientAuth';
const testUser = {
id: 'user-1',
publicUserCode: 'tn-1',
displayName: '测试用户',
avatarUrl: null,
phoneNumber: null,
phoneNumberMasked: '138****0000',
loginMethod: 'password',
bindingStatus: 'active',
wechatBound: false,
wechatDisplayName: null,
wechatAccount: null,
};
beforeEach(() => {
invoke.mockReset();
window.__TAURI__ = {
core: { invoke: (...args: unknown[]) => invoke(...args) },
} as never;
});
test('手机号归一化只保留可提交的纯号码', () => {
expect(normalizeAuthPhoneInput('138 0000 0000')).toBe('13800000000');
expect(normalizeAuthPhoneInput('+8613800000000')).toBe('13800000000');
expect(normalizeAuthPhoneInput('0086-138-0000-0000')).toBe('008613800000000');
});
test('恢复登录态映射为 authenticated / unauthenticated / unavailable', async () => {
invoke.mockResolvedValueOnce({
status: 'authenticated',
user: testUser,
apiBaseUrl: 'https://dev.genarrative.world',
});
await expect(
readClientAuthState('https://dev.genarrative.world'),
).resolves.toEqual({
status: 'authenticated',
user: testUser,
apiBaseUrl: 'https://dev.genarrative.world',
});
expect(invoke).toHaveBeenCalledWith('read_client_auth_state', {
expectedApiBaseUrl: 'https://dev.genarrative.world',
});
invoke.mockResolvedValueOnce({ status: 'unauthenticated' });
await expect(readClientAuthState()).resolves.toEqual({
status: 'unauthenticated',
});
expect(invoke).toHaveBeenCalledWith('read_client_auth_state', {
expectedApiBaseUrl: null,
});
invoke.mockResolvedValueOnce({
status: 'unavailable',
errorMessage: 'network-error: 无法连接登录服务',
});
await expect(readClientAuthState()).resolves.toEqual({
status: 'unavailable',
message: 'network-error: 无法连接登录服务',
});
});
test('登录只提交结构化参数,凭据由 Rust 持有', async () => {
invoke.mockResolvedValue(testUser);
await expect(
loginClientWithPassword(
'138 0000 0000',
' secret ',
'https://dev.genarrative.world',
),
).resolves.toEqual(testUser);
await expect(
loginClientWithPhoneCode(
'13800000000',
' 123456 ',
'https://dev.genarrative.world',
),
).resolves.toEqual(testUser);
await expect(
sendClientPhoneLoginCode('13800000000', 'https://dev.genarrative.world'),
).resolves.toEqual({ cooldownSeconds: 0, expiresInSeconds: 0 });
await logoutClientAuthSession();
expect(invoke.mock.calls).toEqual([
[
'login_client_with_password',
{
apiBaseUrl: 'https://dev.genarrative.world',
phone: '13800000000',
password: 'secret',
},
],
[
'login_client_with_phone_code',
{
apiBaseUrl: 'https://dev.genarrative.world',
phone: '13800000000',
code: '123456',
},
],
[
'send_client_phone_login_code',
{
apiBaseUrl: 'https://dev.genarrative.world',
phone: '13800000000',
},
],
['logout_client_session'],
]);
const serialized = JSON.stringify(invoke.mock.calls);
expect(serialized).not.toContain('token');
expect(serialized).not.toContain('Authorization');
expect(serialized).not.toContain('cookie');
});
test('续期结果区分成功、失效、身份变化与暂时失败', async () => {
invoke.mockResolvedValueOnce({ status: 'refreshed', user: testUser });
await expect(refreshClientAuthSession('user-1')).resolves.toEqual({
status: 'refreshed',
user: testUser,
});
invoke.mockResolvedValueOnce({ status: 'unauthenticated' });
await expect(refreshClientAuthSession()).resolves.toEqual({
status: 'unauthenticated',
});
invoke.mockResolvedValueOnce({ status: 'stale' });
await expect(refreshClientAuthSession()).resolves.toEqual({
status: 'stale',
});
invoke.mockResolvedValueOnce({
status: 'failed',
authoritative: false,
errorMessage: 'network-error: 无法连接登录服务',
});
await expect(refreshClientAuthSession()).resolves.toEqual({
status: 'failed',
message: 'network-error: 无法连接登录服务',
authoritative: false,
});
});
test('没有原生宿主时登录能力明确失败关闭', async () => {
delete window.__TAURI__;
await expect(readClientAuthState()).rejects.toThrow(
'需要在 Tauri App 内登录',
);
await expect(
loginClientWithPassword(
'13800000000',
'secret',
'https://dev.genarrative.world',
),
).rejects.toThrow('需要在 Tauri App 内登录');
expect(invoke).not.toHaveBeenCalled();
});
test('错误文案优先使用服务端原因,缺失时回落到调用方文案', () => {
expect(
getClientAuthErrorMessage(new Error('手机号或密码错误'), '登录失败'),
).toBe('手机号或密码错误');
expect(getClientAuthErrorMessage('', '登录失败')).toBe('登录失败');
});
test('登录失败分流只看类型化变体:业务拒绝给提示,系统与未识别变体才进池', () => {
expect(
resolveClientAuthFailure(
{ type: 'phoneOrPasswordMismatch', message: '手机号或密码错误' },
'登录失败',
),
).toEqual({ kind: 'notice', message: '手机号或密码错误' });
expect(
resolveClientAuthFailure(
{
type: 'passwordEntryInputRejected',
message: '密码长度需要在 6 到 128 位之间',
},
'登录失败',
),
).toEqual({ kind: 'notice', message: '密码长度需要在 6 到 128 位之间' });
// 会话 401/403 按"未登录"处理:给提示,但不进错误池。
expect(
resolveClientAuthFailure(
{ type: 'sessionAuthorityRejected', message: '登录已失效,请重新登录' },
'登录失败',
),
).toEqual({ kind: 'notice', message: '登录已失效,请重新登录' });
// 文案为空的可识别变体仍按"用户可改"处理:给兜底提示,不进错误池
// (分流判据是 kind,不是"有没有提示文案")。
expect(
resolveClientAuthFailure(
{ type: 'phoneNumberInvalid', message: ' ' },
'登录失败',
),
).toEqual({ kind: 'notice', message: '登录失败' });
// 系统变体与未识别变体:显示 Rust 文案,同时交给错误池。
expect(
resolveClientAuthFailure(
{ type: 'authNetworkUnavailable', message: '无法连接登录服务' },
'登录失败',
),
).toEqual({ kind: 'report', message: '无法连接登录服务' });
expect(
resolveClientAuthFailure(
{ type: 'brandNewRejection', message: '新变体' },
'登录失败',
),
).toEqual({ kind: 'report', message: '新变体' });
// 非结构化:裸字符串保留原文,其它形状回落调用方文案,不显示 `[object Object]`。
expect(
resolveClientAuthFailure('network-error: 连接超时', '登录失败'),
).toEqual({ kind: 'report', message: 'network-error: 连接超时' });
expect(
resolveClientAuthFailure(new Error('runner clear rejected'), '登录失败'),
).toEqual({ kind: 'report', message: 'runner clear rejected' });
expect(resolveClientAuthFailure({ status: 500 }, '登录失败')).toEqual({
kind: 'report',
message: '登录失败',
});
});