// Static-grep contract for the darwin install branches in index.ts.
//
// Task 297 audited every `platform === "darwin"` block; this test pins the
// shape so a future refactor that removes a darwin branch without an explicit
// reason fails loudly here. The test reads src/index.ts at runtime (the
// authoritative source) and regex-asserts the per-branch markers — keeping
// the spec adjacent to the code rather than buried in a doc.
//
// Why static-grep, not behaviour: the install branches each spawn (sudo,
// brew, launchctl), and end-to-end darwin reproduction belongs to the manual
// smoke path in the task brief. The unit suite's job is to ensure the
// branch points still exist after a refactor.
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
// dist/__tests__/macos-darwin-branch.test.js → ../../src/index.ts
const here = dirname(fileURLToPath(import.meta.url));
const INDEX_TS = resolve(here, "../../src/index.ts");
const SRC = readFileSync(INDEX_TS, "utf-8");
test("installSystemDeps has darwin branch that calls setMacosHostnameViaScutil on --hostname and scutil --get LocalHostName without", () => {
    // The hostname block at index.ts:installSystemDeps must:
    //   - call setMacosHostnameViaScutil(HOSTNAME_FLAG) when HOSTNAME_FLAG is set
    //   - call scutil --get LocalHostName when HOSTNAME_FLAG is absent
    // The two paths are the contract Task 297 locks in.
    assert.match(SRC, /setMacosHostnameViaScutil\(HOSTNAME_FLAG\)/, "scutil-set path on --hostname must call setMacosHostnameViaScutil");
    assert.match(SRC, /scutil["']?,\s*\[["']--get["']\s*,\s*["']LocalHostName["']\]/, "scutil --get LocalHostName must be the no-flag default on darwin");
    assert.match(SRC, /darwin-hostname-mode=scutil-get/, "scutil-get success path must emit observability log");
    assert.match(SRC, /darwin-hostname-mode=scutil-set/, "scutil-set path must emit observability log");
    assert.match(SRC, /darwin-hostname-mode=brand-fallback/, "brand-fallback path must emit observability log");
});
test("installSystemDeps darwin branch installs DARWIN_BASE_DEPS via brew", () => {
    // The shared dep set the darwin path feeds to installAllBrewPackages. If
    // this list shrinks or a name changes, brew-resolve.test.ts (DARWIN_BASE_DEPS_FIXTURE)
    // must move in lockstep — the comment in brew-resolve.test.ts says so.
    assert.match(SRC, /DARWIN_BASE_DEPS\s*=\s*\[\s*"curl"\s*,\s*"git"\s*,\s*"unzip"\s*,\s*"jq"\s*,\s*"poppler"\s*,\s*"ffmpeg"\s*,\s*"tesseract"\s*,\s*"ocrmypdf"\s*\]/);
    assert.match(SRC, /installAllBrewPackages\(DARWIN_BASE_DEPS,\s*logFile\)/);
});
test("installCloudflared darwin branch logs the documented skip and brew-installs cloudflared", () => {
    // Task 297 doctrine: skip tunnel provisioning entirely on darwin. The brew
    // install puts the binary on PATH so the operator can opt in to `cloudflared
    // tunnel login` post-install; the explicit log line is the diagnostic
    // anchor for investigators grepping the install log.
    assert.match(SRC, /darwin-cloudflare-skip=true/);
    assert.match(SRC, /installAllBrewPackages\(\["cloudflared"\],\s*logFile\)/);
    // No `cloudflared service install` or `cloudflared tunnel route dns`
    // invocation in the installer surface — those are operator-driven,
    // post-install steps. Match actual argv shapes (string-literal list to
    // spawnSync/shell) rather than prose: argv arrays carry the args as
    // quoted siblings inside `[...]`. Comment text is filtered by requiring
    // a quote character immediately adjacent.
    assert.ok(!/\[\s*["']cloudflared["'][^\]]*["']service["'][^\]]*["']install["']/.test(SRC), "installer must not invoke `cloudflared service install`");
    assert.ok(!/\[\s*["']cloudflared["'][^\]]*["']tunnel["'][^\]]*["']route["'][^\]]*["']dns["']/.test(SRC), "installer must not invoke `cloudflared tunnel route dns`");
});
test("installService darwin branch delegates to installServiceDarwin (launchd path, not systemd)", () => {
    // Two contracts: the platform switch routes darwin to installServiceDarwin,
    // and installServiceDarwin writes the plist + bootstraps it via launchctl.
    assert.match(SRC, /platform\s*===\s*"darwin"\s*\)\s*\{\s*installServiceDarwin\(\)/);
    assert.match(SRC, /launchctl["']?,\s*\[["']bootstrap["']/);
    assert.match(SRC, /launchd-plist=\$\{path\}\s+loaded=true/, "successful bootstrap must emit launchd-plist=…  loaded=true");
    assert.match(SRC, /launchd-plist=\$\{path\}\s+loaded=false/, "failed bootstrap must emit launchd-plist=…  loaded=false");
});
test("installNodejs darwin branch brew-installs the `nodejs` alias name (resolver hops to node@22)", () => {
    // The shared call site uses the apt-style `nodejs` name. brew-resolve.ts
    // hops it to `node@22` via DARWIN_ALIASES — the alias is the deliberate
    // wedge so a single dep name works across both backends.
    assert.match(SRC, /platform\s*===\s*"darwin"[\s\S]{0,400}installAllBrewPackages\(\["nodejs"\]/);
});
test("installNeo4j darwin branch brew-installs `neo4j` (no alias hop)", () => {
    // `neo4j` is the concrete brew formula name on darwin, no alias needed.
    assert.match(SRC, /platform\s*===\s*"darwin"[\s\S]{0,800}installAllBrewPackages\(\["neo4j"\]/);
});
test("chromium binary-path file write skips darwin (Playwright-managed)", () => {
    // writeChromiumBinaryPathFile early-returns on non-linux. The completion URL
    // and VNC stack on darwin use Playwright/brew-managed Chromium instead of
    // the linux per-brand chromium-binary.path file.
    assert.match(SRC, /writeChromiumBinaryPathFile[\s\S]{0,200}process\.platform\s*!==\s*"linux"/);
});
test("uninstall darwin path bootsout the LaunchAgent and removes the plist", () => {
    // The uninstall surface lives in uninstall.ts (separate file); this index.ts
    // sanity check just confirms the launchdLabel helper still exists and is
    // brand-keyed — install + uninstall both depend on the same label shape.
    assert.match(SRC, /function launchdLabel\(\):\s*string\s*\{\s*return\s*`com\.rubytech\.\$\{BRAND\.hostname\}`/);
});
