75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
AgentProfileApiError,
|
|
getAgentProfile,
|
|
putAgentProfile,
|
|
} from '@/lib/agent-profile';
|
|
|
|
const hostApiFetchMock = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock('@/lib/host-api', () => ({
|
|
hostApiFetch: (...args: unknown[]) => hostApiFetchMock(...args),
|
|
}));
|
|
|
|
const profile = {
|
|
display_name: '小泥',
|
|
age: 18,
|
|
gender: 'female' as const,
|
|
share_age_with_agents: true,
|
|
share_gender_with_agents: true,
|
|
analysis_enabled: true,
|
|
completed: true,
|
|
version: 2,
|
|
updated_at: '2026-07-12T00:00:00Z',
|
|
};
|
|
|
|
describe('Agent Profile API client', () => {
|
|
beforeEach(() => {
|
|
hostApiFetchMock.mockReset();
|
|
});
|
|
|
|
it('loads the current profile through the authenticated Host API proxy', async () => {
|
|
hostApiFetchMock.mockResolvedValue({ success: true, profile });
|
|
|
|
await expect(getAgentProfile('access-token')).resolves.toEqual(profile);
|
|
expect(hostApiFetchMock).toHaveBeenCalledWith('/api/works/user/agent-profile', {
|
|
headers: { 'X-NianCode-Access-Token': 'access-token' },
|
|
});
|
|
});
|
|
|
|
it('sends a complete versioned PUT payload', async () => {
|
|
hostApiFetchMock.mockResolvedValue({ success: true, profile });
|
|
const update = {
|
|
display_name: '小泥',
|
|
age: 18,
|
|
gender: 'female' as const,
|
|
share_age_with_agents: true,
|
|
share_gender_with_agents: true,
|
|
analysis_enabled: true,
|
|
version: 1,
|
|
};
|
|
|
|
await expect(putAgentProfile('access-token', update)).resolves.toEqual(profile);
|
|
expect(hostApiFetchMock).toHaveBeenCalledWith('/api/works/user/agent-profile', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(update),
|
|
headers: { 'X-NianCode-Access-Token': 'access-token' },
|
|
});
|
|
});
|
|
|
|
it('retains upstream conflict detail for the store to handle', async () => {
|
|
hostApiFetchMock.mockResolvedValue({
|
|
success: false,
|
|
status: 409,
|
|
error: 'Profile version conflict',
|
|
detail: { code: 'agent_profile_version_conflict', current_version: 4 },
|
|
});
|
|
|
|
await expect(getAgentProfile('access-token')).rejects.toMatchObject<Partial<AgentProfileApiError>>({
|
|
name: 'AgentProfileApiError',
|
|
status: 409,
|
|
detail: { code: 'agent_profile_version_conflict', current_version: 4 },
|
|
});
|
|
});
|
|
});
|