@apps-in-toss/devtools 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2610 @@
1
+ //#region src/mock/throttle-registry.ts
2
+ /**
3
+ * throttle 레지스트리 (devtools#834).
4
+ *
5
+ * 메서드별 마지막 *허용* 호출 시각만 담는 leaf 모듈이다. `state.ts`가 `reset()`에서
6
+ * 이걸 비워야 하는데, 로직 본체(`throttle.ts`)는 거꾸로 `state.ts`를 읽는다 —
7
+ * 그래서 **의존이 없는 이 파일로 레지스트리를 분리해** 순환 import를 피한다.
8
+ *
9
+ * `state.ts`의 `AitStateManager`와 같은 이유로 `globalThis` 싱글턴이어야 한다 (#836):
10
+ * `tsdown.config.ts`가 mock/panel/unplugin entry를 각각 self-contained로 빌드하므로,
11
+ * 소비자가 두 entry를 동시에 import하면 이 모듈이 entry당 하나씩 복제된다. Map을
12
+ * 모듈 지역 변수로 두면 `aitState.reset()`이 비우는 Map(싱글턴을 먼저 생성한 번들의 것)과
13
+ * `throttleErrorFor`가 읽는 Map(mock 번들의 것)이 갈려, 리셋 직후 첫 호출이 낡은
14
+ * 타임스탬프 때문에 거부된다 — 이 파일이 막으려던 바로 그 증상이다.
15
+ */
16
+ const SINGLETON_KEY$1 = "__aitDevtoolsThrottleRegistry__";
17
+ const globalRef$1 = globalThis;
18
+ if (!globalRef$1[SINGLETON_KEY$1]) globalRef$1[SINGLETON_KEY$1] = /* @__PURE__ */ new Map();
19
+ /** 메서드별 마지막 *허용* 호출 시각(ms). 페이지 안의 모든 entry가 공유한다. */
20
+ const lastAllowedAt = globalRef$1[SINGLETON_KEY$1];
21
+ /**
22
+ * throttle 레지스트리를 비운다. `aitState.reset()`이 호출한다 — 상태를 되돌렸는데
23
+ * 직전 세션의 호출 시각이 남아 있으면 첫 호출이 이유 없이 거부된다.
24
+ */
25
+ function resetThrottleRegistry() {
26
+ lastAllowedAt.clear();
27
+ }
28
+ //#endregion
29
+ //#region src/mock/state.ts
30
+ /** SDK 호출 로그 ring buffer 상한 */
31
+ const SDK_CALL_LOG_MAX = 200;
32
+ const DEFAULT_STATE = {
33
+ platform: "ios",
34
+ environment: "sandbox",
35
+ appVersion: "5.240.0",
36
+ locale: "ko-KR",
37
+ schemeUri: "/",
38
+ groupId: "mock-group-id",
39
+ deploymentId: "mock-deployment-id",
40
+ deviceId: "",
41
+ brand: {
42
+ displayName: "Mock App",
43
+ icon: "",
44
+ primaryColor: "#3182F6"
45
+ },
46
+ networkStatus: "WIFI",
47
+ navigation: { iosSwipeGestureEnabled: null },
48
+ permissions: {
49
+ clipboard: "allowed",
50
+ contacts: "allowed",
51
+ photos: "allowed",
52
+ geolocation: "allowed",
53
+ camera: "allowed",
54
+ microphone: "notDetermined"
55
+ },
56
+ location: {
57
+ coords: {
58
+ latitude: 37.5665,
59
+ longitude: 126.978,
60
+ altitude: 0,
61
+ accuracy: 10,
62
+ altitudeAccuracy: 0,
63
+ heading: 0
64
+ },
65
+ timestamp: Date.now(),
66
+ accessLocation: "FINE"
67
+ },
68
+ safeAreaInsets: {
69
+ top: 54,
70
+ bottom: 34,
71
+ left: 0,
72
+ right: 0
73
+ },
74
+ contacts: [{
75
+ name: "홍길동",
76
+ phoneNumber: "010-1234-5678"
77
+ }, {
78
+ name: "김토스",
79
+ phoneNumber: "010-9876-5432"
80
+ }],
81
+ iap: {
82
+ products: [{
83
+ sku: "mock-gem-100",
84
+ type: "CONSUMABLE",
85
+ displayName: "보석 100개",
86
+ displayAmount: "1,000원",
87
+ iconUrl: "",
88
+ description: "게임에서 사용할 수 있는 보석 100개"
89
+ }],
90
+ nextResult: "success",
91
+ pendingOrders: [],
92
+ completedOrders: []
93
+ },
94
+ payment: {
95
+ nextResult: "success",
96
+ failReason: ""
97
+ },
98
+ auth: {
99
+ isLoggedIn: true,
100
+ isTossLoginIntegrated: true,
101
+ userKeyHash: "mock-user-hash-abc123",
102
+ anonymousKeyHash: "mock-anon-hash-xyz789",
103
+ consentedUserData: { USER_NAME: "mock-user-name" },
104
+ declaredAgeRange: {
105
+ status: "NOT_AVAILABLE",
106
+ activeParentalControls: []
107
+ }
108
+ },
109
+ notification: { nextResult: "newAgreement" },
110
+ ads: {
111
+ isLoaded: false,
112
+ nextEvent: "loaded",
113
+ forceNoFill: false,
114
+ lastEvent: null,
115
+ rewardUnitType: "coins",
116
+ rewardAmount: 10
117
+ },
118
+ game: {
119
+ profile: {
120
+ nickname: "MockPlayer",
121
+ profileImageUri: ""
122
+ },
123
+ leaderboardScores: []
124
+ },
125
+ analyticsLog: [],
126
+ sdkCallLog: [],
127
+ deviceModes: {
128
+ camera: "mock",
129
+ photos: "mock",
130
+ location: "mock",
131
+ network: "mock",
132
+ clipboard: "mock"
133
+ },
134
+ mockData: {
135
+ images: [],
136
+ clipboardText: ""
137
+ },
138
+ panelEditable: true,
139
+ viewport: {
140
+ preset: "none",
141
+ orientation: "auto",
142
+ appOrientation: null,
143
+ customWidth: 402,
144
+ customHeight: 874,
145
+ frame: false,
146
+ aitNavBar: true,
147
+ aitNavBarType: "partner"
148
+ },
149
+ failureModes: { sdkLine: "2.x" }
150
+ };
151
+ function generateDeviceId() {
152
+ const stored = localStorage.getItem("__ait_device_id");
153
+ if (stored) return stored;
154
+ const id = crypto.randomUUID();
155
+ localStorage.setItem("__ait_device_id", id);
156
+ return id;
157
+ }
158
+ var AitStateManager = class {
159
+ _state;
160
+ _listeners = /* @__PURE__ */ new Set();
161
+ _inTransaction = false;
162
+ constructor() {
163
+ this._state = structuredClone(DEFAULT_STATE);
164
+ try {
165
+ this._state.deviceId = generateDeviceId();
166
+ } catch {
167
+ this._state.deviceId = `mock-device-${Math.random().toString(36).slice(2)}`;
168
+ }
169
+ }
170
+ get state() {
171
+ return this._state;
172
+ }
173
+ update(partial) {
174
+ this._state = {
175
+ ...this._state,
176
+ ...partial
177
+ };
178
+ this._notify();
179
+ }
180
+ /** 중첩 객체 업데이트용 */
181
+ patch(key, partial) {
182
+ const current = this._state[key];
183
+ if (typeof current === "object" && current !== null && !Array.isArray(current)) this._state = {
184
+ ...this._state,
185
+ [key]: {
186
+ ...current,
187
+ ...partial
188
+ }
189
+ };
190
+ else this._state = {
191
+ ...this._state,
192
+ [key]: partial
193
+ };
194
+ this._notify();
195
+ }
196
+ subscribe(listener) {
197
+ this._listeners.add(listener);
198
+ return () => this._listeners.delete(listener);
199
+ }
200
+ /**
201
+ * 한 묶음의 update/patch 호출을 묶어 listener notify 1회로 만든다.
202
+ * preset 적용처럼 여러 슬라이스를 동시에 바꿀 때 panel re-render 폭주를
203
+ * 방지한다. 중첩 호출은 outermost transaction이 끝날 때 한 번만 notify
204
+ * (inner도 throw해도 outer finally가 flag를 복구한다).
205
+ *
206
+ * Rollback은 없다 — `fn`이 throw해도 그때까지의 state 변경은 유지된다.
207
+ * 구독자가 partial state를 영원히 못 보는 사고를 막기 위해, throw 여부와
208
+ * 무관하게 항상 한 번 notify한 뒤 throw를 propagate한다. DB transaction이
209
+ * 아니라 "여러 mutation을 한 notify로 묶는 batch"라고 생각하면 된다.
210
+ *
211
+ * Listener는 throw해선 안 된다 — finally 안의 `_notify()`가 throw하면 원래
212
+ * `fn`의 throw를 덮어버린다. 우리 구독자는 panel re-render뿐이라 실제
213
+ * 발생 사례는 없지만, 외부에서 listener를 등록할 때 주의.
214
+ */
215
+ transaction(fn) {
216
+ if (this._inTransaction) {
217
+ fn();
218
+ return;
219
+ }
220
+ this._inTransaction = true;
221
+ try {
222
+ fn();
223
+ } finally {
224
+ this._inTransaction = false;
225
+ this._notify();
226
+ }
227
+ }
228
+ /** 분석 로그 추가 */
229
+ logAnalytics(entry) {
230
+ this._state = {
231
+ ...this._state,
232
+ analyticsLog: [...this._state.analyticsLog, {
233
+ ...entry,
234
+ timestamp: Date.now()
235
+ }]
236
+ };
237
+ this._notify();
238
+ }
239
+ /**
240
+ * SDK 호출 로그 추가 (ring buffer, 상한 SDK_CALL_LOG_MAX).
241
+ * `observe()`가 호출하고, proxy의 KNOWN_UNIMPLEMENTED 경로도 직접 호출한다.
242
+ */
243
+ logSdkCall(entry) {
244
+ const log = this._state.sdkCallLog;
245
+ const next = log.length >= SDK_CALL_LOG_MAX ? log.slice(1 - SDK_CALL_LOG_MAX) : log;
246
+ this._state = {
247
+ ...this._state,
248
+ sdkCallLog: [...next, entry]
249
+ };
250
+ this._notify();
251
+ }
252
+ /** 이벤트 트리거 (backEvent, homeEvent 등) */
253
+ trigger(event) {
254
+ window.dispatchEvent(new CustomEvent(`__ait:${event}`));
255
+ }
256
+ reset() {
257
+ const deviceId = this._state.deviceId;
258
+ this._state = {
259
+ ...structuredClone(DEFAULT_STATE),
260
+ deviceId
261
+ };
262
+ resetThrottleRegistry();
263
+ this._notify();
264
+ }
265
+ _notify() {
266
+ if (this._inTransaction) return;
267
+ for (const listener of this._listeners) listener();
268
+ }
269
+ };
270
+ const SINGLETON_KEY = "__aitDevtoolsStateSingleton__";
271
+ const globalRef = globalThis;
272
+ if (!globalRef[SINGLETON_KEY]) globalRef[SINGLETON_KEY] = new AitStateManager();
273
+ const aitState = globalRef[SINGLETON_KEY];
274
+ if (typeof window !== "undefined") window.__ait = aitState;
275
+ //#endregion
276
+ //#region src/mock/device/_helpers.ts
277
+ /**
278
+ * 디바이스 모듈 내부 공유 헬퍼
279
+ */
280
+ function generatePlaceholderImage(width, height, text, color) {
281
+ const canvas = document.createElement("canvas");
282
+ canvas.width = width;
283
+ canvas.height = height;
284
+ const ctx = canvas.getContext("2d");
285
+ if (!ctx) {
286
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}"><rect fill="${color}" width="${width}" height="${height}"/><text x="50%" y="50%" fill="white" font-size="16" text-anchor="middle" dominant-baseline="middle">${text}</text></svg>`;
287
+ return `data:image/svg+xml;base64,${btoa(svg)}`;
288
+ }
289
+ ctx.fillStyle = color;
290
+ ctx.fillRect(0, 0, width, height);
291
+ ctx.fillStyle = "white";
292
+ ctx.font = "16px sans-serif";
293
+ ctx.textAlign = "center";
294
+ ctx.textBaseline = "middle";
295
+ ctx.fillText(text, width / 2, height / 2);
296
+ return canvas.toDataURL("image/png");
297
+ }
298
+ const DEFAULT_PLACEHOLDERS = [
299
+ {
300
+ text: "Mock Photo 1",
301
+ color: "#3182F6"
302
+ },
303
+ {
304
+ text: "Mock Photo 2",
305
+ color: "#27ae60"
306
+ },
307
+ {
308
+ text: "Mock Photo 3",
309
+ color: "#e67e22"
310
+ }
311
+ ];
312
+ let cachedPlaceholders = null;
313
+ function getDefaultPlaceholderImages() {
314
+ if (!cachedPlaceholders) cachedPlaceholders = DEFAULT_PLACEHOLDERS.map((p) => generatePlaceholderImage(320, 240, p.text, p.color));
315
+ return [...cachedPlaceholders];
316
+ }
317
+ /** @internal device 모듈 내부 전용 */
318
+ function getMockImages() {
319
+ const images = aitState.state.mockData.images;
320
+ if (images.length > 0) return images;
321
+ return getDefaultPlaceholderImages();
322
+ }
323
+ const PROMPT_TIMEOUT_MS = 3e4;
324
+ /** @internal device 모듈 내부 전용 */
325
+ function waitForPromptResponse(type) {
326
+ return new Promise((resolve, reject) => {
327
+ const eventName = `__ait:prompt-response:${type}`;
328
+ const cancelName = "__ait:prompt-cancel";
329
+ function cleanup() {
330
+ clearTimeout(timer);
331
+ window.removeEventListener(eventName, handler);
332
+ window.removeEventListener(cancelName, cancelHandler);
333
+ }
334
+ const timer = setTimeout(() => {
335
+ cleanup();
336
+ const hint = !!document.querySelector(".ait-panel") ? "Please provide input via the DevTools panel." : "Is @apps-in-toss/devtools/panel imported?";
337
+ reject(/* @__PURE__ */ new Error(`[@apps-in-toss/devtools] Prompt timeout for "${type}" after ${PROMPT_TIMEOUT_MS / 1e3}s. ${hint}`));
338
+ }, PROMPT_TIMEOUT_MS);
339
+ const handler = (e) => {
340
+ cleanup();
341
+ resolve(e.detail);
342
+ };
343
+ const cancelHandler = () => {
344
+ cleanup();
345
+ reject(/* @__PURE__ */ new Error(`[@apps-in-toss/devtools] Prompt cancelled for "${type}"`));
346
+ };
347
+ window.addEventListener(eventName, handler);
348
+ window.addEventListener(cancelName, cancelHandler);
349
+ window.dispatchEvent(new CustomEvent("__ait:prompt-request", { detail: { type } }));
350
+ });
351
+ }
352
+ //#endregion
353
+ //#region src/mock/native-error.ts
354
+ /**
355
+ * 실패-모드 다이얼 — 네이티브 에러 envelope 조립 (devtools#770).
356
+ *
357
+ * env1(mock)이 env3(실기기)의 프로비저닝-의존 reject 계약을 재현할 수 있게 하는
358
+ * 공용 헬퍼. `aitState.failureModes`(state.ts)에 per-API 코드를 설정하면, 각
359
+ * mock 도메인(auth/ads 등)이 이 모듈의 {@link buildNativeError}로 실기기 캡처와
360
+ * 필드 단위로 일치하는 에러 객체를 만들어 reject/onError한다.
361
+ *
362
+ * 코드 인벤토리와 envelope shape의 정본은 sdk-example#284 2.x↔3.0 diff
363
+ * 매트릭스(2026-07-10 iOS run10 캡처 + 이후 manual 패스)다:
364
+ *
365
+ * - 2.x 라인: 네이티브 브리지가 `{ name, code, userInfo, moduleName, __isError }`
366
+ * envelope을 얹은 `Error` 인스턴스로 reject한다. `errorKeys` 관측이 이 5개
367
+ * 필드로 고정.
368
+ * - 3.x 라인: 같은 실패가 "맨 Error"(위 envelope 필드 없음)로 평탄화된다 —
369
+ * sdk-example#284 "패턴 ① 오류 envelope 평탄화" 표.
370
+ *
371
+ * `@ait-co/debugger`의 `packages/debugger/src/test-runner/bridge-stub.ts`(분리 전
372
+ * 이 repo의 `src/test-runner/bridge-stub.ts`)에 있는
373
+ * `NativeBridgeErrorShape`/`makeNativeError`가
374
+ * 같은 shape을 이미 CI 러너 쪽에서 재현하고 있다(devtools#740) — 이 모듈은 그
375
+ * shape을 mock 소비자 표면(`src/mock/**`)에 대해 재사용 가능한 형태로 옮긴 것이다.
376
+ * 두 모듈은 레이어가 다르다: test-runner 쪽은 env3 blocking-call 인터셉터(빌드에
377
+ * 안 실림, CI 전용), 이쪽은 `@apps-in-toss/devtools` 소비자가 `pnpm dev`에서 실제로
378
+ * import하는 mock 런타임이다 — 서로 import하지 않는다(devtools#740이 test-runner
379
+ * 전용 모듈임을 명시).
380
+ */
381
+ /** 코드별 기본 message/moduleName — 실기기 캡처 관측값. */
382
+ const CODE_META = {
383
+ APP_LOGIN: {
384
+ message: "Login failed",
385
+ moduleName: "RNTossLogin"
386
+ },
387
+ PLACEMENT_ID_FETCH_FAILED: {
388
+ message: "Failed to fetch placement id",
389
+ moduleName: "RNAdMob"
390
+ },
391
+ EXECUTION_ERROR: {
392
+ message: "Execution error",
393
+ moduleName: "RNFullScreenAd"
394
+ },
395
+ NO_PERMISSION: {
396
+ message: "No permission",
397
+ moduleName: "RNPermissions"
398
+ },
399
+ INVALID_REQUEST: {
400
+ message: "Invalid request",
401
+ moduleName: "RNPermissions"
402
+ },
403
+ INVALID_DATA: {
404
+ message: "Invalid data",
405
+ moduleName: "RNFileSystem"
406
+ },
407
+ FAILED_TO_GET_LOADED_AD: {
408
+ message: "Failed to get loaded ad",
409
+ moduleName: "RNAdMob"
410
+ },
411
+ APP_BRIDGE_THROTTLED: {
412
+ message: "Too many app bridge calls from this method.",
413
+ moduleName: "RNBridge"
414
+ },
415
+ "1006": {
416
+ message: "광고가 로드 중이거나 준비되지 않았습니다",
417
+ moduleName: "RNFullScreenAd"
418
+ },
419
+ "4000": {
420
+ message: "Notification agreement failed",
421
+ moduleName: "RNNotification"
422
+ }
423
+ };
424
+ /**
425
+ * `aitState.failureModes.sdkLine`이 가리키는 라인으로 네이티브 실패를 조립한다.
426
+ *
427
+ * - `'2.x'`(기본): {@link NativeErrorEnvelope} 필드가 실린 `Error` — 실기기 2.x
428
+ * 캡처(`{name, code, userInfo, moduleName, __isError}`)와 필드 단위 일치.
429
+ * - `'3.x'`: 같은 실패가 "맨 Error"로 평탄화된 것을 재현 — envelope 필드 없이
430
+ * message만 실린 순수 `Error` 인스턴스.
431
+ *
432
+ * 호출부는 두 종류다:
433
+ *
434
+ * - **다이얼 게이트 뒤**(대부분): 프로비저닝처럼 환경에 따라 갈리는 실패는
435
+ * 다이얼이 설정된 경우에만 재현한다 — 미설정 시 기존 동작 무변화
436
+ * (zero behavior change). 호출 전에 다이얼 값을 명시적으로 확인한다.
437
+ * - **무조건**(입력 검증): 빈 `data`나 알 수 없는 haptic type처럼 환경과 무관하게
438
+ * 실기기가 **항상** 거부하는 입력은 다이얼 없이 바로 던진다. 이건 "가끔 일어나는
439
+ * 실패"의 시뮬레이션이 아니라 결정적 계약이므로 opt-in 대상이 아니다.
440
+ */
441
+ function buildNativeError(code) {
442
+ const meta = CODE_META[code];
443
+ if (aitState.state.failureModes.sdkLine === "3.x") return new Error(meta.message);
444
+ const err = new Error(meta.message);
445
+ err.name = "Error";
446
+ err.code = code;
447
+ err.userInfo = {};
448
+ err.moduleName = meta.moduleName;
449
+ err.__isError = true;
450
+ return err;
451
+ }
452
+ //#endregion
453
+ //#region src/mock/permissions.ts
454
+ /**
455
+ * web-framework 3.0+ 권한 에러 기반 클래스.
456
+ * `instanceof PermissionError`로 체크하는 코드와 호환된다.
457
+ */
458
+ var PermissionError = class extends Error {
459
+ constructor({ methodName, message }) {
460
+ super(message ?? `${methodName}: permission denied`);
461
+ this.name = `${methodName}PermissionError`;
462
+ }
463
+ };
464
+ /** openCamera 권한 에러 */
465
+ var OpenCameraPermissionError = class extends PermissionError {
466
+ constructor() {
467
+ super({ methodName: "openCamera" });
468
+ }
469
+ };
470
+ /** fetchAlbumPhotos 권한 에러 */
471
+ var FetchAlbumPhotosPermissionError = class extends PermissionError {
472
+ constructor() {
473
+ super({ methodName: "fetchAlbumPhotos" });
474
+ }
475
+ };
476
+ /** fetchContacts 권한 에러 */
477
+ var FetchContactsPermissionError = class extends PermissionError {
478
+ constructor() {
479
+ super({ methodName: "fetchContacts" });
480
+ }
481
+ };
482
+ /** getCurrentLocation 권한 에러 */
483
+ var GetCurrentLocationPermissionError = class extends PermissionError {
484
+ constructor() {
485
+ super({ methodName: "getCurrentLocation" });
486
+ }
487
+ };
488
+ /** getClipboardText 권한 에러 */
489
+ var GetClipboardTextPermissionError = class extends PermissionError {
490
+ constructor() {
491
+ super({ methodName: "getClipboardText" });
492
+ }
493
+ };
494
+ /** setClipboardText 권한 에러 */
495
+ var SetClipboardTextPermissionError = class extends PermissionError {
496
+ constructor() {
497
+ super({ methodName: "setClipboardText" });
498
+ }
499
+ };
500
+ /**
501
+ * startUpdateLocation 권한 에러.
502
+ * web-framework 3.0에서 GetCurrentLocationPermissionError의 alias.
503
+ */
504
+ const StartUpdateLocationPermissionError = GetCurrentLocationPermissionError;
505
+ const permissionErrorMap = {
506
+ openCamera: OpenCameraPermissionError,
507
+ fetchAlbumPhotos: FetchAlbumPhotosPermissionError,
508
+ fetchAlbumItems: FetchAlbumPhotosPermissionError,
509
+ fetchContacts: FetchContactsPermissionError,
510
+ getCurrentLocation: GetCurrentLocationPermissionError,
511
+ getClipboardText: GetClipboardTextPermissionError,
512
+ setClipboardText: SetClipboardTextPermissionError
513
+ };
514
+ async function getPermission(permission) {
515
+ const failureCode = permissionGateCode(permission);
516
+ if (failureCode) throw buildNativeError(failureCode);
517
+ return aitState.state.permissions[permission.name];
518
+ }
519
+ /**
520
+ * 선언 게이트에 걸리는 이름·access 조합이면 다이얼에 등록된 native errorCode를
521
+ * 돌려준다. 세 권한 API(`getPermission`/`requestPermission`/`openPermissionDialog`)가
522
+ * **걸리는 조건**은 공유하지만 **떨어지는 코드**는 공유하지 않는다 — 아래 참조.
523
+ */
524
+ function permissionGateCode(permission) {
525
+ if (permission.access === "access") return;
526
+ return aitState.state.failureModes.getPermission?.[permission.name];
527
+ }
528
+ /**
529
+ * `openPermissionDialog`가 선언 게이트에 걸렸을 때의 코드.
530
+ *
531
+ * 형제 API와 갈린다 — env3 run11 실측(2.x/iOS, sdk-example#313에서 시나리오 키가
532
+ * 통일되며 비교 대상에 들어온 값):
533
+ *
534
+ * getPermission { geolocation, read } → rejected NO_PERMISSION
535
+ * requestPermission { geolocation, read } → rejected NO_PERMISSION
536
+ * openPermissionDialog { geolocation, read } → rejected INVALID_REQUEST
537
+ * openPermissionDialog { camera, access } → resolved
538
+ *
539
+ * 즉 **걸리는 조건은 셋이 같고(access 축 모델 그대로) 코드만 갈린다.** 처음엔
540
+ * `requestPermission`이 `openPermissionDialog`에 위임하니 게이트도 위임하면
541
+ * 된다고 보고 한 지점에만 배선했는데, 그 모델은 코드 층위에서 실측과 어긋났다
542
+ * (env1 NO_PERMISSION ↔ env3 INVALID_REQUEST). 그래서 `requestPermission`은
543
+ * 위임 **전에** 자기 게이트를 먼저 타고, 다이얼로그를 여는 호출만 이 코드를 쓴다.
544
+ *
545
+ * 관측이 geolocation/read 한 조합뿐이라 이름·access별 분기는 두지 않는다 —
546
+ * 근거 없는 확장은 #783에서 이름 단위 맵을 택한 원칙에 어긋난다.
547
+ */
548
+ const OPEN_DIALOG_GATE_CODE = "INVALID_REQUEST";
549
+ async function openPermissionDialog(permission) {
550
+ if (permissionGateCode(permission)) throw buildNativeError(OPEN_DIALOG_GATE_CODE);
551
+ if (aitState.state.permissions[permission.name] === "allowed") return "allowed";
552
+ aitState.patch("permissions", { [permission.name]: "allowed" });
553
+ return "allowed";
554
+ }
555
+ async function requestPermission(permission) {
556
+ const failureCode = permissionGateCode(permission);
557
+ if (failureCode) throw buildNativeError(failureCode);
558
+ return openPermissionDialog(permission);
559
+ }
560
+ /** 권한이 필요한 함수에 .getPermission(), .openPermissionDialog()를 부착 */
561
+ function withPermission(fn, permissionName) {
562
+ const enhanced = fn;
563
+ enhanced.getPermission = () => getPermission({
564
+ name: permissionName,
565
+ access: "access"
566
+ });
567
+ enhanced.openPermissionDialog = () => openPermissionDialog({
568
+ name: permissionName,
569
+ access: "access"
570
+ });
571
+ return enhanced;
572
+ }
573
+ /**
574
+ * 권한 체크 후 denied면 per-API *PermissionError 서브클래스를 throw한다.
575
+ * 실 3.0 SDK 동작과 일치 — `instanceof PermissionError` 분기가 mock에서도 동작한다 (#372).
576
+ */
577
+ function checkPermission(name, fnName) {
578
+ if (aitState.state.permissions[name] === "denied") {
579
+ const ErrorClass = permissionErrorMap[fnName];
580
+ if (ErrorClass) throw new ErrorClass();
581
+ throw new PermissionError({ methodName: fnName });
582
+ }
583
+ }
584
+ //#endregion
585
+ //#region src/mock/device/camera.ts
586
+ /**
587
+ * Camera & Album Photos & Album Items mock
588
+ * mock/web/prompt 모드 지원
589
+ */
590
+ async function openCameraMock() {
591
+ const images = getMockImages();
592
+ return {
593
+ id: crypto.randomUUID(),
594
+ dataUri: images[0]
595
+ };
596
+ }
597
+ async function openCameraWeb() {
598
+ return new Promise((resolve, reject) => {
599
+ const input = document.createElement("input");
600
+ input.type = "file";
601
+ input.accept = "image/*";
602
+ input.capture = "environment";
603
+ let settled = false;
604
+ input.onchange = () => {
605
+ settled = true;
606
+ const file = input.files?.[0];
607
+ if (!file) {
608
+ reject(/* @__PURE__ */ new Error("No file selected"));
609
+ return;
610
+ }
611
+ const reader = new FileReader();
612
+ reader.onload = () => resolve({
613
+ id: crypto.randomUUID(),
614
+ dataUri: reader.result
615
+ });
616
+ reader.onerror = () => reject(/* @__PURE__ */ new Error("Failed to read file"));
617
+ reader.readAsDataURL(file);
618
+ };
619
+ const onFocus = () => {
620
+ setTimeout(() => {
621
+ if (!settled) reject(/* @__PURE__ */ new Error("File picker cancelled"));
622
+ window.removeEventListener("focus", onFocus);
623
+ }, 300);
624
+ };
625
+ window.addEventListener("focus", onFocus);
626
+ input.click();
627
+ });
628
+ }
629
+ async function openCameraPrompt() {
630
+ const dataUri = await waitForPromptResponse("camera");
631
+ return {
632
+ id: crypto.randomUUID(),
633
+ dataUri
634
+ };
635
+ }
636
+ const _openCamera = async (_options) => {
637
+ checkPermission("camera", "openCamera");
638
+ const mode = aitState.state.deviceModes.camera;
639
+ if (mode === "web") return openCameraWeb();
640
+ if (mode === "prompt") return openCameraPrompt();
641
+ return openCameraMock();
642
+ };
643
+ const openCamera = withPermission(_openCamera, "camera");
644
+ async function fetchAlbumPhotosMock(maxCount) {
645
+ return getMockImages().slice(0, maxCount).map((dataUri) => ({
646
+ id: crypto.randomUUID(),
647
+ dataUri
648
+ }));
649
+ }
650
+ async function fetchAlbumPhotosWeb(maxCount) {
651
+ return new Promise((resolve, reject) => {
652
+ const input = document.createElement("input");
653
+ input.type = "file";
654
+ input.accept = "image/*";
655
+ input.multiple = true;
656
+ let settled = false;
657
+ input.onchange = async () => {
658
+ settled = true;
659
+ const files = Array.from(input.files ?? []).slice(0, maxCount);
660
+ if (files.length === 0) {
661
+ reject(/* @__PURE__ */ new Error("No files selected"));
662
+ return;
663
+ }
664
+ resolve(await Promise.all(files.map((file) => new Promise((res, rej) => {
665
+ const reader = new FileReader();
666
+ reader.onload = () => res({
667
+ id: crypto.randomUUID(),
668
+ dataUri: reader.result
669
+ });
670
+ reader.onerror = () => rej(/* @__PURE__ */ new Error("Failed to read file"));
671
+ reader.readAsDataURL(file);
672
+ }))));
673
+ };
674
+ const onFocus = () => {
675
+ setTimeout(() => {
676
+ if (!settled) reject(/* @__PURE__ */ new Error("File picker cancelled"));
677
+ window.removeEventListener("focus", onFocus);
678
+ }, 300);
679
+ };
680
+ window.addEventListener("focus", onFocus);
681
+ input.click();
682
+ });
683
+ }
684
+ async function fetchAlbumPhotosPrompt(maxCount) {
685
+ return (await waitForPromptResponse("photos")).slice(0, maxCount).map((dataUri) => ({
686
+ id: crypto.randomUUID(),
687
+ dataUri
688
+ }));
689
+ }
690
+ const _fetchAlbumPhotos = async (options) => {
691
+ checkPermission("photos", "fetchAlbumPhotos");
692
+ const maxCount = options?.maxCount ?? 10;
693
+ const mode = aitState.state.deviceModes.photos;
694
+ if (mode === "web") return fetchAlbumPhotosWeb(maxCount);
695
+ if (mode === "prompt") return fetchAlbumPhotosPrompt(maxCount);
696
+ return fetchAlbumPhotosMock(maxCount);
697
+ };
698
+ const fetchAlbumPhotos = withPermission(_fetchAlbumPhotos, "photos");
699
+ async function fetchAlbumItemsMock(maxCount, types) {
700
+ return getMockImages().slice(0, maxCount).filter(() => types.includes("PHOTO")).map((dataUri) => ({
701
+ id: crypto.randomUUID(),
702
+ dataUri,
703
+ type: "PHOTO"
704
+ }));
705
+ }
706
+ async function fetchAlbumItemsWeb(maxCount, types) {
707
+ return new Promise((resolve) => {
708
+ const input = document.createElement("input");
709
+ input.type = "file";
710
+ input.accept = types.includes("VIDEO") ? "image/*,video/*" : "image/*";
711
+ input.multiple = true;
712
+ let settled = false;
713
+ input.onchange = async () => {
714
+ settled = true;
715
+ const files = Array.from(input.files ?? []).slice(0, maxCount);
716
+ if (files.length === 0) {
717
+ resolve([]);
718
+ return;
719
+ }
720
+ resolve(await Promise.all(files.map((file) => new Promise((res, rej) => {
721
+ const itemType = file.type.startsWith("video/") ? "VIDEO" : "PHOTO";
722
+ const reader = new FileReader();
723
+ reader.onload = () => res({
724
+ id: crypto.randomUUID(),
725
+ dataUri: reader.result,
726
+ type: itemType
727
+ });
728
+ reader.onerror = () => rej(/* @__PURE__ */ new Error("Failed to read file"));
729
+ reader.readAsDataURL(file);
730
+ }))));
731
+ };
732
+ const onFocus = () => {
733
+ setTimeout(() => {
734
+ if (!settled) resolve([]);
735
+ window.removeEventListener("focus", onFocus);
736
+ }, 300);
737
+ };
738
+ window.addEventListener("focus", onFocus);
739
+ input.click();
740
+ });
741
+ }
742
+ async function fetchAlbumItemsPrompt(maxCount) {
743
+ return (await waitForPromptResponse("photos")).slice(0, maxCount).map((dataUri) => ({
744
+ id: crypto.randomUUID(),
745
+ dataUri,
746
+ type: "PHOTO"
747
+ }));
748
+ }
749
+ const _fetchAlbumItems = async (options) => {
750
+ checkPermission("photos", "fetchAlbumItems");
751
+ const maxCount = options?.maxCount ?? 10;
752
+ const types = options?.types ?? ["PHOTO"];
753
+ const mode = aitState.state.deviceModes.photos;
754
+ if (mode === "web") return fetchAlbumItemsWeb(maxCount, types);
755
+ if (mode === "prompt") return fetchAlbumItemsPrompt(maxCount);
756
+ return fetchAlbumItemsMock(maxCount, types);
757
+ };
758
+ const _fetchAlbumItemsWithPermission = withPermission(_fetchAlbumItems, "photos");
759
+ const fetchAlbumItems = Object.assign(_fetchAlbumItemsWithPermission, { isSupported: () => true });
760
+ //#endregion
761
+ //#region src/mock/throttle.ts
762
+ /**
763
+ * THROTTLED 시뮬레이션 (devtools#834 — #770 §3 분리).
764
+ *
765
+ * 실기기 네이티브 브리지는 같은 메서드를 짧은 간격으로 연타하면
766
+ * `APP_BRIDGE_THROTTLED`로 거부한다. 이 모듈은 그 per-method rate limit을
767
+ * env1(mock)에서 **opt-in으로** 재현한다 — 다이얼(`failureModes.throttled`)이
768
+ * 미설정이면 아무 일도 하지 않으므로 zero behavior change다.
769
+ *
770
+ * 설계 메모 두 가지:
771
+ *
772
+ * 1. **거부된 호출은 창을 갱신하지 않는다.** 마지막 *허용된* 호출 시각만 기록하므로,
773
+ * `intervalMs` 안에서 계속 연타해도 창이 밀려나지 않고 최초 허용 시점 기준으로
774
+ * 풀린다. 네이티브 rate limit의 통상 동작이자, 연타 중 영원히 막히는(창이 계속
775
+ * 갱신되는) 반-패턴을 피한다.
776
+ * 2. **`observe()`에 걸지 않는다.** `observe()`는 `fn`을 호출하기 *전에* 감싸므로
777
+ * 거기서 throw하면 원래 Promise를 반환하는 API가 **동기 throw**로 바뀐다.
778
+ * `threwSync`는 env1↔env3 동치 diff의 관측 축이라 그 차이가 곧 가짜 불일치가 된다.
779
+ * 그래서 각 mock 구현 본문 안(= 그 API 자신의 sync/async 계약 안)에 수동 삽입한다.
780
+ */
781
+ /**
782
+ * `method`가 지금 throttle에 걸리면 네이티브 실패 에러를 돌려주고, 아니면
783
+ * 호출 시각을 기록한 뒤 `undefined`를 돌려준다.
784
+ *
785
+ * 반환된 에러를 **호출부가 자신의 계약대로** 흘려보낸다 — async mock은 `throw`
786
+ * (= reject), 콜백형 mock은 `onError(err)`. 그래서 이 함수 자체는 던지지 않는다.
787
+ */
788
+ function throttleErrorFor(method) {
789
+ const dial = aitState.state.failureModes.throttled;
790
+ if (!dial?.methods?.includes(method)) return void 0;
791
+ const now = Date.now();
792
+ const prev = lastAllowedAt.get(method);
793
+ if (prev !== void 0 && now - prev < dial.intervalMs) return buildNativeError("APP_BRIDGE_THROTTLED");
794
+ lastAllowedAt.set(method, now);
795
+ }
796
+ /**
797
+ * `throttleErrorFor`의 throw 버전 — async mock 본문에서 한 줄로 쓴다.
798
+ * async 함수 안에서 던지므로 호출자에게는 rejection으로 도달한다.
799
+ */
800
+ function checkThrottle(method) {
801
+ const err = throttleErrorFor(method);
802
+ if (err) throw err;
803
+ }
804
+ //#endregion
805
+ //#region src/mock/device/clipboard.ts
806
+ /**
807
+ * Clipboard mock
808
+ * mock/web 모드 지원
809
+ */
810
+ const _getClipboardText = async () => {
811
+ checkPermission("clipboard", "getClipboardText");
812
+ checkThrottle("getClipboardText");
813
+ if (aitState.state.deviceModes.clipboard === "mock") return aitState.state.mockData.clipboardText;
814
+ try {
815
+ return await navigator.clipboard.readText();
816
+ } catch {
817
+ return "";
818
+ }
819
+ };
820
+ const getClipboardText = withPermission(_getClipboardText, "clipboard");
821
+ const _setClipboardText = async (text) => {
822
+ checkPermission("clipboard", "setClipboardText");
823
+ checkThrottle("setClipboardText");
824
+ if (aitState.state.deviceModes.clipboard === "mock") {
825
+ aitState.patch("mockData", { clipboardText: text });
826
+ return { text };
827
+ }
828
+ await navigator.clipboard.writeText(text);
829
+ return { text };
830
+ };
831
+ const setClipboardText = withPermission(_setClipboardText, "clipboard");
832
+ //#endregion
833
+ //#region src/mock/device/contacts.ts
834
+ const _fetchContacts = async (options) => {
835
+ checkPermission("contacts", "fetchContacts");
836
+ let contacts = aitState.state.contacts;
837
+ if (options.query?.contains) {
838
+ const q = options.query.contains.toLowerCase();
839
+ contacts = contacts.filter((c) => c.name.toLowerCase().includes(q) || c.phoneNumber.includes(q));
840
+ }
841
+ const sliced = contacts.slice(options.offset, options.offset + options.size);
842
+ const nextOffset = options.offset + options.size;
843
+ return {
844
+ result: sliced,
845
+ nextOffset: nextOffset < contacts.length ? nextOffset : null,
846
+ done: nextOffset >= contacts.length
847
+ };
848
+ };
849
+ /**
850
+ * 상류 SDK는 `fetchContacts`의 타입에 `.getPermission`/`.openPermissionDialog`를
851
+ * `PermissionFunctionWithDialog`로 선언하지만, 실기기(2.x×iOS)에는 그 메서드가
852
+ * **런타임에 붙어 있지 않다**(devtools#795 — 호출 시 `fetchContacts.getPermission
853
+ * is not a function` native `TypeError`). standalone `getPermission({name:
854
+ * 'contacts', access: 'access'})`는 실기기에서 정상 resolve하므로(env3 run11,
855
+ * `../permissions.ts`) 부재는 fetchContacts에 **부착된** 메서드에만 해당하는
856
+ * 상류 타입↔런타임 불일치다.
857
+ *
858
+ * mock은 다른 device API처럼 `withPermission()`으로 감싸지 않고, bare async fn을
859
+ * 상류 시그니처로만 캐스트한다 — `.getPermission`/`.openPermissionDialog` 접근은
860
+ * `undefined`가 되고, 호출하면 `undefined()` → native `TypeError`로 떨어져
861
+ * 실기기와 일치한다(`__typecheck.ts`/`__typecheck-2x.ts`는 캐스트 타입에 두
862
+ * 메서드가 여전히 남아 있어 그대로 통과). `_fetchContacts` 내부에서 이미
863
+ * `checkPermission`을 호출하므로 메인 동작(권한 거부 시
864
+ * `FetchContactsPermissionError`)은 변화 없다.
865
+ *
866
+ * 직접 관측된 것은 `getPermission` 부재뿐이다. `openPermissionDialog` 부재는
867
+ * "상류가 fetchContacts에 권한 헬퍼 전체를 붙이지 않는다"는 합리적 추론이지
868
+ * 별도 실측은 아니다. 다른 `withPermission` API(clipboard/camera/location)로는
869
+ * 이 부재를 확장하지 않는다 — 그쪽 부착 메서드가 실기기에서 없다는 관측은
870
+ * 없다(#783 "측정 밖 확장 금지" 원칙).
871
+ */
872
+ const fetchContacts = _fetchContacts;
873
+ //#endregion
874
+ //#region src/mock/device/haptic.ts
875
+ /**
876
+ * Haptic Feedback & saveBase64Data mock
877
+ *
878
+ * generateHapticFeedback — 영역 3 (하드웨어 API 관측):
879
+ * - 10종 HapticFeedbackType을 navigator.vibrate 패턴으로 매핑(근사, best-effort).
880
+ * - `typeof navigator.vibrate === 'function'` 가드 — API 없는 환경에서 throw 없이 skip.
881
+ * - @ait-co/polyfill 동시 사용 시 재귀 방지: polyfill이 navigator.vibrate를 override하고
882
+ * 내부에서 mock의 generateHapticFeedback을 호출하므로 무한 재귀가 발생한다. polyfill이
883
+ * 원본 vibrate를 BACKUP_KEY(Symbol.for('@ait-co/polyfill/vibrate.original'))에 저장하면
884
+ * 그 원본을 직접 호출해 재귀를 끊는다.
885
+ * - sdkCallLog에 🟡(partial)로 기록. params: { hapticType, vibrated: boolean }.
886
+ * - 시그니처 불변 — __typecheck.ts의 Assert<Mock, Original> 통과.
887
+ */
888
+ /**
889
+ * HapticFeedbackType 10종 → navigator.vibrate 패턴 매핑.
890
+ * 숫자: 진동 ms. 배열: [진동, 정지, 진동, …] 교대 패턴.
891
+ */
892
+ const HAPTIC_VIBRATE_PATTERN = {
893
+ tickWeak: 10,
894
+ tap: 20,
895
+ tickMedium: 30,
896
+ softMedium: 40,
897
+ basicWeak: 15,
898
+ basicMedium: 50,
899
+ success: [
900
+ 10,
901
+ 40,
902
+ 10
903
+ ],
904
+ error: [
905
+ 40,
906
+ 30,
907
+ 40
908
+ ],
909
+ wiggle: [
910
+ 20,
911
+ 20,
912
+ 20,
913
+ 20,
914
+ 20
915
+ ],
916
+ confetti: [
917
+ 10,
918
+ 20,
919
+ 10,
920
+ 20,
921
+ 10,
922
+ 20,
923
+ 10
924
+ ]
925
+ };
926
+ /**
927
+ * navigator.vibrate를 안전하게 호출한다.
928
+ *
929
+ * @ait-co/polyfill/auto가 설치된 환경에서는 navigator.vibrate가 polyfill shim으로
930
+ * override되어 있고, 그 shim은 내부적으로 mock의 generateHapticFeedback을 호출한다.
931
+ * mock이 다시 navigator.vibrate(현재 = shim)를 호출하면 무한 재귀가 발생한다.
932
+ * polyfill은 원본 vibrate를 BACKUP_KEY에 저장하므로 그쪽을 직접 호출한다.
933
+ */
934
+ const POLYFILL_VIBRATE_BACKUP = Symbol.for("@ait-co/polyfill/vibrate.original");
935
+ function callVibrate(pattern) {
936
+ if (typeof navigator === "undefined") return false;
937
+ const nav = navigator;
938
+ const original = POLYFILL_VIBRATE_BACKUP in nav ? nav[POLYFILL_VIBRATE_BACKUP] : null;
939
+ if (typeof original === "function") return original(pattern);
940
+ return typeof navigator.vibrate === "function" ? navigator.vibrate(pattern) : false;
941
+ }
942
+ async function generateHapticFeedback(options) {
943
+ if (!Object.hasOwn(HAPTIC_VIBRATE_PATTERN, options.type)) throw buildNativeError("EXECUTION_ERROR");
944
+ const timestamp = Date.now();
945
+ aitState.logAnalytics({
946
+ type: "haptic",
947
+ params: { hapticType: options.type }
948
+ });
949
+ const vibrated = callVibrate(HAPTIC_VIBRATE_PATTERN[options.type] ?? 30);
950
+ aitState.logSdkCall({
951
+ method: "generateHapticFeedback",
952
+ args: [{ type: options.type }],
953
+ timestamp,
954
+ status: "resolved",
955
+ result: {
956
+ hapticType: options.type,
957
+ vibrated
958
+ },
959
+ fidelity: "partial"
960
+ });
961
+ }
962
+ async function saveBase64Data(params) {
963
+ if (params.data === "") throw buildNativeError("INVALID_DATA");
964
+ const a = document.createElement("a");
965
+ a.href = `data:${params.mimeType};base64,${params.data}`;
966
+ a.download = params.fileName;
967
+ a.click();
968
+ }
969
+ //#endregion
970
+ //#region src/mock/device/location.ts
971
+ /**
972
+ * Location mock (getCurrentLocation, startUpdateLocation)
973
+ * mock/web/prompt 모드 지원
974
+ */
975
+ var Accuracy = /* @__PURE__ */ function(Accuracy) {
976
+ Accuracy[Accuracy["Lowest"] = 1] = "Lowest";
977
+ Accuracy[Accuracy["Low"] = 2] = "Low";
978
+ Accuracy[Accuracy["Balanced"] = 3] = "Balanced";
979
+ Accuracy[Accuracy["High"] = 4] = "High";
980
+ Accuracy[Accuracy["Highest"] = 5] = "Highest";
981
+ Accuracy[Accuracy["BestForNavigation"] = 6] = "BestForNavigation";
982
+ return Accuracy;
983
+ }(Accuracy || {});
984
+ function buildLocation() {
985
+ return {
986
+ coords: { ...aitState.state.location.coords },
987
+ timestamp: Date.now(),
988
+ accessLocation: aitState.state.location.accessLocation
989
+ };
990
+ }
991
+ async function getCurrentLocationMock() {
992
+ const { coords, timestamp } = buildLocation();
993
+ return {
994
+ coords,
995
+ timestamp
996
+ };
997
+ }
998
+ async function getCurrentLocationWeb() {
999
+ return new Promise((resolve) => {
1000
+ if (!navigator.geolocation) {
1001
+ console.warn("[@apps-in-toss/devtools] Geolocation API not available, falling back to mock");
1002
+ resolve(buildLocation());
1003
+ return;
1004
+ }
1005
+ navigator.geolocation.getCurrentPosition((pos) => {
1006
+ resolve({
1007
+ coords: {
1008
+ latitude: pos.coords.latitude,
1009
+ longitude: pos.coords.longitude,
1010
+ altitude: pos.coords.altitude ?? 0,
1011
+ accuracy: pos.coords.accuracy,
1012
+ altitudeAccuracy: pos.coords.altitudeAccuracy ?? 0,
1013
+ heading: pos.coords.heading ?? 0
1014
+ },
1015
+ timestamp: pos.timestamp
1016
+ });
1017
+ }, () => {
1018
+ console.warn("[@apps-in-toss/devtools] Geolocation failed, falling back to mock");
1019
+ resolve(buildLocation());
1020
+ });
1021
+ });
1022
+ }
1023
+ async function getCurrentLocationPrompt() {
1024
+ const { coords, timestamp } = await waitForPromptResponse("location");
1025
+ return {
1026
+ coords,
1027
+ timestamp
1028
+ };
1029
+ }
1030
+ const _getCurrentLocation = async (_options) => {
1031
+ checkPermission("geolocation", "getCurrentLocation");
1032
+ checkThrottle("getCurrentLocation");
1033
+ const mode = aitState.state.deviceModes.location;
1034
+ if (mode === "web") return getCurrentLocationWeb();
1035
+ if (mode === "prompt") return getCurrentLocationPrompt();
1036
+ return getCurrentLocationMock();
1037
+ };
1038
+ const getCurrentLocation = withPermission(_getCurrentLocation, "geolocation");
1039
+ function startUpdateLocationMock(eventParams) {
1040
+ const { onEvent, options } = eventParams;
1041
+ const interval = Math.max(options.timeInterval, 500);
1042
+ const id = setInterval(() => {
1043
+ const loc = buildLocation();
1044
+ loc.coords.latitude += (Math.random() - .5) * 1e-4;
1045
+ loc.coords.longitude += (Math.random() - .5) * 1e-4;
1046
+ onEvent(loc);
1047
+ }, interval);
1048
+ return () => clearInterval(id);
1049
+ }
1050
+ function startUpdateLocationWeb(eventParams) {
1051
+ const { onEvent, onError } = eventParams;
1052
+ if (!navigator.geolocation) {
1053
+ console.warn("[@apps-in-toss/devtools] Geolocation API not available, falling back to mock");
1054
+ return startUpdateLocationMock(eventParams);
1055
+ }
1056
+ const watchId = navigator.geolocation.watchPosition((pos) => {
1057
+ onEvent({
1058
+ coords: {
1059
+ latitude: pos.coords.latitude,
1060
+ longitude: pos.coords.longitude,
1061
+ altitude: pos.coords.altitude ?? 0,
1062
+ accuracy: pos.coords.accuracy,
1063
+ altitudeAccuracy: pos.coords.altitudeAccuracy ?? 0,
1064
+ heading: pos.coords.heading ?? 0
1065
+ },
1066
+ timestamp: pos.timestamp,
1067
+ accessLocation: "FINE"
1068
+ });
1069
+ }, (err) => onError(err));
1070
+ return () => navigator.geolocation.clearWatch(watchId);
1071
+ }
1072
+ function startUpdateLocationPrompt(eventParams) {
1073
+ const { onEvent } = eventParams;
1074
+ const handler = (e) => {
1075
+ onEvent(e.detail);
1076
+ };
1077
+ window.addEventListener("__ait:prompt-response:location-update", handler);
1078
+ window.dispatchEvent(new CustomEvent("__ait:prompt-request", { detail: { type: "location-update" } }));
1079
+ return () => window.removeEventListener("__ait:prompt-response:location-update", handler);
1080
+ }
1081
+ const _startUpdateLocation = (eventParams) => {
1082
+ const mode = aitState.state.deviceModes.location;
1083
+ if (mode === "web") return startUpdateLocationWeb(eventParams);
1084
+ if (mode === "prompt") return startUpdateLocationPrompt(eventParams);
1085
+ return startUpdateLocationMock(eventParams);
1086
+ };
1087
+ const startUpdateLocation = withPermission(_startUpdateLocation, "geolocation");
1088
+ //#endregion
1089
+ //#region src/mock/device/network.ts
1090
+ /**
1091
+ * Network Status mock (mode-aware helper)
1092
+ * navigation 모듈에서 사용. circular dep 방지를 위해 device에 위치.
1093
+ */
1094
+ /**
1095
+ * Web mode: uses navigator.connection.effectiveType (4g/3g/2g) and navigator.onLine.
1096
+ * Limitations: WIFI, 5G, WWAN cannot be detected via the Network Information API.
1097
+ * Falls back to state-based value when effectiveType is unavailable.
1098
+ */
1099
+ function getNetworkStatusByMode() {
1100
+ const mode = aitState.state.deviceModes.network;
1101
+ if (mode === "mock") return null;
1102
+ if (mode === "web") {
1103
+ if (!navigator.onLine) return "OFFLINE";
1104
+ const conn = navigator.connection;
1105
+ if (conn?.effectiveType) return {
1106
+ "4g": "4G",
1107
+ "3g": "3G",
1108
+ "2g": "2G",
1109
+ "slow-2g": "2G"
1110
+ }[conn.effectiveType] ?? "UNKNOWN";
1111
+ return aitState.state.networkStatus;
1112
+ }
1113
+ return null;
1114
+ }
1115
+ //#endregion
1116
+ //#region src/mock/device/pdf.ts
1117
+ /**
1118
+ * Base64로 인코딩된 PDF 데이터를 네이티브 PDF 뷰어로 여는 mock.
1119
+ * mock 환경에서는 즉시 `'CLOSE'`를 반환한다.
1120
+ */
1121
+ const _openPDFViewerImpl = async (_params) => {
1122
+ await Promise.resolve();
1123
+ return "CLOSE";
1124
+ };
1125
+ const openPDFViewer = Object.assign(_openPDFViewerImpl, { isSupported: () => true });
1126
+ //#endregion
1127
+ //#region src/mock/proxy.ts
1128
+ /**
1129
+ * 미구현 API용 Proxy 트립와이어.
1130
+ *
1131
+ * 미구현 프로퍼티에 접근하면 throw한다. 이는 "devtools에서는 멀쩡히 돌지만
1132
+ * 실 SDK에선 실제로 동작하는" 시나리오를 차단하기 위한 의도적 선택이다.
1133
+ * mock이 미구현인 API는 실 SDK에서는 존재할 수 있고, 사용자가 이를 인지하지
1134
+ * 못한 채 개발을 이어가면 배포 시점에 놀라게 된다. 에러 메시지에 이슈 URL을
1135
+ * 포함해 사용자가 mock 누락을 제보할 수 있게 한다.
1136
+ *
1137
+ * ## KNOWN_UNIMPLEMENTED 정책
1138
+ * SDK에 존재하는 것으로 알려져 있으나 현재 mock이 없는 API 이름만 이 집합에 둔다.
1139
+ * 이 경우에만 throw 대신 🔴 inert no-op을 반환하고 sdkCallLog에 기록한다.
1140
+ * 완전히 미지의 이름은 여전히 throw — "잘 되는 척" 방지.
1141
+ */
1142
+ const ISSUES_URL = "https://github.com/apps-in-toss-community/devtools/issues";
1143
+ /**
1144
+ * SDK에 존재하나 mock이 아직 없는 것으로 확인된 이름 목록.
1145
+ * 새 API가 SDK에 추가되면 여기에 추가하고 별도 PR에서 mock 구현으로 이동한다.
1146
+ * 확인되지 않은 이름은 절대 여기에 추가하지 않는다 — throw가 더 안전하다.
1147
+ */
1148
+ const KNOWN_UNIMPLEMENTED = /* @__PURE__ */ new Set([]);
1149
+ function createMockProxy(moduleName, implementations) {
1150
+ return new Proxy(implementations, { get(target, prop) {
1151
+ if (typeof prop === "symbol") return void 0;
1152
+ if (prop in target) return target[prop];
1153
+ const name = String(prop);
1154
+ if (KNOWN_UNIMPLEMENTED.has(name)) return (...args) => {
1155
+ console.warn(`[@apps-in-toss/devtools] ${moduleName}.${name} is known-unimplemented (🔴 inert). Returning undefined. Please file or upvote an issue: ${ISSUES_URL}`);
1156
+ aitState.logSdkCall({
1157
+ method: `${moduleName}.${name}`,
1158
+ args,
1159
+ timestamp: Date.now(),
1160
+ status: "resolved",
1161
+ result: void 0,
1162
+ fidelity: "inert"
1163
+ });
1164
+ };
1165
+ throw new Error(`[@apps-in-toss/devtools] ${moduleName}.${prop} is not mocked. This API may exist in @apps-in-toss/web-framework, but devtools' mock does not cover it yet. Please file an issue: ${ISSUES_URL}`);
1166
+ } });
1167
+ }
1168
+ //#endregion
1169
+ //#region src/mock/device/storage.ts
1170
+ /**
1171
+ * Storage mock
1172
+ * localStorage에 `__ait_storage:` prefix로 저장하여 앱 자체 localStorage와 분리
1173
+ *
1174
+ * 실기기(2.x×iOS) capture는 `setItem`/`removeItem`/`clearItems` 세 메서드가
1175
+ * `undefined`가 아니라 `null`로 resolve됨을 보였다(devtools#770). 원본 SDK 타입
1176
+ * 선언은 여전히 `Promise<void>`이므로 시그니처는 그대로 두고, 런타임 반환값만
1177
+ * `null`로 캐스트해 실측과 동치시킨다 — Analytics·setClipboardText와 같은 처리(#775).
1178
+ * `getItem`은 이미 `string | null`이라 실측과 일치하므로 손대지 않는다.
1179
+ */
1180
+ const Storage = createMockProxy("Storage", {
1181
+ getItem: async (key) => {
1182
+ return localStorage.getItem(`__ait_storage:${key}`);
1183
+ },
1184
+ setItem: async (key, value) => {
1185
+ localStorage.setItem(`__ait_storage:${key}`, value);
1186
+ return null;
1187
+ },
1188
+ removeItem: async (key) => {
1189
+ localStorage.removeItem(`__ait_storage:${key}`);
1190
+ return null;
1191
+ },
1192
+ clearItems: async () => {
1193
+ const keys = Object.keys(localStorage).filter((k) => k.startsWith("__ait_storage:"));
1194
+ for (const k of keys) localStorage.removeItem(k);
1195
+ return null;
1196
+ }
1197
+ });
1198
+ //#endregion
1199
+ //#region src/mock/observe.ts
1200
+ /**
1201
+ * fn을 observe로 감싼다.
1202
+ *
1203
+ * @param apiName - 로그에 기록할 SDK 메서드 이름 (예: `'setScreenAwakeMode'`)
1204
+ * @param fidelity - 이 mock의 fidelity grade ('faithful' | 'partial' | 'inert')
1205
+ * @param fn - 실제 mock 구현체. 시그니처를 그대로 통과시킨다.
1206
+ * @returns fn과 동일한 타입의 래퍼 함수
1207
+ */
1208
+ function observe(apiName, fidelity, fn) {
1209
+ return (...args) => {
1210
+ const timestamp = Date.now();
1211
+ const safeArgs = args.map((a) => safeSerialize(a));
1212
+ const result = fn(...args);
1213
+ if (result instanceof Promise) {
1214
+ aitState.logSdkCall({
1215
+ method: apiName,
1216
+ args: safeArgs,
1217
+ timestamp,
1218
+ status: "pending",
1219
+ fidelity
1220
+ });
1221
+ result.then((value) => {
1222
+ aitState.logSdkCall({
1223
+ method: apiName,
1224
+ args: safeArgs,
1225
+ timestamp,
1226
+ status: "resolved",
1227
+ result: safeSerialize(value),
1228
+ fidelity
1229
+ });
1230
+ }, (err) => {
1231
+ aitState.logSdkCall({
1232
+ method: apiName,
1233
+ args: safeArgs,
1234
+ timestamp,
1235
+ status: "rejected",
1236
+ error: err instanceof Error ? err.message : String(err),
1237
+ fidelity
1238
+ });
1239
+ });
1240
+ return result;
1241
+ }
1242
+ aitState.logSdkCall({
1243
+ method: apiName,
1244
+ args: safeArgs,
1245
+ timestamp,
1246
+ status: "resolved",
1247
+ result: safeSerialize(result),
1248
+ fidelity
1249
+ });
1250
+ return result;
1251
+ };
1252
+ }
1253
+ /**
1254
+ * 값을 JSON-safe한 형태로 변환한다.
1255
+ * - null / primitive — 그대로.
1256
+ * - 함수 — `'[Function: name]'` 문자열.
1257
+ * - 기타 객체 — JSON.stringify 실패 시 `'[unserializable]'`.
1258
+ */
1259
+ function safeSerialize(value) {
1260
+ if (value === null || value === void 0) return value;
1261
+ if (typeof value === "function") return `[Function: ${value.name || "anonymous"}]`;
1262
+ if (typeof value !== "object") return value;
1263
+ try {
1264
+ return JSON.parse(JSON.stringify(value));
1265
+ } catch {
1266
+ return "[unserializable]";
1267
+ }
1268
+ }
1269
+ //#endregion
1270
+ //#region src/mock/navigation/index.ts
1271
+ async function closeView() {
1272
+ console.log("[@apps-in-toss/devtools] closeView called");
1273
+ window.history.back();
1274
+ }
1275
+ async function openURL(url) {
1276
+ console.log("[@apps-in-toss/devtools] openURL:", url);
1277
+ window.open(url, "_blank");
1278
+ }
1279
+ async function share(message) {
1280
+ if (navigator.share) {
1281
+ await navigator.share({ text: message.message });
1282
+ return;
1283
+ }
1284
+ console.log("[@apps-in-toss/devtools] share:", message.message);
1285
+ }
1286
+ const URI_SCHEME_PATTERN = /^[a-zA-Z][a-zA-Z0-9+.-]*:/;
1287
+ async function getTossShareLink(path, _ogImageUrl) {
1288
+ if (!URI_SCHEME_PATTERN.test(path)) throw buildNativeError("EXECUTION_ERROR");
1289
+ return `https://toss.im/share/mock${path}`;
1290
+ }
1291
+ async function setIosSwipeGestureEnabled(options) {
1292
+ console.log("[@apps-in-toss/devtools] setIosSwipeGestureEnabled:", options.isEnabled);
1293
+ aitState.patch("navigation", { iosSwipeGestureEnabled: options.isEnabled });
1294
+ }
1295
+ async function setDeviceOrientation(options) {
1296
+ const current = aitState.state.viewport.orientation;
1297
+ if (current === "auto") {
1298
+ console.log("[@apps-in-toss/devtools] setDeviceOrientation:", options.type);
1299
+ aitState.patch("viewport", { appOrientation: options.type });
1300
+ return;
1301
+ }
1302
+ console.warn(`[@apps-in-toss/devtools] setDeviceOrientation(${options.type}) ignored — Panel is forcing "${current}". Change the Viewport tab's orientation to "auto" to let the app control rotation.`);
1303
+ }
1304
+ const setScreenAwakeMode = observe("setScreenAwakeMode", "inert", async (options) => {
1305
+ console.log("[@apps-in-toss/devtools] setScreenAwakeMode:", options.enabled);
1306
+ return { enabled: options.enabled };
1307
+ });
1308
+ const setSecureScreen = observe("setSecureScreen", "inert", async (options) => {
1309
+ console.log("[@apps-in-toss/devtools] setSecureScreen:", options.enabled);
1310
+ return { enabled: options.enabled };
1311
+ });
1312
+ const requestReview = observe("requestReview", "inert", async () => {
1313
+ console.log("[@apps-in-toss/devtools] requestReview called");
1314
+ });
1315
+ requestReview.isSupported = () => true;
1316
+ /**
1317
+ * 아래 6개 함수(`getPlatformOS`/`getOperationalEnvironment`/`isMinVersionSupported`/
1318
+ * `getSchemeUri`/`getLocale`/`getDeviceId`, `getSchemeUri`는 devtools#806) + 이
1319
+ * 파일 최하단의 `getSafeAreaInsets`는 실기기(2.x×iOS) capture에서 전부
1320
+ * **Promise를 반환**함이 확인됐다(devtools#795/#806 — sdk-example type-probe
1321
+ * 실측). 그런데 상류 `.d.ts`는 이 함수들을 전부 **동기**로 선언한다 — 선언과
1322
+ * 런타임이 어긋난 상류 타입 버그다.
1323
+ *
1324
+ * mock은 타입 선언이 아니라 런타임 실측을 재현해야 개발자가 env1(브라우저)에서
1325
+ * 겪는 동작이 env3(실기기)와 같아진다(#775 원칙 — Analytics·setClipboardText·
1326
+ * Storage·getSafeAreaInsets(#770)에 이미 적용). 그래서 시그니처는 상류와 동일하게
1327
+ * 두고(`__typecheck.ts`/`__typecheck-2x.ts`의 `Assert*`가 계속 컴파일되도록)
1328
+ * 반환값만 `Promise.resolve(...)`로 감싸 기존 시그니처로 캐스트한다 — 선언 타입이
1329
+ * 안 바뀌므로 런타임 Promise는 tsc에 보이지 않는다.
1330
+ *
1331
+ * `getTossAppVersion`/`getGroupId`/`getAppsInTossGlobals`/`env.getDeploymentId`는
1332
+ * devtools#806 env3 재캡처에서도 여전히 미측정이다 — environment 테스트가
1333
+ * `getSchemeUri` 단언에서 조기 실패해 뒤 4개 accessor 캡처가 애초에 안 떨어졌다.
1334
+ * 같은 async 축일 가능성은 있으나 관측 전까지 손대지 않는다(#783 "측정 밖 확장
1335
+ * 금지").
1336
+ */
1337
+ function getPlatformOS() {
1338
+ return Promise.resolve(aitState.state.platform);
1339
+ }
1340
+ function getOperationalEnvironment() {
1341
+ return Promise.resolve(aitState.state.environment);
1342
+ }
1343
+ function getTossAppVersion() {
1344
+ return aitState.state.appVersion;
1345
+ }
1346
+ function isMinVersionSupported(minVersions) {
1347
+ const result = computeIsMinVersionSupported(minVersions);
1348
+ return Promise.resolve(result);
1349
+ }
1350
+ function computeIsMinVersionSupported(minVersions) {
1351
+ const required = aitState.state.platform === "ios" ? minVersions.ios : minVersions.android;
1352
+ if (required === "always") return true;
1353
+ if (required === "never") return false;
1354
+ const current = aitState.state.appVersion.split(".").map(Number);
1355
+ const min = required.split(".").map(Number);
1356
+ for (let i = 0; i < 3; i++) {
1357
+ if ((current[i] ?? 0) > (min[i] ?? 0)) return true;
1358
+ if ((current[i] ?? 0) < (min[i] ?? 0)) return false;
1359
+ }
1360
+ return true;
1361
+ }
1362
+ function getSchemeUri() {
1363
+ const result = aitState.state.schemeUri || window.location.pathname;
1364
+ return Promise.resolve(result);
1365
+ }
1366
+ function getLocale() {
1367
+ return Promise.resolve(aitState.state.locale);
1368
+ }
1369
+ function getDeviceId() {
1370
+ return Promise.resolve(aitState.state.deviceId);
1371
+ }
1372
+ function getGroupId() {
1373
+ return aitState.state.groupId;
1374
+ }
1375
+ async function getNetworkStatus() {
1376
+ const modeResult = getNetworkStatusByMode();
1377
+ if (modeResult) return modeResult;
1378
+ return aitState.state.networkStatus;
1379
+ }
1380
+ const _getServerTimeImpl = async () => {
1381
+ return Date.now();
1382
+ };
1383
+ const getServerTime = Object.assign(_getServerTimeImpl, { isSupported: () => true });
1384
+ /**
1385
+ * 현재 backEvent 구독자 수. graniteEvent.addEventListener('backEvent', …)가
1386
+ * 증가시키고, 반환된 cleanup이 감소시킨다. 호스트 back 메시지 처리 시 인터셉트
1387
+ * 여부를 판단하는 데 쓰인다.
1388
+ *
1389
+ * @internal 테스트 및 safe-area-bridge에서만 사용.
1390
+ */
1391
+ let _backEventSubscriberCount = 0;
1392
+ const graniteEvent = { addEventListener(event, { onEvent, onError }) {
1393
+ const handler = () => {
1394
+ try {
1395
+ onEvent();
1396
+ } catch (e) {
1397
+ onError?.(e instanceof Error ? e : new Error(String(e)));
1398
+ }
1399
+ };
1400
+ window.addEventListener(`__ait:${event}`, handler);
1401
+ if (event === "backEvent") _backEventSubscriberCount++;
1402
+ let cleaned = false;
1403
+ return () => {
1404
+ if (cleaned) return;
1405
+ cleaned = true;
1406
+ window.removeEventListener(`__ait:${event}`, handler);
1407
+ if (event === "backEvent") _backEventSubscriberCount--;
1408
+ };
1409
+ } };
1410
+ /**
1411
+ * 호스트 back 내비게이션을 처리한다.
1412
+ *
1413
+ * backEvent 구독자가 1명 이상이면 `window.dispatchEvent(new CustomEvent('__ait:backEvent'))`만
1414
+ * 발사한다 — 미니앱이 back을 가로채는(intercept) 채널이고 실제 토스 호스트와 동일한 시맨틱.
1415
+ * 구독자가 없으면 `history.back()`을 호출해 기본 브라우저 뒤로가기를 수행한다.
1416
+ *
1417
+ * env 1 패널의 back 버튼(`src/panel/viewport.ts` `aitState.trigger('backEvent')`)과
1418
+ * 동일한 경로를 거쳐 back 시맨틱의 단일 소유처를 navigation 모듈에 유지한다.
1419
+ */
1420
+ function dispatchHostBackNavigation() {
1421
+ if (_backEventSubscriberCount > 0) window.dispatchEvent(new CustomEvent("__ait:backEvent"));
1422
+ else history.back();
1423
+ }
1424
+ const appsInTossEvent = { addEventListener(_event, _handlers) {
1425
+ return () => {};
1426
+ } };
1427
+ const tdsEvent = { addEventListener(event, { onEvent }) {
1428
+ const handler = (e) => {
1429
+ const detail = e.detail;
1430
+ onEvent(detail);
1431
+ };
1432
+ window.addEventListener(`__ait:${event}`, handler);
1433
+ return () => window.removeEventListener(`__ait:${event}`, handler);
1434
+ } };
1435
+ /**
1436
+ * @deprecated web-framework 3.0 에서 제거됨. 2.x 소비자 back-compat용으로 유지.
1437
+ */
1438
+ function onVisibilityChangedByTransparentServiceWeb(eventParams) {
1439
+ const handler = () => eventParams.onEvent(!document.hidden);
1440
+ document.addEventListener("visibilitychange", handler);
1441
+ return () => document.removeEventListener("visibilitychange", handler);
1442
+ }
1443
+ const env = { getDeploymentId: () => aitState.state.deploymentId };
1444
+ function getAppsInTossGlobals() {
1445
+ return {
1446
+ deploymentId: aitState.state.deploymentId,
1447
+ brandDisplayName: aitState.state.brand.displayName,
1448
+ brandIcon: aitState.state.brand.icon,
1449
+ brandPrimaryColor: aitState.state.brand.primaryColor
1450
+ };
1451
+ }
1452
+ const SafeAreaInsets = {
1453
+ get: () => ({ ...aitState.state.safeAreaInsets }),
1454
+ subscribe: ({ onEvent }) => {
1455
+ return aitState.subscribe(() => onEvent({ ...aitState.state.safeAreaInsets }));
1456
+ }
1457
+ };
1458
+ /**
1459
+ * @deprecated `SafeAreaInsets.get()`을 쓸 것.
1460
+ *
1461
+ * 상류 SDK의 타입 선언은 `getSafeAreaInsets(): number`지만, 실기기(2.x×iOS)
1462
+ * capture는 이 함수가 숫자가 아니라 `SafeAreaInsets.get()`과 같은 객체
1463
+ * (`{ top, right, bottom, left }`)를 반환함을 보였다(devtools#770 —
1464
+ * `returnType: "object"`, `valueKeys: ["top","right","bottom","left"]`).
1465
+ * 즉 선언과 런타임이 어긋나 있는 상류 타입 버그다. 게다가 그 반환 자체도
1466
+ * 동기가 아니라 **Promise**다(devtools#795 — 위 "환경 정보" 섹션 상단 주석과
1467
+ * 같은 축, type-probe 실측). shape(object)와 sync/async 두 축 모두 선언과
1468
+ * 어긋나 있다.
1469
+ *
1470
+ * mock은 타입 선언이 아니라 **런타임 실측**을 재현해야 개발자가 env1에서 겪는
1471
+ * 동작이 실기기와 같아진다. 그래서 시그니처는 상류와 동일하게 `number`로 두되
1472
+ * (`__typecheck.ts`가 SDK 타입에 대해 계속 컴파일되도록) 반환값만 실측 객체를
1473
+ * `Promise.resolve`로 감싸 캐스트한다 — Analytics·setClipboardText·Storage와
1474
+ * 같은 처리(#775).
1475
+ */
1476
+ function getSafeAreaInsets() {
1477
+ return Promise.resolve({ ...aitState.state.safeAreaInsets });
1478
+ }
1479
+ //#endregion
1480
+ //#region src/mock/safe-area-bridge.ts
1481
+ /**
1482
+ * env-2 postMessage bridges (#484, #510).
1483
+ *
1484
+ * In the AITC Sandbox PWA (env 2) the dev app runs inside the launcher's
1485
+ * full-viewport `<iframe>`. The launcher is the top-level document, so its
1486
+ * `env(safe-area-inset-*)` measurement is the ground truth for the real device
1487
+ * geometry. The framed page's mock would otherwise report a synthetic preset
1488
+ * value (e.g. top=54), which sdk-example then double-pads on top of a viewport
1489
+ * that already starts below the status bar — the env-2 "dead band" defect.
1490
+ *
1491
+ * This module installs receive-half listeners for two message types:
1492
+ *
1493
+ * 1. `ait:safe-area-insets` (#484): the launcher forwards its real env() insets
1494
+ * to the framed page on iframe load and resize/orientationchange. Validates the
1495
+ * envelope and writes real insets into the mock SafeAreaInsets state, firing the
1496
+ * subscribe path (see navigation/index.ts) so apps that subscribe re-read the
1497
+ * corrected values.
1498
+ *
1499
+ * 2. `ait:navigate-back` (#510): the launcher partner bar's `←` button posts this
1500
+ * command to the framed page. The receive half calls `dispatchHostBackNavigation()`
1501
+ * (navigation/index.ts): if backEvent subscribers are present, a `__ait:backEvent`
1502
+ * CustomEvent is dispatched (the mini-app intercept channel, matching the env-1
1503
+ * panel path); otherwise `history.back()` is called. No data other than `type` is
1504
+ * read from or written to the message — shape validation rejects anything that
1505
+ * carries extra fields with the wrong type. Apps that do not install this mock
1506
+ * (older builds) silently ignore the message (natural no-op).
1507
+ *
1508
+ * Origin policy: neither message type carries sensitive data, so we do NOT
1509
+ * restrict by origin — the launcher posts cross-origin from a *.trycloudflare.com
1510
+ * tunnel with targetOrigin '*'. Shape validation is still mandatory: a malformed
1511
+ * or out-of-range message is silently ignored so a stray postMessage can never
1512
+ * corrupt the mock state or trigger spurious navigation.
1513
+ *
1514
+ * Message-driven by design: env 1 (desktop browser, no launcher) never receives
1515
+ * these messages, so the panel preset stays authoritative there with zero special
1516
+ * casing here.
1517
+ */
1518
+ /** The postMessage envelope the launcher posts to the framed dev app (inset forward). */
1519
+ const SAFE_AREA_INSETS_MESSAGE_TYPE = "ait:safe-area-insets";
1520
+ /**
1521
+ * The postMessage command the launcher partner bar's `←` button sends to the
1522
+ * framed dev app (#510). The framed page calls `history.back()` in response.
1523
+ *
1524
+ * Protocol: only `{ type: 'ait:navigate-back' }` is valid. No other fields are
1525
+ * read or acted on — extra fields are silently ignored by the shape guard.
1526
+ * Game variant never sends this message (back button is partner-bar-only).
1527
+ */
1528
+ const NAVIGATE_BACK_MESSAGE_TYPE = "ait:navigate-back";
1529
+ /**
1530
+ * The postMessage envelope the framed mini-app self-reports its webViewType
1531
+ * with (#580). The mini-app knows its own type from the build constant
1532
+ * `__WEB_VIEW_TYPE__` (`granite.config.ts`'s `webViewProps.type`, injected by
1533
+ * the devtools unplugin). The launcher is cross-origin so it cannot read that
1534
+ * constant directly — the mini-app posts it to `window.parent` once so the
1535
+ * launcher (env-2 PWA shell) switches to game mode automatically, with no
1536
+ * manual `?navBarType=game` URL edit.
1537
+ *
1538
+ * Direction: this is the SEND side's contract (posted from inside the iframe by
1539
+ * `@ait-co/debug-console`'s `packages/debug-console/src/attach.ts`). The
1540
+ * launcher's receive half lives in
1541
+ * `e2e/fixture/launcher/Launcher.tsx` and mirrors the same value enum inline,
1542
+ * staying decoupled from the mock package internals — the same pattern the
1543
+ * other launcher message types follow.
1544
+ *
1545
+ * Value enum: only `'partner'` and `'game'` are valid. The SDK's deprecated
1546
+ * `'external'` alias of `partner` (web-framework 2.6.1) is mapped to `'partner'`
1547
+ * at the send site so the wire only ever carries the two shapes the launcher
1548
+ * emulates.
1549
+ */
1550
+ const WEB_VIEW_TYPE_MESSAGE_TYPE = "ait:web-view-type";
1551
+ const MAX_INSET_PX = 200;
1552
+ function isValidInset(value) {
1553
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= MAX_INSET_PX;
1554
+ }
1555
+ /**
1556
+ * Parse + validate a raw postMessage payload into a `SafeAreaInsets`, or return
1557
+ * null when it is not a well-formed `ait:safe-area-insets` message. Pure — unit
1558
+ * tested without a real MessageEvent.
1559
+ */
1560
+ function parseSafeAreaInsetsMessage(data) {
1561
+ if (typeof data !== "object" || data === null) return null;
1562
+ if (data.type !== "ait:safe-area-insets") return null;
1563
+ const insets = data.insets;
1564
+ if (typeof insets !== "object" || insets === null) return null;
1565
+ const { top, bottom, left, right } = insets;
1566
+ if (!isValidInset(top) || !isValidInset(bottom) || !isValidInset(left) || !isValidInset(right)) return null;
1567
+ return {
1568
+ top,
1569
+ bottom,
1570
+ left,
1571
+ right
1572
+ };
1573
+ }
1574
+ /**
1575
+ * Parse + validate a raw postMessage payload into a webViewType value
1576
+ * (`'partner'` | `'game'`), or return `null` when it is not a well-formed
1577
+ * `ait:web-view-type` message (#580). Pure — unit tested without a real
1578
+ * MessageEvent.
1579
+ *
1580
+ * Strict shape guard (the safety boundary for the cross-origin receive path):
1581
+ * the payload must be a non-null object whose `type` is exactly
1582
+ * {@link WEB_VIEW_TYPE_MESSAGE_TYPE} and whose `value` is exactly `'partner'`
1583
+ * or `'game'` (an enum allow-list). Anything else — a foreign type, a missing
1584
+ * or non-string value, the deprecated `'external'` alias, or any other string —
1585
+ * returns `null` so a stray postMessage can never flip the launcher's visual
1586
+ * mode. The send site is responsible for collapsing `'external'` → `'partner'`
1587
+ * before posting; the parser does NOT silently accept it.
1588
+ */
1589
+ function parseWebViewTypeMessage(data) {
1590
+ if (typeof data !== "object" || data === null) return null;
1591
+ if (data.type !== "ait:web-view-type") return null;
1592
+ const value = data.value;
1593
+ if (value === "partner" || value === "game") return value;
1594
+ return null;
1595
+ }
1596
+ /**
1597
+ * Apply forwarded insets to the mock state. Skips the write (and the resulting
1598
+ * subscribe notify) when nothing changed, so repeated identical messages from a
1599
+ * resize storm don't churn subscribers.
1600
+ */
1601
+ function applyForwardedSafeAreaInsets(insets) {
1602
+ const current = aitState.state.safeAreaInsets;
1603
+ if (current.top === insets.top && current.bottom === insets.bottom && current.left === insets.left && current.right === insets.right) return;
1604
+ aitState.update({ safeAreaInsets: insets });
1605
+ }
1606
+ /**
1607
+ * The `id` attribute of the `<style>` element injected by
1608
+ * {@link applyEnv2Compensation}. Using a stable id makes the injection
1609
+ * idempotent — the element is reused on every subsequent message rather than
1610
+ * accumulating multiple `<style>` nodes.
1611
+ */
1612
+ const ENV2_COMPENSATION_STYLE_ID = "ait-env2-safe-area-compensation";
1613
+ /**
1614
+ * The CSS text injected when the launcher forwards partner-mode insets
1615
+ * (`top === 0`). The negative margin pulls the mini-app document up by exactly
1616
+ * the device's status-bar inset so the launcher's own bar offset is not
1617
+ * double-counted.
1618
+ *
1619
+ * jsdom cannot evaluate `env()` expressions, so unit tests assert on this
1620
+ * string literal rather than computed geometry.
1621
+ */
1622
+ const ENV2_COMPENSATION_CSS = "body { margin-top: calc(-1 * env(safe-area-inset-top)); }";
1623
+ /**
1624
+ * Inject or remove the env-2 safe-area compensation `<style>` based on the
1625
+ * forwarded insets received from the launcher.
1626
+ *
1627
+ * ### Why this is needed
1628
+ *
1629
+ * In env 2 (AITC Sandbox PWA) the launcher frames the mini-app in a
1630
+ * full-bleed `<iframe>` whose top edge is already positioned BELOW the
1631
+ * launcher's partner bar (at `env(safe-area-inset-top) + 54px`). However,
1632
+ * inside the cross-origin child iframe, CSS `env(safe-area-inset-top)` STILL
1633
+ * reports the full device status-bar inset (e.g. 62 px on an iPhone 15). Any
1634
+ * mini-app that pads itself by that CSS `env()` value ends up double-counting
1635
+ * the status-bar, producing a ≈62 px white dead-band between the partner bar
1636
+ * and the first content.
1637
+ *
1638
+ * The fix is a compensating negative margin on `body` inside the iframe. When
1639
+ * the launcher forwards partner insets (`top === 0`, meaning "the iframe
1640
+ * already starts below the bar"), we inject:
1641
+ *
1642
+ * ```css
1643
+ * body { margin-top: calc(-1 * env(safe-area-inset-top)); }
1644
+ * ```
1645
+ *
1646
+ * This cancels the duplicate padding without knowing the app's internal DOM
1647
+ * structure. The launcher's html/body `screen.height` force (#527) lives in the
1648
+ * LAUNCHER document (cross-origin) and never touches the mini-app document, so
1649
+ * margin and height force compose independently — they cannot interfere.
1650
+ *
1651
+ * ### Gating
1652
+ *
1653
+ * - **`top === 0`** (partner mode) → install the style (dead-band compensation).
1654
+ * - **`top > 0`** (game mode — launcher forwards the raw device inset for the
1655
+ * floating status-bar capsule) → remove any previously installed style so
1656
+ * `env()` is the authoritative clearance and nothing is double-removed.
1657
+ *
1658
+ * On every insets message (resize/orientation re-post) this function is called
1659
+ * so the gate is re-evaluated and the style toggled accordingly.
1660
+ *
1661
+ * ### env 1 / env 3 / env 4 no-regression
1662
+ *
1663
+ * This function is only called from the `message` listener installed by
1664
+ * {@link installSafeAreaInsetsBridge}. That listener fires only when the
1665
+ * launcher posts an `ait:safe-area-insets` message, which never happens in
1666
+ * env 1 (desktop browser, no launcher), env 3 (intoss-private WebView), or
1667
+ * env 4 (live relay) — they have no launcher posting to the iframe.
1668
+ *
1669
+ * @param top - The forwarded `top` inset value from the launcher message.
1670
+ */
1671
+ function applyEnv2Compensation(top) {
1672
+ if (typeof document === "undefined") return;
1673
+ const existing = document.getElementById(ENV2_COMPENSATION_STYLE_ID);
1674
+ if (top === 0) {
1675
+ if (existing !== null) return;
1676
+ const style = document.createElement("style");
1677
+ style.id = ENV2_COMPENSATION_STYLE_ID;
1678
+ style.textContent = ENV2_COMPENSATION_CSS;
1679
+ document.head.appendChild(style);
1680
+ } else if (existing !== null) existing.remove();
1681
+ }
1682
+ let installed = false;
1683
+ /**
1684
+ * Install the window `message` listener that receives forwarded insets. Safe to
1685
+ * call multiple times (idempotent) and a no-op outside a browser (SSR/jsdom
1686
+ * without a window). Imported for its side effect by the mock barrel so any
1687
+ * consumer that aliases `@apps-in-toss/web-framework` to the mock gets it wired.
1688
+ *
1689
+ * On each valid `ait:safe-area-insets` message:
1690
+ * 1. Writes the corrected insets into the mock `SafeAreaInsets` state
1691
+ * (existing #484 behaviour — for apps that still read SDK insets).
1692
+ * 2. Drives the env-2 dead-band compensation style via
1693
+ * {@link applyEnv2Compensation} — injects a `body { margin-top: calc(-1 *
1694
+ * env(safe-area-inset-top)) }` style when `top === 0` (partner mode) and
1695
+ * removes it when `top > 0` (game / full-bleed mode).
1696
+ */
1697
+ function installSafeAreaInsetsBridge() {
1698
+ if (installed || typeof window === "undefined") return;
1699
+ installed = true;
1700
+ window.addEventListener("message", (event) => {
1701
+ const insets = parseSafeAreaInsetsMessage(event.data);
1702
+ if (insets) {
1703
+ applyForwardedSafeAreaInsets(insets);
1704
+ applyEnv2Compensation(insets.top);
1705
+ }
1706
+ });
1707
+ }
1708
+ /**
1709
+ * Parse a raw postMessage payload as an `ait:navigate-back` command.
1710
+ *
1711
+ * Returns true when the payload is a well-formed navigate-back command
1712
+ * (`{ type: 'ait:navigate-back' }`), false otherwise. Pure — unit tested
1713
+ * without a real MessageEvent.
1714
+ *
1715
+ * Shape guard: only the `type` field is inspected; any extra fields are
1716
+ * ignored so future extensions do not break older receivers. The function
1717
+ * does NOT read any data field beyond `type` — no sensitive values, no host
1718
+ * disclosure (same principle as the insets bridge).
1719
+ */
1720
+ function isNavigateBackMessage(data) {
1721
+ if (typeof data !== "object" || data === null) return false;
1722
+ return data.type === NAVIGATE_BACK_MESSAGE_TYPE;
1723
+ }
1724
+ let navigateBackInstalled = false;
1725
+ /**
1726
+ * Install the window `message` listener that handles `ait:navigate-back`
1727
+ * commands (#510). When the launcher partner bar's `←` button is clicked it
1728
+ * posts `{ type: 'ait:navigate-back' }` to the framed dev app; this listener
1729
+ * calls `dispatchHostBackNavigation()` from the navigation module.
1730
+ *
1731
+ * Dispatch semantics: if there are any `graniteEvent.addEventListener('backEvent', …)`
1732
+ * subscribers the CustomEvent `__ait:backEvent` is fired (same path as the env-1
1733
+ * panel back button — the mini-app intercept channel). When there are no
1734
+ * subscribers `history.back()` is called as the fallback. Back semantics are
1735
+ * owned entirely by the navigation module; this bridge only delegates.
1736
+ *
1737
+ * Safe to call multiple times (idempotent) and a no-op outside a browser.
1738
+ * Installed together with the inset bridge by `installBridges()` so any consumer
1739
+ * of the mock barrel gets both wired automatically.
1740
+ *
1741
+ * No-op on apps that predate this bridge — the launcher posts the message but
1742
+ * older mocks simply have no listener (harmless).
1743
+ */
1744
+ function installNavigateBackBridge() {
1745
+ if (navigateBackInstalled || typeof window === "undefined") return;
1746
+ navigateBackInstalled = true;
1747
+ window.addEventListener("message", (event) => {
1748
+ if (isNavigateBackMessage(event.data)) dispatchHostBackNavigation();
1749
+ });
1750
+ }
1751
+ /**
1752
+ * Install both env-2 postMessage bridges in one call (#484 insets + #510
1753
+ * navigate-back). The mock barrel calls this at import time so consumers get
1754
+ * all bridges wired without any explicit setup.
1755
+ */
1756
+ function installBridges() {
1757
+ installSafeAreaInsetsBridge();
1758
+ installNavigateBackBridge();
1759
+ }
1760
+ //#endregion
1761
+ //#region src/mock/ads/index.ts
1762
+ function withIsSupported(fn) {
1763
+ fn.isSupported = () => true;
1764
+ return fn;
1765
+ }
1766
+ const _slotRegistry = /* @__PURE__ */ new Map();
1767
+ let _slotCounter = 0;
1768
+ function _nextSlotId(adGroupId) {
1769
+ _slotCounter += 1;
1770
+ return `mock-slot-${adGroupId}-${_slotCounter}`;
1771
+ }
1772
+ const GoogleAdMob = createMockProxy("GoogleAdMob", {
1773
+ loadAppsInTossAdMob: withIsSupported(observe("GoogleAdMob.loadAppsInTossAdMob", "faithful", (args) => {
1774
+ setTimeout(() => {
1775
+ const failureCode = aitState.state.failureModes.loadAdMob;
1776
+ if (failureCode) {
1777
+ args.onError(buildNativeError(failureCode));
1778
+ return;
1779
+ }
1780
+ const throttleError = throttleErrorFor("loadAppsInTossAdMob");
1781
+ if (throttleError) {
1782
+ args.onError(throttleError);
1783
+ return;
1784
+ }
1785
+ if (aitState.state.ads.forceNoFill) {
1786
+ args.onError(/* @__PURE__ */ new Error("No fill"));
1787
+ return;
1788
+ }
1789
+ aitState.patch("ads", { isLoaded: true });
1790
+ args.onEvent({
1791
+ type: "loaded",
1792
+ data: {
1793
+ adGroupId: args.options.adGroupId,
1794
+ adUnitId: `mock-unit-${args.options.adGroupId}`,
1795
+ responseInfo: {
1796
+ responseId: `mock-response-${args.options.adGroupId}`,
1797
+ adNetworkInfoArray: [],
1798
+ loadedAdNetworkInfo: null
1799
+ }
1800
+ }
1801
+ });
1802
+ }, 200);
1803
+ return () => {};
1804
+ })),
1805
+ showAppsInTossAdMob: withIsSupported(observe("GoogleAdMob.showAppsInTossAdMob", "faithful", (args) => {
1806
+ if (!aitState.state.ads.isLoaded) {
1807
+ args.onError(/* @__PURE__ */ new Error("Ad not loaded"));
1808
+ return () => {};
1809
+ }
1810
+ const { rewardUnitType, rewardAmount } = aitState.state.ads;
1811
+ setTimeout(() => args.onEvent({
1812
+ type: "userEarnedReward",
1813
+ data: {
1814
+ unitType: rewardUnitType,
1815
+ unitAmount: rewardAmount
1816
+ }
1817
+ }), 1e3);
1818
+ setTimeout(() => {
1819
+ args.onEvent({ type: "dismissed" });
1820
+ aitState.patch("ads", { isLoaded: false });
1821
+ }, 1500);
1822
+ return () => {};
1823
+ })),
1824
+ isAppsInTossAdMobLoaded: withIsSupported(observe("GoogleAdMob.isAppsInTossAdMobLoaded", "faithful", async (_options) => {
1825
+ if (_options?.adGroupId !== void 0 && _options.adGroupId.trim() === "") throw buildNativeError("INVALID_REQUEST");
1826
+ return aitState.state.ads.isLoaded;
1827
+ }))
1828
+ });
1829
+ const TossAds = createMockProxy("TossAds", {
1830
+ initialize: withIsSupported(observe("TossAds.initialize", "partial", (options) => {
1831
+ if (aitState.state.ads.forceNoFill) {
1832
+ options.callbacks?.onInitializationFailed?.(/* @__PURE__ */ new Error("No fill"));
1833
+ return;
1834
+ }
1835
+ options.callbacks?.onInitialized?.();
1836
+ })),
1837
+ attach: withIsSupported(observe("TossAds.attach", "partial", (_adGroupId, target, _options) => {
1838
+ const el = typeof target === "string" ? document.querySelector(target) : target;
1839
+ if (el) {
1840
+ const placeholder = document.createElement("div");
1841
+ placeholder.style.cssText = "background:#f0f0f0;border:1px dashed #999;padding:16px;text-align:center;color:#666;font-size:14px;";
1842
+ placeholder.textContent = "[@apps-in-toss/devtools] TossAds Placeholder";
1843
+ el.appendChild(placeholder);
1844
+ }
1845
+ })),
1846
+ attachBanner: withIsSupported(observe("TossAds.attachBanner", "faithful", (adGroupId, target, options) => {
1847
+ const el = typeof target === "string" ? document.querySelector(target) : target;
1848
+ const slotId = _nextSlotId(adGroupId);
1849
+ const placeholder = document.createElement("div");
1850
+ const theme = options?.theme ?? "auto";
1851
+ const variant = options?.variant ?? "card";
1852
+ const isDark = theme === "dark" || theme === "auto" && typeof window !== "undefined" && window.matchMedia?.("(prefers-color-scheme: dark)").matches;
1853
+ const bg = isDark ? "#1a1a1a" : "#f0f0f0";
1854
+ const textColor = isDark ? "#aaa" : "#666";
1855
+ const borderColor = isDark ? "#555" : "#999";
1856
+ const height = variant === "expanded" ? "120px" : "60px";
1857
+ placeholder.dataset.aitSlotId = slotId;
1858
+ placeholder.style.cssText = `background:${bg};border:1px dashed ${borderColor};padding:8px 12px;text-align:center;color:${textColor};font-size:12px;min-height:${height};display:flex;align-items:center;justify-content:center;`;
1859
+ placeholder.textContent = `[@apps-in-toss/devtools] Banner Ad (${variant})`;
1860
+ if (el) {
1861
+ el.appendChild(placeholder);
1862
+ _slotRegistry.set(slotId, placeholder);
1863
+ }
1864
+ const destroySlot = () => {
1865
+ const registered = _slotRegistry.get(slotId);
1866
+ if (registered) {
1867
+ registered.remove();
1868
+ _slotRegistry.delete(slotId);
1869
+ }
1870
+ };
1871
+ setTimeout(() => {
1872
+ if (aitState.state.ads.forceNoFill) {
1873
+ options?.callbacks?.onNoFill?.({
1874
+ slotId,
1875
+ adGroupId,
1876
+ adMetadata: {}
1877
+ });
1878
+ options?.callbacks?.onAdFailedToRender?.({
1879
+ slotId,
1880
+ adGroupId,
1881
+ adMetadata: {},
1882
+ error: {
1883
+ code: 0,
1884
+ message: "No fill"
1885
+ }
1886
+ });
1887
+ return;
1888
+ }
1889
+ const eventPayload = {
1890
+ slotId,
1891
+ adGroupId,
1892
+ adMetadata: {
1893
+ creativeId: `mock-creative-${slotId}`,
1894
+ requestId: `mock-req-${slotId}`
1895
+ }
1896
+ };
1897
+ options?.callbacks?.onAdRendered?.(eventPayload);
1898
+ options?.callbacks?.onAdImpression?.(eventPayload);
1899
+ }, 100);
1900
+ return { destroy: destroySlot };
1901
+ })),
1902
+ destroy: withIsSupported(observe("TossAds.destroy", "faithful", (slotId) => {
1903
+ const el = _slotRegistry.get(slotId);
1904
+ if (el) {
1905
+ el.remove();
1906
+ _slotRegistry.delete(slotId);
1907
+ }
1908
+ })),
1909
+ destroyAll: withIsSupported(observe("TossAds.destroyAll", "faithful", () => {
1910
+ for (const el of _slotRegistry.values()) el.remove();
1911
+ _slotRegistry.clear();
1912
+ }))
1913
+ });
1914
+ const loadFullScreenAd = observe("loadFullScreenAd", "faithful", (args) => {
1915
+ setTimeout(() => {
1916
+ const failureCode = aitState.state.failureModes.loadFullScreenAd;
1917
+ if (failureCode) {
1918
+ args.onError(buildNativeError(failureCode));
1919
+ return;
1920
+ }
1921
+ const throttleError = throttleErrorFor("loadFullScreenAd");
1922
+ if (throttleError) {
1923
+ args.onError(throttleError);
1924
+ return;
1925
+ }
1926
+ if (aitState.state.ads.forceNoFill) {
1927
+ args.onError(/* @__PURE__ */ new Error("No fill"));
1928
+ return;
1929
+ }
1930
+ aitState.patch("ads", { isLoaded: true });
1931
+ args.onEvent({ type: "loaded" });
1932
+ }, 200);
1933
+ return () => {};
1934
+ });
1935
+ const showFullScreenAd = withIsSupported(observe("showFullScreenAd", "faithful", (args) => {
1936
+ if (!aitState.state.ads.isLoaded) {
1937
+ args.onError(/* @__PURE__ */ new Error("Ad not loaded"));
1938
+ return () => {};
1939
+ }
1940
+ setTimeout(() => args.onEvent({ type: "clicked" }), 100);
1941
+ setTimeout(() => args.onEvent({ type: "dismissed" }), 1500);
1942
+ return () => {};
1943
+ }));
1944
+ //#endregion
1945
+ //#region src/mock/analytics/index.ts
1946
+ /**
1947
+ * Analytics mock
1948
+ */
1949
+ const Analytics = {
1950
+ screen: (params) => {
1951
+ aitState.logAnalytics({
1952
+ type: "screen",
1953
+ params: params ?? {}
1954
+ });
1955
+ return Promise.resolve(null);
1956
+ },
1957
+ impression: (params) => {
1958
+ aitState.logAnalytics({
1959
+ type: "impression",
1960
+ params: params ?? {}
1961
+ });
1962
+ return Promise.resolve(null);
1963
+ },
1964
+ click: (params) => {
1965
+ aitState.logAnalytics({
1966
+ type: "click",
1967
+ params: params ?? {}
1968
+ });
1969
+ return Promise.resolve(null);
1970
+ }
1971
+ };
1972
+ async function eventLog(params) {
1973
+ aitState.logAnalytics({
1974
+ type: params.log_type,
1975
+ params: {
1976
+ log_name: params.log_name,
1977
+ ...params.params
1978
+ }
1979
+ });
1980
+ return null;
1981
+ }
1982
+ //#endregion
1983
+ //#region src/mock/auth/index.ts
1984
+ /**
1985
+ * 인증/로그인 mock
1986
+ */
1987
+ async function appLogin() {
1988
+ const failureCode = aitState.state.failureModes.appLogin;
1989
+ if (failureCode) throw buildNativeError(failureCode);
1990
+ return {
1991
+ authorizationCode: `mock-auth-${crypto.randomUUID()}`,
1992
+ referrer: aitState.state.environment === "toss" ? "DEFAULT" : "SANDBOX"
1993
+ };
1994
+ }
1995
+ async function getIsTossLoginIntegratedService() {
1996
+ const failureCode = aitState.state.failureModes.getIsTossLoginIntegratedService;
1997
+ if (failureCode) throw buildNativeError(failureCode);
1998
+ return aitState.state.auth.isTossLoginIntegrated;
1999
+ }
2000
+ async function getUserKeyForGame() {
2001
+ return {
2002
+ hash: aitState.state.auth.userKeyHash ?? "",
2003
+ type: "HASH"
2004
+ };
2005
+ }
2006
+ async function getAnonymousKey() {
2007
+ if (!aitState.state.auth.anonymousKeyHash) return void 0;
2008
+ return {
2009
+ hash: aitState.state.auth.anonymousKeyHash,
2010
+ type: "HASH"
2011
+ };
2012
+ }
2013
+ async function appsInTossSignTossCert(_params) {
2014
+ console.log("[@apps-in-toss/devtools] appsInTossSignTossCert called (no-op in mock)");
2015
+ }
2016
+ /**
2017
+ * 사용자 동의 기반 데이터 mock (devtools#798 — env1에 배선 부재였던 실 export).
2018
+ *
2019
+ * SDK는 이 API를 `@apps-in-toss/web-bridge` 호환 export로 노출하며 2.x와 3.x GA
2020
+ * 양쪽 facade에서 제공한다. 선언 시그니처는 `Promise<Partial<Record<ConsentedUserDataKey, string>>
2021
+ * | undefined>` — appLogin과 같은 async bridge 모양이라 항상 resolve하는 낙관적
2022
+ * 패턴을 따른다.
2023
+ *
2024
+ * 어떤 키가 채워지는지는 콘솔에 등록된 동의문/데이터 묶음(`consentedUserDataKey`)에
2025
+ * 달려 있고 그 매핑은 서버 쪽 설정이라 mock이 알 수 없다 — 호출 파라미터와 무관하게
2026
+ * 상태에 저장된 최소 plausible 객체를 그대로 resolve한다. SDK 선언 밖의 필드는
2027
+ * 추가하지 않는다(devtools#783 — 실측/타입 밖 추정 금지).
2028
+ */
2029
+ async function getConsentedUserData(_options) {
2030
+ return aitState.state.auth.consentedUserData;
2031
+ }
2032
+ async function getDeclaredAgeRange(_params) {
2033
+ return { ...aitState.state.auth.declaredAgeRange };
2034
+ }
2035
+ //#endregion
2036
+ //#region src/mock/iap/index.ts
2037
+ let orderCounter = 0;
2038
+ function generateOrderId() {
2039
+ return `mock-order-${++orderCounter}-${Date.now()}`;
2040
+ }
2041
+ function buildOrderResult(sku) {
2042
+ const product = aitState.state.iap.products.find((p) => p.sku === sku);
2043
+ const amountStr = product?.displayAmount?.replace(/[^0-9]/g, "") ?? "1000";
2044
+ return {
2045
+ orderId: generateOrderId(),
2046
+ displayName: product?.displayName ?? "Mock Product",
2047
+ displayAmount: product?.displayAmount ?? "1,000원",
2048
+ amount: parseInt(amountStr, 10) || 1e3,
2049
+ currency: "KRW",
2050
+ fraction: 0,
2051
+ miniAppIconUrl: product?.iconUrl || null
2052
+ };
2053
+ }
2054
+ async function handlePurchase(sku, processProductGrant, onEvent, onError) {
2055
+ const nextResult = aitState.state.iap.nextResult;
2056
+ await new Promise((r) => setTimeout(r, 300));
2057
+ if (nextResult !== "success") {
2058
+ onError({ code: nextResult });
2059
+ return;
2060
+ }
2061
+ const result = buildOrderResult(sku);
2062
+ try {
2063
+ if (!await processProductGrant({ orderId: result.orderId })) {
2064
+ onError({ code: "PRODUCT_NOT_GRANTED_BY_PARTNER" });
2065
+ return;
2066
+ }
2067
+ } catch (e) {
2068
+ onError(e);
2069
+ return;
2070
+ }
2071
+ aitState.patch("iap", { completedOrders: [...aitState.state.iap.completedOrders, {
2072
+ orderId: result.orderId,
2073
+ sku,
2074
+ status: "COMPLETED",
2075
+ date: (/* @__PURE__ */ new Date()).toISOString()
2076
+ }] });
2077
+ await onEvent({
2078
+ type: "success",
2079
+ data: result
2080
+ });
2081
+ }
2082
+ const IAP = createMockProxy("IAP", {
2083
+ createOneTimePurchaseOrder(params) {
2084
+ handlePurchase(params.options.sku ?? params.options.productId ?? "", params.options.processProductGrant, params.onEvent, params.onError).catch((e) => console.error("[@apps-in-toss/devtools] IAP unexpected error:", e));
2085
+ return () => {};
2086
+ },
2087
+ createSubscriptionPurchaseOrder(params) {
2088
+ handlePurchase(params.options.sku, params.options.processProductGrant, params.onEvent, params.onError).catch((e) => console.error("[@apps-in-toss/devtools] IAP unexpected error:", e));
2089
+ return () => {};
2090
+ },
2091
+ async getProductItemList() {
2092
+ return { products: aitState.state.iap.products.map((p) => ({
2093
+ ...p,
2094
+ ...p.type === "SUBSCRIPTION" ? { renewalCycle: p.renewalCycle ?? "MONTHLY" } : {}
2095
+ })) };
2096
+ },
2097
+ async getPendingOrders() {
2098
+ const orders = [...aitState.state.iap.pendingOrders];
2099
+ return {
2100
+ orders,
2101
+ orderIds: orders.map((o) => o.orderId)
2102
+ };
2103
+ },
2104
+ async getCompletedOrRefundedOrders() {
2105
+ return {
2106
+ hasNext: false,
2107
+ orders: [...aitState.state.iap.completedOrders]
2108
+ };
2109
+ },
2110
+ async completeProductGrant(args) {
2111
+ const idx = aitState.state.iap.pendingOrders.findIndex((o) => o.orderId === args.params.orderId);
2112
+ if (idx !== -1) {
2113
+ const order = aitState.state.iap.pendingOrders[idx];
2114
+ const pendingOrders = aitState.state.iap.pendingOrders.filter((_, i) => i !== idx);
2115
+ const completedOrders = [...aitState.state.iap.completedOrders, {
2116
+ orderId: order.orderId,
2117
+ sku: order.sku,
2118
+ status: "COMPLETED",
2119
+ date: (/* @__PURE__ */ new Date()).toISOString()
2120
+ }];
2121
+ aitState.patch("iap", {
2122
+ pendingOrders,
2123
+ completedOrders
2124
+ });
2125
+ }
2126
+ return true;
2127
+ },
2128
+ async getSubscriptionInfo(_args) {
2129
+ if (aitState.state.failureModes.softResolve?.getSubscriptionInfo) return {};
2130
+ return { subscription: {
2131
+ catalogId: 1,
2132
+ status: "ACTIVE",
2133
+ expiresAt: new Date(Date.now() + 720 * 60 * 60 * 1e3).toISOString(),
2134
+ isAutoRenew: true,
2135
+ gracePeriodExpiresAt: null,
2136
+ isAccessible: true
2137
+ } };
2138
+ }
2139
+ });
2140
+ async function checkoutPayment(options) {
2141
+ const { nextResult, failReason } = aitState.state.payment;
2142
+ console.log("[@apps-in-toss/devtools] checkoutPayment:", options.params.payToken);
2143
+ if (aitState.state.failureModes.softResolve?.checkoutPayment) return {
2144
+ false: "PAYMENT_FAILED",
2145
+ reason: "mock soft-resolve (env3 unprovisioned)"
2146
+ };
2147
+ await new Promise((r) => setTimeout(r, 300));
2148
+ if (nextResult === "success") return { success: true };
2149
+ return {
2150
+ success: false,
2151
+ reason: failReason || "Mock payment failed"
2152
+ };
2153
+ }
2154
+ const requestTossPayPaysBilling = Object.assign(async function requestTossPayPaysBilling(options) {
2155
+ const { nextResult, failReason } = aitState.state.payment;
2156
+ console.log("[@apps-in-toss/devtools] requestTossPayPaysBilling:", options.params.wrappedToken);
2157
+ if (aitState.state.failureModes.softResolve?.requestTossPayPaysBilling) return {
2158
+ false: "BILLING_FAILED",
2159
+ reason: "mock soft-resolve (env3 unprovisioned)"
2160
+ };
2161
+ await new Promise((r) => setTimeout(r, 300));
2162
+ if (nextResult === "success") return { success: true };
2163
+ return {
2164
+ success: false,
2165
+ reason: failReason || "Mock billing auth failed"
2166
+ };
2167
+ }, { isSupported: () => true });
2168
+ //#endregion
2169
+ //#region src/mock/bridge-factories.ts
2170
+ /** Generic bridge escape hatches exposed by both SDK lines. */
2171
+ const asyncBridgeRegistry = {
2172
+ appLogin,
2173
+ appsInTossSignTossCert,
2174
+ checkoutPayment,
2175
+ closeView,
2176
+ generateHapticFeedback,
2177
+ getAnonymousKey,
2178
+ getConsentedUserData,
2179
+ getDeclaredAgeRange,
2180
+ getNetworkStatus,
2181
+ getServerTime,
2182
+ openPDFViewer,
2183
+ openURL,
2184
+ requestReview,
2185
+ saveBase64Data,
2186
+ share
2187
+ };
2188
+ function createAsyncBridge(method) {
2189
+ return async (...args) => {
2190
+ const fn = asyncBridgeRegistry[method];
2191
+ if (!fn) throw new Error(`@apps-in-toss/devtools: unknown async bridge method "${method}"`);
2192
+ return await fn(...args);
2193
+ };
2194
+ }
2195
+ function createConstantBridge(name) {
2196
+ return () => {
2197
+ const constants = {
2198
+ deploymentId: aitState.state.deploymentId,
2199
+ deviceId: aitState.state.deviceId,
2200
+ groupId: aitState.state.groupId,
2201
+ locale: aitState.state.locale,
2202
+ operationalEnvironment: aitState.state.environment,
2203
+ platformOS: aitState.state.platform,
2204
+ safeAreaInsets: { ...aitState.state.safeAreaInsets },
2205
+ schemeUri: aitState.state.schemeUri || window.location.pathname,
2206
+ tossAppVersion: aitState.state.appVersion
2207
+ };
2208
+ if (!(name in constants)) throw new Error(`@apps-in-toss/devtools: unknown constant bridge "${name}"`);
2209
+ return constants[name];
2210
+ };
2211
+ }
2212
+ function createEventBridge(method) {
2213
+ return (args) => {
2214
+ if (method === "onVisibilityChangedByTransparentServiceWeb") return onVisibilityChangedByTransparentServiceWeb({
2215
+ options: args.options,
2216
+ onEvent: args.onEvent,
2217
+ onError: (error) => args.onError(error instanceof Error ? error : new Error(String(error)))
2218
+ });
2219
+ throw new Error(`@apps-in-toss/devtools: unknown event bridge method "${method}"`);
2220
+ };
2221
+ }
2222
+ //#endregion
2223
+ //#region src/mock/game/index.ts
2224
+ async function grantPromotionReward(params) {
2225
+ console.log("[@apps-in-toss/devtools] grantPromotionReward:", params.params);
2226
+ if (aitState.state.failureModes.softResolve?.grantPromotionReward) return {
2227
+ errorCode: "PROMOTION_NOT_FOUND",
2228
+ message: "no active promotion"
2229
+ };
2230
+ return { key: `mock-reward-${Date.now()}` };
2231
+ }
2232
+ async function grantPromotionRewardForGame(params) {
2233
+ console.log("[@apps-in-toss/devtools] grantPromotionRewardForGame:", params.params);
2234
+ if (aitState.state.failureModes.softResolve?.grantPromotionRewardForGame) return {
2235
+ errorCode: "PROMOTION_NOT_FOUND",
2236
+ message: "no active promotion"
2237
+ };
2238
+ return { key: `mock-reward-${Date.now()}` };
2239
+ }
2240
+ async function submitGameCenterLeaderBoardScore(params) {
2241
+ aitState.patch("game", { leaderboardScores: [...aitState.state.game.leaderboardScores, {
2242
+ score: params.score,
2243
+ timestamp: Date.now()
2244
+ }] });
2245
+ return { statusCode: "SUCCESS" };
2246
+ }
2247
+ async function getGameCenterGameProfile() {
2248
+ const profile = aitState.state.game.profile;
2249
+ if (!profile) return { statusCode: "PROFILE_NOT_FOUND" };
2250
+ return {
2251
+ statusCode: "SUCCESS",
2252
+ gameSessionId: "mock-session",
2253
+ nickname: profile.nickname,
2254
+ profileImageUri: profile.profileImageUri
2255
+ };
2256
+ }
2257
+ async function openGameCenterLeaderboard() {
2258
+ console.log("[@apps-in-toss/devtools] openGameCenterLeaderboard (no-op in browser)");
2259
+ }
2260
+ function contactsViral(params) {
2261
+ setTimeout(() => {
2262
+ params.onEvent({
2263
+ type: "close",
2264
+ data: {
2265
+ closeReason: "noReward",
2266
+ sentRewardsCount: 0
2267
+ }
2268
+ });
2269
+ }, 500);
2270
+ return () => {};
2271
+ }
2272
+ //#endregion
2273
+ //#region src/mock/notification.ts
2274
+ /**
2275
+ * 알림 동의 mock
2276
+ *
2277
+ * 상류 SDK 타입 선언은 `requestNotificationAgreement(params)`이 callback-style로
2278
+ * 즉시 cancel 함수를 반환한다고 선언하지만, 실기기(2.x×iOS) capture는 반환값이
2279
+ * 함수가 아니라 **object**임을 보였다(devtools#806 — env3 재캡처, "Expected
2280
+ * function, received object" 단언 실패 2건: happy-default cancel 단언 +
2281
+ * A1-빈-templateCode 단언, 동일 원인 이중 측정). object의 내부 shape은 이번
2282
+ * run에서 미기록(단언이 "function인가"에서 끊겨 뒤 관측이 안 남음).
2283
+ *
2284
+ * mock은 #775 원칙대로 선언 타입은 상류와 동일하게 두고(`__typecheck.ts`/
2285
+ * `__typecheck-2x.ts`가 계속 컴파일되도록) 반환값만 실측 있는 수준까지
2286
+ * 캐스트한다 — 1차 착수는 "함수가 아니라 object"까지만, shape은 과잉 발명하지
2287
+ * 않는다(#783 "측정 밖 확장 금지"). 다음 재캡처에서 object keys가 잡히면 그때
2288
+ * shape을 채운다.
2289
+ *
2290
+ * 결과는 panel(Notifications 탭)이 토글한
2291
+ * `aitState.state.notification.nextResult`를 그대로 사용한다.
2292
+ *
2293
+ * `agreementRejected`도 정상 결과의 한 종류이므로 `onEvent`로 전달한다.
2294
+ * `onError`는 `onEvent` 호출 자체가 throw할 때만 들어간다 (실제 SDK도 reject를
2295
+ * error가 아닌 event type으로 표현한다).
2296
+ */
2297
+ const _requestNotificationAgreementImpl = (params) => {
2298
+ Promise.resolve().then(async () => {
2299
+ const failureCode = aitState.state.failureModes.requestNotificationAgreement;
2300
+ if (failureCode) {
2301
+ await params.onError(buildNativeError(failureCode));
2302
+ return;
2303
+ }
2304
+ const type = aitState.state.notification.nextResult;
2305
+ console.log("[@apps-in-toss/devtools] requestNotificationAgreement:", params.options.templateCode, "→", type);
2306
+ try {
2307
+ params.onEvent({ type });
2308
+ } catch (e) {
2309
+ await params.onError(e);
2310
+ }
2311
+ });
2312
+ return {};
2313
+ };
2314
+ const requestNotificationAgreement = Object.assign(_requestNotificationAgreementImpl, { isSupported: () => true });
2315
+ //#endregion
2316
+ //#region src/mock/partner/index.ts
2317
+ const partner = {
2318
+ async addAccessoryButton(options) {
2319
+ console.log("[@apps-in-toss/devtools] partner.addAccessoryButton:", options);
2320
+ },
2321
+ async removeAccessoryButton() {
2322
+ console.log("[@apps-in-toss/devtools] partner.removeAccessoryButton");
2323
+ }
2324
+ };
2325
+ //#endregion
2326
+ //#region src/mock/preset-store.ts
2327
+ const PREFIX = "__ait_preset:";
2328
+ function safeLocalStorage() {
2329
+ try {
2330
+ if (typeof localStorage === "undefined") return null;
2331
+ return localStorage;
2332
+ } catch {
2333
+ return null;
2334
+ }
2335
+ }
2336
+ function isObject(v) {
2337
+ return typeof v === "object" && v !== null && !Array.isArray(v);
2338
+ }
2339
+ /**
2340
+ * Storage에서 읽은 임의 JSON을 MockPreset으로 검증. id/label 필수, state는
2341
+ * object여야 함. 실패하면 null — caller가 storage entry를 무시하거나 정리하면 된다.
2342
+ *
2343
+ * `state`의 내부 키/값은 검증하지 않는다. `applyPreset`이 `pickKnownKeys`로
2344
+ * 키만 거른 뒤 그대로 state에 패치하므로 잘못된 enum 값이 통과될 수 있지만,
2345
+ * mock state라 보안 위협은 없다 — 새 enum 값이 추가됐을 때 저장된 preset을
2346
+ * reject하지 않으려는 의도.
2347
+ */
2348
+ function parsePreset(raw) {
2349
+ try {
2350
+ const parsed = JSON.parse(raw);
2351
+ if (!isObject(parsed)) return null;
2352
+ const { id, label, description, state } = parsed;
2353
+ if (typeof id !== "string" || id.length === 0) return null;
2354
+ if (typeof label !== "string" || label.length === 0) return null;
2355
+ if (!isObject(state)) return null;
2356
+ return {
2357
+ id,
2358
+ label,
2359
+ description: typeof description === "string" ? description : void 0,
2360
+ state
2361
+ };
2362
+ } catch {
2363
+ return null;
2364
+ }
2365
+ }
2366
+ function listUserPresets() {
2367
+ const ls = safeLocalStorage();
2368
+ if (!ls) return [];
2369
+ const out = [];
2370
+ for (let i = 0; i < ls.length; i++) {
2371
+ const key = ls.key(i);
2372
+ if (!key?.startsWith(PREFIX)) continue;
2373
+ const raw = ls.getItem(key);
2374
+ if (!raw) continue;
2375
+ const preset = parsePreset(raw);
2376
+ if (preset) out.push(preset);
2377
+ }
2378
+ return out.sort((a, b) => a.label.localeCompare(b.label));
2379
+ }
2380
+ /**
2381
+ * Preset을 저장한다. label에서 slug를 derive — 같은 slug가 이미 있으면 `-2`, `-3`
2382
+ * suffix를 붙여 새 entry를 만든다 (기존 entry 덮어쓰기 아님). UI는 label만 받으면 된다.
2383
+ *
2384
+ * Throws:
2385
+ * - label trim한 뒤 빈 문자열일 때
2386
+ * - localStorage 미가용 환경일 때 (SSR 등)
2387
+ * - `setItem` 실패 (`QuotaExceededError` 등) — caller가 처리해야 함
2388
+ */
2389
+ function saveUserPreset(label, state, description) {
2390
+ const trimmed = label.trim();
2391
+ if (trimmed.length === 0) throw new Error("Preset label cannot be empty");
2392
+ const ls = safeLocalStorage();
2393
+ if (!ls) throw new Error("localStorage not available");
2394
+ const id = generateId(trimmed, ls);
2395
+ const preset = {
2396
+ id,
2397
+ label: trimmed,
2398
+ state,
2399
+ ...description !== void 0 && description.length > 0 ? { description } : {}
2400
+ };
2401
+ ls.setItem(PREFIX + id, JSON.stringify(preset));
2402
+ return preset;
2403
+ }
2404
+ function deleteUserPreset(id) {
2405
+ const ls = safeLocalStorage();
2406
+ if (!ls) return;
2407
+ ls.removeItem(PREFIX + id);
2408
+ }
2409
+ /** 충돌 시 `-2`, `-3` 등 suffix를 붙여 unique한 id 만든다. */
2410
+ function generateId(label, ls) {
2411
+ const base = label.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 40) || "preset";
2412
+ let candidate = base;
2413
+ let n = 2;
2414
+ while (ls.getItem(PREFIX + candidate) !== null) {
2415
+ candidate = `${base}-${n}`;
2416
+ n += 1;
2417
+ }
2418
+ return candidate;
2419
+ }
2420
+ //#endregion
2421
+ //#region src/mock/presets.ts
2422
+ const builtInPresets = [
2423
+ {
2424
+ id: "all-allowed",
2425
+ label: "All allowed (default-ish)",
2426
+ description: "모든 권한 허용, WIFI, 로그인됨, IAP success",
2427
+ state: {
2428
+ networkStatus: "WIFI",
2429
+ permissions: {
2430
+ camera: "allowed",
2431
+ photos: "allowed",
2432
+ geolocation: "allowed",
2433
+ clipboard: "allowed",
2434
+ contacts: "allowed",
2435
+ microphone: "allowed"
2436
+ },
2437
+ auth: { isLoggedIn: true },
2438
+ iap: { nextResult: "success" },
2439
+ ads: { forceNoFill: false },
2440
+ payment: {
2441
+ nextResult: "success",
2442
+ failReason: ""
2443
+ }
2444
+ }
2445
+ },
2446
+ {
2447
+ id: "permission-denied",
2448
+ label: "Permissions denied",
2449
+ description: "camera / photos / geolocation / contacts 거부",
2450
+ state: { permissions: {
2451
+ camera: "denied",
2452
+ photos: "denied",
2453
+ geolocation: "denied",
2454
+ contacts: "denied"
2455
+ } }
2456
+ },
2457
+ {
2458
+ id: "offline",
2459
+ label: "Offline",
2460
+ description: "getNetworkStatus → OFFLINE, IAP NETWORK_ERROR",
2461
+ state: {
2462
+ networkStatus: "OFFLINE",
2463
+ iap: { nextResult: "NETWORK_ERROR" },
2464
+ payment: {
2465
+ nextResult: "fail",
2466
+ failReason: "NETWORK_ERROR"
2467
+ }
2468
+ }
2469
+ },
2470
+ {
2471
+ id: "logged-out",
2472
+ label: "Logged out",
2473
+ description: "auth.isLoggedIn=false. login flow 검증용",
2474
+ state: { auth: { isLoggedIn: false } }
2475
+ },
2476
+ {
2477
+ id: "iap-pending",
2478
+ label: "IAP payment pending",
2479
+ description: "결제 진행 중 분기 검증",
2480
+ state: { iap: { nextResult: "PAYMENT_PENDING" } }
2481
+ },
2482
+ {
2483
+ id: "ads-no-fill",
2484
+ label: "Ads — no fill",
2485
+ description: "광고 fill 실패 분기 검증",
2486
+ state: {
2487
+ networkStatus: "WIFI",
2488
+ ads: { forceNoFill: true }
2489
+ }
2490
+ }
2491
+ ];
2492
+ /**
2493
+ * Preset의 nested slice를 검증된 키만 골라서 풀어낸다. Forward-compat 차원에서
2494
+ * 알지 못하는 키는 drop, drop된 키 전부를 모아 한 번에 warn한다.
2495
+ *
2496
+ * Value 단위 검증은 하지 않는다 — `permissions.camera`에 enum 외 값이 들어와도
2497
+ * 그대로 통과한다. mock state라 잘못된 값은 mock 함수 분기 결과만 흔든다.
2498
+ * 새 enum 값이 추가됐을 때 저장된 preset을 reject하지 않으려는 의도.
2499
+ */
2500
+ function pickKnownKeys(input, allowed) {
2501
+ if (typeof input !== "object" || input === null) return {};
2502
+ const out = {};
2503
+ const dropped = [];
2504
+ for (const [key, value] of Object.entries(input)) if (allowed.includes(key)) out[key] = value;
2505
+ else dropped.push(key);
2506
+ if (dropped.length > 0) console.warn(`[@apps-in-toss/devtools] Preset dropped unknown keys: ${dropped.join(", ")}`);
2507
+ return out;
2508
+ }
2509
+ const PERMISSION_KEYS = [
2510
+ "camera",
2511
+ "photos",
2512
+ "geolocation",
2513
+ "clipboard",
2514
+ "contacts",
2515
+ "microphone"
2516
+ ];
2517
+ const AUTH_KEYS = [
2518
+ "isLoggedIn",
2519
+ "isTossLoginIntegrated",
2520
+ "userKeyHash"
2521
+ ];
2522
+ const IAP_KEYS = ["nextResult"];
2523
+ const ADS_KEYS = [
2524
+ "isLoaded",
2525
+ "nextEvent",
2526
+ "forceNoFill",
2527
+ "lastEvent"
2528
+ ];
2529
+ const PAYMENT_KEYS = ["nextResult", "failReason"];
2530
+ /**
2531
+ * Preset state를 현재 `aitState`에 적용한다. 정의된 키만 덮어쓰고, 알지 못하는 키는
2532
+ * 조용히 drop한다 (한 번 warn). 여러 슬라이스를 적용해도 listener notify는 한 번이다
2533
+ * (`aitState.transaction` 사용 — panel re-render 폭주 방지).
2534
+ */
2535
+ function applyPreset(state) {
2536
+ aitState.transaction(() => {
2537
+ if (state.networkStatus !== void 0) aitState.update({ networkStatus: state.networkStatus });
2538
+ if (state.permissions !== void 0) aitState.patch("permissions", pickKnownKeys(state.permissions, PERMISSION_KEYS));
2539
+ if (state.auth !== void 0) aitState.patch("auth", pickKnownKeys(state.auth, AUTH_KEYS));
2540
+ if (state.iap !== void 0) {
2541
+ const picked = pickKnownKeys(state.iap, IAP_KEYS);
2542
+ aitState.patch("iap", picked);
2543
+ }
2544
+ if (state.ads !== void 0) aitState.patch("ads", pickKnownKeys(state.ads, ADS_KEYS));
2545
+ if (state.payment !== void 0) aitState.patch("payment", pickKnownKeys(state.payment, PAYMENT_KEYS));
2546
+ });
2547
+ }
2548
+ /**
2549
+ * Preset의 모든 정의된 슬라이스가 현재 state와 일치하는지 검사. UI에서 dirty
2550
+ * indicator를 그릴 때 쓴다.
2551
+ *
2552
+ * 일치한다 = preset이 정의한 키 전부가 그대로다. preset이 정의하지 않은 키는
2553
+ * 비교 대상이 아니다 — preset은 partial이므로 다른 토글이 바뀌어도 dirty가 아니다.
2554
+ */
2555
+ function matchesPreset(snapshot, preset) {
2556
+ if (preset.networkStatus !== void 0 && snapshot.networkStatus !== preset.networkStatus) return false;
2557
+ if (preset.permissions !== void 0) for (const k of PERMISSION_KEYS) {
2558
+ const want = preset.permissions[k];
2559
+ if (want !== void 0 && snapshot.permissions[k] !== want) return false;
2560
+ }
2561
+ if (preset.auth !== void 0) for (const k of AUTH_KEYS) {
2562
+ const want = preset.auth[k];
2563
+ if (want !== void 0 && snapshot.auth[k] !== want) return false;
2564
+ }
2565
+ if (preset.iap !== void 0) {
2566
+ if (preset.iap.nextResult !== void 0 && snapshot.iap.nextResult !== preset.iap.nextResult) return false;
2567
+ }
2568
+ if (preset.ads !== void 0) {
2569
+ if (preset.ads.forceNoFill !== void 0 && snapshot.ads.forceNoFill !== preset.ads.forceNoFill) return false;
2570
+ if (preset.ads.isLoaded !== void 0 && snapshot.ads.isLoaded !== preset.ads.isLoaded) return false;
2571
+ if (preset.ads.nextEvent !== void 0 && snapshot.ads.nextEvent !== preset.ads.nextEvent) return false;
2572
+ }
2573
+ if (preset.payment !== void 0) for (const k of PAYMENT_KEYS) {
2574
+ const want = preset.payment[k];
2575
+ if (want !== void 0 && snapshot.payment[k] !== want) return false;
2576
+ }
2577
+ return true;
2578
+ }
2579
+ /**
2580
+ * 현재 state에서 preset에 저장할 만한 슬라이스를 추출. "save current as preset"에서 쓴다.
2581
+ */
2582
+ function captureCurrentState(snapshot) {
2583
+ return {
2584
+ networkStatus: snapshot.networkStatus,
2585
+ permissions: { ...snapshot.permissions },
2586
+ auth: {
2587
+ isLoggedIn: snapshot.auth.isLoggedIn,
2588
+ isTossLoginIntegrated: snapshot.auth.isTossLoginIntegrated,
2589
+ userKeyHash: snapshot.auth.userKeyHash
2590
+ },
2591
+ iap: { nextResult: snapshot.iap.nextResult },
2592
+ ads: {
2593
+ forceNoFill: snapshot.ads.forceNoFill,
2594
+ isLoaded: snapshot.ads.isLoaded,
2595
+ nextEvent: snapshot.ads.nextEvent
2596
+ },
2597
+ payment: { ...snapshot.payment }
2598
+ };
2599
+ }
2600
+ //#endregion
2601
+ //#region src/mock/index.ts
2602
+ /**
2603
+ * @apps-in-toss/devtools/mock
2604
+ *
2605
+ * @apps-in-toss/web-framework의 모든 export를 mock으로 대체한다.
2606
+ * 번들러 alias로 원본 대신 이 모듈이 resolve된다.
2607
+ */
2608
+ installBridges();
2609
+ //#endregion
2610
+ export { Accuracy, Analytics, FetchAlbumPhotosPermissionError, FetchContactsPermissionError, GetClipboardTextPermissionError, GetCurrentLocationPermissionError, GoogleAdMob, IAP, NAVIGATE_BACK_MESSAGE_TYPE, OpenCameraPermissionError, PermissionError, SAFE_AREA_INSETS_MESSAGE_TYPE, SafeAreaInsets, SetClipboardTextPermissionError, StartUpdateLocationPermissionError, Storage, TossAds, WEB_VIEW_TYPE_MESSAGE_TYPE, aitState, appLogin, applyForwardedSafeAreaInsets, applyPreset, appsInTossEvent, appsInTossSignTossCert, builtInPresets, captureCurrentState, checkoutPayment, closeView, contactsViral, createAsyncBridge, createConstantBridge, createEventBridge, deleteUserPreset, env, eventLog, fetchAlbumItems, fetchAlbumPhotos, fetchContacts, generateHapticFeedback, getAnonymousKey, getAppsInTossGlobals, getClipboardText, getConsentedUserData, getCurrentLocation, getDeclaredAgeRange, getDefaultPlaceholderImages, getDeviceId, getGameCenterGameProfile, getGroupId, getIsTossLoginIntegratedService, getLocale, getNetworkStatus, getOperationalEnvironment, getPermission, getPlatformOS, getSafeAreaInsets, getSchemeUri, getServerTime, getTossAppVersion, getTossShareLink, getUserKeyForGame, graniteEvent, grantPromotionReward, grantPromotionRewardForGame, installBridges, installNavigateBackBridge, installSafeAreaInsetsBridge, isMinVersionSupported, isNavigateBackMessage, listUserPresets, loadFullScreenAd, matchesPreset, onVisibilityChangedByTransparentServiceWeb, openCamera, openGameCenterLeaderboard, openPDFViewer, openPermissionDialog, openURL, parseSafeAreaInsetsMessage, parseWebViewTypeMessage, partner, requestNotificationAgreement, requestPermission, requestReview, requestTossPayPaysBilling, saveBase64Data, saveUserPreset, setClipboardText, setDeviceOrientation, setIosSwipeGestureEnabled, setScreenAwakeMode, setSecureScreen, share, showFullScreenAd, startUpdateLocation, submitGameCenterLeaderBoardScore, tdsEvent };