// Coverage for runInitLogging's success and failure branches via injected
// fs / stderr / exit impls. (maxy-code).
//
// Brief asked for vitest. Codebase convention is node:test (matches
// apt-resolve.test.ts, peer-brand-detect.test.ts, etc); the package has no
// vitest dep and the test runner stanza in package.json invokes
// `node --test 'dist/__tests__/*.test.js'`.
import test from "node:test";
import assert from "node:assert/strict";
import { runInitLogging } from "../init-logging.js";
const LOG_DIR = "/tmp/test-task-062-logs";
const LOG_FILE = "/tmp/test-task-062-logs/install-2026-01-01T00-00-00-000Z.log";
const PERSIST_DIR = "/tmp/test-task-062-persist";
test("success path: header written via injected impls, no stderr, no exit", () => {
    const appendCalls = [];
    let stderrOut = "";
    let exitCode = null;
    runInitLogging({
        logDir: LOG_DIR,
        logFile: LOG_FILE,
        persistDir: PERSIST_DIR,
        headerLines: ["", "===", "  Maxy Code Install Log", "==="],
        mkdirSyncImpl: () => { },
        appendFileSyncImpl: (p, d) => { appendCalls.push([p, d]); },
        stderrWriteImpl: (c) => { stderrOut += c; },
        exitImpl: (c) => { exitCode = c; },
    });
    assert.equal(appendCalls.length, 1);
    assert.equal(appendCalls[0][0], LOG_FILE);
    assert.equal(appendCalls[0][1], "\n===\n  Maxy Code Install Log\n===\n");
    assert.equal(stderrOut, "");
    assert.equal(exitCode, null);
});
test("failure path: appendFileSync throws → stderr emits FAILED line, exit(1) called", () => {
    let stderrOut = "";
    let exitCode = null;
    runInitLogging({
        logDir: LOG_DIR,
        logFile: LOG_FILE,
        persistDir: PERSIST_DIR,
        headerLines: ["a"],
        mkdirSyncImpl: () => { },
        appendFileSyncImpl: () => { throw new Error("EACCES: permission denied"); },
        stderrWriteImpl: (c) => { stderrOut += c; },
        exitImpl: (c) => { exitCode = c; },
    });
    assert.ok(stderrOut.includes("[create-maxy] init-logging FAILED reason=EACCES"), `stderr should contain init-logging FAILED line; got: ${JSON.stringify(stderrOut)}`);
    assert.ok(stderrOut.includes(`log=${LOG_FILE}`));
    assert.ok(stderrOut.includes(`persist=${PERSIST_DIR}`));
    assert.equal(exitCode, 1);
});
test("failure path: mkdirSync throws → stderr emits FAILED line, exit(1) called", () => {
    let stderrOut = "";
    let exitCode = null;
    let appendCalled = false;
    runInitLogging({
        logDir: LOG_DIR,
        logFile: LOG_FILE,
        persistDir: PERSIST_DIR,
        headerLines: ["a"],
        mkdirSyncImpl: () => { throw new Error("EROFS: read-only file system"); },
        appendFileSyncImpl: () => { appendCalled = true; },
        stderrWriteImpl: (c) => { stderrOut += c; },
        exitImpl: (c) => { exitCode = c; },
    });
    assert.equal(appendCalled, false, "appendFileSync must not be invoked when mkdirSync throws");
    assert.ok(stderrOut.includes("[create-maxy] init-logging FAILED reason=EROFS"));
    assert.equal(exitCode, 1);
});
test("failure path: non-Error thrown value still surfaces as reason", () => {
    let stderrOut = "";
    let exitCode = null;
    runInitLogging({
        logDir: LOG_DIR,
        logFile: LOG_FILE,
        persistDir: PERSIST_DIR,
        headerLines: ["a"],
        mkdirSyncImpl: () => { },
        appendFileSyncImpl: () => { throw "raw-string"; },
        stderrWriteImpl: (c) => { stderrOut += c; },
        exitImpl: (c) => { exitCode = c; },
    });
    assert.ok(stderrOut.includes("reason=raw-string"));
    assert.equal(exitCode, 1);
});
