ce54c729e8
- 新增 ClientAuthFailure:payload 为 ts-rs 判别联合,context 固定 source=auth + 命令名,cause 为原始拒绝值 - clientAuth 的六个命令统一走 invokeClientAuth:结构化拒绝转成 ClientAuthFailure,非结构化拒绝原样抛出,不兜底文案 - 删除 clientAuthError.ts 的形状读取与 getClientAuthErrorMessage 文案回落层 - AuthenticatedClient 的两个 catch 内联 19 个变体的 switch:业务 / 会话变体原样展示载荷 message,系统变体原样抛出经 unhandledrejection 入池,default: expectNever 保证漏接变体编译失败 - hydrateAuth 失败先离开 loading 态;系统变体不再伪装成 unavailable 投影 - ClientAuthState 收窄为 authenticated / unauthenticated,ClientAuthRefreshResult 收窄为 refreshed / unauthenticated / stale - platformSession 续期只保留 refreshed / unauthenticated / stale 三条路径 - check-config 的 App 调用扫描登记 invokeClientAuth,继续静态证明命令名可达 - 测试:新增 clientAuthFailure 载体与编译期穷尽用例、jsdom unhandledrejection 桥;删除 clientAuthError.test.ts;clientAuthHost / authFailureReporting / authSurface 跟改新口径 - 共享记忆记录 check-config 扫描形态新增 invokeClientAuth
172 lines
4.9 KiB
TypeScript
172 lines
4.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
/**
|
|
* 登录态 façade 的边界。
|
|
*
|
|
* 凭据持久化、续期 HTTP、原生会话安装都在 Rust;这里只验证渲染层提交的结构化参数、三态
|
|
* 投影映射,以及没有原生宿主时的失败关闭。
|
|
*/
|
|
import { beforeEach, expect, test, vi } from 'vitest';
|
|
|
|
const invoke = vi.fn();
|
|
|
|
import {
|
|
loginClientWithPassword,
|
|
loginClientWithPhoneCode,
|
|
logoutClientAuthSession,
|
|
normalizeAuthPhoneInput,
|
|
readClientAuthState,
|
|
refreshClientAuthSession,
|
|
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', 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,
|
|
});
|
|
});
|
|
|
|
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',
|
|
});
|
|
});
|
|
|
|
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('结构化失败不降级成投影值,而是以 ClientAuthFailure 拒绝', async () => {
|
|
const rejection = {
|
|
type: 'authNetworkUnavailable',
|
|
message: '无法连接登录服务',
|
|
};
|
|
invoke.mockRejectedValueOnce(rejection);
|
|
await expect(refreshClientAuthSession('user-1')).rejects.toMatchObject({
|
|
payload: rejection,
|
|
context: { source: 'auth', action: 'refresh_client_auth_session' },
|
|
});
|
|
});
|