Files
SnowLuma/packages/webui/tests/password-strength-api.test.ts
T
TanTan fa9c73363d
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
Publish MCP to npm / Build, test, and publish MCP (push) Waiting to run
Publish SDK to npm / Build, test, and publish SDK (push) Waiting to run
Sync action catalog to docs / sync (push) Waiting to run
Publish UI to npm / Build and publish UI (push) Waiting to run
Build and Release / preflight (push) Waiting to run
SnowLuma v2.4.2 source snapshot
2026-11-13 09:02:00 +08:00

101 lines
3.2 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { createApiClient } from '../src/lib/api/client';
const tokenStore = {
load: () => 'test-token',
save: () => undefined,
};
afterEach(() => {
vi.unstubAllGlobals();
});
describe('password strength API client', () => {
it('returns the server-owned rule evaluation', async () => {
const rules = [
{ id: 'length', label: '长度不少于 10 位', ok: true },
{ id: 'no-space', label: '不包含空格', ok: true },
];
const fetchMock = vi.fn(
async () =>
new Response(
JSON.stringify({
rules,
valid: true,
}),
{
status: 200,
headers: { 'content-type': 'application/json' },
},
),
);
vi.stubGlobal('fetch', fetchMock);
const client = createApiClient({ tokenStore });
await expect(client.checkPasswordStrength('Strong-password')).resolves.toEqual({
rules,
valid: true,
});
expect(fetchMock).toHaveBeenCalledWith(
'/api/auth/check-strength',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ password: 'Strong-password' }),
headers: new Headers({
'X-SnowLuma-Token': 'test-token',
'Content-Type': 'application/json',
}),
signal: expect.any(AbortSignal),
}),
);
});
it('rejects malformed responses instead of silently disabling the form', async () => {
vi.stubGlobal(
'fetch',
vi.fn(
async () =>
new Response(
JSON.stringify({
valid: false,
}),
{
status: 200,
headers: { 'content-type': 'application/json' },
},
),
),
);
const client = createApiClient({ tokenStore });
await expect(client.checkPasswordStrength('candidate')).rejects.toMatchObject({
status: 502,
message: '密码强度接口返回了无效响应',
});
});
it('propagates request failures for observable inline validation', async () => {
vi.stubGlobal(
'fetch',
vi.fn(
async () =>
new Response(
JSON.stringify({
message: 'strength service unavailable',
}),
{
status: 503,
headers: { 'content-type': 'application/json' },
},
),
),
);
const client = createApiClient({ tokenStore });
await expect(client.checkPasswordStrength('candidate')).rejects.toMatchObject({
status: 503,
message: 'strength service unavailable',
});
});
});