Skip to content

Commit 5f2aed5

Browse files
committed
Refactor date-check strategy in privacy policy unit test to run on Vercel
1 parent 2b2b427 commit 5f2aed5

2 files changed

Lines changed: 79 additions & 19 deletions

File tree

src/integrations/PrivacyPolicyVersion/index.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616

1717
import { execSync } from 'node:child_process'
1818
import type { AstroIntegration } from 'astro'
19-
import { BuildError } from '../../lib/errors/BuildError'
19+
20+
const PRIVACY_POLICY_PATH = 'src/pages/privacy/index.astro'
21+
22+
export const toIsoDateString = (date: Date): string => date.toISOString().slice(0, 10)
2023

2124
/**
2225
* Get privacy policy version from git commit date
2326
* @param filePath - Path to privacy policy file (relative to project root)
2427
* @returns ISO date string (YYYY-MM-DD) of last commit
25-
* @throws {BuildError} If git command fails or returns empty result
2628
*/
27-
function getPrivacyPolicyVersionFromGit(filePath: string): string {
29+
function getPrivacyPolicyVersionFromGit(filePath: string): string | null {
2830
try {
2931
// Get last commit date for privacy policy file in YYYY-MM-DD format
3032
const lastCommitDate = execSync(
@@ -36,21 +38,37 @@ function getPrivacyPolicyVersionFromGit(filePath: string): string {
3638
return lastCommitDate
3739
}
3840

39-
// If no commits found (new file), throw error
40-
throw new BuildError(
41-
`No git commits found for privacy policy file: ${filePath}`,
42-
{ phase: 'config-setup', filePath },
41+
console.warn(
42+
`[privacy-policy-version] No git commits found for privacy policy file: ${filePath}. Falling back to current date.`,
4343
)
44+
return null
4445
} catch (error) {
45-
// Git not available or command failed
46-
if (error instanceof BuildError) {
47-
throw error
48-
}
49-
throw new BuildError(
50-
`Could not get privacy policy version from git: ${error instanceof Error ? error.message : String(error)}`,
51-
{ phase: 'config-setup', tool: 'git', cause: error },
46+
console.warn(
47+
`[privacy-policy-version] Could not get privacy policy version from git: ${error instanceof Error ? error.message : String(error)}`,
5248
)
49+
return null
50+
}
51+
}
52+
53+
/**
54+
* Resolve privacy policy version using env, git metadata, or current date fallback.
55+
*/
56+
export function resolvePrivacyPolicyVersion(): string {
57+
const envVersion = process.env.PRIVACY_POLICY_VERSION?.trim()
58+
if (envVersion) {
59+
console.log(`✅ Privacy policy version sourced from env: ${envVersion}`)
60+
return envVersion
61+
}
62+
63+
const gitVersion = getPrivacyPolicyVersionFromGit(PRIVACY_POLICY_PATH)
64+
if (gitVersion) {
65+
console.log(`✅ Privacy policy version set from git: ${gitVersion}`)
66+
return gitVersion
5367
}
68+
69+
const fallback = toIsoDateString(new Date())
70+
console.log(`⚠️ Privacy policy version fallback applied: ${fallback}`)
71+
return fallback
5472
}
5573

5674
/**
@@ -61,11 +79,7 @@ export function privacyPolicyVersion(): AstroIntegration {
6179
name: 'privacy-policy-version',
6280
hooks: {
6381
'astro:config:setup': async ({ updateConfig }) => {
64-
// Get version from git commit date
65-
const privacyPolicyPath = 'src/pages/privacy/index.astro'
66-
const version = getPrivacyPolicyVersionFromGit(privacyPolicyPath)
67-
68-
console.log(`✅ Privacy policy version set to: ${version}`)
82+
const version = resolvePrivacyPolicyVersion()
6983

7084
// Inject as Vite define so it's available as import.meta.env.PRIVACY_POLICY_VERSION
7185
updateConfig({
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it, afterEach, vi } from 'vitest'
2+
3+
vi.mock('node:child_process', () => ({
4+
execSync: vi.fn(),
5+
}))
6+
7+
import { execSync } from 'node:child_process'
8+
9+
import { resolvePrivacyPolicyVersion } from '../../../src/integrations/PrivacyPolicyVersion/index'
10+
11+
describe('resolvePrivacyPolicyVersion', () => {
12+
afterEach(() => {
13+
vi.restoreAllMocks()
14+
vi.unstubAllEnvs()
15+
vi.useRealTimers()
16+
})
17+
18+
it('returns PRIVACY_POLICY_VERSION when provided', () => {
19+
vi.stubEnv('PRIVACY_POLICY_VERSION', '2024-01-02')
20+
const version = resolvePrivacyPolicyVersion()
21+
22+
expect(version).toBe('2024-01-02')
23+
expect(execSync).not.toHaveBeenCalled()
24+
})
25+
26+
it('falls back to git metadata when env var is absent', () => {
27+
vi.mocked(execSync).mockReturnValue('2023-05-05\n' as never)
28+
29+
const version = resolvePrivacyPolicyVersion()
30+
31+
expect(version).toBe('2023-05-05')
32+
})
33+
34+
it('falls back to the current date when git is unavailable', () => {
35+
vi.useFakeTimers()
36+
vi.setSystemTime(new Date('2025-12-06T12:00:00Z'))
37+
vi.mocked(execSync).mockImplementation(() => {
38+
throw new Error('git missing')
39+
})
40+
vi.spyOn(console, 'warn').mockImplementation(() => {})
41+
42+
const version = resolvePrivacyPolicyVersion()
43+
44+
expect(version).toBe('2025-12-06')
45+
})
46+
})

0 commit comments

Comments
 (0)