-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfetch-default-org-slug.test.mts
More file actions
201 lines (167 loc) · 5.34 KB
/
fetch-default-org-slug.test.mts
File metadata and controls
201 lines (167 loc) · 5.34 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Unit tests for getDefaultOrgSlug function.
*
* Tests the organization slug resolution logic used in CI environments.
* This function checks multiple sources in priority order.
*
* Test Coverage:
* - Config file defaultOrg value (highest priority)
* - SOCKET_CLI_ORG_SLUG environment variable
* - Fallback to fetching first organization from API
* - Error handling when no organizations exist
* - API call failures during organization fetch
*
* Testing Approach:
* - Mock getConfigValueOrUndef from utils/config.mts
* - Mock fetchOrganization from organization/fetch-organization-list.mts
* - Mock env.SOCKET_CLI_ORG_SLUG environment variable
* - Test priority order and fallback chain
* - Verify CResult pattern (ok/error states)
*
* Related Files:
* - src/commands/ci/fetch-default-org-slug.mts - Implementation
* - src/commands/ci/handle-ci.mts - CI command handler that uses this
* - src/utils/config.mts - Config file utilities
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { getDefaultOrgSlug } from '../../../../src/commands/ci/fetch-default-org-slug.mts'
// Create mock functions with hoisting.
const { mockOrgSlug, mockFetchOrganization, mockGetConfigValueOrUndef } =
vi.hoisted(() => {
return {
mockGetConfigValueOrUndef: vi.fn(),
mockFetchOrganization: vi.fn(),
mockOrgSlug: { value: undefined as string | undefined },
}
})
// Mock the dependencies.
vi.mock('../../../../src/utils/config.mts', () => ({
getConfigValueOrUndef: mockGetConfigValueOrUndef,
}))
// Mock the SOCKET_CLI_ORG_SLUG environment variable module.
vi.mock('../../../../src/env/socket-cli-org-slug.mts', () => ({
get SOCKET_CLI_ORG_SLUG() {
return mockOrgSlug.value
},
}))
vi.mock(
'../../../../src/commands/organization/fetch-organization-list.mts',
() => ({
fetchOrganization: mockFetchOrganization,
}),
)
describe('getDefaultOrgSlug', () => {
const mockFn = mockGetConfigValueOrUndef
const mockFetchFn = mockFetchOrganization
beforeEach(() => {
vi.clearAllMocks()
mockOrgSlug.value = undefined
})
it('uses config defaultOrg when set', async () => {
mockFn.mockReturnValue('config-org-slug')
const result = await getDefaultOrgSlug()
expect(result).toEqual({
ok: true,
data: 'config-org-slug',
})
expect(mockFn).toHaveBeenCalledWith('defaultOrg')
})
it('uses environment variable when no config', async () => {
mockFn.mockReturnValue(undefined)
mockOrgSlug.value = 'env-org-slug'
const result = await getDefaultOrgSlug()
expect(result).toEqual({
ok: true,
data: 'env-org-slug',
})
})
it('fetches from API when no config or env', async () => {
mockFn.mockReturnValue(undefined)
mockOrgSlug.value = undefined
mockFetchFn.mockResolvedValue({
ok: true,
data: {
// fetchOrganization converts the SDK dict into an array of org
// objects before returning, so mock the array shape directly.
organizations: [
{
id: 'org-1',
name: 'Test Organization',
slug: 'test-org',
},
],
},
})
const result = await getDefaultOrgSlug()
expect(result).toEqual({
ok: true,
message: 'Retrieved default org from server',
data: 'test-org',
})
})
it('returns slug (not display name) for orgs with spaces', async () => {
// Regression guard for SMO-622: returning display name produced
// URLs like `/v0/orgs/Alamos%20GmbH/...` that 404'd.
mockFn.mockReturnValue(undefined)
mockOrgSlug.value = undefined
mockFetchFn.mockResolvedValue({
ok: true,
data: {
organizations: [
{ id: 'org-1', name: 'Alamos GmbH', slug: 'alamos-gmbh' },
],
},
})
const result = await getDefaultOrgSlug()
expect(result).toEqual({
ok: true,
message: 'Retrieved default org from server',
data: 'alamos-gmbh',
})
})
it('returns error when fetchOrganization fails', async () => {
mockFn.mockReturnValue(undefined)
mockOrgSlug.value = undefined
const error = {
ok: false,
code: 401,
message: 'Unauthorized',
}
mockFetchFn.mockResolvedValue(error)
const result = await getDefaultOrgSlug()
expect(result).toEqual(error)
})
it('returns error when no organizations found', async () => {
mockFn.mockReturnValue(undefined)
mockOrgSlug.value = undefined
mockFetchFn.mockResolvedValue({
ok: true,
data: {
organizations: [],
},
})
const result = await getDefaultOrgSlug()
expect(result).toEqual({
ok: false,
message: 'Failed to establish identity',
data: 'No organization associated with the Socket API token. Unable to continue.',
})
})
it('returns error when organization has no slug', async () => {
mockFn.mockReturnValue(undefined)
mockOrgSlug.value = undefined
mockFetchFn.mockResolvedValue({
ok: true,
data: {
// Missing slug — defensive check in case the API ever omits it.
organizations: [{ id: 'org-1', name: 'Test Org' }],
},
})
const result = await getDefaultOrgSlug()
expect(result).toEqual({
ok: false,
message: 'Failed to establish identity',
data: 'Cannot determine the default organization for the API token. Unable to continue.',
})
})
})