65d7a57eb7
Project CI / Frontend tests (push) Successful in 4m0s
Project CI / Repository checks (push) Successful in 4m4s
Project CI / Backend tests (push) Successful in 8m40s
Project CI / Native shell tests (push) Successful in 17m56s
Project CI / Frontend tests (pull_request) Successful in 5m16s
Project CI / Repository checks (pull_request) Successful in 6m57s
Project CI / Backend tests (pull_request) Successful in 6m31s
Project CI / Native shell tests (pull_request) Successful in 16m57s
Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/241 Co-authored-by: Linghong <ink29535@proton.me> Co-committed-by: Linghong <ink29535@proton.me>
254 lines
7.8 KiB
TypeScript
254 lines
7.8 KiB
TypeScript
import { fetch as tauriHttpFetch } from '@tauri-apps/plugin-http';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import {
|
|
API_RESPONSE_ENVELOPE_HEADER,
|
|
API_RESPONSE_ENVELOPE_VERSION,
|
|
} from '../../../packages/shared/src/http';
|
|
import {
|
|
AGC_CLIENT_MARKER_HEADER,
|
|
AGC_CLIENT_MARKER_VALUE,
|
|
AGC_DEVELOPMENT_API_BASE_URL,
|
|
AGC_RELEASE_API_BASE_URL,
|
|
fetchClientHttp,
|
|
getClientServerBaseUrl,
|
|
getClientServerSelection,
|
|
normalizeClientServerBaseUrl,
|
|
resetClientServerSelectionForTests,
|
|
resolveClientHttpTarget,
|
|
setClientServerSelection,
|
|
} from '../src/services/clientHttp';
|
|
|
|
vi.mock('@tauri-apps/plugin-http', () => ({
|
|
fetch: vi.fn(),
|
|
}));
|
|
|
|
describe('AGC client HTTP transport', () => {
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.unstubAllEnvs();
|
|
vi.unstubAllGlobals();
|
|
resetClientServerSelectionForTests();
|
|
});
|
|
|
|
it('adds the AGC marker while preserving and overriding request headers', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValue(new Response(null, { status: 204 }));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
const inputHeaders = new Headers({
|
|
Authorization: 'Bearer fixture-token',
|
|
'X-Request-ID': 'request-123',
|
|
[API_RESPONSE_ENVELOPE_HEADER]: API_RESPONSE_ENVELOPE_VERSION,
|
|
[AGC_CLIENT_MARKER_HEADER]: 'caller-value',
|
|
});
|
|
const init: RequestInit = {
|
|
method: 'POST',
|
|
headers: inputHeaders,
|
|
body: '{}',
|
|
credentials: 'same-origin',
|
|
};
|
|
|
|
await fetchClientHttp('/api/auth/me', init);
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
const [target, forwardedInit] = fetchMock.mock.calls[0] as [
|
|
string,
|
|
RequestInit,
|
|
];
|
|
const forwardedHeaders = new Headers(forwardedInit.headers);
|
|
expect(target).toBe('/api/auth/me');
|
|
expect(forwardedHeaders.get(AGC_CLIENT_MARKER_HEADER)).toBe(
|
|
AGC_CLIENT_MARKER_VALUE,
|
|
);
|
|
expect(forwardedHeaders.get('Authorization')).toBe('Bearer fixture-token');
|
|
expect(forwardedHeaders.get('X-Request-ID')).toBe('request-123');
|
|
expect(forwardedHeaders.get(API_RESPONSE_ENVELOPE_HEADER)).toBe(
|
|
API_RESPONSE_ENVELOPE_VERSION,
|
|
);
|
|
expect(forwardedInit.method).toBe('POST');
|
|
expect(forwardedInit.body).toBe('{}');
|
|
expect(forwardedInit.credentials).toBe('same-origin');
|
|
expect(inputHeaders.get(AGC_CLIENT_MARKER_HEADER)).toBe('caller-value');
|
|
});
|
|
|
|
it.each([
|
|
'/api/auth/me',
|
|
'/api/profile/dashboard',
|
|
'/api/editor/projects',
|
|
'/api/assets/read-bytes?objectKey=fixture',
|
|
])('marks %s through the shared Web transport', async (url) => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValue(new Response(null, { status: 204 }));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
await fetchClientHttp(url, {});
|
|
|
|
const [, forwardedInit] = fetchMock.mock.calls[0] as [string, RequestInit];
|
|
expect(
|
|
new Headers(forwardedInit.headers).get(AGC_CLIENT_MARKER_HEADER),
|
|
).toBe(AGC_CLIENT_MARKER_VALUE);
|
|
});
|
|
|
|
it('adds the AGC marker to the Tauri HTTP transport', async () => {
|
|
const tauriFetchMock = vi.mocked(tauriHttpFetch);
|
|
tauriFetchMock.mockResolvedValue(new Response(null, { status: 204 }));
|
|
vi.stubEnv('MODE', 'production');
|
|
vi.stubEnv('DEV', false);
|
|
vi.stubGlobal('window', {
|
|
__TAURI__: {},
|
|
location: { protocol: 'tauri:' },
|
|
});
|
|
|
|
await fetchClientHttp(
|
|
'/api/auth/me',
|
|
{ headers: { Authorization: 'Bearer fixture-token' } },
|
|
{ serverBaseUrl: AGC_DEVELOPMENT_API_BASE_URL },
|
|
);
|
|
|
|
expect(tauriFetchMock).toHaveBeenCalledTimes(1);
|
|
const [target, forwardedInit] = tauriFetchMock.mock.calls[0] as [
|
|
string,
|
|
RequestInit,
|
|
];
|
|
const forwardedHeaders = new Headers(forwardedInit.headers);
|
|
expect(target).toBe(`${AGC_DEVELOPMENT_API_BASE_URL}/api/auth/me`);
|
|
expect(forwardedHeaders.get(AGC_CLIENT_MARKER_HEADER)).toBe(
|
|
AGC_CLIENT_MARKER_VALUE,
|
|
);
|
|
expect(forwardedHeaders.get('Authorization')).toBe('Bearer fixture-token');
|
|
});
|
|
|
|
it('keeps local development requests on the Vite API proxy', () => {
|
|
expect(
|
|
resolveClientHttpTarget('/api/auth/me', {
|
|
isDevelopment: true,
|
|
isTauri: true,
|
|
pageProtocol: 'http:',
|
|
}),
|
|
).toEqual({ transport: 'web', url: '/api/auth/me' });
|
|
});
|
|
|
|
it('routes release Tauri requests through the scoped dev API transport', () => {
|
|
expect(
|
|
resolveClientHttpTarget('/api/auth/me', {
|
|
isDevelopment: false,
|
|
isTauri: true,
|
|
pageProtocol: 'tauri:',
|
|
mode: 'production',
|
|
serverBaseUrl: AGC_RELEASE_API_BASE_URL,
|
|
}),
|
|
).toEqual({
|
|
transport: 'tauri-http',
|
|
url: `${AGC_RELEASE_API_BASE_URL}/api/auth/me`,
|
|
});
|
|
});
|
|
|
|
it('keeps ordinary web releases on same-origin relative requests', () => {
|
|
expect(
|
|
resolveClientHttpTarget('/api/auth/me', {
|
|
isDevelopment: false,
|
|
isTauri: false,
|
|
pageProtocol: 'https:',
|
|
mode: 'test',
|
|
}),
|
|
).toEqual({ transport: 'web', url: '/api/auth/me' });
|
|
});
|
|
|
|
it('rejects release Tauri requests outside the fixed dev API origin', () => {
|
|
expect(() =>
|
|
resolveClientHttpTarget('https://example.com/api/auth/me', {
|
|
isDevelopment: false,
|
|
isTauri: true,
|
|
pageProtocol: 'tauri:',
|
|
mode: 'production',
|
|
serverBaseUrl: AGC_RELEASE_API_BASE_URL,
|
|
}),
|
|
).toThrow('当前选择的服务器范围');
|
|
});
|
|
|
|
it('persists release, dev, and custom server selection', () => {
|
|
const release = setClientServerSelection({
|
|
preset: 'release',
|
|
customBaseUrl: '',
|
|
});
|
|
expect(release).toEqual({
|
|
preset: 'release',
|
|
customBaseUrl: '',
|
|
});
|
|
expect(getClientServerBaseUrl(release)).toBe(AGC_RELEASE_API_BASE_URL);
|
|
const dev = setClientServerSelection({ preset: 'dev', customBaseUrl: '' });
|
|
expect(getClientServerBaseUrl(dev)).toBe(AGC_DEVELOPMENT_API_BASE_URL);
|
|
|
|
const custom = setClientServerSelection({
|
|
preset: 'custom',
|
|
customBaseUrl: 'https://staging.example.com/',
|
|
});
|
|
expect(custom).toEqual({
|
|
preset: 'custom',
|
|
customBaseUrl: 'https://staging.example.com',
|
|
});
|
|
expect(getClientServerSelection().preset).toBe('dev');
|
|
expect(getClientServerBaseUrl(custom)).toBe('https://staging.example.com');
|
|
});
|
|
|
|
it('accepts HTTPS custom servers and loopback HTTP only', () => {
|
|
expect(normalizeClientServerBaseUrl('https://example.com/')).toBe(
|
|
'https://example.com',
|
|
);
|
|
expect(normalizeClientServerBaseUrl('http://127.0.0.1:8080/')).toBe(
|
|
'http://127.0.0.1:8080',
|
|
);
|
|
expect(() => normalizeClientServerBaseUrl('http://example.com')).toThrow(
|
|
'必须使用 HTTPS',
|
|
);
|
|
expect(() =>
|
|
normalizeClientServerBaseUrl('https://example.com/api'),
|
|
).toThrow('纯 HTTP(S)');
|
|
});
|
|
|
|
it('routes selected custom servers for both web and Tauri clients', () => {
|
|
const serverBaseUrl = 'https://staging.example.com';
|
|
expect(
|
|
resolveClientHttpTarget('/api/auth/me', {
|
|
isDevelopment: true,
|
|
isTauri: false,
|
|
pageProtocol: 'http:',
|
|
mode: 'development',
|
|
serverBaseUrl,
|
|
}),
|
|
).toEqual({
|
|
transport: 'web',
|
|
url: `${serverBaseUrl}/api/auth/me`,
|
|
});
|
|
expect(
|
|
resolveClientHttpTarget('/api/auth/me', {
|
|
isDevelopment: false,
|
|
isTauri: true,
|
|
pageProtocol: 'tauri:',
|
|
mode: 'production',
|
|
serverBaseUrl,
|
|
}),
|
|
).toEqual({
|
|
transport: 'tauri-http',
|
|
url: `${serverBaseUrl}/api/auth/me`,
|
|
});
|
|
});
|
|
|
|
it('keeps Tauri HTTP transport when the WebView reports an http page protocol', () => {
|
|
expect(
|
|
resolveClientHttpTarget('/api/auth/me', {
|
|
isDevelopment: false,
|
|
isTauri: true,
|
|
pageProtocol: 'http:',
|
|
mode: 'production',
|
|
serverBaseUrl: AGC_DEVELOPMENT_API_BASE_URL,
|
|
}),
|
|
).toEqual({
|
|
transport: 'tauri-http',
|
|
url: `${AGC_DEVELOPMENT_API_BASE_URL}/api/auth/me`,
|
|
});
|
|
});
|
|
});
|