chore: sync local latest state and repository cleanup
This commit is contained in:
@@ -12,6 +12,7 @@ import { fileURLToPath } from 'url';
|
||||
export interface TestData {
|
||||
activityId: number;
|
||||
apiKey: string;
|
||||
userToken: string;
|
||||
userId: number;
|
||||
shortCode: string;
|
||||
baseUrl: string;
|
||||
@@ -25,6 +26,18 @@ export interface ApiResponse<T = any> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
const DEFAULT_TEST_API_KEY = 'test-api-key-000000000000';
|
||||
const DEFAULT_TEST_USER_TOKEN = 'test-e2e-token';
|
||||
|
||||
export function hasRealApiCredentials(data: TestData): boolean {
|
||||
return Boolean(
|
||||
data.apiKey &&
|
||||
data.userToken &&
|
||||
data.apiKey !== DEFAULT_TEST_API_KEY &&
|
||||
data.userToken !== DEFAULT_TEST_USER_TOKEN
|
||||
);
|
||||
}
|
||||
|
||||
// API客户端类
|
||||
export class ApiClient {
|
||||
constructor(
|
||||
@@ -46,7 +59,25 @@ export class ApiClient {
|
||||
},
|
||||
});
|
||||
|
||||
return await response.json();
|
||||
// 处理非200响应
|
||||
if (!response.ok()) {
|
||||
return {
|
||||
code: response.status(),
|
||||
message: `HTTP ${response.status()}: ${response.statusText()}`,
|
||||
data: null as any
|
||||
};
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) {
|
||||
return {
|
||||
code: response.status(),
|
||||
message: 'Empty response',
|
||||
data: null as any
|
||||
};
|
||||
}
|
||||
|
||||
return JSON.parse(text);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +94,25 @@ export class ApiClient {
|
||||
},
|
||||
});
|
||||
|
||||
return await response.json();
|
||||
// 处理非200响应
|
||||
if (!response.ok()) {
|
||||
return {
|
||||
code: response.status(),
|
||||
message: `HTTP ${response.status()}: ${response.statusText()}`,
|
||||
data: null as any
|
||||
};
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) {
|
||||
return {
|
||||
code: response.status(),
|
||||
message: 'Empty response',
|
||||
data: null as any
|
||||
};
|
||||
}
|
||||
|
||||
return JSON.parse(text);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +120,7 @@ export class ApiClient {
|
||||
*/
|
||||
async validateApiKey(apiKey: string): Promise<boolean> {
|
||||
try {
|
||||
const response = await this.request.post(`${this.baseURL}/api/v1/api-keys/validate`, {
|
||||
const response = await this.request.post(`${this.baseURL}/api/v1/keys/validate`, {
|
||||
data: { apiKey },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -143,7 +192,8 @@ function loadTestData(): TestData {
|
||||
// 默认测试数据
|
||||
const defaultData: TestData = {
|
||||
activityId: 1,
|
||||
apiKey: 'test-api-key',
|
||||
apiKey: DEFAULT_TEST_API_KEY,
|
||||
userToken: process.env.E2E_USER_TOKEN || DEFAULT_TEST_USER_TOKEN,
|
||||
userId: 10001,
|
||||
shortCode: 'test123',
|
||||
baseUrl: process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:5173',
|
||||
@@ -189,7 +239,7 @@ export const test = baseTest.extend<TestFixtures>({
|
||||
const client = new ApiClient(
|
||||
request,
|
||||
testData.apiKey,
|
||||
'test-e2e-token',
|
||||
testData.userToken,
|
||||
testData.apiBaseUrl
|
||||
);
|
||||
await use(client);
|
||||
@@ -199,7 +249,7 @@ export const test = baseTest.extend<TestFixtures>({
|
||||
authenticatedPage: async ({ page, testData }, use) => {
|
||||
// 设置localStorage模拟登录状态
|
||||
await page.addInitScript((data) => {
|
||||
localStorage.setItem('token', 'test-e2e-token');
|
||||
localStorage.setItem('token', data.userToken);
|
||||
localStorage.setItem('userId', data.userId.toString());
|
||||
localStorage.setItem('apiKey', data.apiKey);
|
||||
localStorage.setItem('activityId', data.activityId.toString());
|
||||
|
||||
Reference in New Issue
Block a user