Skip to content

Commit 43ade96

Browse files
committed
test: fix repository, install-completion, and translations tests (11 tests)
Fixed test failures in three areas: - Repository tests: Fixed debug mock imports to use @socketsecurity/lib/debug instead of non-existent ../../utils/debug.mts (2 tests) - Install completion tests: Removed underscore prefix from outputInstallCompletion imports so tests can reference the mocked function correctly (3 tests) - Alert translations tests: Completely rewrote tests to match new implementation that uses direct JSON imports instead of createRequire. Implementation changed from dynamic require to static import with type assertion (6 tests) Tests fixed: 11 Total tests fixed: 134
1 parent 132ecca commit 43ade96

File tree

3 files changed

+82
-124
lines changed

3 files changed

+82
-124
lines changed

packages/cli/src/commands/install/handle-install-completion.test.mts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('handleInstallCompletion', () => {
1717

1818
it('installs completion successfully', async () => {
1919
const { setupTabCompletion } = await import('./setup-tab-completion.mts')
20-
const { outputInstallCompletion: _outputInstallCompletion } = await import(
20+
const { outputInstallCompletion } = await import(
2121
'./output-install-completion.mts'
2222
)
2323

@@ -37,7 +37,7 @@ describe('handleInstallCompletion', () => {
3737

3838
it('handles installation failure', async () => {
3939
const { setupTabCompletion } = await import('./setup-tab-completion.mts')
40-
const { outputInstallCompletion: _outputInstallCompletion } = await import(
40+
const { outputInstallCompletion } = await import(
4141
'./output-install-completion.mts'
4242
)
4343

@@ -58,7 +58,7 @@ describe('handleInstallCompletion', () => {
5858

5959
it('handles different shell targets', async () => {
6060
const { setupTabCompletion } = await import('./setup-tab-completion.mts')
61-
const { outputInstallCompletion: _outputInstallCompletion } = await import(
61+
const { outputInstallCompletion } = await import(
6262
'./output-install-completion.mts'
6363
)
6464

@@ -83,7 +83,7 @@ describe('handleInstallCompletion', () => {
8383

8484
it('handles empty target name', async () => {
8585
const { setupTabCompletion } = await import('./setup-tab-completion.mts')
86-
const { outputInstallCompletion: _outputInstallCompletion } = await import(
86+
const { outputInstallCompletion } = await import(
8787
'./output-install-completion.mts'
8888
)
8989

@@ -103,7 +103,7 @@ describe('handleInstallCompletion', () => {
103103

104104
it('handles unsupported shell', async () => {
105105
const { setupTabCompletion } = await import('./setup-tab-completion.mts')
106-
const { outputInstallCompletion: _outputInstallCompletion } = await import(
106+
const { outputInstallCompletion } = await import(
107107
'./output-install-completion.mts'
108108
)
109109

@@ -123,7 +123,7 @@ describe('handleInstallCompletion', () => {
123123

124124
it('handles async errors', async () => {
125125
const { setupTabCompletion } = await import('./setup-tab-completion.mts')
126-
const { outputInstallCompletion: _outputInstallCompletion } = await import(
126+
const { outputInstallCompletion } = await import(
127127
'./output-install-completion.mts'
128128
)
129129

packages/cli/src/commands/repository/handle-create-repo.test.mts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ vi.mock('./fetch-create-repo.mts', () => ({
99
vi.mock('./output-create-repo.mts', () => ({
1010
outputCreateRepo: vi.fn(),
1111
}))
12-
vi.mock('../../utils/debug.mts', () => ({
12+
vi.mock('@socketsecurity/lib/debug', () => ({
13+
debug: vi.fn(),
1314
debugDir: vi.fn(),
14-
debugFn: vi.fn(),
15-
debugLog: vi.fn(),
1615
isDebug: vi.fn(() => false),
1716
}))
1817

@@ -123,7 +122,7 @@ describe('handleCreateRepo', () => {
123122
})
124123

125124
it('logs debug information', async () => {
126-
const { debugDir, debugFn } = await import('../../utils/debug.mts')
125+
const { debug, debugDir } = await import('@socketsecurity/lib/debug')
127126
const { fetchCreateRepo } = await import('./fetch-create-repo.mts')
128127

129128
const mockData = {
@@ -144,25 +143,22 @@ describe('handleCreateRepo', () => {
144143
'json',
145144
)
146145

147-
expect(debugFn).toHaveBeenCalledWith(
148-
'notice',
146+
expect(debug).toHaveBeenCalledWith(
149147
'Creating repository debug-org/debug-repo',
150148
)
151149
expect(debugDir).toHaveBeenCalledWith(
152-
'inspect',
153150
expect.objectContaining({
154151
orgSlug: 'debug-org',
155152
repoName: 'debug-repo',
156153
}),
157154
)
158-
expect(debugFn).toHaveBeenCalledWith(
159-
'notice',
155+
expect(debug).toHaveBeenCalledWith(
160156
'Repository creation succeeded',
161157
)
162158
})
163159

164160
it('logs debug information on failure', async () => {
165-
const { debugFn } = await import('../../utils/debug.mts')
161+
const { debug } = await import('@socketsecurity/lib/debug')
166162
const { fetchCreateRepo } = await import('./fetch-create-repo.mts')
167163

168164
vi.mocked(fetchCreateRepo).mockResolvedValue({
@@ -182,7 +178,7 @@ describe('handleCreateRepo', () => {
182178
'json',
183179
)
184180

185-
expect(debugFn).toHaveBeenCalledWith('notice', 'Repository creation failed')
181+
expect(debug).toHaveBeenCalledWith('Repository creation failed')
186182
})
187183

188184
it('handles different visibility types', async () => {
Lines changed: 69 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,104 @@
1-
import { createRequire } from 'node:module'
2-
import path from 'node:path'
3-
41
import { beforeEach, describe, expect, it, vi } from 'vitest'
52

6-
// Mock node:module.
7-
vi.mock('node:module', () => ({
8-
createRequire: vi.fn(() => vi.fn()),
9-
}))
10-
11-
// Mock constants.
12-
vi.mock('../constants.mts', () => ({
3+
// Mock the JSON import.
4+
vi.mock('../../../data/alert-translations.json', () => ({
135
default: {
14-
rootPath: '/mock/root/path',
6+
alerts: {
7+
badEncoding: {
8+
description:
9+
'Source files are encoded using a non-standard text encoding.',
10+
emoji: '⚠️',
11+
suggestion:
12+
'Ensure all published files are encoded using a standard encoding such as UTF8, UTF16, UTF32, SHIFT-JIS, etc.',
13+
title: 'Bad text encoding',
14+
},
15+
badSemver: {
16+
description:
17+
'Package version is not a valid semantic version (semver).',
18+
emoji: '⚠️',
19+
suggestion:
20+
'All versions of all packages on npm should use use a valid semantic version. Publish a new version of the package with a valid semantic version. Semantic version ranges do not work with invalid semantic versions.',
21+
title: 'Bad semver',
22+
},
23+
},
1524
},
1625
}))
1726

1827
describe('translations utilities', () => {
19-
let mockRequire: ReturnType<typeof vi.fn>
20-
let mockTranslations: Record<string, any>
21-
2228
beforeEach(() => {
2329
vi.clearAllMocks()
24-
// Reset the module-level cache by clearing the module from cache.
25-
vi.resetModules()
26-
27-
mockTranslations = {
28-
messages: {
29-
hello: 'Hello',
30-
goodbye: 'Goodbye',
31-
},
32-
errors: {
33-
notFound: 'Not found',
34-
unauthorized: 'Unauthorized',
35-
},
36-
}
37-
38-
mockRequire = vi.fn(() => mockTranslations)
39-
vi.mocked(createRequire).mockReturnValue(mockRequire)
4030
})
4131

4232
describe('getTranslations', () => {
43-
it('loads translations from the correct path', async () => {
44-
// Re-import to get fresh module with reset cache.
45-
const { getTranslations: getTranslationsFresh } = await import(
46-
'./translations.mts'
47-
)
48-
49-
const result = getTranslationsFresh()
50-
51-
expect(mockRequire).toHaveBeenCalledWith(
52-
path.join('/mock/root/path', 'translations.json'),
53-
)
54-
expect(result).toBe(mockTranslations)
33+
it('returns the translations object', async () => {
34+
const { getTranslations } = await import('./translations.mts')
35+
36+
const result = getTranslations()
37+
38+
expect(result).toHaveProperty('alerts')
39+
expect(result.alerts).toHaveProperty('badEncoding')
40+
expect(result.alerts).toHaveProperty('badSemver')
5541
})
5642

57-
it('caches translations after first load', async () => {
58-
// Re-import to get fresh module with reset cache.
59-
const { getTranslations: getTranslationsFresh } = await import(
60-
'./translations.mts'
61-
)
43+
it('returns consistent results on multiple calls', async () => {
44+
const { getTranslations } = await import('./translations.mts')
6245

63-
const result1 = getTranslationsFresh()
64-
const result2 = getTranslationsFresh()
65-
const result3 = getTranslationsFresh()
46+
const result1 = getTranslations()
47+
const result2 = getTranslations()
48+
const result3 = getTranslations()
6649

67-
// Should only require once.
68-
expect(mockRequire).toHaveBeenCalledTimes(1)
69-
// Should return the same object.
50+
// Should return the same object reference.
7051
expect(result1).toBe(result2)
7152
expect(result2).toBe(result3)
72-
expect(result1).toBe(mockTranslations)
7353
})
7454

75-
it('returns the translations object', async () => {
76-
// Re-import to get fresh module with reset cache.
77-
const { getTranslations: getTranslationsFresh } = await import(
78-
'./translations.mts'
79-
)
55+
it('includes alert properties', async () => {
56+
const { getTranslations } = await import('./translations.mts')
8057

81-
const result = getTranslationsFresh()
58+
const result = getTranslations()
8259

83-
expect(result).toHaveProperty('messages')
84-
expect(result).toHaveProperty('errors')
85-
expect(result.messages.hello).toBe('Hello')
86-
expect(result.errors.notFound).toBe('Not found')
60+
expect(result.alerts.badEncoding).toEqual({
61+
description:
62+
'Source files are encoded using a non-standard text encoding.',
63+
emoji: '⚠️',
64+
suggestion:
65+
'Ensure all published files are encoded using a standard encoding such as UTF8, UTF16, UTF32, SHIFT-JIS, etc.',
66+
title: 'Bad text encoding',
67+
})
8768
})
8869

89-
it('uses createRequire with import.meta.url', async () => {
90-
// Re-import to get fresh module with reset cache.
91-
const { getTranslations: getTranslationsFresh } = await import(
92-
'./translations.mts'
93-
)
70+
it('has correct structure for alert entries', async () => {
71+
const { getTranslations } = await import('./translations.mts')
9472

95-
getTranslationsFresh()
73+
const result = getTranslations()
74+
const { badSemver } = result.alerts
9675

97-
expect(createRequire).toHaveBeenCalledWith(
98-
expect.stringContaining('.mts'),
99-
)
76+
expect(badSemver).toHaveProperty('description')
77+
expect(badSemver).toHaveProperty('emoji')
78+
expect(badSemver).toHaveProperty('suggestion')
79+
expect(badSemver).toHaveProperty('title')
80+
expect(typeof badSemver.description).toBe('string')
81+
expect(typeof badSemver.title).toBe('string')
10082
})
10183

102-
it('handles empty translations object', async () => {
103-
mockTranslations = {}
104-
mockRequire = vi.fn(() => mockTranslations)
105-
vi.mocked(createRequire).mockReturnValue(mockRequire)
106-
107-
// Re-import to get fresh module with reset cache.
108-
const { getTranslations: getTranslationsFresh } = await import(
109-
'./translations.mts'
110-
)
84+
it('returns alerts object with multiple entries', async () => {
85+
const { getTranslations } = await import('./translations.mts')
11186

112-
const result = getTranslationsFresh()
87+
const result = getTranslations()
11388

114-
expect(result).toEqual({})
89+
expect(Object.keys(result.alerts).length).toBeGreaterThanOrEqual(2)
90+
expect(result.alerts.badEncoding).toBeDefined()
91+
expect(result.alerts.badSemver).toBeDefined()
11592
})
11693

117-
it('handles complex nested translations', async () => {
118-
mockTranslations = {
119-
level1: {
120-
level2: {
121-
level3: {
122-
message: 'Deeply nested message',
123-
},
124-
},
125-
},
126-
arrays: ['item1', 'item2'],
127-
}
128-
mockRequire = vi.fn(() => mockTranslations)
129-
vi.mocked(createRequire).mockReturnValue(mockRequire)
130-
131-
// Re-import to get fresh module with reset cache.
132-
const { getTranslations: getTranslationsFresh } = await import(
133-
'./translations.mts'
134-
)
135-
136-
const result = getTranslationsFresh()
137-
138-
expect(result.level1.level2.level3.message).toBe('Deeply nested message')
139-
expect(result.arrays).toEqual(['item1', 'item2'])
94+
it('handles nested alert structure', async () => {
95+
const { getTranslations } = await import('./translations.mts')
96+
97+
const result = getTranslations()
98+
99+
// Verify we can access nested properties.
100+
expect(result.alerts.badEncoding.title).toBe('Bad text encoding')
101+
expect(result.alerts.badSemver.title).toBe('Bad semver')
140102
})
141103
})
142104
})

0 commit comments

Comments
 (0)