-
-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathdomain-verification-job.unit.test.ts
More file actions
123 lines (109 loc) · 3.22 KB
/
domain-verification-job.unit.test.ts
File metadata and controls
123 lines (109 loc) · 3.22 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DomainStatus, type Domain } from "@prisma/client";
const {
mockFindMany,
mockIsDomainVerificationDue,
mockRefreshDomainVerification,
mockUpsertJobScheduler,
mockWorkerOn,
mockQueue,
mockWorker,
} = vi.hoisted(() => ({
mockFindMany: vi.fn(),
mockIsDomainVerificationDue: vi.fn(),
mockRefreshDomainVerification: vi.fn(),
mockUpsertJobScheduler: vi.fn(),
mockWorkerOn: vi.fn(),
mockQueue: vi.fn().mockImplementation(() => ({
upsertJobScheduler: mockUpsertJobScheduler,
})),
mockWorker: vi.fn().mockImplementation(() => ({
on: mockWorkerOn,
})),
}));
vi.mock("bullmq", () => ({
Queue: mockQueue,
Worker: mockWorker,
}));
vi.mock("~/server/db", () => ({
db: {
domain: {
findMany: mockFindMany,
},
},
}));
vi.mock("~/server/redis", () => ({
BULL_PREFIX: "bull",
getRedis: vi.fn(() => ({})),
}));
vi.mock("~/server/logger/log", () => ({
logger: {
error: vi.fn(),
info: vi.fn(),
},
}));
vi.mock("~/server/service/domain-service", () => ({
isDomainVerificationDue: mockIsDomainVerificationDue,
refreshDomainVerification: mockRefreshDomainVerification,
}));
import {
initDomainVerificationJob,
runDueDomainVerifications,
} from "~/server/jobs/domain-verification-job";
function createDomain(id: number, status: DomainStatus): Domain {
return {
id,
name: `example-${id}.com`,
teamId: 7,
status,
region: "us-east-1",
clickTracking: false,
openTracking: false,
publicKey: "public-key",
dkimSelector: "usesend",
dkimStatus: DomainStatus.NOT_STARTED,
spfDetails: DomainStatus.NOT_STARTED,
dmarcAdded: false,
errorMessage: null,
subdomain: null,
sesTenantId: null,
isVerifying: status !== DomainStatus.SUCCESS,
createdAt: new Date("2026-03-01T00:00:00.000Z"),
updatedAt: new Date("2026-03-01T00:00:00.000Z"),
};
}
describe("domain-verification-job", () => {
beforeEach(() => {
mockFindMany.mockReset();
mockIsDomainVerificationDue.mockReset();
mockRefreshDomainVerification.mockReset();
mockUpsertJobScheduler.mockReset();
mockWorkerOn.mockReset();
mockQueue.mockReset();
mockWorker.mockReset();
mockQueue.mockImplementation(() => ({
upsertJobScheduler: mockUpsertJobScheduler,
}));
mockWorker.mockImplementation(() => ({
on: mockWorkerOn,
}));
});
it("refreshes only domains that are due", async () => {
const firstDomain = createDomain(1, DomainStatus.PENDING);
const secondDomain = createDomain(2, DomainStatus.SUCCESS);
mockFindMany.mockResolvedValue([firstDomain, secondDomain]);
mockIsDomainVerificationDue
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(false);
await runDueDomainVerifications();
expect(mockRefreshDomainVerification).toHaveBeenCalledTimes(1);
expect(mockRefreshDomainVerification).toHaveBeenCalledWith(firstDomain);
});
it("initializes the worker lazily", async () => {
await initDomainVerificationJob();
expect(mockQueue).toHaveBeenCalledTimes(1);
expect(mockWorker).toHaveBeenCalledTimes(1);
expect(mockUpsertJobScheduler).toHaveBeenCalledTimes(1);
expect(mockWorkerOn).toHaveBeenCalledTimes(2);
});
});