-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathconstants.test.ts
More file actions
108 lines (95 loc) · 3.47 KB
/
constants.test.ts
File metadata and controls
108 lines (95 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
DEFAULT_HEADERS,
getClientPlatform,
getClientPlatformVersion,
getClientRuntime,
getClientRuntimeVersion,
} from '../../src/lib/constants'
import { version } from '../../src/lib/version'
test('it has the correct type of returning with the correct value', () => {
let JS_ENV = ''
// @ts-ignore
if (typeof Deno !== 'undefined') {
JS_ENV = 'deno'
} else if (typeof document !== 'undefined') {
JS_ENV = 'web'
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
JS_ENV = 'react-native'
} else {
JS_ENV = 'node'
}
expect(typeof DEFAULT_HEADERS).toBe('object')
expect(typeof DEFAULT_HEADERS['X-Client-Info']).toBe('string')
expect(DEFAULT_HEADERS['X-Client-Info']).toBe(`supabase-js-${JS_ENV}/${version}`)
// X-Client-Info should always be present
expect(DEFAULT_HEADERS).toHaveProperty('X-Client-Info')
// Other headers should only be present if they can be detected
Object.keys(DEFAULT_HEADERS).forEach((key) => {
expect(typeof DEFAULT_HEADERS[key]).toBe('string')
expect(DEFAULT_HEADERS[key].length).toBeGreaterThan(0)
})
})
describe('Client Platform Detection', () => {
test('getClientPlatform returns platform or null', () => {
const platform = getClientPlatform()
expect(platform === null || typeof platform === 'string').toBe(true)
if (platform) {
expect(platform.length).toBeGreaterThan(0)
expect(['macOS', 'Windows', 'Linux', 'iOS', 'Android'].includes(platform)).toBe(true)
}
})
test('getClientPlatformVersion returns version string or null', () => {
const version = getClientPlatformVersion()
expect(version === null || typeof version === 'string').toBe(true)
if (version) {
expect(version.length).toBeGreaterThan(0)
}
})
})
describe('Client Runtime Detection', () => {
test('getClientRuntime returns runtime or null', () => {
const runtime = getClientRuntime()
expect(runtime === null || typeof runtime === 'string').toBe(true)
if (runtime) {
expect(runtime.length).toBeGreaterThan(0)
expect(['node', 'deno', 'bun'].includes(runtime)).toBe(true)
}
})
test('getClientRuntimeVersion returns version string or null', () => {
const version = getClientRuntimeVersion()
expect(version === null || typeof version === 'string').toBe(true)
if (version) {
expect(version.length).toBeGreaterThan(0)
}
})
})
describe('Header Constants', () => {
test('X-Client-Info header format', () => {
const header = DEFAULT_HEADERS['X-Client-Info']
expect(header).toMatch(/^supabase-js-.+\/\d+\.\d+\.\d+/)
})
test('X-Client-Info is always present', () => {
expect(DEFAULT_HEADERS).toHaveProperty('X-Client-Info')
})
test('Optional headers are only present when detected', () => {
// Test that optional headers are either not present or have valid values
const optionalHeaders = [
'X-Supabase-Client-Platform',
'X-Supabase-Client-Platform-Version',
'X-Supabase-Client-Runtime',
'X-Supabase-Client-Runtime-Version',
]
optionalHeaders.forEach((headerName) => {
if (DEFAULT_HEADERS[headerName]) {
expect(typeof DEFAULT_HEADERS[headerName]).toBe('string')
expect(DEFAULT_HEADERS[headerName].length).toBeGreaterThan(0)
}
})
})
test('All present headers are properly formatted', () => {
Object.values(DEFAULT_HEADERS).forEach((value) => {
expect(typeof value).toBe('string')
expect(value.length).toBeGreaterThan(0)
})
})
})