48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { API_CONFIG } from './config';
|
|
import { getApiClient } from './client';
|
|
|
|
const { getAccessTokenMock } = vi.hoisted(() => ({
|
|
getAccessTokenMock: vi.fn(),
|
|
}));
|
|
|
|
const fetchMock = vi.fn();
|
|
|
|
vi.mock('./auth', () => ({
|
|
getAuthClient: () => ({
|
|
getAccessToken: getAccessTokenMock,
|
|
}),
|
|
}));
|
|
|
|
function jsonResponse(data: unknown, status = 200) {
|
|
return {
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
statusText: status === 200 ? 'OK' : 'Error',
|
|
json: () => Promise.resolve(data),
|
|
};
|
|
}
|
|
|
|
describe('mobile API client', () => {
|
|
beforeEach(() => {
|
|
fetchMock.mockReset();
|
|
getAccessTokenMock.mockReset();
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
});
|
|
|
|
it('propagates product identity, auth token, and request id through the shared API client', async () => {
|
|
getAccessTokenMock.mockReturnValue('mobile-token');
|
|
fetchMock.mockResolvedValue(jsonResponse({ ok: true }));
|
|
|
|
await getApiClient().fetch('/workspaces');
|
|
|
|
const [, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
|
const headers = init.headers as Record<string, string>;
|
|
|
|
expect(headers['x-product-id']).toBe(API_CONFIG.productId);
|
|
expect(headers.Authorization).toBe('Bearer mobile-token');
|
|
expect(headers['x-request-id']).toBeTruthy();
|
|
});
|
|
});
|