47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
|
|
import { describe, it, expect } from 'vitest'
|
||
|
|
import { mkdtempSync, rmSync, readFileSync, existsSync } from 'node:fs'
|
||
|
|
import { tmpdir } from 'node:os'
|
||
|
|
import path from 'node:path'
|
||
|
|
import { loadConfig } from '../src/config.js'
|
||
|
|
|
||
|
|
function tempDir(): string {
|
||
|
|
return mkdtempSync(path.join(tmpdir(), 'rs-config-'))
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('loadConfig', () => {
|
||
|
|
it('creates data + artwork dirs and returns paths', () => {
|
||
|
|
const dir = tempDir()
|
||
|
|
try {
|
||
|
|
const cfg = loadConfig({ DATA_DIR: dir })
|
||
|
|
expect(cfg.dataDir).toBe(dir)
|
||
|
|
expect(cfg.dbPath).toBe(path.join(dir, 'record-shop.db'))
|
||
|
|
expect(cfg.port).toBe(3000)
|
||
|
|
expect(existsSync(path.join(dir, 'artwork-cache'))).toBe(true)
|
||
|
|
expect(cfg.sessionSecret).toMatch(/^[0-9a-f]{64}$/)
|
||
|
|
} finally {
|
||
|
|
rmSync(dir, { recursive: true, force: true })
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
it('persists and reuses the session secret', () => {
|
||
|
|
const dir = tempDir()
|
||
|
|
try {
|
||
|
|
const a = loadConfig({ DATA_DIR: dir })
|
||
|
|
const b = loadConfig({ DATA_DIR: dir })
|
||
|
|
expect(a.sessionSecret).toBe(b.sessionSecret)
|
||
|
|
expect(readFileSync(path.join(dir, 'session-secret'), 'utf8')).toBe(a.sessionSecret)
|
||
|
|
} finally {
|
||
|
|
rmSync(dir, { recursive: true, force: true })
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
it('honours PORT', () => {
|
||
|
|
const dir = tempDir()
|
||
|
|
try {
|
||
|
|
expect(loadConfig({ DATA_DIR: dir, PORT: '8080' }).port).toBe(8080)
|
||
|
|
} finally {
|
||
|
|
rmSync(dir, { recursive: true, force: true })
|
||
|
|
}
|
||
|
|
})
|
||
|
|
})
|