Add unit tests for data file rotation utility functions
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 5m24s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped

- Implement tests for writing data files with rotation, ensuring backups are created only on changes.
- Verify that old backups are rotated correctly and the maximum number of backups is maintained.
- Test restoration of backups while preserving the current state as a backup.
- Utilize Vitest for testing framework and manage temporary file storage during tests.
This commit is contained in:
Torsten Schulz (local)
2026-06-01 11:21:21 +02:00
parent 80834d8652
commit 2014abe660
17 changed files with 563 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import { promises as fs } from 'fs'
import path from 'path'
import { randomUUID } from 'crypto'
import { writeDataFileWithRotation } from './data-file-rotation.js'
// Handle both dev and production paths
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
@@ -38,7 +39,7 @@ export async function readNews() {
// Write news to file
export async function writeNews(news) {
try {
await fs.writeFile(NEWS_FILE, JSON.stringify(news, null, 2), 'utf-8')
await writeDataFileWithRotation(NEWS_FILE, JSON.stringify(news, null, 2), { encoding: 'utf-8' })
return true
} catch (error) {
console.error('Fehler beim Schreiben der News:', error)