-- 결제 1회 = 지급 1회 검사(가짜 프로필): lune run tests/Receipts.spec.luau local Receipts = require("../src/server/Receipts") local T = require("./check") local function deepcopy(t) local out = {} for k, v in t do out[k] = if type(v) == "table" then deepcopy(v) else v end return out end local function fakeProfile(saveWorks: boolean) local p = { Data = { PurchaseIdCache = {}, Money = 0 }, LastSavedData = { PurchaseIdCache = {} }, active = true, saves = 0, } function p:IsActive() return self.active end function p:Save() self.saves += 1 if saveWorks then self.LastSavedData = deepcopy(self.Data) else self.active = false -- 저장 실패 끝에 세션이 끝난 상황 end end p.OnAfterSave = { Wait = function() end } return p end local p = fakeProfile(true) local grants = 0 local function grant() grants += 1 p.Data.Money += 100 return true end T.eq(Receipts.process(p, "A", grant), "Granted", "첫 결제 지급") T.eq(grants, 1, "지급 1회") T.eq(p.Data.Money, 100, "돈 들어옴") T.check(table.find(p.LastSavedData.PurchaseIdCache, "A") ~= nil, "저장된 데이터에 PurchaseId 기록") T.eq(Receipts.process(p, "A", grant), "Granted", "같은 결제 재시도도 완료로 답함") T.eq(grants, 1, "같은 결제는 다시 지급하지 않음") T.eq(Receipts.process(p, "B", grant), "Granted", "다른 결제는 지급") T.eq(grants, 2, "지급 2회") T.eq(Receipts.process(nil, "C", grant), "NotProcessedYet", "프로필 없으면 나중에") T.eq(grants, 2, "프로필 없으면 지급 안 함") local q = fakeProfile(true) T.eq( Receipts.process(q, "X", function() return false end), "NotProcessedYet", "지급 실패면 나중에" ) T.eq(#q.Data.PurchaseIdCache, 0, "실패한 결제는 기록 안 함") T.eq( Receipts.process(q, "Y", function() error("boom") end), "NotProcessedYet", "지급 중 오류면 나중에" ) local r = fakeProfile(false) local rg = 0 T.eq( Receipts.process(r, "Z", function() rg += 1 return true end), "NotProcessedYet", "저장 확인 못 하면 완료로 답하지 않음" ) local c = fakeProfile(true) for i = 1, Receipts.CACHE_SIZE + 5 do Receipts.process(c, "P" .. i, function() return true end) end T.eq(#c.Data.PurchaseIdCache, Receipts.CACHE_SIZE, "기록은 최근 100개까지") T.done("Receipts.spec")