import test from 'brittle'
import fs from 'bare-fs'
import path from 'bare-path'
import os from 'bare-os'
// We test the StorageLocator by checking its static behavior.
// To avoid polluting ~/.config/hypernote, we test the core logic patterns.
test('StorageLocator - module loads', async (t) => {
const { StorageLocator } = await import('../../lib/storage-locator.js')
t.ok(StorageLocator, 'StorageLocator class exists')
t.is(typeof StorageLocator.read, 'function', 'has read()')
t.is(typeof StorageLocator.readRaw, 'function', 'has readRaw()')
t.is(typeof StorageLocator.write, 'function', 'has write()')
})
test('StorageLocator - filePath points to hypernote config', async (t) => {
const { StorageLocator } = await import('../../lib/storage-locator.js')
const fp = StorageLocator.filePath
t.ok(fp.includes('hypernote'), 'file path contains hypernote')
t.ok(fp.includes('storage-locator.json'), 'file path contains storage-locator.json')
t.absent(fp.includes('pear-git'), 'file path does NOT contain pear-git')
})
test('StorageLocator - read returns null when no file exists', async (t) => {
const { StorageLocator } = await import('../../lib/storage-locator.js')
// This test is safe only if no hypernote config exists yet.
// If it does, the test just verifies it returns a string or null.
const result = StorageLocator.read()
t.is(typeof result, result === null ? 'object' : 'string', 'returns null or string')
})
test('StorageLocator - write and readRaw round-trip', async (t) => {
const { StorageLocator } = await import('../../lib/storage-locator.js')
// Create a temp directory to write to
const tmpDir = path.join(os.tmpdir(), 'hn-locator-test-' + Date.now())
fs.mkdirSync(tmpDir, { recursive: true })
// Write a fake server-identity.json so read() validates
const identityFile = path.join(tmpDir, 'server-identity.json')
fs.writeFileSync(identityFile, JSON.stringify({ publicKey: 'test' }))
// We can't easily test write() without affecting real config,
// but we can verify the guard logic by checking readRaw()
const raw = StorageLocator.readRaw()
// Just verify it's null or a string
t.is(typeof raw, raw === null ? 'object' : 'string', 'readRaw returns null or string')
// Cleanup
fs.unlinkSync(identityFile)
fs.rmdirSync(tmpDir)
})