@jetthai/cuora 1.1.488-dev.1 → 1.2.0

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.
Files changed (221) hide show
  1. package/.husky/pre-commit +3 -1
  2. package/.prettierignore +1 -2
  3. package/CHANGELOG.md +0 -746
  4. package/README.md +15 -299
  5. package/package.json +3 -6
  6. package/resetGitBranch.sh +101 -0
  7. package/src/api/activity.js +39 -24
  8. package/src/api/auth.js +35 -4
  9. package/src/api/common.js +1 -8
  10. package/src/api/game.js +10 -0
  11. package/src/api/index.js +1 -3
  12. package/src/api/payment.js +55 -18
  13. package/src/api/user.js +16 -11
  14. package/src/index.js +2 -2
  15. package/src/lib/fingerprint_v3_11_12.js +11433 -0
  16. package/src/machine/activity/activity.js +209 -0
  17. package/src/machine/activity/activity_cash.js +18 -71
  18. package/src/machine/activity/diamond_shop_activity_cash.js +1 -0
  19. package/src/machine/activity/diamond_shop_activity_fc.js +196 -0
  20. package/src/machine/activity/diamond_shop_activity_vn.js +178 -0
  21. package/src/machine/activity/fafafa_activity.js +10 -0
  22. package/src/machine/activity/index.js +12 -0
  23. package/src/machine/activity/invite_friends_activity.js +114 -0
  24. package/src/machine/activity/mini_game_activity.js +68 -0
  25. package/src/machine/activity/return_bonus_activity.js +153 -0
  26. package/src/machine/activity/roulette_activity_v2.js +0 -5
  27. package/src/machine/auth.js +140 -192
  28. package/src/machine/auth_otp_v2.js +177 -0
  29. package/src/machine/auth_otp_v3.js +32 -111
  30. package/src/machine/bank_binding.js +2 -2
  31. package/src/machine/bank_binding_amb.js +145 -0
  32. package/src/machine/bonus.js +75 -0
  33. package/src/machine/common.js +91 -27
  34. package/src/machine/deposit/amb.js +92 -0
  35. package/src/machine/deposit/crypto_bank.js +325 -0
  36. package/src/machine/deposit/crypto_bank_cash.js +8 -19
  37. package/src/machine/deposit/crypto_qrcode.js +148 -0
  38. package/src/machine/deposit/crypto_qrcode_cash.js +41 -26
  39. package/src/machine/deposit/crypto_way.js +247 -0
  40. package/src/machine/deposit/crypto_way_cash.js +32 -52
  41. package/src/machine/deposit/deposit_fc.js +271 -0
  42. package/src/machine/deposit/direct.js +310 -0
  43. package/src/machine/deposit/direct_cash.js +9 -8
  44. package/src/machine/deposit/float.js +374 -0
  45. package/src/machine/deposit/index.js +28 -0
  46. package/src/machine/deposit/krway.js +151 -0
  47. package/src/machine/deposit/loadcard_cash.js +31 -14
  48. package/src/machine/deposit/loadcard_vn.js +351 -0
  49. package/src/machine/deposit/manual_cash.js +10 -8
  50. package/src/machine/deposit/manual_vn.js +458 -0
  51. package/src/machine/deposit/qrcode_cash.js +30 -14
  52. package/src/machine/deposit/qrcode_v2.js +338 -0
  53. package/src/machine/deposit/usdt.js +144 -0
  54. package/src/machine/deposit/virtual_bank_cash.js +2 -5
  55. package/src/machine/deposit/virtual_bank_vn.js +269 -0
  56. package/src/machine/deposit/vpay.js +367 -0
  57. package/src/machine/error.js +0 -7
  58. package/src/machine/game_hot_new.js +79 -0
  59. package/src/machine/game_info.js +12 -5
  60. package/src/machine/game_replay.js +46 -0
  61. package/src/machine/game_search_v2.js +401 -0
  62. package/src/machine/game_v2_1.js +88 -58
  63. package/src/machine/honor_board.js +132 -0
  64. package/src/machine/honor_board_cash.js +18 -15
  65. package/src/machine/inbox.js +12 -0
  66. package/src/machine/index.js +32 -0
  67. package/src/machine/leaderboard.js +11 -8
  68. package/src/machine/merge.js +57 -0
  69. package/src/machine/payment_data.js +86 -0
  70. package/src/machine/promotions.js +170 -0
  71. package/src/machine/ranking.js +25 -43
  72. package/src/machine/reward.js +255 -0
  73. package/src/machine/reward_cash.js +41 -2
  74. package/src/machine/reward_fc.js +98 -0
  75. package/src/machine/roulette_v2.js +25 -20
  76. package/src/machine/side.js +154 -0
  77. package/src/machine/side_cash.js +2 -3
  78. package/src/machine/side_v2.js +209 -0
  79. package/src/machine/user_reset_password.js +15 -4
  80. package/src/machine/user_sign_up.js +60 -49
  81. package/src/machine/vip_table.js +57 -0
  82. package/src/machine/websocket.js +325 -378
  83. package/src/machine/welcome.js +110 -0
  84. package/src/machine/withdraw/index.js +6 -0
  85. package/src/machine/withdraw/withdraw_crypto_cash.js +327 -0
  86. package/src/machine/withdraw/withdraw_krway.js +222 -0
  87. package/src/machine/withdraw/withdraw_manual.js +533 -0
  88. package/src/machine/withdraw/withdraw_manual_cash.js +43 -74
  89. package/src/request/config.js +0 -2
  90. package/src/request/request.js +64 -231
  91. package/src/util/constant.js +2 -12
  92. package/src/util/deviceToken.js +59 -0
  93. package/src/util/index.js +4 -2
  94. package/src/util/math.js +4 -0
  95. package/.claude/commands/sync-ai-prompts.md +0 -180
  96. package/.claude/settings.local.json +0 -25
  97. package/.github/copilot-instructions.md +0 -459
  98. package/.husky/post-checkout +0 -32
  99. package/.husky/pre-push +0 -36
  100. package/AGENTS.md +0 -459
  101. package/AI_PROMPT_SOURCE.md +0 -457
  102. package/CLAUDE.md +0 -459
  103. package/GEMINI.md +0 -459
  104. package/docs/kibana/crypto-way-deposit-amount-bug-analysis.md +0 -1003
  105. package/docs/kibana/play-providerId-error.md +0 -202
  106. package/docs/kibana/play-selectedWalletType.md +0 -1
  107. package/docs/kibana/play-selectedWalletType_2.md +0 -1
  108. package/docs/kibana/play-selectedWalletType_analysis.md +0 -565
  109. package/docs/my-claude/feat-sharon-JT-1790247240-socket/branch-transfer.md +0 -14
  110. package/docs/my-claude/feat-sharon-JT-1790247240-socket/cash-module-preparation.json +0 -40
  111. package/docs/my-claude/feat-sharon-JT-1790247240-socket/cash-module-verification.md +0 -41
  112. package/docs/my-claude/feat-sharon-JT-1790247240-socket/implementation-ledger.md +0 -87
  113. package/docs/my-claude/feat-sharon-JT-1790247240-socket/module-report.md +0 -73
  114. package/docs/my-claude/feat-sharon-JT-1790247240-socket/plan.md +0 -309
  115. package/docs/my-claude/feat-sharon-JT-1790247240-socket/test-cases.md +0 -245
  116. package/docs/my-claude/feat-sharon-JT-1791180540-onesignal/transfer.md +0 -9
  117. package/e2e/feature-map.md +0 -19
  118. package/prepare-for-pr/JT-1776243420.md +0 -85
  119. package/prepare-for-pr/TI-16026/STATE.md +0 -18
  120. package/prepare-for-pr/TI-16026/after-prepare-authorization.md +0 -3
  121. package/prepare-for-pr/TI-16026/analysis.md +0 -38
  122. package/prepare-for-pr/TI-16026/answer-analyze-01.md +0 -1
  123. package/prepare-for-pr/TI-16026/answer-commit-01.md +0 -2
  124. package/prepare-for-pr/TI-16026/answer-commit-02.md +0 -1
  125. package/prepare-for-pr/TI-16026/bundle.json +0 -18
  126. package/prepare-for-pr/TI-16026/checklist.md +0 -196
  127. package/prepare-for-pr/TI-16026/checklist.unit-test-original.md +0 -39
  128. package/prepare-for-pr/TI-16026/claude-after-model +0 -1
  129. package/prepare-for-pr/TI-16026/claude-commit-session +0 -1
  130. package/prepare-for-pr/TI-16026/claude-harvest-session +0 -1
  131. package/prepare-for-pr/TI-16026/claude-hops/01-analyze.err +0 -1
  132. package/prepare-for-pr/TI-16026/claude-hops/01-analyze.out +0 -2
  133. package/prepare-for-pr/TI-16026/claude-hops/02-analyze.err +0 -1
  134. package/prepare-for-pr/TI-16026/claude-hops/02-analyze.out +0 -4
  135. package/prepare-for-pr/TI-16026/claude-hops/03-dev.err +0 -1
  136. package/prepare-for-pr/TI-16026/claude-hops/03-dev.out +0 -4
  137. package/prepare-for-pr/TI-16026/claude-hops/04-dev.err +0 -1
  138. package/prepare-for-pr/TI-16026/claude-hops/04-dev.out +0 -4
  139. package/prepare-for-pr/TI-16026/claude-hops/05-dev.err +0 -1
  140. package/prepare-for-pr/TI-16026/claude-hops/05-dev.out +0 -3
  141. package/prepare-for-pr/TI-16026/claude-hops/06-after.err +0 -1
  142. package/prepare-for-pr/TI-16026/claude-hops/06-after.out +0 -2
  143. package/prepare-for-pr/TI-16026/claude-hops/07-commit.err +0 -1
  144. package/prepare-for-pr/TI-16026/claude-hops/07-commit.out +0 -2
  145. package/prepare-for-pr/TI-16026/claude-hops/08-commit.err +0 -1
  146. package/prepare-for-pr/TI-16026/claude-hops/08-commit.out +0 -2
  147. package/prepare-for-pr/TI-16026/claude-hops/09-commit.err +0 -1
  148. package/prepare-for-pr/TI-16026/claude-hops/09-commit.out +0 -8
  149. package/prepare-for-pr/TI-16026/claude-hops/10-harvest.err +0 -1
  150. package/prepare-for-pr/TI-16026/claude-hops/10-harvest.out +0 -3
  151. package/prepare-for-pr/TI-16026/claude-hops/11-harvest.err +0 -1
  152. package/prepare-for-pr/TI-16026/claude-hops/11-harvest.out +0 -3
  153. package/prepare-for-pr/TI-16026/commit-note.md +0 -5
  154. package/prepare-for-pr/TI-16026/evidence-index.json +0 -215
  155. package/prepare-for-pr/TI-16026/feature-map.before.md +0 -61
  156. package/prepare-for-pr/TI-16026/harvest-redaction.json +0 -34
  157. package/prepare-for-pr/TI-16026/harvest-redaction.out +0 -37
  158. package/prepare-for-pr/TI-16026/harvest.md +0 -46
  159. package/prepare-for-pr/TI-16026/lessons.md +0 -5
  160. package/prepare-for-pr/TI-16026/note-harvest-redaction.md +0 -1
  161. package/prepare-for-pr/TI-16026/open-questions.md +0 -6
  162. package/prepare-for-pr/TI-16026/publish.md +0 -32
  163. package/prepare-for-pr/TI-16026/review-convergence-response.md +0 -3
  164. package/prepare-for-pr/TI-16026/review-input/scope.md +0 -18
  165. package/prepare-for-pr/TI-16026/review-log.md +0 -57
  166. package/prepare-for-pr/TI-16026/review-notes.md +0 -97
  167. package/prepare-for-pr/TI-16026/review-response-r1.md +0 -7
  168. package/prepare-for-pr/TI-16026/self-test-report.md +0 -67
  169. package/prepare-for-pr/TI-16026/spec-proposal.md +0 -34
  170. package/prepare-for-pr/TI-16026/spec-snapshot.md +0 -291
  171. package/prepare-for-pr/TI-16026/state-log.md +0 -57
  172. package/prepare-for-pr/TI-16026/user-messages/01.md +0 -12
  173. package/prepare-for-pr/TI-16026/user-messages/02.md +0 -20
  174. package/prepare-for-pr/TI-16026/user-messages/03.md +0 -1
  175. package/prepare-for-pr/TI-16026/user-messages/04.md +0 -1
  176. package/prepare-for-pr/TI-16026/user-messages/05.md +0 -1
  177. package/prepare-for-pr/TI-16026/user-messages/06.md +0 -1
  178. package/prepare-for-pr/TI-16026/user-messages/07.md +0 -1
  179. package/prepare-for-pr/TI-16026/user-messages/08.md +0 -1
  180. package/prepare-for-pr/TI-16026/user-messages/09.md +0 -1
  181. package/prepare-for-pr/TI-16026/user-messages/10.md +0 -1
  182. package/prepare-for-pr/TI-16026/user-messages/11.md +0 -1
  183. package/prepare-for-pr/TI-16026/user-messages/12.md +0 -1
  184. package/prepare-for-pr/TI-16026/user-messages/13.md +0 -1
  185. package/prepare-for-pr/TI-16026/user-messages/14.md +0 -1
  186. package/prepare-for-pr/TI-16026/user-messages/15.md +0 -1
  187. package/prepare-for-pr/TI-16026/user-messages/16.md +0 -1
  188. package/prepare-for-pr/TI-16026/user-messages/17.md +0 -1
  189. package/prepare-for-pr/TI-16026/user-messages/18.md +0 -10
  190. package/prepare-for-pr/TI-16026/user-messages/19.md +0 -1
  191. package/prepare-for-pr/TI-16026/user-messages/20.md +0 -1
  192. package/prepare-for-pr/TI-16026/user-messages/21.md +0 -1
  193. package/prepare-for-pr/TI-16026/user-messages/22.md +0 -1
  194. package/prepare-for-pr/TI-16026/user-messages/23.md +0 -8
  195. package/prepare-for-pr/TI-16026/user-messages/24.md +0 -24
  196. package/prepare-for-pr/TI-16026/user-question.md +0 -24
  197. package/prepare-for-pr/TI-16026/verify/metrics-check.json +0 -13
  198. package/prepare-for-pr/TI-16026/verify/redaction.json +0 -38
  199. package/prepare-for-pr/TI-16026/verify/row-closure.json +0 -45
  200. package/prepare-for-pr/TI-16026/verify/s04-ws-capture.md +0 -23
  201. package/prepare-for-pr/TI-16026/verify-after.md +0 -23
  202. package/prepare-for-pr/TI-16026/verify.json +0 -80
  203. package/prepare-for-pr/TI-16026.md +0 -408
  204. package/scripts/auto-version.js +0 -69
  205. package/scripts/auto-version.sh +0 -33
  206. package/scripts/branch-auto-npm-publish.js +0 -36
  207. package/scripts/check-version.sh +0 -36
  208. package/scripts/clean-tags.sh +0 -84
  209. package/src/api/account.js +0 -7
  210. package/src/api/otp.js +0 -5
  211. package/src/util/amount.js +0 -8
  212. package/src/util/err998SentryExtra.js +0 -26
  213. package/src/util/paramStore.js +0 -139
  214. package/src/util/socketPolicy.js +0 -57
  215. package/src/util/tokenStore.js +0 -44
  216. package/test/auth_otp_v3.test.js +0 -323
  217. package/test/auth_self_timeout.test.js +0 -171
  218. package/test/request_self.test.js +0 -288
  219. package/test/withdraw_manual_cash.test.js +0 -149
  220. package/tests/session.test.mjs +0 -177
  221. package/tests/websocket.test.mjs +0 -779
@@ -1,323 +0,0 @@
1
- import test from 'node:test'
2
- import assert from 'node:assert/strict'
3
- import { interpret } from 'xstate'
4
- import authOtpV3 from '../src/machine/auth_otp_v3.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
- test('phone entry skips OTP only after /self confirms phone verification', async () => {
19
- let selfReads = 0
20
- const requests = []
21
- const request = {
22
- async send(descriptor) {
23
- requests.push(descriptor.url)
24
- if (descriptor.url === 'self') {
25
- selfReads++
26
- return {
27
- phone: selfReads === 1 ? null : '123456789',
28
- phone_verification_status: selfReads > 1,
29
- email: null,
30
- email_verification_status: false,
31
- }
32
- }
33
- if (descriptor.url === 'otp-verification-point/verify') return { needAcknowledge: false }
34
- throw new Error(`Unexpected request: ${descriptor.url}`)
35
- },
36
- }
37
- const service = interpret(authOtpV3(request)).start()
38
- try {
39
- service.send({ type: 'SET_STRATEGY', data: 'phone' })
40
- await waitForState(service, 'notVerified.phone')
41
- service.send({ type: 'SEND_PHONE', data: { countryCode: '886', phone: '123456789' } })
42
- await waitForState(service, 'verified')
43
- assert.equal(selfReads, 2)
44
- assert.deepEqual(requests, ['self', 'otp-verification-point/verify', 'self'])
45
- } finally {
46
- service.stop()
47
- }
48
- })
49
-
50
- test('email entry skips OTP only after /self confirms email verification', async () => {
51
- let selfReads = 0
52
- const request = {
53
- async send(descriptor) {
54
- if (descriptor.url === 'self') {
55
- selfReads++
56
- return {
57
- phone: null,
58
- phone_verification_status: false,
59
- email: selfReads === 1 ? null : 'test@example.com',
60
- email_verification_status: selfReads > 1,
61
- }
62
- }
63
- if (descriptor.url === 'otp-verification-point/verify') return { needAcknowledge: false }
64
- throw new Error(`Unexpected request: ${descriptor.url}`)
65
- },
66
- }
67
- const service = interpret(authOtpV3(request)).start()
68
- try {
69
- service.send({ type: 'SET_STRATEGY', data: 'email' })
70
- await waitForState(service, 'notVerified.email')
71
- service.send({ type: 'SEND_EMAIL', data: { email: 'test@example.com' } })
72
- await waitForState(service, 'verified')
73
- assert.equal(selfReads, 2)
74
- } finally {
75
- service.stop()
76
- }
77
- })
78
-
79
- test('existing phone auto verification checks /self before finishing', async () => {
80
- let selfReads = 0
81
- const request = {
82
- async send(descriptor) {
83
- if (descriptor.url === 'self') {
84
- selfReads++
85
- return {
86
- phone: '123456789',
87
- phone_verification_status: selfReads > 1,
88
- email: null,
89
- email_verification_status: false,
90
- }
91
- }
92
- if (descriptor.url === 'otp-verification-point/verify') return { needAcknowledge: false }
93
- throw new Error(`Unexpected request: ${descriptor.url}`)
94
- },
95
- }
96
- const service = interpret(authOtpV3(request)).start()
97
- try {
98
- service.send({ type: 'SET_STRATEGY', data: 'phone' })
99
- await waitForState(service, 'verified')
100
- assert.equal(selfReads, 2)
101
- } finally {
102
- service.stop()
103
- }
104
- })
105
-
106
- test('needAcknowledge false never grants verification when /self is still unverified', async () => {
107
- let verifyCalls = 0
108
- const request = {
109
- async send(descriptor) {
110
- if (descriptor.url === 'self') {
111
- return { phone: null, phone_verification_status: false, email: null, email_verification_status: false }
112
- }
113
- if (descriptor.url === 'otp-verification-point/verify') {
114
- verifyCalls++
115
- return { needAcknowledge: false }
116
- }
117
- throw new Error(`Unexpected request: ${descriptor.url}`)
118
- },
119
- }
120
- const service = interpret(authOtpV3(request)).start()
121
- try {
122
- service.send({ type: 'SET_STRATEGY', data: 'phone' })
123
- await waitForState(service, 'notVerified.phone')
124
- service.send({ type: 'SEND_PHONE', data: { countryCode: '886', phone: '123456789' } })
125
- await waitForState(service, 'autoVerifyWaiting')
126
- const stale = { phone: '123456789', phone_verification_status: false, email: null, email_verification_status: false }
127
- service.send({ type: 'USER_INFO_UPDATED', data: stale })
128
- assert.equal(service.state.matches('autoVerifyWaiting'), true)
129
- service.send({ type: 'USER_INFO_UPDATED', data: stale })
130
- assert.equal(service.state.matches('fail'), true)
131
- assert.equal(verifyCalls, 1)
132
- } finally {
133
- service.stop()
134
- }
135
- })
136
-
137
- const autoVerifyEntries = [
138
- {
139
- name: 'existing phone',
140
- strategy: 'phone',
141
- initialUser: { phone: '123456789', phone_verification_status: false },
142
- },
143
- {
144
- name: 'filled phone',
145
- strategy: 'phone',
146
- initialUser: { phone: null, phone_verification_status: false },
147
- fill: { type: 'SEND_PHONE', data: { countryCode: '886', phone: '123456789' } },
148
- waitFor: 'notVerified.phone',
149
- },
150
- {
151
- name: 'filled email',
152
- strategy: 'email',
153
- initialUser: { email: null, email_verification_status: false },
154
- fill: { type: 'SEND_EMAIL', data: { email: 'test@example.com' } },
155
- waitFor: 'notVerified.email',
156
- },
157
- ]
158
-
159
- // 第 1 次 /self 為進入流程時抓的,第 2 次為 verify 後主動打的;兩次都尚未反映驗證結果
160
- function staleSelfRequest(strategy, initialUser) {
161
- const requests = []
162
- const contact = strategy === 'phone' ? { phone: '123456789' } : { email: 'test@example.com' }
163
- return {
164
- requests,
165
- contact,
166
- async send(descriptor) {
167
- requests.push(descriptor.url)
168
- if (descriptor.url === 'self') {
169
- return requests.filter((url) => url === 'self').length === 1 ? { ...initialUser } : { ...initialUser, ...contact }
170
- }
171
- if (descriptor.url === 'otp-verification-point/verify') return { needAcknowledge: false }
172
- throw new Error(`Unexpected request: ${descriptor.url}`)
173
- },
174
- }
175
- }
176
-
177
- async function startAutoVerify(entry) {
178
- const request = staleSelfRequest(entry.strategy, entry.initialUser)
179
- const service = interpret(authOtpV3(request)).start()
180
- service.send({ type: 'SET_STRATEGY', data: entry.strategy })
181
- if (entry.fill) {
182
- await waitForState(service, entry.waitFor)
183
- service.send(entry.fill)
184
- }
185
- await waitForState(service, 'autoVerifyWaiting')
186
- return { service, request }
187
- }
188
-
189
- for (const entry of autoVerifyEntries) {
190
- test(`${entry.name}: stale /self waits and passes on a later polled /self`, async () => {
191
- const { service, request } = await startAutoVerify(entry)
192
- try {
193
- const statusKey = `${entry.strategy}_verification_status`
194
- service.send({ type: 'USER_INFO_UPDATED', data: { ...request.contact, [statusKey]: false } })
195
- assert.equal(service.state.matches('autoVerifyWaiting'), true)
196
- service.send({ type: 'USER_INFO_UPDATED', data: { ...request.contact, [statusKey]: true } })
197
- assert.equal(service.state.matches('verified'), true)
198
- assert.equal(service.state.context.userInfo[statusKey], true)
199
- assert.deepEqual(request.requests, ['self', 'otp-verification-point/verify', 'self'])
200
- } finally {
201
- service.stop()
202
- }
203
- })
204
-
205
- test(`${entry.name}: two stale polled /self responses fail`, async () => {
206
- const { service, request } = await startAutoVerify(entry)
207
- try {
208
- const stale = { ...request.contact, [`${entry.strategy}_verification_status`]: false }
209
- service.send({ type: 'USER_INFO_UPDATED', data: stale })
210
- service.send({ type: 'USER_INFO_UPDATED', data: stale })
211
- assert.equal(service.state.matches('fail'), true)
212
- assert.deepEqual(request.requests, ['self', 'otp-verification-point/verify', 'self'])
213
- } finally {
214
- service.stop()
215
- }
216
- })
217
- }
218
-
219
- test('auto verify waiting fails after the 5 second limit without polled /self', async (t) => {
220
- t.mock.timers.enable({ apis: ['setTimeout'] })
221
- const { service } = await startAutoVerify(autoVerifyEntries[0])
222
- try {
223
- t.mock.timers.tick(4999)
224
- assert.equal(service.state.matches('autoVerifyWaiting'), true)
225
- t.mock.timers.tick(1)
226
- assert.equal(service.state.matches('fail'), true)
227
- } finally {
228
- service.stop()
229
- }
230
- })
231
-
232
- test('the 5 second limit is not reset by a stale polled /self', async (t) => {
233
- t.mock.timers.enable({ apis: ['setTimeout'] })
234
- const { service, request } = await startAutoVerify(autoVerifyEntries[0])
235
- try {
236
- t.mock.timers.tick(4000)
237
- service.send({ type: 'USER_INFO_UPDATED', data: { ...request.contact, phone_verification_status: false } })
238
- t.mock.timers.tick(1000)
239
- assert.equal(service.state.matches('fail'), true)
240
- } finally {
241
- service.stop()
242
- }
243
- })
244
-
245
- test('USER_INFO_UPDATED outside auto verify waiting does not change state', async () => {
246
- const request = {
247
- async send(descriptor) {
248
- if (descriptor.url === 'self') return { phone: null, phone_verification_status: false }
249
- throw new Error(`Unexpected request: ${descriptor.url}`)
250
- },
251
- }
252
- const service = interpret(authOtpV3(request)).start()
253
- try {
254
- service.send({ type: 'SET_STRATEGY', data: 'phone' })
255
- await waitForState(service, 'notVerified.phone')
256
- service.send({ type: 'USER_INFO_UPDATED', data: { phone: '123456789', phone_verification_status: true } })
257
- assert.equal(service.state.matches('notVerified.phone'), true)
258
- } finally {
259
- service.stop()
260
- }
261
- })
262
-
263
- test('needAcknowledge true keeps the existing OTP input route', async () => {
264
- const request = {
265
- async send(descriptor) {
266
- if (descriptor.url === 'self') {
267
- return { phone: null, phone_verification_status: false, email: null, email_verification_status: false }
268
- }
269
- if (descriptor.url === 'otp-verification-point/verify') {
270
- return { needAcknowledge: true, request_id: 'test-request', ttl: 60000, countryCode: '886', phone: '123456789' }
271
- }
272
- throw new Error(`Unexpected request: ${descriptor.url}`)
273
- },
274
- }
275
- const service = interpret(authOtpV3(request)).start()
276
- try {
277
- service.send({ type: 'SET_STRATEGY', data: 'phone' })
278
- await waitForState(service, 'notVerified.phone')
279
- service.send({ type: 'SEND_PHONE', data: { countryCode: '886', phone: '123456789' } })
280
- await waitForState(service, 'notVerified.otp')
281
- assert.equal(service.state.context.requestId, 'test-request')
282
- } finally {
283
- service.stop()
284
- }
285
- })
286
-
287
- test('email without an address still requires input even if its verification flag is true', async () => {
288
- const request = {
289
- async send(descriptor) {
290
- if (descriptor.url === 'self') {
291
- return { phone: '123456789', phone_verification_status: true, email: null, email_verification_status: true }
292
- }
293
- throw new Error(`Unexpected request: ${descriptor.url}`)
294
- },
295
- }
296
- const service = interpret(authOtpV3(request)).start()
297
- try {
298
- service.send({ type: 'SET_STRATEGY', data: 'email' })
299
- await waitForState(service, 'notVerified.email')
300
- assert.equal(service.state.matches('verified'), false)
301
- } finally {
302
- service.stop()
303
- }
304
- })
305
-
306
- test('phone without a number still requires input even if its verification flag is true', async () => {
307
- const request = {
308
- async send(descriptor) {
309
- if (descriptor.url === 'self') {
310
- return { phone: null, phone_verification_status: true, email: 'test@example.com', email_verification_status: true }
311
- }
312
- throw new Error(`Unexpected request: ${descriptor.url}`)
313
- },
314
- }
315
- const service = interpret(authOtpV3(request)).start()
316
- try {
317
- service.send({ type: 'SET_STRATEGY', data: 'phone' })
318
- await waitForState(service, 'notVerified.phone')
319
- assert.equal(service.state.matches('verified'), false)
320
- } finally {
321
- service.stop()
322
- }
323
- })
@@ -1,171 +0,0 @@
1
- import test from 'node:test'
2
- import assert from 'node:assert/strict'
3
- import { registerHooks } from 'node:module'
4
- import { interpret } from 'xstate'
5
-
6
- // src/api/auth.js 以無副檔名的 'lodash/pickBy' 匯入(專案慣例,交由下游 bundler 解析),Node 原生 ESM 需補副檔名
7
- registerHooks({
8
- resolve: (specifier, context, nextResolve) => nextResolve(specifier === 'lodash/pickBy' ? 'lodash/pickBy.js' : specifier, context),
9
- })
10
-
11
- const store = new Map()
12
- globalThis.localStorage = {
13
- getItem: (k) => (store.has(k) ? store.get(k) : null),
14
- setItem: (k, v) => store.set(k, v),
15
- removeItem: (k) => store.delete(k),
16
- }
17
- globalThis.window = { atob: (value) => Buffer.from(value, 'base64').toString(), location: { search: '', protocol: 'https:' } }
18
-
19
- const { default: auth } = await import('../src/machine/auth.js')
20
-
21
- const TIMEOUT = { code: 'ERR_998', endpoint: 'self', isTimeout: true }
22
- const NETWORK = { code: 'ERR_998', endpoint: 'self' }
23
- // 不帶 rank:isNotInline 成立,inline 立即轉 login(多數帳號的實際路徑)
24
- const USER = { uid_site: 'u@test' }
25
- const INLINE_USER = { uid_site: 'u@test', rank: 1 }
26
- const siteConfig = Buffer.from(JSON.stringify({ loginMethods: {}, registerMethods: {}, dtVerify: false })).toString('base64')
27
-
28
- // selfResults 依序決定每一筆 /self 的結果(Error 物件以外視為成功回應);用完後一律成功
29
- function start({ token = null, selfResults = [] } = {}) {
30
- store.clear()
31
- const calls = { self: 0, logout: 0 }
32
- // auth machine 只經 Request 的 token API 讀寫 token(實作是 cookie),這裡以記憶體值代替
33
- const session = { token }
34
- const request = {
35
- config: { appSecret: 's', clientId: 'test' },
36
- getToken: () => session.token,
37
- setToken: (value) => {
38
- session.token = value || null
39
- },
40
- extendToken: () => session.token,
41
- removeToken: () => {
42
- session.token = null
43
- },
44
- async send(descriptor) {
45
- if (descriptor.url === 'getConfig') return { config: siteConfig }
46
- if (descriptor.url === 'login') return { token: 'token-new' }
47
- if (descriptor.url === 'logout') return void calls.logout++
48
- if (descriptor.url === 'self') {
49
- const result = calls.self < selfResults.length ? selfResults[calls.self] : USER
50
- calls.self++
51
- if (result.code) throw result
52
- return result
53
- }
54
- throw new Error(`Unexpected request: ${descriptor.url}`)
55
- },
56
- }
57
- const machine = auth(request)
58
- const visited = []
59
- const service = interpret(machine.withContext({ ...machine.context, tick: 5 }))
60
- service.onTransition((state) => visited.push(state.toStrings().at(-1)))
61
- service.start()
62
- return { service, calls, visited, session }
63
- }
64
-
65
- function waitFor(service, predicate) {
66
- return new Promise((resolve, reject) => {
67
- const timer = setTimeout(() => reject(new Error(`Timed out, state: ${JSON.stringify(service.state.value)}`)), 1000)
68
- const check = (state) => {
69
- if (!predicate(state)) return
70
- clearTimeout(timer)
71
- resolve(state)
72
- }
73
- service.onTransition(check)
74
- })
75
- }
76
-
77
- const count = (visited, name) => visited.filter((value) => value === name).length
78
-
79
- test('開站帶 token:/self 逾時不登出,重發到成功後完成登入', async () => {
80
- const { service, calls, visited, session } = start({ token: 'token-A', selfResults: [TIMEOUT, TIMEOUT] })
81
- try {
82
- await waitFor(service, (state) => state.matches('login.pending'))
83
- assert.equal(count(visited, 'leaving'), 0)
84
- assert.equal(count(visited, 'logout'), 0)
85
- assert.equal(calls.logout, 0)
86
- assert.equal(service.state.context.token, 'token-A')
87
- assert.deepEqual(service.state.context.userInfo, USER)
88
- assert.equal(session.token, 'token-A')
89
- } finally {
90
- service.stop()
91
- }
92
- })
93
-
94
- test('開站帶 token:一般 ERR_998(非逾時)維持原行為,走 leaving 登出', async () => {
95
- const { service, calls, visited, session } = start({ token: 'token-A', selfResults: [NETWORK] })
96
- try {
97
- await waitFor(service, (state) => state.matches('idle') && visited.includes('logoutFinish'))
98
- assert.equal(count(visited, 'leaving'), 1)
99
- assert.equal(calls.self, 1)
100
- assert.equal(session.token, null)
101
- } finally {
102
- service.stop()
103
- }
104
- })
105
-
106
- test('剛登入:/self 逾時改在 userInfoRetrying 重發,authenticated 只進入一次', async () => {
107
- const { service, calls, visited } = start({ selfResults: [TIMEOUT, TIMEOUT] })
108
- try {
109
- await waitFor(service, (state) => state.matches('idle'))
110
- service.send({ type: 'LOGIN_ID', data: { username: 'u', password: 'p' } })
111
- await waitFor(service, (state) => state.matches('otpChecking'))
112
- assert.equal(count(visited, 'authenticated'), 1)
113
- assert.equal(count(visited, 'userInfoRetrying'), 2)
114
- assert.equal(count(visited, 'leaving'), 0)
115
- assert.equal(calls.self, 3)
116
- assert.equal(service.state.context.token, 'token-new')
117
- assert.deepEqual(service.state.context.userInfo, USER)
118
- } finally {
119
- service.stop()
120
- }
121
- })
122
-
123
- test('剛登入:重發期間遇到非逾時錯誤仍走 leaving', async () => {
124
- const { service, visited } = start({ selfResults: [TIMEOUT, { code: 'ERR_A113' }] })
125
- try {
126
- await waitFor(service, (state) => state.matches('idle'))
127
- service.send({ type: 'LOGIN_ID', data: { username: 'u', password: 'p' } })
128
- await waitFor(service, (state) => visited.includes('logoutFinish') && state.matches('idle'))
129
- assert.equal(count(visited, 'userInfoRetrying'), 1)
130
- assert.equal(count(visited, 'leaving'), 1)
131
- } finally {
132
- service.stop()
133
- }
134
- })
135
-
136
- test('登入態輪詢:/self 逾時回 pending 等下一輪,不登出', async () => {
137
- // 第 1 筆 tokenAuthenticated、第 2 筆 checkDeviceTokenDone 成功;第 3、4 筆(checkDeviceTokenDone 後的輪詢)逾時
138
- const { service, calls, visited } = start({ token: 'token-A', selfResults: [USER, TIMEOUT, TIMEOUT, TIMEOUT] })
139
- try {
140
- await waitFor(service, () => calls.self >= 6)
141
- assert.equal(count(visited, 'leaving'), 0)
142
- assert.ok(service.state.matches('login'))
143
- assert.equal(service.state.context.token, 'token-A')
144
- assert.deepEqual(service.state.context.userInfo, USER)
145
- } finally {
146
- service.stop()
147
- }
148
- })
149
-
150
- test('登入態輪詢:驗證失敗(非逾時)維持原行為,走 leaving 登出', async () => {
151
- const { service, visited, session } = start({ token: 'token-A', selfResults: [USER, USER, { code: 'ERR_A113' }] })
152
- try {
153
- await waitFor(service, (state) => visited.includes('logoutFinish') && state.matches('idle'))
154
- assert.equal(count(visited, 'leaving'), 1)
155
- assert.equal(session.token, null)
156
- } finally {
157
- service.stop()
158
- }
159
- })
160
-
161
- test('inline 輪詢:/self 逾時回 pending 等下一輪,不登出', async () => {
162
- const { service, calls, visited } = start({ token: 'token-A', selfResults: [INLINE_USER, TIMEOUT, TIMEOUT, INLINE_USER, TIMEOUT] })
163
- try {
164
- await waitFor(service, () => calls.self >= 6)
165
- assert.equal(count(visited, 'leaving'), 0)
166
- assert.ok(visited.includes('inline.fetching'))
167
- assert.equal(service.state.context.token, 'token-A')
168
- } finally {
169
- service.stop()
170
- }
171
- })