@jetthai/cuora 1.1.487-otp.1 → 1.1.488-dev.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.
@@ -0,0 +1,177 @@
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
+ })