Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,95 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
const findUnique = vi.fn();
|
||||
const update = vi.fn();
|
||||
const verifyMock = vi.fn();
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: { emailSequenceRun: { findUnique, update } },
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/email/unsubscribeToken.server", () => ({
|
||||
verifyUnsubscribeToken: verifyMock,
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/logger.server", () => ({
|
||||
createScopedLogger: () => ({
|
||||
info: () => undefined,
|
||||
warn: () => undefined,
|
||||
error: () => undefined,
|
||||
}),
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
findUnique.mockReset();
|
||||
update.mockReset();
|
||||
verifyMock.mockReset();
|
||||
});
|
||||
|
||||
describe("unsubscribe.$token loader", () => {
|
||||
it("returns invalid when token verification fails", async () => {
|
||||
verifyMock.mockReturnValueOnce(null);
|
||||
const route = await import("./unsubscribe.$token.tsx");
|
||||
const res = await route.loader({
|
||||
params: { token: "bad" },
|
||||
request: new Request("http://x/unsubscribe/bad"),
|
||||
context: {},
|
||||
} as unknown as Parameters<typeof route.loader>[0]);
|
||||
expect(res).toEqual({ status: "invalid" });
|
||||
});
|
||||
|
||||
it("returns run_not_found when row missing", async () => {
|
||||
verifyMock.mockReturnValueOnce({ runId: "run_1", leadId: null, tenantId: null, iat: 0 });
|
||||
findUnique.mockResolvedValueOnce(null);
|
||||
const route = await import("./unsubscribe.$token.tsx");
|
||||
const res = await route.loader({
|
||||
params: { token: "ok" },
|
||||
request: new Request("http://x/unsubscribe/ok"),
|
||||
context: {},
|
||||
} as unknown as Parameters<typeof route.loader>[0]);
|
||||
expect(res).toEqual({ status: "run_not_found" });
|
||||
});
|
||||
|
||||
it("returns already_unsubscribed and skips write when status=unsubscribed", async () => {
|
||||
verifyMock.mockReturnValueOnce({ runId: "run_1", leadId: null, tenantId: null, iat: 0 });
|
||||
findUnique.mockResolvedValueOnce({ id: "run_1", status: "unsubscribed" });
|
||||
const route = await import("./unsubscribe.$token.tsx");
|
||||
const res = await route.loader({
|
||||
params: { token: "ok" },
|
||||
request: new Request("http://x/unsubscribe/ok"),
|
||||
context: {},
|
||||
} as unknown as Parameters<typeof route.loader>[0]);
|
||||
expect(res).toEqual({ status: "already_unsubscribed" });
|
||||
expect(update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("updates run status to unsubscribed on valid token + running run", async () => {
|
||||
verifyMock.mockReturnValueOnce({ runId: "run_1", leadId: "lead_1", tenantId: null, iat: 0 });
|
||||
findUnique.mockResolvedValueOnce({ id: "run_1", status: "running" });
|
||||
update.mockResolvedValueOnce({});
|
||||
const route = await import("./unsubscribe.$token.tsx");
|
||||
const res = await route.loader({
|
||||
params: { token: "ok" },
|
||||
request: new Request("http://x/unsubscribe/ok"),
|
||||
context: {},
|
||||
} as unknown as Parameters<typeof route.loader>[0]);
|
||||
expect(res).toEqual({ status: "ok" });
|
||||
expect(update).toHaveBeenCalledWith({
|
||||
where: { id: "run_1" },
|
||||
data: expect.objectContaining({ status: "unsubscribed" }),
|
||||
});
|
||||
});
|
||||
|
||||
it("supports POST action (one-click unsubscribe)", async () => {
|
||||
verifyMock.mockReturnValueOnce({ runId: "run_1", leadId: null, tenantId: null, iat: 0 });
|
||||
findUnique.mockResolvedValueOnce({ id: "run_1", status: "running" });
|
||||
update.mockResolvedValueOnce({});
|
||||
const route = await import("./unsubscribe.$token.tsx");
|
||||
const res = await route.action({
|
||||
params: { token: "ok" },
|
||||
request: new Request("http://x/unsubscribe/ok", { method: "POST" }),
|
||||
context: {},
|
||||
} as unknown as Parameters<typeof route.action>[0]);
|
||||
expect(res).toEqual({ status: "ok" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user