/ opencodehub / tests / integration / cron-mirror-sync-route.test.ts
swadhinbiswas Polish mirror sync scheduling and cron observability · 2/19/2026
75ea0df
99 lines · 3.1 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
92
93
94
95
96
97
98
99
import { beforeEach, describe, expect, it, vi } from "vitest";

const { syncAllMirrorsScheduledMock } = vi.hoisted(() => ({
  syncAllMirrorsScheduledMock: vi.fn(async () => ({
    synced: 2,
    failed: 1,
    total: 5,
    eligible: 3,
    skipped: 2,
    stale: 4,
    failedRepoIds: ["repo-failed-1"],
    durationMs: 123,
  })),
}));

vi.mock("@/lib/mirror-sync", () => ({
  syncAllMirrorsScheduled: syncAllMirrorsScheduledMock,
}));

describe("cron mirror sync route", () => {
  beforeEach(() => {
    vi.resetModules();
    vi.clearAllMocks();
    vi.stubEnv("CRON_SECRET", "cron-secret");
  });

  it("rejects requests with invalid cron secret", async () => {
    const { POST: mirrorCronPost } = await import("@/pages/api/cron/mirror-sync");
    const response = await mirrorCronPost({
      request: new Request("http://localhost/api/cron/mirror-sync", {
        method: "POST",
        headers: { Authorization: "Bearer wrong-secret" },
      }),
    } as any);

    expect(response.status).toBe(401);
    expect(syncAllMirrorsScheduledMock).not.toHaveBeenCalled();
  });

  it("runs scheduled sync with query options and returns monitoring metrics", async () => {
    const { POST: mirrorCronPost } = await import("@/pages/api/cron/mirror-sync");
    const response = await mirrorCronPost({
      request: new Request(
        "http://localhost/api/cron/mirror-sync?staleOnly=false&minSyncIntervalMinutes=15&maxRepos=10&staleAfterMinutes=60",
        {
          method: "POST",
          headers: { Authorization: "Bearer cron-secret" },
        }
      ),
    } as any);

    const body = await response.json();
    expect(response.status).toBe(200);
    expect(body?.success).toBe(true);
    expect(body?.synced).toBe(2);
    expect(body?.eligible).toBe(3);
    expect(body?.failedRepoIds).toEqual(["repo-failed-1"]);
    expect(syncAllMirrorsScheduledMock).toHaveBeenCalledWith({
      staleOnly: false,
      minSyncIntervalMinutes: 15,
      maxRepos: 10,
      staleAfterMinutes: 60,
    });
  });

  it("supports GET requests", async () => {
    const { GET: mirrorCronGet } = await import("@/pages/api/cron/mirror-sync");
    const response = await mirrorCronGet({
      request: new Request("http://localhost/api/cron/mirror-sync", {
        method: "GET",
        headers: { Authorization: "Bearer cron-secret" },
      }),
    } as any);

    expect(response.status).toBe(200);
  });

  it("falls back to safe defaults for invalid query options", async () => {
    const { POST: mirrorCronPost } = await import("@/pages/api/cron/mirror-sync");
    const response = await mirrorCronPost({
      request: new Request(
        "http://localhost/api/cron/mirror-sync?staleOnly=invalid&minSyncIntervalMinutes=-5&maxRepos=0&staleAfterMinutes=abc",
        {
          method: "POST",
          headers: { Authorization: "Bearer cron-secret" },
        }
      ),
    } as any);

    expect(response.status).toBe(200);
    expect(syncAllMirrorsScheduledMock).toHaveBeenCalledWith({
      staleOnly: true,
      minSyncIntervalMinutes: undefined,
      maxRepos: undefined,
      staleAfterMinutes: undefined,
    });
  });
});