import test from 'brittle'
import fs from 'bare-fs'
import path from 'bare-path'
import os from 'bare-os'
// Test the config module's logic patterns.
// We avoid modifying the real ~/.config/hypernote by testing the defaults.
test('cli-config - module loads', async (t) => {
const config = await import('../../lib/cli/config.js')
t.ok(config.loadConfig, 'has loadConfig')
t.ok(config.saveConfig, 'has saveConfig')
t.ok(config.getServer, 'has getServer')
t.ok(config.getToken, 'has getToken')
t.ok(config.hasDeviceIdentity, 'has hasDeviceIdentity')
})
test('cli-config - defaults', async (t) => {
const { loadConfig } = await import('../../lib/cli/config.js')
const cfg = loadConfig()
t.is(cfg.server, 'http://localhost:4040', 'default server is port 4040')
t.is(cfg.token, null, 'default token is null')
t.absent(cfg.serverKey, 'no serverKey in defaults')
t.absent(cfg.defaultRepo, 'no defaultRepo in defaults')
})
test('cli-config - getServer uses flags override', async (t) => {
const { getServer } = await import('../../lib/cli/config.js')
const server = getServer({ server: 'http://custom:8080' })
t.is(server, 'http://custom:8080', 'flag overrides config')
})
test('cli-config - getServer falls back to config/defaults', async (t) => {
const { getServer } = await import('../../lib/cli/config.js')
const server = getServer({})
t.ok(server.includes('localhost'), 'falls back to default')
})
test('cli-config - getToken uses flags override', async (t) => {
const { getToken } = await import('../../lib/cli/config.js')
const token = getToken({ token: 'my-secret-token' })
t.is(token, 'my-secret-token', 'flag overrides config')
})
test('cli-config - getToken returns null with no config', async (t) => {
const { getToken } = await import('../../lib/cli/config.js')
const token = getToken({})
// Could be null or a configured token
t.is(typeof token, token === null ? 'object' : 'string', 'returns null or string')
})
test('cli-config - config dir uses hypernote path', async (t) => {
// Verify the config path doesn't reference pear-git
const configMod = await import('../../lib/cli/config.js')
// loadConfig reads from ~/.config/hypernote/hn.json
// We can verify by checking the source behavior
const cfg = configMod.loadConfig()
t.is(typeof cfg, 'object', 'loadConfig returns object')
// The key check: defaults should NOT include pear-git fields
t.absent(cfg.defaultNamespace, 'no defaultNamespace (pear-git field)')
})