@jetthai/cuora 1.1.485-dev.2 → 1.1.485
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.
- package/.claude/commands/sync-ai-prompts.md +1 -88
- package/.claude/settings.local.json +1 -3
- package/.github/copilot-instructions.md +32 -69
- package/.husky/post-checkout +3 -3
- package/.husky/pre-push +1 -1
- package/.prettierignore +1 -2
- package/AGENTS.md +32 -69
- package/AI_PROMPT_SOURCE.md +32 -69
- package/CHANGELOG.md +23 -382
- package/CLAUDE.md +32 -69
- package/GEMINI.md +32 -69
- package/package.json +1 -1
- package/scripts/auto-version.js +0 -1
- package/scripts/branch-auto-npm-publish.js +2 -9
- package/src/api/payment.js +0 -1
- package/src/machine/auth.js +18 -5
- package/src/machine/game_v2_1.js +0 -15
- package/src/machine/reward_cash.js +2 -2
- package/src/machine/user_sign_up.js +0 -3
- package/src/machine/withdraw/withdraw_manual_cash.js +4 -7
- package/src/request/request.js +4 -44
- package/src/util/constant.js +1 -5
- package/src/util/tokenStore.js +0 -44
- package/test/withdraw_manual_cash.test.js +0 -149
- package/tests/session.test.mjs +0 -177
package/src/machine/game_v2_1.js
CHANGED
|
@@ -6,8 +6,6 @@ import { GAME_SEARCH } from '../util/constant.js'
|
|
|
6
6
|
// 對應 intersectionObserver 版本
|
|
7
7
|
export default function (request) {
|
|
8
8
|
const { appSecret } = request.config
|
|
9
|
-
// 不呼叫 game/logout 直接回 idle:session 已換新時,舊 session 的遊戲登出沒有意義
|
|
10
|
-
const abandonGame = { target: 'idle', actions: 'clearGameURL' }
|
|
11
9
|
return createMachine(
|
|
12
10
|
{
|
|
13
11
|
id: 'game',
|
|
@@ -162,7 +160,6 @@ export default function (request) {
|
|
|
162
160
|
EXIT_GAME: {
|
|
163
161
|
target: 'disconnect',
|
|
164
162
|
},
|
|
165
|
-
ABANDON_GAME: abandonGame,
|
|
166
163
|
},
|
|
167
164
|
},
|
|
168
165
|
demoGame: {
|
|
@@ -181,7 +178,6 @@ export default function (request) {
|
|
|
181
178
|
EXIT_GAME: {
|
|
182
179
|
target: 'disconnect',
|
|
183
180
|
},
|
|
184
|
-
ABANDON_GAME: abandonGame,
|
|
185
181
|
},
|
|
186
182
|
},
|
|
187
183
|
connectSwitching: {
|
|
@@ -200,7 +196,6 @@ export default function (request) {
|
|
|
200
196
|
EXIT_GAME: {
|
|
201
197
|
target: 'disconnect',
|
|
202
198
|
},
|
|
203
|
-
ABANDON_GAME: abandonGame,
|
|
204
199
|
},
|
|
205
200
|
},
|
|
206
201
|
playing: {
|
|
@@ -208,7 +203,6 @@ export default function (request) {
|
|
|
208
203
|
EXIT_GAME: {
|
|
209
204
|
target: 'disconnect',
|
|
210
205
|
},
|
|
211
|
-
ABANDON_GAME: abandonGame,
|
|
212
206
|
REFRESH: {
|
|
213
207
|
target: 'refreshing',
|
|
214
208
|
},
|
|
@@ -230,7 +224,6 @@ export default function (request) {
|
|
|
230
224
|
EXIT_GAME: {
|
|
231
225
|
target: 'disconnect',
|
|
232
226
|
},
|
|
233
|
-
ABANDON_GAME: abandonGame,
|
|
234
227
|
EXIT_DEMO_GAME: {
|
|
235
228
|
target: 'idle',
|
|
236
229
|
},
|
|
@@ -252,9 +245,6 @@ export default function (request) {
|
|
|
252
245
|
actions: 'setGameURL',
|
|
253
246
|
},
|
|
254
247
|
},
|
|
255
|
-
on: {
|
|
256
|
-
ABANDON_GAME: abandonGame,
|
|
257
|
-
},
|
|
258
248
|
},
|
|
259
249
|
refreshPending: {
|
|
260
250
|
after: {
|
|
@@ -262,9 +252,6 @@ export default function (request) {
|
|
|
262
252
|
target: 'connecting',
|
|
263
253
|
},
|
|
264
254
|
},
|
|
265
|
-
on: {
|
|
266
|
-
ABANDON_GAME: abandonGame,
|
|
267
|
-
},
|
|
268
255
|
},
|
|
269
256
|
disconnect: {
|
|
270
257
|
invoke: {
|
|
@@ -279,7 +266,6 @@ export default function (request) {
|
|
|
279
266
|
},
|
|
280
267
|
},
|
|
281
268
|
on: {
|
|
282
|
-
ABANDON_GAME: abandonGame,
|
|
283
269
|
LOAD: {
|
|
284
270
|
target: 'routing',
|
|
285
271
|
actions: 'setLoadable',
|
|
@@ -440,7 +426,6 @@ export default function (request) {
|
|
|
440
426
|
return event.data.gameWalletType
|
|
441
427
|
},
|
|
442
428
|
}),
|
|
443
|
-
clearGameURL: assign({ type: undefined, gameToken: undefined, gameHtml: undefined, gameURL: undefined, gameWalletType: undefined }),
|
|
444
429
|
setGameInfo: assign((context, event) => {
|
|
445
430
|
const { providerId, gameId, gameTypeId, mappingId, portrait, gameCode, name, gameName } = event.data.game
|
|
446
431
|
return { game: { providerId, gameId, gameTypeId, mappingId, portrait: portrait ? 1 : 0, gameCode, name, gameName } }
|
|
@@ -197,7 +197,7 @@ export default function (request, context) {
|
|
|
197
197
|
)
|
|
198
198
|
const getOtherRewardAPI = request.send(
|
|
199
199
|
activity.getRewardList({
|
|
200
|
-
rewardType: 'CASH,FREE,PRIZE
|
|
200
|
+
rewardType: 'CASH,FREE,PRIZE',
|
|
201
201
|
count: 30,
|
|
202
202
|
appSecret,
|
|
203
203
|
orderColumn,
|
|
@@ -212,7 +212,7 @@ export default function (request, context) {
|
|
|
212
212
|
const lang = context.lang
|
|
213
213
|
const getOtherRewardAPI = request.send(
|
|
214
214
|
activity.getRewardList({
|
|
215
|
-
rewardType: 'CASH,FREE,PRIZE
|
|
215
|
+
rewardType: 'CASH,FREE,PRIZE',
|
|
216
216
|
count: 30,
|
|
217
217
|
appSecret,
|
|
218
218
|
orderColumn,
|
|
@@ -107,7 +107,6 @@ export default function (request) {
|
|
|
107
107
|
return {
|
|
108
108
|
signUpData: {
|
|
109
109
|
provider: 'local',
|
|
110
|
-
birthday,
|
|
111
110
|
...pickBindData(event.data),
|
|
112
111
|
birthday,
|
|
113
112
|
email,
|
|
@@ -131,7 +130,6 @@ export default function (request) {
|
|
|
131
130
|
return {
|
|
132
131
|
signUpData: {
|
|
133
132
|
provider: 'phone',
|
|
134
|
-
birthday,
|
|
135
133
|
...pickBindData(event.data),
|
|
136
134
|
birthday,
|
|
137
135
|
email,
|
|
@@ -154,7 +152,6 @@ export default function (request) {
|
|
|
154
152
|
return {
|
|
155
153
|
signUpData: {
|
|
156
154
|
provider: 'email',
|
|
157
|
-
birthday,
|
|
158
155
|
...pickBindData(event.data),
|
|
159
156
|
birthday,
|
|
160
157
|
email,
|
|
@@ -36,8 +36,9 @@ export default function (request, context) {
|
|
|
36
36
|
target: 'checking',
|
|
37
37
|
actions: 'setInitData',
|
|
38
38
|
},
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
onError: {
|
|
40
|
+
target: 'loaded',
|
|
41
|
+
},
|
|
41
42
|
},
|
|
42
43
|
},
|
|
43
44
|
checking: {
|
|
@@ -95,11 +96,7 @@ export default function (request, context) {
|
|
|
95
96
|
},
|
|
96
97
|
},
|
|
97
98
|
},
|
|
98
|
-
submitted: {
|
|
99
|
-
on: {
|
|
100
|
-
REFRESH: { target: 'loading', internal: false },
|
|
101
|
-
},
|
|
102
|
-
},
|
|
99
|
+
submitted: {},
|
|
103
100
|
otp: {
|
|
104
101
|
type: 'parallel',
|
|
105
102
|
states: {
|
package/src/request/request.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import pickBy from 'lodash/pickBy.js'
|
|
2
|
-
import
|
|
2
|
+
import storage from '../util/storage.js'
|
|
3
3
|
import { getErr998SentryExtra } from '../util/err998SentryExtra.js'
|
|
4
4
|
|
|
5
5
|
// 暫停 ERR_998 上報:在組裝 Sentry 資料之前就中止,不建立事件;仍照常 reject ERR_998 給呼叫端。恢復時改回 false 即可
|
|
@@ -7,9 +7,6 @@ const IS_ERR_998_REPORT_PAUSED = true
|
|
|
7
7
|
|
|
8
8
|
const CANCELLED_MESSAGE = 'Requests Cancelled'
|
|
9
9
|
|
|
10
|
-
const SELF_URL = 'self'
|
|
11
|
-
const TOKEN_EXPIRED_CODE = 'ERR_A108'
|
|
12
|
-
|
|
13
10
|
// const pickBy = require('lodash/pickBy')
|
|
14
11
|
const supportVersion2List = [
|
|
15
12
|
// 04-09 Update
|
|
@@ -162,7 +159,6 @@ export default class Request {
|
|
|
162
159
|
this.config = config
|
|
163
160
|
this.sentry = config.sentry
|
|
164
161
|
this.key = `cuora_${config.clientId}`
|
|
165
|
-
this.tokenStore = createTokenStore(this.key)
|
|
166
162
|
this.errorHandler = null
|
|
167
163
|
this.hosts = []
|
|
168
164
|
this.isBlocked = false
|
|
@@ -285,27 +281,7 @@ export default class Request {
|
|
|
285
281
|
return !!this.axios.isCancel?.(error) || error?.message === CANCELLED_MESSAGE
|
|
286
282
|
}
|
|
287
283
|
|
|
288
|
-
send(req, customToken) {
|
|
289
|
-
return this._send(req, customToken, false)
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
_isSelf(req) {
|
|
293
|
-
return req.url.split('?')[0] === SELF_URL
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// ERR_A108 多半是續期的短暫落差:先以 /self 確認 session 仍在,成功才把原請求重送一次。
|
|
297
|
-
// /self 自己、已重送過的請求、送出後身分已變的請求都不重送
|
|
298
|
-
async _renewSession(req, requestToken) {
|
|
299
|
-
if (!requestToken || this._isSelf(req) || this.getToken() !== requestToken) return false
|
|
300
|
-
try {
|
|
301
|
-
await this._send({ url: SELF_URL, method: 'get', silent: true }, undefined, true)
|
|
302
|
-
} catch (error) {
|
|
303
|
-
return false
|
|
304
|
-
}
|
|
305
|
-
return this.getToken() === requestToken
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
async _send(req, customToken, isRetry) {
|
|
284
|
+
async send(req, customToken) {
|
|
309
285
|
let tracked = null
|
|
310
286
|
let requestToken = null
|
|
311
287
|
try {
|
|
@@ -316,7 +292,7 @@ export default class Request {
|
|
|
316
292
|
if (!req.isPublic) this._throwIfRevoked()
|
|
317
293
|
|
|
318
294
|
// /self 是 gate 自己的探測請求,不可等待 gate,否則死鎖
|
|
319
|
-
if (this.tokenGate && !req.isPublic &&
|
|
295
|
+
if (this.tokenGate && !req.isPublic && req.url.split('?')[0] !== 'self') {
|
|
320
296
|
await this.tokenGate
|
|
321
297
|
this._throwIfRevoked()
|
|
322
298
|
if (!this.getToken()) return Promise.reject({ code: 'ERR_A103' })
|
|
@@ -346,9 +322,6 @@ export default class Request {
|
|
|
346
322
|
}
|
|
347
323
|
} catch (error) {
|
|
348
324
|
if (this._isCancelled(error)) throw this._cancelledError()
|
|
349
|
-
if (error?.code === TOKEN_EXPIRED_CODE && !isRetry && (await this._renewSession(req, requestToken))) {
|
|
350
|
-
return await this._send(req, customToken, true)
|
|
351
|
-
}
|
|
352
325
|
// 不可列舉:避免 Bearer token 隨 JSON.stringify/Sentry 序列化外洩,但 error.requestToken 仍可讀取
|
|
353
326
|
if (requestToken && error && typeof error === 'object' && Object.isExtensible(error)) {
|
|
354
327
|
Object.defineProperty(error, 'requestToken', { value: requestToken, enumerable: false, configurable: true })
|
|
@@ -448,19 +421,6 @@ export default class Request {
|
|
|
448
421
|
}
|
|
449
422
|
|
|
450
423
|
getToken() {
|
|
451
|
-
return
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
setToken(token) {
|
|
455
|
-
this.tokenStore.set(token)
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// 回傳續期當下的 token;已不存在時回傳 null
|
|
459
|
-
extendToken() {
|
|
460
|
-
return this.tokenStore.extend()
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
removeToken() {
|
|
464
|
-
this.tokenStore.clear()
|
|
424
|
+
return storage.get(this.key)
|
|
465
425
|
}
|
|
466
426
|
}
|
package/src/util/constant.js
CHANGED
|
@@ -11,17 +11,13 @@ export const GAME_TYPE_LIST = [
|
|
|
11
11
|
{ typeId: 'SCRATCHCARDS', name: 'Scratch Cards', gameTypeMappingId: 9 },
|
|
12
12
|
{ typeId: 'EGAME', name: 'E-Game', gameTypeMappingId: 10 },
|
|
13
13
|
{ typeId: 'BONUS', name: 'Bonus', gameTypeMappingId: 11 },
|
|
14
|
-
|
|
14
|
+
{ typeId: 'BALL', name: 'Ball', gameTypeMappingId: 12 },
|
|
15
15
|
{ typeId: 'PACHINKO', name: 'Pachinko', gameTypeMappingId: 13 },
|
|
16
16
|
{ typeId: 'GRAPH', name: 'Graph', gameTypeMappingId: 14 },
|
|
17
17
|
{ typeId: 'TURBO', name: 'Turbo', gameTypeMappingId: 15 },
|
|
18
18
|
{ typeId: 'SPORT', name: 'Sports', gameTypeMappingId: 16 },
|
|
19
|
-
{ typeId: 'PREDICTION', name: 'Prediction', gameTypeMappingId: 12 },
|
|
20
19
|
]
|
|
21
20
|
|
|
22
21
|
// OTP 自動驗證:主動打的 /self 未通過後,最多再等幾次 auth 例行輪詢的 /self、等待上限多久
|
|
23
22
|
export const AUTO_VERIFY_MAX_USER_INFO_UPDATES = 2
|
|
24
23
|
export const AUTO_VERIFY_WAIT_MS = 5000
|
|
25
|
-
|
|
26
|
-
// 登入 token 的存活秒數:每次 /self 成功續期一次,逾時由瀏覽器自行移除 cookie
|
|
27
|
-
export const TOKEN_MAX_AGE = 300
|
package/src/util/tokenStore.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import storage from './storage.js'
|
|
2
|
-
import { TOKEN_MAX_AGE } from './constant.js'
|
|
3
|
-
|
|
4
|
-
// document.cookie 在受限的環境(sandbox iframe、停用 cookie)會直接拋錯:讀不到視為沒有,寫不進去就算了
|
|
5
|
-
function read(name) {
|
|
6
|
-
const prefix = `${name}=`
|
|
7
|
-
try {
|
|
8
|
-
const pair = document.cookie.split('; ').find((item) => item.startsWith(prefix))
|
|
9
|
-
return pair ? decodeURIComponent(pair.slice(prefix.length)) || null : null
|
|
10
|
-
} catch (error) {
|
|
11
|
-
return null
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// 不帶 Domain:cookie 只屬於寫入它的網域,不跨子網域
|
|
16
|
-
function write(name, value, maxAge) {
|
|
17
|
-
try {
|
|
18
|
-
const secure = location.protocol === 'https:' ? '; Secure' : ''
|
|
19
|
-
document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAge}; SameSite=Lax${secure}`
|
|
20
|
-
} catch (error) {
|
|
21
|
-
console.error(`Error writing cookie: ${error}`)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export default function createTokenStore(key) {
|
|
26
|
-
// token 原本存在 localStorage;改放 cookie 後不沿用舊值,殘留的一併清掉
|
|
27
|
-
storage.remove(key)
|
|
28
|
-
|
|
29
|
-
const store = {
|
|
30
|
-
get: () => read(key),
|
|
31
|
-
set(token) {
|
|
32
|
-
if (token) write(key, token, TOKEN_MAX_AGE)
|
|
33
|
-
else store.clear()
|
|
34
|
-
},
|
|
35
|
-
// 以現有的值重寫一次,讓存活時間重新起算
|
|
36
|
-
extend() {
|
|
37
|
-
const token = read(key)
|
|
38
|
-
if (token) write(key, token, TOKEN_MAX_AGE)
|
|
39
|
-
return token
|
|
40
|
-
},
|
|
41
|
-
clear: () => write(key, '', 0),
|
|
42
|
-
}
|
|
43
|
-
return store
|
|
44
|
-
}
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { interpret } from 'xstate'
|
|
4
|
-
import withdrawManualCash from '../src/machine/withdraw/withdraw_manual_cash.js'
|
|
5
|
-
|
|
6
|
-
function waitForState(service, matches) {
|
|
7
|
-
if (service.state.matches(matches)) return Promise.resolve(service.state)
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
const timeout = setTimeout(() => reject(new Error(`Timed out waiting for ${matches}`)), 1000)
|
|
10
|
-
service.onTransition((state) => {
|
|
11
|
-
if (!state.matches(matches)) return
|
|
12
|
-
clearTimeout(timeout)
|
|
13
|
-
resolve(state)
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const context = {
|
|
19
|
-
doubleWalletWithdrawLimitCheck: '0',
|
|
20
|
-
paymentDetail: [{ category: 'manual', transferType: 'withdraw', method: 'MANUAL', minimum: 100, maximum: 10000 }],
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const boundBank = { bankCode: 'BANK', accountNumber: '0001', holderName: 'holder' }
|
|
24
|
-
|
|
25
|
-
// pendingOrders[i] 為第 i+1 次 pendingOrder 的回應;Error 代表該次 reject
|
|
26
|
-
function stubRequest(pendingOrders) {
|
|
27
|
-
const calls = { pendingOrder: 0 }
|
|
28
|
-
return {
|
|
29
|
-
calls,
|
|
30
|
-
async send(descriptor) {
|
|
31
|
-
if (descriptor.url === 'payment/v2/getBindingPaymentInfo') return { bankList: [boundBank], ewalletList: [], cryptoList: [] }
|
|
32
|
-
if (descriptor.url === 'payment/v2/withdrawRule') return { enableWithdraw: true }
|
|
33
|
-
if (descriptor.url === 'payment/v2/pendingOrder') {
|
|
34
|
-
const response = pendingOrders[calls.pendingOrder++] ?? []
|
|
35
|
-
if (response instanceof Error) throw response
|
|
36
|
-
return response
|
|
37
|
-
}
|
|
38
|
-
throw new Error(`Unexpected request: ${descriptor.url}`)
|
|
39
|
-
},
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function waitForPendingOrderCalls(request, count) {
|
|
44
|
-
return new Promise((resolve, reject) => {
|
|
45
|
-
const started = Date.now()
|
|
46
|
-
const poll = () => {
|
|
47
|
-
if (request.calls.pendingOrder >= count) return resolve()
|
|
48
|
-
if (Date.now() - started > 1000) return reject(new Error(`Timed out waiting for ${count} pendingOrder calls`))
|
|
49
|
-
setTimeout(poll, 5)
|
|
50
|
-
}
|
|
51
|
-
poll()
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const firstOrder = { applyId: 'W-1', amount: 500 }
|
|
56
|
-
const secondOrder = { applyId: 'W-2', amount: 800 }
|
|
57
|
-
|
|
58
|
-
test('REFRESH in submitted re-runs loading and fetches the pending order again', async () => {
|
|
59
|
-
const request = stubRequest([[firstOrder], []])
|
|
60
|
-
const service = interpret(withdrawManualCash(request, context)).start()
|
|
61
|
-
try {
|
|
62
|
-
await waitForState(service, 'submitted')
|
|
63
|
-
assert.equal(request.calls.pendingOrder, 1)
|
|
64
|
-
service.send({ type: 'REFRESH' })
|
|
65
|
-
assert.equal(service.state.matches('loading'), true)
|
|
66
|
-
await waitForPendingOrderCalls(request, 2)
|
|
67
|
-
} finally {
|
|
68
|
-
service.stop()
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
test('REFRESH in submitted returns to the form when no order is pending', async () => {
|
|
73
|
-
const request = stubRequest([[firstOrder], []])
|
|
74
|
-
const service = interpret(withdrawManualCash(request, context)).start()
|
|
75
|
-
try {
|
|
76
|
-
await waitForState(service, 'submitted')
|
|
77
|
-
service.send({ type: 'REFRESH' })
|
|
78
|
-
const state = await waitForState(service, 'loaded')
|
|
79
|
-
assert.equal(request.calls.pendingOrder, 2)
|
|
80
|
-
assert.equal(state.context.processing, undefined)
|
|
81
|
-
} finally {
|
|
82
|
-
service.stop()
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('REFRESH in submitted keeps submitted with the updated order when one is still pending', async () => {
|
|
87
|
-
const request = stubRequest([[firstOrder], [secondOrder]])
|
|
88
|
-
const service = interpret(withdrawManualCash(request, context)).start()
|
|
89
|
-
try {
|
|
90
|
-
await waitForState(service, 'submitted')
|
|
91
|
-
assert.equal(service.state.context.processing.applyId, 'W-1')
|
|
92
|
-
service.send({ type: 'REFRESH' })
|
|
93
|
-
await waitForPendingOrderCalls(request, 2)
|
|
94
|
-
const state = await waitForState(service, 'submitted')
|
|
95
|
-
assert.equal(request.calls.pendingOrder, 2)
|
|
96
|
-
assert.deepEqual(state.context.processing, secondOrder)
|
|
97
|
-
} finally {
|
|
98
|
-
service.stop()
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
function networkError() {
|
|
103
|
-
return Object.assign(new Error('Network Error'), { code: 'ERR_998' })
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
test('a failed refresh in submitted keeps the pending order and a later REFRESH retries', async () => {
|
|
107
|
-
const request = stubRequest([[firstOrder], networkError(), []])
|
|
108
|
-
const service = interpret(withdrawManualCash(request, context)).start()
|
|
109
|
-
try {
|
|
110
|
-
await waitForState(service, 'submitted')
|
|
111
|
-
service.send({ type: 'REFRESH' })
|
|
112
|
-
await waitForPendingOrderCalls(request, 2)
|
|
113
|
-
const state = await waitForState(service, 'submitted')
|
|
114
|
-
assert.deepEqual(state.context.processing, firstOrder)
|
|
115
|
-
service.send({ type: 'REFRESH' })
|
|
116
|
-
await waitForState(service, 'loaded')
|
|
117
|
-
assert.equal(request.calls.pendingOrder, 3)
|
|
118
|
-
} finally {
|
|
119
|
-
service.stop()
|
|
120
|
-
}
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
test('a failed first load without a pending order still shows the form', async () => {
|
|
124
|
-
const request = stubRequest([networkError()])
|
|
125
|
-
const service = interpret(withdrawManualCash(request, context)).start()
|
|
126
|
-
try {
|
|
127
|
-
await waitForState(service, 'loaded')
|
|
128
|
-
assert.equal(request.calls.pendingOrder, 1)
|
|
129
|
-
assert.equal(service.state.context.processing, null)
|
|
130
|
-
} finally {
|
|
131
|
-
service.stop()
|
|
132
|
-
}
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
test('REFRESH in loaded with a bound bank is still ignored', async () => {
|
|
136
|
-
const request = stubRequest([[]])
|
|
137
|
-
const service = interpret(withdrawManualCash(request, context)).start()
|
|
138
|
-
try {
|
|
139
|
-
await waitForState(service, 'loaded')
|
|
140
|
-
assert.equal(request.calls.pendingOrder, 1)
|
|
141
|
-
service.send({ type: 'REFRESH' })
|
|
142
|
-
assert.equal(service.state.matches('loaded'), true)
|
|
143
|
-
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
144
|
-
assert.equal(request.calls.pendingOrder, 1)
|
|
145
|
-
assert.equal(service.state.matches('loaded'), true)
|
|
146
|
-
} finally {
|
|
147
|
-
service.stop()
|
|
148
|
-
}
|
|
149
|
-
})
|
package/tests/session.test.mjs
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { interpret } from 'xstate'
|
|
4
|
-
import Request from '../src/request/request.js'
|
|
5
|
-
import createTokenStore from '../src/util/tokenStore.js'
|
|
6
|
-
import { TOKEN_MAX_AGE } from '../src/util/constant.js'
|
|
7
|
-
import game from '../src/machine/game_v2_1.js'
|
|
8
|
-
|
|
9
|
-
globalThis.localStorage = { getItem: () => null, setItem() {}, removeItem() {} }
|
|
10
|
-
globalThis.location = { protocol: 'http:' }
|
|
11
|
-
|
|
12
|
-
// 只模擬 tokenStore 用到的行為:以 name 覆寫、Max-Age=0 即刪除
|
|
13
|
-
function installDocument() {
|
|
14
|
-
const jar = new Map()
|
|
15
|
-
const writes = []
|
|
16
|
-
globalThis.document = {
|
|
17
|
-
get cookie() {
|
|
18
|
-
return [...jar].map(([name, value]) => `${name}=${value}`).join('; ')
|
|
19
|
-
},
|
|
20
|
-
set cookie(text) {
|
|
21
|
-
writes.push(text)
|
|
22
|
-
const [pair, ...attributes] = text.split('; ')
|
|
23
|
-
const index = pair.indexOf('=')
|
|
24
|
-
const name = pair.slice(0, index)
|
|
25
|
-
const maxAge = attributes.find((item) => item.startsWith('Max-Age='))
|
|
26
|
-
if (maxAge && Number(maxAge.slice(8)) <= 0) jar.delete(name)
|
|
27
|
-
else jar.set(name, pair.slice(index + 1))
|
|
28
|
-
},
|
|
29
|
-
}
|
|
30
|
-
return { jar, writes }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function createRequest(respond) {
|
|
34
|
-
const calls = []
|
|
35
|
-
const axios = {
|
|
36
|
-
defaults: {},
|
|
37
|
-
interceptors: { request: { use() {} }, response: { use() {} } },
|
|
38
|
-
isCancel: () => false,
|
|
39
|
-
async request(config) {
|
|
40
|
-
calls.push(config.url)
|
|
41
|
-
return respond(config, calls.length)
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
const request = new Request(axios, { clientId: 'SITE', fullURL: 'https://api.test/v1/SITE' })
|
|
45
|
-
const handled = []
|
|
46
|
-
request.setErrorHandler((error) => handled.push(error.code))
|
|
47
|
-
return { request, calls, handled }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const expired = () => Promise.reject({ code: 'ERR_A108' })
|
|
51
|
-
const ok = (data = {}) => Promise.resolve({ data })
|
|
52
|
-
|
|
53
|
-
test('token lives in a host-only cookie with a 300 second lifetime', () => {
|
|
54
|
-
const { jar, writes } = installDocument()
|
|
55
|
-
const store = createTokenStore('cuora_SITE')
|
|
56
|
-
|
|
57
|
-
assert.equal(store.get(), null)
|
|
58
|
-
store.set('token.a')
|
|
59
|
-
assert.equal(store.get(), 'token.a')
|
|
60
|
-
assert.equal(writes.at(-1), `cuora_SITE=token.a; Path=/; Max-Age=${TOKEN_MAX_AGE}; SameSite=Lax`)
|
|
61
|
-
assert.ok(!/Domain=/i.test(writes.at(-1)))
|
|
62
|
-
|
|
63
|
-
jar.set('cuora_SITE-cred', 'sealed')
|
|
64
|
-
assert.equal(store.get(), 'token.a')
|
|
65
|
-
|
|
66
|
-
const before = writes.length
|
|
67
|
-
assert.equal(store.extend(), 'token.a')
|
|
68
|
-
assert.equal(writes.length, before + 1)
|
|
69
|
-
|
|
70
|
-
store.clear()
|
|
71
|
-
assert.equal(store.get(), null)
|
|
72
|
-
assert.equal(store.extend(), null)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('request reads and writes the token through the cookie', () => {
|
|
76
|
-
installDocument()
|
|
77
|
-
const { request } = createRequest(() => ok())
|
|
78
|
-
|
|
79
|
-
request.setToken('token.a')
|
|
80
|
-
assert.equal(request.getToken(), 'token.a')
|
|
81
|
-
assert.equal(request.extendToken(), 'token.a')
|
|
82
|
-
request.removeToken()
|
|
83
|
-
assert.equal(request.getToken(), null)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('ERR_A108 renews through /self and resends the request once', async () => {
|
|
87
|
-
installDocument()
|
|
88
|
-
const { request, calls, handled } = createRequest((config, count) => (count === 1 ? expired() : ok({ url: config.url })))
|
|
89
|
-
request.setToken('token.a')
|
|
90
|
-
|
|
91
|
-
const result = await request.send({ url: 'payment/v2/setting', method: 'get' })
|
|
92
|
-
|
|
93
|
-
assert.deepEqual(calls, ['payment/v2/setting', 'self', 'payment/v2/setting'])
|
|
94
|
-
assert.deepEqual(result, { url: 'payment/v2/setting' })
|
|
95
|
-
assert.deepEqual(handled, [])
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
test('a resent request that still fails with ERR_A108 is routed as before', async () => {
|
|
99
|
-
installDocument()
|
|
100
|
-
const { request, calls, handled } = createRequest((config) => (config.url === 'self' ? ok() : expired()))
|
|
101
|
-
request.setToken('token.a')
|
|
102
|
-
|
|
103
|
-
await assert.rejects(request.send({ url: 'payment/v2/setting', method: 'get' }), { code: 'ERR_A108' })
|
|
104
|
-
|
|
105
|
-
assert.deepEqual(calls, ['payment/v2/setting', 'self', 'payment/v2/setting'])
|
|
106
|
-
assert.deepEqual(handled, ['ERR_A108'])
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
test('ERR_A108 is not resent when /self cannot renew the session', async () => {
|
|
110
|
-
installDocument()
|
|
111
|
-
const { request, calls, handled } = createRequest(() => expired())
|
|
112
|
-
request.setToken('token.a')
|
|
113
|
-
|
|
114
|
-
await assert.rejects(request.send({ url: 'payment/v2/setting', method: 'get' }), { code: 'ERR_A108' })
|
|
115
|
-
|
|
116
|
-
assert.deepEqual(calls, ['payment/v2/setting', 'self'])
|
|
117
|
-
assert.deepEqual(handled, ['ERR_A108'])
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
test('/self itself and public requests are never resent', async () => {
|
|
121
|
-
installDocument()
|
|
122
|
-
const { request, calls } = createRequest(() => expired())
|
|
123
|
-
request.setToken('token.a')
|
|
124
|
-
|
|
125
|
-
await assert.rejects(request.send({ url: 'self', method: 'get' }), { code: 'ERR_A108' })
|
|
126
|
-
await assert.rejects(request.send({ url: 'getConfig', method: 'get', isPublic: true }), { code: 'ERR_A108' })
|
|
127
|
-
|
|
128
|
-
assert.deepEqual(calls, ['self', 'get-config'])
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
test('a request whose identity changed while renewing is not resent', async () => {
|
|
132
|
-
installDocument()
|
|
133
|
-
let request
|
|
134
|
-
const created = createRequest((config, count) => {
|
|
135
|
-
if (count === 1) return expired()
|
|
136
|
-
request.setToken('token.b')
|
|
137
|
-
return ok()
|
|
138
|
-
})
|
|
139
|
-
request = created.request
|
|
140
|
-
request.setToken('token.a')
|
|
141
|
-
|
|
142
|
-
await assert.rejects(request.send({ url: 'payment/v2/setting', method: 'get' }), { code: 'ERR_A108' })
|
|
143
|
-
|
|
144
|
-
assert.deepEqual(created.calls, ['payment/v2/setting', 'self'])
|
|
145
|
-
assert.deepEqual(created.handled, [])
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
test('ABANDON_GAME leaves a game without calling game/logout', async () => {
|
|
149
|
-
installDocument()
|
|
150
|
-
const sent = []
|
|
151
|
-
const request = {
|
|
152
|
-
config: { appSecret: 'secret' },
|
|
153
|
-
async send(descriptor) {
|
|
154
|
-
sent.push(descriptor.url)
|
|
155
|
-
return { type: 'url', redirect_url: 'https://game.test', wallet_type: 'CASH' }
|
|
156
|
-
},
|
|
157
|
-
}
|
|
158
|
-
globalThis.window = { location: { origin: 'https://site.test', href: 'https://site.test/play', search: '' } }
|
|
159
|
-
const service = interpret(game(request)).start()
|
|
160
|
-
|
|
161
|
-
service.send({
|
|
162
|
-
type: 'ENTER_GAME',
|
|
163
|
-
data: { game: { providerId: 'P', gameId: 'G', gameTypeId: 'SLOT', mappingId: 1 }, locale: 'en-US' },
|
|
164
|
-
})
|
|
165
|
-
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
166
|
-
assert.ok(service.state.matches('flow.playing'))
|
|
167
|
-
assert.equal(service.state.context.gameURL, 'https://game.test')
|
|
168
|
-
|
|
169
|
-
service.send({ type: 'ABANDON_GAME' })
|
|
170
|
-
assert.ok(service.state.matches('flow.idle'))
|
|
171
|
-
assert.equal(service.state.context.gameURL, undefined)
|
|
172
|
-
|
|
173
|
-
service.send({ type: 'EXIT_GAME', data: 'uid' })
|
|
174
|
-
assert.ok(service.state.matches('flow.idle'))
|
|
175
|
-
assert.ok(!sent.includes('game/logout'))
|
|
176
|
-
service.stop()
|
|
177
|
-
})
|