/ opencodehub / tests / unit / health-route.test.ts
swadhinbiswas feat: optimize Docker CI/CD builds, refine site-wide UI aesthetic, and remediate secrets · 8/19/2026
c3e5c18
91 lines · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { afterEach, describe, expect, it, vi } from "vitest";

const state = vi.hoisted(() => ({
  distributedLocking: true,
  distributedRateLimit: true,
  queueMultiInstanceSafe: true,
}));

const executeMock = vi.hoisted(() => vi.fn(async () => [{ "?column?": 1 }]));
const fsAccessMock = vi.hoisted(() => vi.fn(async () => undefined));

vi.mock("@/db", () => ({
  getDatabase: () => ({
    execute: executeMock,
  }),
}));

vi.mock("@/lib/redis", () => ({
  redis: {
    ping: vi.fn(async () => "PONG"),
  },
}));

vi.mock("@/lib/distributed-lock", () => ({
  get isDistributedLocking() {
    return state.distributedLocking;
  },
}));

vi.mock("@/lib/rate-limit", () => ({
  get isDistributed() {
    return state.distributedRateLimit;
  },
}));

vi.mock("@/lib/queue-worker", () => ({
  getQueueWorkerScalingReadiness: () => ({
    inProcessGuardEnabled: true,
    distributedLockingEnabled: state.distributedLocking,
    multiInstanceSafe: state.queueMultiInstanceSafe,
  }),
}));

vi.mock("fs/promises", () => ({
  access: fsAccessMock,
  mkdir: vi.fn(async () => undefined),
}));

import { GET as healthGet } from "@/pages/api/health";

async function readJson(response: Response): Promise<any> {
  return response.json();
}

describe("health route scaling diagnostics", () => {
  afterEach(() => {
    vi.unstubAllEnvs();
    state.distributedLocking = true;
    state.distributedRateLimit = true;
    state.queueMultiInstanceSafe = true;
  });

  it("returns healthy scaling diagnostics when distributed systems are active", async () => {
    vi.stubEnv("NODE_ENV", "production");
    vi.stubEnv("REDIS_URL", "redis://localhost:6379");

    const response = await healthGet({} as any);
    const body = await readJson(response);

    expect(response.status).toBe(200);
    expect(body?.checks?.scaling?.status).toBe("ok");
    expect(body?.checks?.scaling?.details?.distributedLocking).toBe(true);
    expect(body?.checks?.scaling?.details?.distributedRateLimit).toBe(true);
  });

  it("returns unhealthy when production scaling requirements are not met", async () => {
    vi.stubEnv("NODE_ENV", "production");
    vi.stubEnv("REDIS_URL", "");
    state.distributedLocking = false;
    state.distributedRateLimit = false;
    state.queueMultiInstanceSafe = false;

    const response = await healthGet({} as any);
    const body = await readJson(response);

    expect(response.status).toBe(503);
    expect(body?.checks?.scaling?.status).toBe("error");
    expect(String(body?.checks?.scaling?.message || "")).toContain("REDIS_URL is required");
  });
});