package kr.co.itosm.dutycalendar import android.content.Context import org.json.JSONArray import org.json.JSONObject import java.time.LocalDate import java.util.UUID /** 표시용 자료 묶음의 결과와 회원 자료 숨김을 검사한다. 시간 측정은 같은 기기의 참고값이며 절대 성능 기준으로 쓰지 않는다. */ object SnapshotCheck { fun run(context:Context):String { check(!SyncClient.connected(context)&&!CalendarMirror.enabled(context)) val prefs=context.getSharedPreferences("planner",0);val original=prefs.getString("document",null) val first=LocalDate.of(2026,11,1) fun item(kind:String,data:JSONObject)=JSONObject().put("id",UUID.randomUUID().toString()).put("kind",kind).put("deleted",false).put("data",data) val events=(0 until 200).map{index->item("event",JSONObject().put("title","검증 $index").put("date",first.plusDays((index%7).toLong()).toString()).put("endDate",first.plusDays((index%7).toLong()).toString()).put("allDay",false).put("startTime","09:00").put("endTime","10:00").put("repeat","WEEKLY").put("repeatUntil","").put("notes","").put("category","WORK"))} val name=item("name_rule",JSONObject().put("name","평일 이름").put("mode","WEEKLY").put("anchor","2026-01-01").put("interval",1).put("weekdays",JSONArray(listOf(1,2,3,4,5))).put("enabled",true).put("personId","").put("color","BLUE")) val annual=item("anniversary",JSONObject().put("title","개인 기념일").put("calendar","SOLAR").put("month",11).put("day",23).put("leapMonth",false)) val items=events+name+annual try { val document=JSONObject().put("state",JSONObject().put("items",JSONArray(items))).put("pending",JSONArray()).put("conflicts",JSONArray()) check(prefs.edit().putString("document",document.toString()).commit()) val started=System.nanoTime() val legacy=(0L..41L).map{date->PlannerStore.eventsOn(context,first.plusDays(date)).map{it.getString("id")}} val legacyMs=(System.nanoTime()-started)/1_000_000 val snapshotStart=System.nanoTime();val snapshot=CalendarSnapshot.load(context) val actual=(0L..41L).map{date->snapshot.eventsOn(first.plusDays(date)).map{it.getString("id")}} val snapshotMs=(System.nanoTime()-snapshotStart)/1_000_000 check(legacy==actual){"최적화 전후 일정 결과 불일치"} val day=LocalDate.of(2026,11,23);val schedule=Schedule(first,listOf(Shift.REGULAR)) check(snapshot.anniversariesOn(day).isEmpty()&&snapshot.workNames(day,schedule).isEmpty()) val member=CalendarSnapshot(context,items,null,true) check(member.anniversariesOn(day).single().getString("id")==annual.getString("id")) check(member.workNames(day,schedule).single().name=="평일 이름") return "일정 200개·42일 결과 일치와 회원 전용 자료 숨김 확인. 같은 기기 측정: 반복 읽기 ${legacyMs}ms / 한 번 읽기 ${snapshotMs}ms." } finally { val editor=prefs.edit();if(original==null)editor.remove("document") else editor.putString("document",original);check(editor.commit()) } } }