// harness-tcp-scratch.mjs — a keet-daemon on FRESH storage over the TCP
// transport (the Windows one), for live probes such as hyper-tps
// scenes/test/_probe_identity_flow_live.gd (hyper-games #89). Usage:
// bare test/harness-tcp-scratch.mjs <scratch-dir>
// Writes <scratch-dir>/env.json. Never point it at a real identity's storage.
// env.json holds PORT, TOKEN, and a freshly generated RESTORE_PHRASE with its
// RESTORE_MEMBER_ID. The daemon then stays up until SIGTERM.
import fs from 'bare-fs'
import path from 'bare-path'
import { startDaemon } from '../index.js'
import { generateMnemonic } from 'bip39-mnemonic'
import IdentityKey from 'keet-identity-key'
import z32 from 'z32'
const dir = Bare.argv[2]
fs.mkdirSync(dir, { recursive: true })
const token = 'live-identity-flow-token-' + Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
const d = await startDaemon({ storage: path.join(dir, 'store'), socketPath: path.join(dir, 'unused.sock'), tcp: { host: '127.0.0.1', port: 0, token } })
const phrase = generateMnemonic()
const key = await IdentityKey.from({ mnemonic: phrase })
const out = {
PORT: d.uds.port,
TOKEN: token,
RESTORE_PHRASE: phrase,
RESTORE_MEMBER_ID: z32.encode(key.identityPublicKey)
}
fs.writeFileSync(path.join(dir, 'env.json'), JSON.stringify(out))
console.log('[live] ready ' + JSON.stringify({ PORT: out.PORT, RESTORE_MEMBER_ID: out.RESTORE_MEMBER_ID }))