Skip to content

Commit ea7f2d5

Browse files
committed
Update tests to use SocketSdkGenericResult
- Replace CResult type usage with SocketSdkGenericResult - Update property access patterns (ok -> success, code -> status, message -> error) - Ensure test assertions work with symmetric result types
1 parent 037f18d commit ea7f2d5

4 files changed

Lines changed: 210 additions & 211 deletions

test/api-response-scenarios.test.mts

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
33

44
import { SocketSdk } from '../dist/index'
55

6-
import type { CResult } from '../dist/index'
6+
import type { SocketSdkGenericResult } from '../dist/index'
77

88
describe('API Response Scenarios', () => {
99
let client: SocketSdk
@@ -30,8 +30,8 @@ describe('API Response Scenarios', () => {
3030
const result1 = (await client.getApi('no-match-case', {
3131
responseType: 'json',
3232
throws: false,
33-
})) as CResult<unknown>
34-
expect(result1.ok).toBe(false)
33+
})) as SocketSdkGenericResult<unknown>
34+
expect(result1.success).toBe(false)
3535

3636
// Case 2: match exists but match[1] is undefined
3737
nock('https://api.socket.dev')
@@ -41,8 +41,8 @@ describe('API Response Scenarios', () => {
4141
const result2 = (await client.getApi('match-undefined-case', {
4242
responseType: 'json',
4343
throws: false,
44-
})) as CResult<unknown>
45-
expect(result2.ok).toBe(false)
44+
})) as SocketSdkGenericResult<unknown>
45+
expect(result2.success).toBe(false)
4646

4747
// Case 3: match exists and match[1] is empty string
4848
nock('https://api.socket.dev')
@@ -52,8 +52,8 @@ describe('API Response Scenarios', () => {
5252
const result3 = (await client.getApi('match-empty-case', {
5353
responseType: 'json',
5454
throws: false,
55-
})) as CResult<unknown>
56-
expect(result3.ok).toBe(false)
55+
})) as SocketSdkGenericResult<unknown>
56+
expect(result3.success).toBe(false)
5757

5858
// Case 4: match exists and match[1] has content
5959
nock('https://api.socket.dev')
@@ -63,8 +63,8 @@ describe('API Response Scenarios', () => {
6363
const result4 = (await client.getApi('match-content-case', {
6464
responseType: 'json',
6565
throws: false,
66-
})) as CResult<unknown>
67-
expect(result4.ok).toBe(false)
66+
})) as SocketSdkGenericResult<unknown>
67+
expect(result4.success).toBe(false)
6868
})
6969

7070
it('should test all variations of preview.slice(0, 100) || "" branch', async () => {
@@ -74,9 +74,9 @@ describe('API Response Scenarios', () => {
7474
const result1 = (await client.getApi('empty-response-text', {
7575
responseType: 'json',
7676
throws: false,
77-
})) as CResult<unknown>
77+
})) as SocketSdkGenericResult<unknown>
7878
// Empty becomes {}
79-
expect(result1.ok).toBe(true)
79+
expect(result1.success).toBe(true)
8080

8181
// Case 2: responseText has content, slice returns non-empty
8282
const content50 = 'a'.repeat(50)
@@ -87,8 +87,8 @@ describe('API Response Scenarios', () => {
8787
const result2 = (await client.getApi('content-50-chars', {
8888
responseType: 'json',
8989
throws: false,
90-
})) as CResult<unknown>
91-
expect(result2.ok).toBe(false)
90+
})) as SocketSdkGenericResult<unknown>
91+
expect(result2.success).toBe(false)
9292
})
9393

9494
it('should test all variations of responseText.length > 100 ternary', async () => {
@@ -107,10 +107,10 @@ describe('API Response Scenarios', () => {
107107
const result = (await client.getApi(`length-${length}`, {
108108
responseType: 'json',
109109
throws: false,
110-
})) as CResult<unknown>
110+
})) as SocketSdkGenericResult<unknown>
111111

112-
expect(result.ok).toBe(false)
113-
if (!result.ok) {
112+
expect(result.success).toBe(false)
113+
if (!result.success) {
114114
if (length > 100) {
115115
expect(result.cause).toContain('...')
116116
} else {
@@ -141,14 +141,14 @@ describe('API Response Scenarios', () => {
141141
// eslint-disable-next-line no-await-in-loop
142142
const resultGet = (await client.getApi(scenario, {
143143
throws: false,
144-
})) as CResult<unknown>
145-
expect(resultGet.ok).toBe(false)
144+
})) as SocketSdkGenericResult<unknown>
145+
expect(resultGet.success).toBe(false)
146146

147147
// eslint-disable-next-line no-await-in-loop
148148
const resultSend = (await client.sendApi(scenario, {
149149
throws: false,
150-
})) as CResult<unknown>
151-
expect(resultSend.ok).toBe(false)
150+
})) as SocketSdkGenericResult<unknown>
151+
expect(resultSend.success).toBe(false)
152152
}
153153
})
154154

@@ -163,19 +163,19 @@ describe('API Response Scenarios', () => {
163163
// eslint-disable-next-line no-await-in-loop
164164
const resultGet = (await client.getApi(test, {
165165
throws: false,
166-
})) as CResult<unknown>
167-
expect(resultGet.ok).toBe(false)
168-
if (!resultGet.ok) {
166+
})) as SocketSdkGenericResult<unknown>
167+
expect(resultGet.success).toBe(false)
168+
if (!resultGet.success) {
169169
expect(typeof resultGet.cause).toBe('string')
170170
expect(resultGet.cause?.length).toBeGreaterThan(0)
171171
}
172172

173173
// eslint-disable-next-line no-await-in-loop
174174
const resultSend = (await client.sendApi(test, {
175175
throws: false,
176-
})) as CResult<unknown>
177-
expect(resultSend.ok).toBe(false)
178-
if (!resultSend.ok) {
176+
})) as SocketSdkGenericResult<unknown>
177+
expect(resultSend.success).toBe(false)
178+
if (!resultSend.success) {
179179
expect(typeof resultSend.cause).toBe('string')
180180
expect(resultSend.cause?.length).toBeGreaterThan(0)
181181
}
@@ -209,8 +209,8 @@ describe('API Response Scenarios', () => {
209209
const result = (await client.getApi(`whitespace-trim-${index}`, {
210210
responseType: 'json',
211211
throws: false,
212-
})) as CResult<unknown>
213-
expect(result.ok).toBe(false)
212+
})) as SocketSdkGenericResult<unknown>
213+
expect(result.success).toBe(false)
214214
}
215215
})
216216

@@ -232,8 +232,8 @@ describe('API Response Scenarios', () => {
232232
// eslint-disable-next-line no-await-in-loop
233233
const result = (await client.getApi(test, {
234234
throws: false,
235-
})) as CResult<unknown>
236-
expect(result.ok).toBe(false)
235+
})) as SocketSdkGenericResult<unknown>
236+
expect(result.success).toBe(false)
237237
}
238238

239239
// Test SyntaxError with "Invalid JSON response" but no regex match
@@ -244,8 +244,8 @@ describe('API Response Scenarios', () => {
244244
const result1 = (await client.getApi('invalid-json-no-regex', {
245245
responseType: 'json',
246246
throws: false,
247-
})) as CResult<unknown>
248-
expect(result1.ok).toBe(false)
247+
})) as SocketSdkGenericResult<unknown>
248+
expect(result1.success).toBe(false)
249249

250250
// Test SyntaxError with regex match but empty capture
251251
nock('https://api.socket.dev')
@@ -255,8 +255,8 @@ describe('API Response Scenarios', () => {
255255
const result2 = (await client.getApi('invalid-json-empty-capture', {
256256
responseType: 'json',
257257
throws: false,
258-
})) as CResult<unknown>
259-
expect(result2.ok).toBe(false)
258+
})) as SocketSdkGenericResult<unknown>
259+
expect(result2.success).toBe(false)
260260
})
261261

262262
it('should test massive combinations of error scenarios', async () => {
@@ -273,14 +273,14 @@ describe('API Response Scenarios', () => {
273273
// eslint-disable-next-line no-await-in-loop
274274
const result = (await client.getApi(scenario, {
275275
throws: false,
276-
})) as CResult<unknown>
277-
expect(result.ok).toBe(false)
276+
})) as SocketSdkGenericResult<unknown>
277+
expect(result.success).toBe(false)
278278
} else {
279279
// eslint-disable-next-line no-await-in-loop
280280
const result = (await client.sendApi(scenario, {
281281
throws: false,
282-
})) as CResult<unknown>
283-
expect(result.ok).toBe(false)
282+
})) as SocketSdkGenericResult<unknown>
283+
expect(result.success).toBe(false)
284284
}
285285
}
286286
})
@@ -304,10 +304,10 @@ describe('API Response Scenarios', () => {
304304
const result = (await client.getApi(`json-format-${index}`, {
305305
responseType: 'json',
306306
throws: false,
307-
})) as CResult<unknown>
308-
expect(result.ok).toBe(false)
309-
if (!result.ok) {
310-
expect(result.message).toBe('Server returned invalid JSON')
307+
})) as SocketSdkGenericResult<unknown>
308+
expect(result.success).toBe(false)
309+
if (!result.success) {
310+
expect(result.error).toBe('Server returned invalid JSON')
311311
}
312312
}
313313
})
@@ -323,14 +323,14 @@ describe('API Response Scenarios', () => {
323323
// eslint-disable-next-line no-await-in-loop
324324
const getResult = (await client.getApi(getPath, {
325325
throws: false,
326-
})) as CResult<unknown>
327-
expect(getResult.ok).toBe(false)
326+
})) as SocketSdkGenericResult<unknown>
327+
expect(getResult.success).toBe(false)
328328

329329
// eslint-disable-next-line no-await-in-loop
330330
const sendResult = (await client.sendApi(sendPath, {
331331
throws: false,
332-
})) as CResult<unknown>
333-
expect(sendResult.ok).toBe(false)
332+
})) as SocketSdkGenericResult<unknown>
333+
expect(sendResult.success).toBe(false)
334334
}
335335
})
336336
})

0 commit comments

Comments
 (0)