'use client' import { useState } from 'react' import { Button } from '@/components/ui/button' export default function DebugMockPage() { const [testResults, setTestResults] = useState([]) const addResult = (message: string) => { setTestResults((prev) => [...prev, `${new Date().toLocaleTimeString()}: ${message}`]) } const testDirectRequest = async () => { try { addResult('🔥 开始测试直接请求...') const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: 'user@example.com', password: 'password', }), }) const result = await response.json() addResult(`✅ 直接请求成功: ${JSON.stringify(result)}`) } catch (error) { addResult(`❌ 直接请求失败: ${error}`) } } const testMockRequest = async () => { try { addResult('🎭 开始测试 Mock 请求...') // 这里使用你定义的服务 URL const baseUrl = process.env.NEXT_PUBLIC_AUTH_API_URL || 'http://localhost:3000/api/auth' const response = await fetch(`${baseUrl}/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: 'user@example.com', password: 'password', }), }) const result = await response.json() addResult(`✅ Mock 请求成功: ${JSON.stringify(result)}`) } catch (error) { addResult(`❌ Mock 请求失败: ${error}`) } } const testServiceWorker = () => { if ('serviceWorker' in navigator) { navigator.serviceWorker.getRegistrations().then((registrations) => { const mswWorker = registrations.find((reg) => reg.active?.scriptURL.includes('mockServiceWorker') ) if (mswWorker) { addResult('✅ MSW Service Worker 已注册') addResult(`📍 Worker URL: ${mswWorker.active?.scriptURL}`) } else { addResult('❌ MSW Service Worker 未找到') } }) } else { addResult('❌ 浏览器不支持 Service Worker') } } const clearResults = () => { setTestResults([]) } return (

🔧 Mock API 调试页面

{/* 测试按钮 */}

test options

{/* 环境信息 */}

Environmental information

NODE_ENV: {process.env.NODE_ENV}
ENABLE_MOCK: {process.env.NEXT_PUBLIC_ENABLE_MOCK}
AUTH_API_URL: {process.env.NEXT_PUBLIC_AUTH_API_URL || '未设置'}
Service Worker支持: {'serviceWorker' in navigator ? '是' : '否'}
{/* 测试结果 */}

Test Results

{testResults.length === 0 ? (
Waiting for the test results...
) : ( testResults.map((result, index) => (
{result}
)) )}
) }