// pure-decision grid for snap-chromium.ts.
//
// Locks the resolver branches that decide whether the laptop installer must
// replace snap-confined Chromium with Google Chrome stable. Inputs are passed
// directly so no spawn/fs mocking is needed — every test exercises the pure
// decision with realpath fixtures captured from real devices (Ubuntu Noble
// laptop and Debian Bookworm Pi).
//
// Runs via Node's built-in test runner; no vitest dependency. Compiles to
// dist/__tests__/snap-chromium.test.js alongside the rest of the package so
// `node --test dist/__tests__/*.test.js` picks it up after build.
import test from "node:test";
import assert from "node:assert/strict";
import { decideChromiumAction, isSnapConfinedPath } from "../snap-chromium.js";
// ---------------------------------------------------------------------------
// isSnapConfinedPath — `readlink -f` outputs from real devices.
// ---------------------------------------------------------------------------
test("isSnapConfinedPath: laptop noble realpath /usr/bin/snap is snap-confined", () => {
    assert.equal(isSnapConfinedPath("/usr/bin/snap"), true);
});
test("isSnapConfinedPath: nested snap revision path is snap-confined", () => {
    assert.equal(isSnapConfinedPath("/snap/chromium/2924/usr/lib/chromium/chromium"), true);
});
test("isSnapConfinedPath: snap wrapper bin path is snap-confined", () => {
    assert.equal(isSnapConfinedPath("/snap/bin/chromium"), true);
});
test("isSnapConfinedPath: bookworm /usr/bin/chromium realpath is non-snap", () => {
    assert.equal(isSnapConfinedPath("/usr/lib/chromium/chromium"), false);
});
test("isSnapConfinedPath: google-chrome-stable realpath is non-snap", () => {
    assert.equal(isSnapConfinedPath("/opt/google/chrome/google-chrome"), false);
});
test("isSnapConfinedPath: empty / null guards return false", () => {
    assert.equal(isSnapConfinedPath(""), false);
});
// ---------------------------------------------------------------------------
// decideChromiumAction — the six branches.
// ---------------------------------------------------------------------------
test("darwin: action=skip-non-linux, no resolvedPath", () => {
    const out = decideChromiumAction({
        platform: "darwin",
        whichChromium: "/usr/bin/chromium",
        realpathChromium: "/usr/bin/chromium",
        whichGoogleChrome: null,
        realpathGoogleChrome: null,
    });
    assert.equal(out.action, "skip-non-linux");
    assert.equal(out.resolvedPath, null);
});
test("linux + non-snap chromium (Pi Bookworm): action=use chromium realpath", () => {
    const out = decideChromiumAction({
        platform: "linux",
        whichChromium: "/usr/bin/chromium",
        realpathChromium: "/usr/lib/chromium/chromium",
        whichGoogleChrome: null,
        realpathGoogleChrome: null,
    });
    assert.equal(out.action, "use");
    assert.equal(out.resolvedPath, "/usr/bin/chromium");
});
test("linux + snap-confined chromium (laptop Noble), no google-chrome: action=install-google-chrome", () => {
    const out = decideChromiumAction({
        platform: "linux",
        whichChromium: "/usr/bin/chromium",
        realpathChromium: "/usr/bin/snap",
        whichGoogleChrome: null,
        realpathGoogleChrome: null,
    });
    assert.equal(out.action, "install-google-chrome");
    assert.equal(out.resolvedPath, null);
    assert.match(out.reason, /snap/);
});
test("linux + snap chromium + google-chrome already installed: action=use google-chrome", () => {
    const out = decideChromiumAction({
        platform: "linux",
        whichChromium: "/usr/bin/chromium",
        realpathChromium: "/usr/bin/snap",
        whichGoogleChrome: "/usr/bin/google-chrome-stable",
        realpathGoogleChrome: "/opt/google/chrome/google-chrome",
    });
    assert.equal(out.action, "use");
    assert.equal(out.resolvedPath, "/usr/bin/google-chrome-stable");
});
test("linux + chromium absent + no google-chrome: action=fail", () => {
    const out = decideChromiumAction({
        platform: "linux",
        whichChromium: null,
        realpathChromium: null,
        whichGoogleChrome: null,
        realpathGoogleChrome: null,
    });
    assert.equal(out.action, "fail");
    assert.equal(out.resolvedPath, null);
});
test("linux + chromium absent + google-chrome present: action=use google-chrome", () => {
    const out = decideChromiumAction({
        platform: "linux",
        whichChromium: null,
        realpathChromium: null,
        whichGoogleChrome: "/usr/bin/google-chrome-stable",
        realpathGoogleChrome: "/opt/google/chrome/google-chrome",
    });
    assert.equal(out.action, "use");
    assert.equal(out.resolvedPath, "/usr/bin/google-chrome-stable");
});
test("linux + snap chromium + google-chrome installed but ALSO under /snap (defensive): action=install-google-chrome", () => {
    const out = decideChromiumAction({
        platform: "linux",
        whichChromium: "/usr/bin/chromium",
        realpathChromium: "/usr/bin/snap",
        whichGoogleChrome: "/snap/bin/google-chrome",
        realpathGoogleChrome: "/snap/google-chrome/current/usr/bin/google-chrome",
    });
    assert.equal(out.action, "install-google-chrome");
});
