online-qiwei-smoke.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const baseUrl = String(
  2. process.argv[2] || process.env.QIWEI_ONLINE_BASE || "https://server.fmode.cn",
  3. ).replace(/\/+$/, "");
  4. const authToken = String(process.env.QIWEI_AUTH_TOKEN || "").trim();
  5. const timeoutMs = 15_000;
  6. async function request(path, options = {}) {
  7. const controller = new AbortController();
  8. const timeout = setTimeout(() => controller.abort(), timeoutMs);
  9. try {
  10. const response = await fetch(`${baseUrl}${path}`, {
  11. ...options,
  12. headers: {
  13. Accept: "application/json",
  14. ...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
  15. ...(options.headers || {}),
  16. },
  17. signal: controller.signal,
  18. });
  19. const contentType = response.headers.get("content-type") || "";
  20. const text = await response.text();
  21. let body = null;
  22. if (contentType.includes("json")) {
  23. try {
  24. body = JSON.parse(text);
  25. } catch {
  26. body = null;
  27. }
  28. }
  29. return {
  30. path,
  31. status: response.status,
  32. contentType,
  33. bytes: Buffer.byteLength(text),
  34. code: body && typeof body === "object" ? body.code ?? null : null,
  35. };
  36. } finally {
  37. clearTimeout(timeout);
  38. }
  39. }
  40. function expect(name, result, statuses) {
  41. const ok = statuses.includes(result.status);
  42. console.log(JSON.stringify({ name, ...result, ok }));
  43. if (!ok) throw new Error(`${name} returned HTTP ${result.status}`);
  44. }
  45. const health = await request("/api/qiwei/health");
  46. expect("health", health, [200]);
  47. const test = await request("/api/qiwei/test");
  48. expect("test", test, [200]);
  49. const plans = await request("/api/qiwei/product/plans?seats=1&months=1");
  50. expect("plans", plans, [200]);
  51. if (authToken) {
  52. const trial = await request("/api/qiwei/trial/status");
  53. expect("trial-status", trial, [200]);
  54. const subscription = await request("/api/qiwei/subscribe/status?seats=1&months=1");
  55. expect("subscription-status", subscription, [200]);
  56. } else {
  57. const unauthenticated = await request("/api/qiwei/trial/status");
  58. expect("trial-status-without-auth", unauthenticated, [401, 403]);
  59. }
  60. console.log(JSON.stringify({ ok: true, baseUrl, authenticatedChecks: Boolean(authToken) }));