chore: bump puppeteer, improve recover.ts coverage
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m7s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m7s
This commit is contained in:
parent
bb3286b1ad
commit
97ad01b133
3 changed files with 135 additions and 12 deletions
123
src/__tests__/recover-coverage.test.ts
Normal file
123
src/__tests__/recover-coverage.test.ts
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
vi.mock("../services/db.js", () => ({
|
||||
default: { query: vi.fn(), connect: vi.fn(), on: vi.fn(), end: vi.fn() },
|
||||
pool: { query: vi.fn(), connect: vi.fn(), on: vi.fn(), end: vi.fn() },
|
||||
queryWithRetry: vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }),
|
||||
connectWithRetry: vi.fn().mockResolvedValue(undefined),
|
||||
initDatabase: vi.fn().mockResolvedValue(undefined),
|
||||
cleanupStaleData: vi.fn(),
|
||||
isTransientError: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../services/logger.js", () => ({
|
||||
default: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock("../services/verification.js", () => ({
|
||||
createPendingVerification: vi.fn(),
|
||||
verifyCode: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../services/email.js", () => ({
|
||||
sendVerificationEmail: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../services/keys.js", () => ({
|
||||
getAllKeys: vi.fn().mockReturnValue([]),
|
||||
loadKeys: vi.fn().mockResolvedValue(undefined),
|
||||
isValidKey: vi.fn(),
|
||||
getKeyInfo: vi.fn(),
|
||||
isProKey: vi.fn(),
|
||||
createFreeKey: vi.fn(),
|
||||
createProKey: vi.fn(),
|
||||
downgradeByCustomer: vi.fn(),
|
||||
findKeyByCustomerId: vi.fn(),
|
||||
updateKeyEmail: vi.fn(),
|
||||
updateEmailByCustomer: vi.fn(),
|
||||
}));
|
||||
|
||||
import { queryWithRetry } from "../services/db.js";
|
||||
import { getAllKeys } from "../services/keys.js";
|
||||
import { createPendingVerification } from "../services/verification.js";
|
||||
import { sendVerificationEmail } from "../services/email.js";
|
||||
import logger from "../services/logger.js";
|
||||
import express from "express";
|
||||
import request from "supertest";
|
||||
import { recoverRouter } from "../routes/recover.js";
|
||||
|
||||
const mockQuery = vi.mocked(queryWithRetry);
|
||||
const mockGetAllKeys = vi.mocked(getAllKeys);
|
||||
const mockCreatePending = vi.mocked(createPendingVerification);
|
||||
const mockSendEmail = vi.mocked(sendVerificationEmail);
|
||||
const mockLogger = vi.mocked(logger);
|
||||
|
||||
function createApp() {
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use("/v1/recover", recoverRouter);
|
||||
return app;
|
||||
}
|
||||
|
||||
describe("recover.ts sendVerificationEmail error handlers", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("logs error when sendVerificationEmail fails in DB fallback path (line 80)", async () => {
|
||||
// No key in cache → triggers DB fallback
|
||||
mockGetAllKeys.mockReturnValueOnce([]);
|
||||
// DB finds a row → triggers email send
|
||||
mockQuery.mockResolvedValueOnce({
|
||||
rows: [{ key: "df_free_abc", tier: "free", email: "user@test.com", created_at: "2026-01-01", stripe_customer_id: null }],
|
||||
rowCount: 1,
|
||||
} as any);
|
||||
mockCreatePending.mockResolvedValueOnce({ email: "user@test.com", code: "123456", createdAt: "", expiresAt: "", attempts: 0 });
|
||||
|
||||
const emailError = new Error("SMTP connection failed");
|
||||
mockSendEmail.mockRejectedValueOnce(emailError);
|
||||
|
||||
const app = createApp();
|
||||
const res = await request(app)
|
||||
.post("/v1/recover")
|
||||
.send({ email: "user@test.com" });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.status).toBe("recovery_sent");
|
||||
|
||||
// Wait for the .catch to execute (it's fire-and-forget)
|
||||
await new Promise(r => setTimeout(r, 50));
|
||||
|
||||
expect(mockLogger.error).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ err: emailError }),
|
||||
"Failed to send recovery email"
|
||||
);
|
||||
});
|
||||
|
||||
it("logs error when sendVerificationEmail fails in main path (line 91)", async () => {
|
||||
// Key found in cache → main path
|
||||
mockGetAllKeys.mockReturnValueOnce([
|
||||
{ key: "df_pro_xyz", tier: "pro" as const, email: "found@test.com", createdAt: "2025-01-01" },
|
||||
]);
|
||||
mockCreatePending.mockResolvedValueOnce({ email: "found@test.com", code: "654321", createdAt: "", expiresAt: "", attempts: 0 });
|
||||
|
||||
const emailError = new Error("Email service down");
|
||||
mockSendEmail.mockRejectedValueOnce(emailError);
|
||||
|
||||
const app = createApp();
|
||||
const res = await request(app)
|
||||
.post("/v1/recover")
|
||||
.send({ email: "found@test.com" });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.status).toBe("recovery_sent");
|
||||
|
||||
// Wait for the .catch to execute
|
||||
await new Promise(r => setTimeout(r, 50));
|
||||
|
||||
expect(mockLogger.error).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ err: emailError }),
|
||||
"Failed to send recovery email"
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue