mmday-firebase/tests/repositories/ledgerWireFormat.test.ts
윤정민 85edc7c7e3 Simplify orders to immediate debit with single refund path
- 주문 생성 시 예약(홀드) 없이 즉시 차감하고 confirmed 로 시작 — 상태 흐름을 확정 → 배송중 → 배송완료(+어드민 환불)로 간소화
- 유저 취소 엔드포인트(POST /orders/:id/cancel)와 cancelOrder 제거 — 상태 전이는 어드민 전용, 되돌림은 환불 전이 하나로 통일 (차감 txId 를 reversalOf 로 링크)
- order_reserve/order_release 원장 타입과 reserve/capture/release op, 지갑 reservedBalance 제거 — 불변식을 available = earned - spent 로 단순화
- 이전 데이터 전면 삭제에 따라 2026-07 개편 이전 원장 문서용 레거시 정규화(StoredLedgerEntry, balanceAfter 역산, refMonth/refDay 합성)도 제거
- 주문/원장/지갑 테스트를 새 스키마로 갱신하고 orderService 에뮬레이터 테스트 6건 추가 (즉시 차감·잔액 부족·권한·전이 검증·환불 반환)
2026-07-21 16:06:13 +09:00

73 lines
2.8 KiB
TypeScript

import { beforeEach, describe, expect, it } from "vitest";
import { Timestamp } from "firebase-admin/firestore";
import { firestore } from "../../src/firebase";
import { listLedger } from "../../src/repositories/pointLedgerRepository";
import { getWallet } from "../../src/repositories/walletRepository";
import { toLedgerEntryDto, toWalletDto } from "../../src/types/dto/rewardDto";
import { PointLedgerType } from "../../src/types/points";
/**
* 앱의 "포인트 내역" 화면 크래시 재현 방지.
*
* Firestore 에 실제로 저장된 Timestamp 를 읽어 응답 DTO 까지 태웠을 때
* 와이어에 `{_seconds,_nanoseconds}` 가 나오지 않는지 끝까지 확인한다.
* (클라이언트는 이 필드를 `DateTime.parse(... as String)` 로 받으므로
* Map 이 오면 캐스트 예외로 화면 전체가 죽는다.)
*/
const uid = "ledger-wire-user";
const UTC_ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
describe("포인트 내역 와이어 형식 (Firestore -> DTO)", () => {
beforeEach(async () => {
await firestore.recursiveDelete(firestore.doc(`users/${uid}`));
});
it("저장된 Timestamp 가 UTC ISO 문자열로 나가고 _seconds 가 남지 않는다", async () => {
const createdAt = Timestamp.fromDate(new Date("2026-07-20T05:30:00.000Z"));
await firestore.doc(`users/${uid}/pointLedger/tx-1`).set({
txId: "tx-1",
uid,
type: PointLedgerType.AttendanceDaily,
op: "credit",
amount: 20,
availableBefore: 0,
availableAfter: 20,
relatedDate: "2026-07-20",
createdAt,
});
const page = await listLedger(uid, 20);
expect(page.items).toHaveLength(1);
const dto = page.items.map((entry) => toLedgerEntryDto(entry.id, uid, entry));
const json = JSON.stringify({ items: dto, cursor: page.cursor });
expect(json).not.toContain("_seconds");
expect(json).not.toContain("_nanoseconds");
expect(JSON.parse(json).items[0].createdAt).toBe("2026-07-20T05:30:00.000Z");
// 날짜 전용 필드는 형식이 유지되어야 한다
expect(JSON.parse(json).items[0].relatedDate).toBe("2026-07-20");
});
it("지갑 문서의 createdAt/updatedAt 도 UTC ISO 문자열로 나간다", async () => {
const now = Timestamp.fromDate(new Date("2026-07-20T05:30:00.000Z"));
await firestore.doc(`users/${uid}/wallet/current`).set({
availableBalance: 20,
totalEarned: 20,
totalSpent: 0,
version: 1,
createdAt: now,
updatedAt: now,
});
const wallet = await getWallet(uid);
expect(wallet).not.toBeNull();
const json = JSON.stringify(toWalletDto(wallet!));
expect(json).not.toContain("_seconds");
expect(JSON.parse(json).createdAt).toMatch(UTC_ISO);
expect(JSON.parse(json).updatedAt).toMatch(UTC_ISO);
});
});