369 lines
10 KiB
TypeScript
369 lines
10 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { action } from "~/routes/api/autodraft.update";
|
|
|
|
// Mock dependencies
|
|
vi.mock("~/database/context");
|
|
vi.mock("~/server/socket", () => ({
|
|
getSocketIO: vi.fn(),
|
|
}));
|
|
vi.mock("@clerk/react-router/server", () => ({
|
|
getAuth: vi.fn(),
|
|
}));
|
|
|
|
describe("Autodraft Settings API", () => {
|
|
let mockDb: any;
|
|
let mockSocketIO: any;
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
|
|
// Mock Clerk auth
|
|
const { getAuth } = await import("@clerk/react-router/server");
|
|
vi.mocked(getAuth).mockResolvedValue({ userId: "user-789" } as any);
|
|
|
|
// Mock Socket.IO
|
|
mockSocketIO = {
|
|
to: vi.fn().mockReturnThis(),
|
|
emit: vi.fn(),
|
|
};
|
|
|
|
const socketModule = await import("~/server/socket");
|
|
vi.mocked(socketModule.getSocketIO).mockReturnValue(mockSocketIO);
|
|
|
|
// Mock database
|
|
mockDb = {
|
|
query: {
|
|
teams: {
|
|
findFirst: vi.fn(),
|
|
},
|
|
autodraftSettings: {
|
|
findFirst: vi.fn(),
|
|
},
|
|
},
|
|
update: vi.fn().mockReturnThis(),
|
|
set: vi.fn().mockReturnThis(),
|
|
where: vi.fn().mockReturnThis(),
|
|
returning: vi.fn(),
|
|
insert: vi.fn().mockReturnThis(),
|
|
values: vi.fn().mockReturnThis(),
|
|
};
|
|
|
|
const { database } = await import("~/database/context");
|
|
vi.mocked(database).mockReturnValue(mockDb);
|
|
});
|
|
|
|
describe("Update Autodraft Settings", () => {
|
|
it("should create new autodraft settings when none exist", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
// Mock team ownership verification
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
// Mock no existing settings
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
// Mock insert returning new settings
|
|
const newSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValue([newSettings]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.settings.id).toBe(newSettings.id);
|
|
expect(data.settings.seasonId).toBe(newSettings.seasonId);
|
|
expect(data.settings.teamId).toBe(newSettings.teamId);
|
|
expect(data.settings.isEnabled).toBe(newSettings.isEnabled);
|
|
expect(data.settings.mode).toBe(newSettings.mode);
|
|
expect(mockDb.insert).toHaveBeenCalled();
|
|
expect(mockSocketIO.to).toHaveBeenCalledWith(`draft-${seasonId}`);
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
});
|
|
});
|
|
|
|
it("should update existing autodraft settings", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
// Mock team ownership verification
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
// Mock existing settings
|
|
const existingSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: false,
|
|
mode: "next_pick",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(
|
|
existingSettings
|
|
);
|
|
|
|
// Mock update returning updated settings
|
|
const updatedSettings = {
|
|
...existingSettings,
|
|
isEnabled: true,
|
|
mode: "while_on",
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValue([updatedSettings]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "while_on");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.settings.isEnabled).toBe(true);
|
|
expect(data.settings.mode).toBe("while_on");
|
|
expect(mockDb.update).toHaveBeenCalled();
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "while_on",
|
|
});
|
|
});
|
|
|
|
it("should reject unauthorized users", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const _userId = "user-789";
|
|
const differentUserId = "user-999";
|
|
|
|
// Mock team owned by different user
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: differentUserId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(403);
|
|
const data = await response.json();
|
|
expect(data.error).toBe("Unauthorized");
|
|
});
|
|
|
|
it("should require all fields", async () => {
|
|
const formData = new FormData();
|
|
formData.append("seasonId", "season-123");
|
|
// Missing teamId and mode
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
const data = await response.json();
|
|
expect(data.error).toBe("Missing required fields");
|
|
});
|
|
|
|
it("should handle both autodraft modes", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
// Test next_pick mode
|
|
const nextPickSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValueOnce([nextPickSettings]);
|
|
|
|
const formData1 = new FormData();
|
|
formData1.append("seasonId", seasonId);
|
|
formData1.append("teamId", teamId);
|
|
formData1.append("isEnabled", "true");
|
|
formData1.append("mode", "next_pick");
|
|
|
|
const request1 = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData1,
|
|
});
|
|
|
|
const response1 = await action({
|
|
request: request1,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
const data1 = await response1.json();
|
|
expect(data1.settings.mode).toBe("next_pick");
|
|
|
|
// Test while_on mode
|
|
const whileOnSettings = {
|
|
id: "settings-2",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "while_on",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValueOnce([whileOnSettings]);
|
|
|
|
const formData2 = new FormData();
|
|
formData2.append("seasonId", seasonId);
|
|
formData2.append("teamId", teamId);
|
|
formData2.append("isEnabled", "true");
|
|
formData2.append("mode", "while_on");
|
|
|
|
const request2 = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData2,
|
|
});
|
|
|
|
const response2 = await action({
|
|
request: request2,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
const data2 = await response2.json();
|
|
expect(data2.settings.mode).toBe("while_on");
|
|
});
|
|
|
|
it("should emit socket event to correct room", async () => {
|
|
const seasonId = "season-abc";
|
|
const teamId = "team-xyz";
|
|
const userId = "user-123";
|
|
|
|
// Override auth mock for this test
|
|
const { getAuth } = await import("@clerk/react-router/server");
|
|
vi.mocked(getAuth).mockResolvedValue({ userId } as any);
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
const newSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValue([newSettings]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
await action({
|
|
request,
|
|
params: {},
|
|
context: {},
|
|
});
|
|
|
|
expect(mockSocketIO.to).toHaveBeenCalledWith(`draft-${seasonId}`);
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
});
|
|
});
|
|
});
|
|
});
|