92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
|
import { firestore } from "../../src/firebase.js";
|
|
import {
|
|
getAll,
|
|
getDay,
|
|
getRange,
|
|
setDay,
|
|
} from "../../src/repositories/voteHistoryRepository.js";
|
|
import type { DateString } from "../../src/types/dateString.js";
|
|
import type { VoteHistoryDoc } from "../../src/types/panit.js";
|
|
|
|
const uid = "test-uid-1";
|
|
|
|
function makeDoc(gameId: string, team: string, result = true): VoteHistoryDoc {
|
|
return { data: [{ gameId, team, result }] };
|
|
}
|
|
|
|
describe("voteHistoryRepository (Firestore)", () => {
|
|
beforeEach(async () => {
|
|
console.log("[setup] users 컬렉션 초기화");
|
|
await firestore.recursiveDelete(firestore.collection("users"));
|
|
});
|
|
|
|
describe("setDay + getDay", () => {
|
|
it("저장 후 조회 시 동일한 문서를 반환한다", async () => {
|
|
const date = "2026-04-12" as DateString;
|
|
const doc = makeDoc("20260412HTLG0", "LG");
|
|
|
|
await setDay(uid, date, doc);
|
|
const result = await getDay(uid, date);
|
|
console.log("[setDay+getDay]", result);
|
|
|
|
expect(result).toEqual(doc);
|
|
});
|
|
|
|
it("존재하지 않는 날짜는 null을 반환한다", async () => {
|
|
const result = await getDay(uid, "2020-01-01" as DateString);
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("getRange", () => {
|
|
it("지정한 날짜 범위(양끝 포함)만 반환한다", async () => {
|
|
const dates: DateString[] = [
|
|
"2026-04-09" as DateString,
|
|
"2026-04-10" as DateString,
|
|
"2026-04-11" as DateString,
|
|
"2026-04-12" as DateString,
|
|
"2026-04-13" as DateString,
|
|
];
|
|
for (const d of dates) {
|
|
await setDay(uid, d, makeDoc(`game-${d}`, "LG"));
|
|
}
|
|
|
|
const result = await getRange(
|
|
uid,
|
|
"2026-04-10" as DateString,
|
|
"2026-04-12" as DateString
|
|
);
|
|
const resultDates = result.map((r) => r.date);
|
|
console.log("[getRange] 반환된 날짜:", resultDates);
|
|
|
|
expect(resultDates).toEqual(["2026-04-10", "2026-04-11", "2026-04-12"]);
|
|
expect(result[0].doc.data[0].gameId).toBe("game-2026-04-10");
|
|
});
|
|
});
|
|
|
|
describe("getAll", () => {
|
|
it("모든 날짜를 오름차순으로 반환한다", async () => {
|
|
const dates: DateString[] = [
|
|
"2026-04-12" as DateString,
|
|
"2026-04-10" as DateString,
|
|
"2026-04-11" as DateString,
|
|
];
|
|
for (const d of dates) {
|
|
await setDay(uid, d, makeDoc(`game-${d}`, "LG"));
|
|
}
|
|
|
|
const result = await getAll(uid);
|
|
const resultDates = result.map((r) => r.date);
|
|
console.log("[getAll] 정렬된 날짜:", resultDates);
|
|
|
|
expect(resultDates).toEqual(["2026-04-10", "2026-04-11", "2026-04-12"]);
|
|
});
|
|
|
|
it("투표 기록이 없으면 빈 배열을 반환한다", async () => {
|
|
const result = await getAll("no-history-uid");
|
|
expect(result).toEqual([]);
|
|
});
|
|
});
|
|
});
|