Skip to content

Commit bfc5124

Browse files
committed
Fix import error in privacy policy unit test
1 parent b233de5 commit bfc5124

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

src/integrations/PrivacyPolicyVersion/__tests__/index.spec.ts

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
22
import { execSync } from 'node:child_process'
33
import { TestError } from '@test/errors'
44

5+
vi.mock('../../../lib/config/environmentServer', () => ({
6+
getOptionalEnv: vi.fn(() => undefined),
7+
}))
8+
59
// Mock child_process
610
vi.mock('node:child_process', () => ({
711
execSync: vi.fn(),
812
}))
913

14+
import { getOptionalEnv } from '../../../lib/config/environmentServer'
15+
1016
describe('PrivacyPolicyVersion Integration', () => {
1117
let consoleLogSpy: ReturnType<typeof vi.spyOn>
1218
let consoleWarnSpy: ReturnType<typeof vi.spyOn>
1319

1420
beforeEach(() => {
15-
// Spy on console methods
21+
vi.mocked(getOptionalEnv).mockReturnValue(undefined)
22+
1623
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
1724
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
1825

@@ -27,6 +34,7 @@ describe('PrivacyPolicyVersion Integration', () => {
2734
// Restore console methods
2835
consoleLogSpy.mockRestore()
2936
consoleWarnSpy.mockRestore()
37+
vi.useRealTimers()
3038
})
3139

3240
describe('getPrivacyPolicyVersionFromGit', () => {
@@ -61,7 +69,7 @@ describe('PrivacyPolicyVersion Integration', () => {
6169
})
6270

6371
expect(consoleLogSpy).toHaveBeenCalledWith(
64-
expect.stringContaining('Privacy policy version set to: 2024-03-15'),
72+
expect.stringContaining('Privacy policy version set from git: 2024-03-15'),
6573
)
6674
})
6775

@@ -89,44 +97,58 @@ describe('PrivacyPolicyVersion Integration', () => {
8997
})
9098
})
9199

92-
it('should throw BuildError when git command fails', async () => {
93-
// Mock git command failure
100+
it('logs warning and falls back when git command fails', async () => {
94101
vi.mocked(execSync).mockImplementation(() => {
95102
throw new TestError('Git command failed')
96103
})
104+
vi.useFakeTimers()
105+
vi.setSystemTime(new Date('2025-12-06T00:00:00Z'))
97106

98107
const { privacyPolicyVersion } = await import('../index')
99108

100109
const mockUpdateConfig = vi.fn()
101110
const integration = privacyPolicyVersion()
102111

103-
await expect(
104-
integration.hooks['astro:config:setup']?.({
105-
updateConfig: mockUpdateConfig,
106-
// @ts-expect-error - Partial mock
107-
config: {},
108-
}),
109-
).rejects.toThrow('Could not get privacy policy version from git')
112+
await integration.hooks['astro:config:setup']?.({
113+
updateConfig: mockUpdateConfig,
114+
// @ts-expect-error - Partial mock
115+
config: {},
116+
})
110117

111-
expect(consoleWarnSpy).not.toHaveBeenCalled()
118+
expect(mockUpdateConfig).toHaveBeenCalledWith({
119+
vite: {
120+
define: {
121+
'import.meta.env.PRIVACY_POLICY_VERSION': '"2025-12-06"',
122+
},
123+
},
124+
})
125+
expect(consoleWarnSpy).toHaveBeenCalled()
112126
})
113127

114-
it('should throw BuildError when git returns empty string', async () => {
115-
// Mock git command returning empty string
128+
it('logs warning and falls back when git returns empty string', async () => {
116129
vi.mocked(execSync).mockReturnValue('')
130+
vi.useFakeTimers()
131+
vi.setSystemTime(new Date('2025-05-01T00:00:00Z'))
117132

118133
const { privacyPolicyVersion } = await import('../index')
119134

120135
const mockUpdateConfig = vi.fn()
121136
const integration = privacyPolicyVersion()
122137

123-
await expect(
124-
integration.hooks['astro:config:setup']?.({
125-
updateConfig: mockUpdateConfig,
126-
// @ts-expect-error - Partial mock
127-
config: {},
128-
}),
129-
).rejects.toThrow('No git commits found for privacy policy file')
138+
await integration.hooks['astro:config:setup']?.({
139+
updateConfig: mockUpdateConfig,
140+
// @ts-expect-error - Partial mock
141+
config: {},
142+
})
143+
144+
expect(mockUpdateConfig).toHaveBeenCalledWith({
145+
vite: {
146+
define: {
147+
'import.meta.env.PRIVACY_POLICY_VERSION': '"2025-05-01"',
148+
},
149+
},
150+
})
151+
expect(consoleWarnSpy).toHaveBeenCalled()
130152
})
131153
})
132154

0 commit comments

Comments
 (0)