// acceptance grid for findPeerBrandOnDefaultNeo4jPort.
//
// Each test exercises one branch of the matching rule. The wrapper in
// index.ts owns fs reads + warning emission; this suite does not touch fs.
//
// Runs via Node's built-in test runner (matches apt-resolve.test.ts +
// port-canonicalisation.test.ts). The brief asked for vitest, but the
// package has no vitest dependency — node:test is the codebase convention
// and ships with the toolchain.
import test from "node:test";
import assert from "node:assert/strict";
import { findPeerBrandOnDefaultNeo4jPort } from "../peer-brand-detect.js";
const DEFAULT_PORT = 7687;
const CURRENT = "realagent"; // installer is realagent; peer would be maxy
test("no peer hostnames → null", () => {
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [],
    });
    assert.equal(result, null);
});
test("peer with content=null (no .env on disk) → null", () => {
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", null]],
    });
    assert.equal(result, null);
});
test("peer pins NEO4J_URI=bolt://localhost:7687 → returns peer hostname", () => {
    const env = `EMBED_MODEL=nomic-embed-text\nNEO4J_URI=bolt://localhost:7687\nACCOUNT_ID=abc\n`;
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", env]],
    });
    assert.equal(result, "maxy");
});
test("peer pins dedicated port 7688 (not the system unit) → null", () => {
    const env = `NEO4J_URI=bolt://localhost:7688\n`;
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", env]],
    });
    assert.equal(result, null);
});
test("peer .env has no NEO4J_URI line → null", () => {
    const env = `EMBED_MODEL=nomic-embed-text\nDISPLAY_MODE=hd\n`;
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", env]],
    });
    assert.equal(result, null);
});
test("peer .env is empty → null", () => {
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", ""]],
    });
    assert.equal(result, null);
});
test("multiple peers, second one matches → returns the matching hostname", () => {
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [
            ["future-brand", `NEO4J_URI=bolt://localhost:7689\n`],
            ["maxy", `NEO4J_URI=bolt://localhost:7687\n`],
        ],
    });
    assert.equal(result, "maxy");
});
test("only the current brand in iterable → filtered, returns null", () => {
    // Caller may pass the full known-brand list; the helper filters self.
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [[CURRENT, `NEO4J_URI=bolt://localhost:7687\n`]],
    });
    assert.equal(result, null);
});
test("URI with non-bolt scheme is not a match", () => {
    const env = `NEO4J_URI=neo4j://localhost:7687\n`;
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", env]],
    });
    assert.equal(result, null);
});
test("URI with non-localhost host is not a match", () => {
    const env = `NEO4J_URI=bolt://neo4j.example.com:7687\n`;
    const result = findPeerBrandOnDefaultNeo4jPort({
        currentBrandHostname: CURRENT,
        defaultNeo4jPort: DEFAULT_PORT,
        peerEnvContents: [["maxy", env]],
    });
    assert.equal(result, null);
});
