import { describe, it, expect, beforeAll, afterAll } from "vitest"; import { startTestServer, createBoard, join, type TestServer } from "./helpers.js"; let server: TestServer; beforeAll(async () => { server = await startTestServer(); }); afterAll(async () => { await server.close(); }); const myCount = (state: { votes: { targetId: string; participantId: string; count: number }[] }, targetId: string) => state.votes .filter((v) => v.targetId === targetId && v.participantId === "alice") .reduce((s, v) => s + v.count, 0); describe("Multivoto", () => { it("apila votos en el mismo ítem hasta el cupo y deja quitar", async () => { const board = await createBoard(server.url, { votesPerUser: 3 }); // allowMultiVote por defecto: true const alice = await join(server.url, board.boardId, "alice", "Alice"); const colId = alice.last!.columns[0].id; alice.emit("note:add", { columnId: colId, text: "favorita" }); const noteId = (await alice.waitFor((s) => s.notes.length === 1)).notes[0].id; alice.emit("vote:add", { targetType: "note", targetId: noteId }); alice.emit("vote:add", { targetType: "note", targetId: noteId }); alice.emit("vote:add", { targetType: "note", targetId: noteId }); const stacked = await alice.waitFor((s) => myCount(s, noteId) === 3); expect(myCount(stacked, noteId)).toBe(3); // 4º voto: cupo agotado, no sube alice.emit("vote:add", { targetType: "note", targetId: noteId }); await new Promise((r) => setTimeout(r, 200)); expect(myCount(alice.last!, noteId)).toBe(3); // quitar uno alice.emit("vote:remove", { targetId: noteId }); const afterRemove = await alice.waitFor((s) => myCount(s, noteId) === 2); expect(myCount(afterRemove, noteId)).toBe(2); alice.close(); }); it("con allowMultiVote=false no se puede apilar en el mismo ítem", async () => { const board = await createBoard(server.url, { votesPerUser: 5 }); const alice = await join(server.url, board.boardId, "alice", "Alice"); alice.emit("board:update", { allowMultiVote: false, moderatorToken: board.moderatorToken }); await alice.waitFor((s) => s.allowMultiVote === false); const colId = alice.last!.columns[0].id; alice.emit("note:add", { columnId: colId, text: "uno" }); const noteId = (await alice.waitFor((s) => s.notes.length === 1)).notes[0].id; alice.emit("vote:add", { targetType: "note", targetId: noteId }); alice.emit("vote:add", { targetType: "note", targetId: noteId }); const after = await alice.waitFor((s) => myCount(s, noteId) >= 1); await new Promise((r) => setTimeout(r, 200)); expect(myCount(after, noteId)).toBe(1); alice.close(); }); });