test-api.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // 测试 linux.do 的 API 接口(本地调试:API_KEY=xxx node scripts/dev/test-api.js)
  2. const API_KEY = process.env.API_KEY ?? "";
  3. const BASE_URL = process.env.BASE_URL ?? "https://hub.linux.do/v1";
  4. if (!API_KEY) {
  5. console.error("请设置环境变量 API_KEY");
  6. process.exit(1);
  7. }
  8. async function testModelList() {
  9. console.log("=== 测试模型列表接口 ===");
  10. const url = `${BASE_URL}/models`;
  11. console.log(`请求 URL: ${url}`);
  12. console.log(`API Key: ${API_KEY.substring(0, 10)}...`);
  13. try {
  14. // 测试 1: 标准请求
  15. console.log("\n1. 标准 Bearer 认证请求:");
  16. let response = await fetch(url, {
  17. method: "GET",
  18. headers: {
  19. "Authorization": `Bearer ${API_KEY}`,
  20. "Content-Type": "application/json"
  21. }
  22. });
  23. console.log(`状态码: ${response.status}`);
  24. console.log(`状态文本: ${response.statusText}`);
  25. const text1 = await response.text();
  26. console.log(`响应内容: ${text1.substring(0, 500)}`);
  27. if (response.status === 403) {
  28. // 测试 2: 添加浏览器 User-Agent
  29. console.log("\n2. 添加浏览器 User-Agent:");
  30. response = await fetch(url, {
  31. method: "GET",
  32. headers: {
  33. "Authorization": `Bearer ${API_KEY}`,
  34. "Content-Type": "application/json",
  35. "Accept": "application/json",
  36. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) QMaiWrite"
  37. }
  38. });
  39. console.log(`状态码: ${response.status}`);
  40. const text2 = await response.text();
  41. console.log(`响应内容: ${text2.substring(0, 500)}`);
  42. if (response.status === 403) {
  43. // 测试 3: 去掉 Origin,添加更多兼容性 header
  44. console.log("\n3. 移除 Origin,添加兼容性 headers:");
  45. response = await fetch(url, {
  46. method: "GET",
  47. headers: {
  48. "Authorization": `Bearer ${API_KEY}`,
  49. "Accept": "application/json",
  50. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
  51. }
  52. });
  53. console.log(`状态码: ${response.status}`);
  54. const text3 = await response.text();
  55. console.log(`响应内容: ${text3.substring(0, 500)}`);
  56. }
  57. }
  58. if (response.ok) {
  59. const data = await response.json();
  60. console.log("\n✅ 成功获取模型列表:");
  61. console.log(`模型数量: ${data.data?.length || 0}`);
  62. if (data.data && data.data.length > 0) {
  63. console.log("前5个模型:", data.data.slice(0, 5).map(m => m.id).join(", "));
  64. }
  65. }
  66. } catch (error) {
  67. console.error("❌ 请求失败:", error.message);
  68. }
  69. }
  70. async function testChatCompletion() {
  71. console.log("\n\n=== 测试聊天接口 ===");
  72. const url = `${BASE_URL}/chat/completions`;
  73. const body = {
  74. model: "deepseek-v4-flash",
  75. messages: [
  76. { role: "user", content: "你好,请回复'测试成功'" }
  77. ],
  78. stream: false
  79. };
  80. try {
  81. console.log("\n使用兼容性 headers:");
  82. const response = await fetch(url, {
  83. method: "POST",
  84. headers: {
  85. "Authorization": `Bearer ${API_KEY}`,
  86. "Content-Type": "application/json",
  87. "Accept": "application/json",
  88. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) QMaiWrite"
  89. },
  90. body: JSON.stringify(body)
  91. });
  92. console.log(`状态码: ${response.status}`);
  93. const text = await response.text();
  94. console.log(`响应内容: ${text.substring(0, 500)}`);
  95. if (response.ok) {
  96. const data = JSON.parse(text);
  97. console.log("\n✅ 聊天接口正常:");
  98. console.log(`回复: ${data.choices?.[0]?.message?.content}`);
  99. }
  100. } catch (error) {
  101. console.error("❌ 请求失败:", error.message);
  102. }
  103. }
  104. // 运行测试
  105. testModelList().then(() => testChatCompletion());