import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; import { existsSync } from 'node:fs'; import { mkdir, rm } from 'node:fs/promises'; import { generateMasterKey, getOrCreateMasterKey, isValidMasterKey, masterKeyExists, readMasterKey, writeMasterKey, } from './master-key'; const TEST_KEY_DIR = './test-keys'; const TEST_KEY_PATH = `${TEST_KEY_DIR}/test-master.key`; describe('Master Key Management', () => { beforeEach(async () => { // Clean up test directory if (existsSync(TEST_KEY_DIR)) { await rm(TEST_KEY_DIR, { recursive: true }); } await mkdir(TEST_KEY_DIR, { recursive: true }); }); afterEach(async () => { // Clean up if (existsSync(TEST_KEY_DIR)) { await rm(TEST_KEY_DIR, { recursive: true }); } }); describe('generateMasterKey', () => { test('should generate 32-byte key', () => { const key = generateMasterKey(); expect(Buffer.isBuffer(key)).toBe(true); expect(key.length).toBe(32); }); test('should generate unique keys', () => { const key1 = generateMasterKey(); const key2 = generateMasterKey(); expect(key1.equals(key2)).toBe(false); }); }); describe('isValidMasterKey', () => { test('should return true for valid 32-byte key', () => { const key = generateMasterKey(); expect(isValidMasterKey(key)).toBe(true); }); test('should return false for wrong length', () => { const shortKey = Buffer.alloc(16); const longKey = Buffer.alloc(64); expect(isValidMasterKey(shortKey)).toBe(false); expect(isValidMasterKey(longKey)).toBe(false); }); test('should return false for non-buffer', () => { expect(isValidMasterKey('not a buffer' as unknown as Buffer)).toBe(false); expect(isValidMasterKey(123 as unknown as Buffer)).toBe(false); expect(isValidMasterKey(null as unknown as Buffer)).toBe(false); }); }); describe('writeMasterKey', () => { test('should write key to file', async () => { const key = generateMasterKey(); await writeMasterKey(key, TEST_KEY_PATH); expect(existsSync(TEST_KEY_PATH)).toBe(true); }); test('should reject invalid key', async () => { const invalidKey = Buffer.alloc(16); await expect(writeMasterKey(invalidKey, TEST_KEY_PATH)).rejects.toThrow('Invalid master key'); }); test('should create directory if it does not exist', async () => { const key = generateMasterKey(); const nestedPath = `${TEST_KEY_DIR}/nested/deep/master.key`; await writeMasterKey(key, nestedPath); expect(existsSync(nestedPath)).toBe(true); }); }); describe('readMasterKey', () => { test('should read key from file', async () => { const key = generateMasterKey(); await writeMasterKey(key, TEST_KEY_PATH); const readKey = await readMasterKey(TEST_KEY_PATH); expect(readKey.equals(key)).toBe(true); }); test('should throw error if file does not exist', async () => { await expect(readMasterKey('./nonexistent.key')).rejects.toThrow('not found'); }); test('should throw error if key is invalid', async () => { // Write invalid key (wrong length) const invalidKey = Buffer.alloc(16); await mkdir(TEST_KEY_DIR, { recursive: true }); const fs = await import('node:fs/promises'); await fs.writeFile(TEST_KEY_PATH, invalidKey.toString('hex')); await expect(readMasterKey(TEST_KEY_PATH)).rejects.toThrow('Invalid master key'); }); }); describe('getOrCreateMasterKey', () => { test('should create new key if file does not exist', async () => { expect(existsSync(TEST_KEY_PATH)).toBe(false); const key = await getOrCreateMasterKey(TEST_KEY_PATH); expect(isValidMasterKey(key)).toBe(true); expect(existsSync(TEST_KEY_PATH)).toBe(true); }); test('should return existing key if file exists', async () => { const originalKey = generateMasterKey(); await writeMasterKey(originalKey, TEST_KEY_PATH); const key = await getOrCreateMasterKey(TEST_KEY_PATH); expect(key.equals(originalKey)).toBe(true); }); test('should not overwrite existing key', async () => { const key1 = await getOrCreateMasterKey(TEST_KEY_PATH); const key2 = await getOrCreateMasterKey(TEST_KEY_PATH); expect(key1.equals(key2)).toBe(true); }); }); describe('masterKeyExists', () => { test('should return true if key exists', async () => { const key = generateMasterKey(); await writeMasterKey(key, TEST_KEY_PATH); expect(masterKeyExists(TEST_KEY_PATH)).toBe(true); }); test('should return false if key does not exist', () => { expect(masterKeyExists(TEST_KEY_PATH)).toBe(false); }); }); });