import { afterEach, expect, test, vi } from "vitest" import { getSystem, getSystemContextAndroid, getSystemContextIos, getSystemContextLinux, getSystemContextMacos, getSystemContextUnknown, getSystemContextWindows, getSystemFlags, getSystemRegistryItem, isAndroid, isIos, isLinux, isMacos, isSatisfiesSystem, isSatisfiesSystems, isSystemRegistered, isUnknown, isWindows, listSystems, registerSystem, unregisterSystem, useAndroid, useIos, useLinux, useMacos, useSystems, useUnknown, useWindows, } from "#Source/environment/system.ts" const internalBuiltInSystems = ["windows", "android", "ios", "macos", "linux", "unknown"] afterEach(() => { for (const systemName of listSystems()) { if (internalBuiltInSystems.includes(systemName) === false) { unregisterSystem(systemName) } } vi.unstubAllGlobals() vi.restoreAllMocks() }) test("isSystemRegistered detects built-in and missing system", () => { expect(isSystemRegistered("windows")).toBe(true) expect(isSystemRegistered("custom-system")).toBe(false) }) test("registerSystem stores custom system", () => { registerSystem({ system: "custom-register", detect: () => false, getSystemContext: () => ({ system: "custom-register" as const }), use: (_use, otherwise) => otherwise(), }) expect(isSystemRegistered("custom-register")).toBe(true) }) test("unregisterSystem removes custom system", () => { registerSystem({ system: "custom-remove", detect: () => false, getSystemContext: () => ({ system: "custom-remove" as const }), use: (_use, otherwise) => otherwise(), }) unregisterSystem("custom-remove") expect(isSystemRegistered("custom-remove")).toBe(false) }) test("listSystems returns system keys", () => { const systems = listSystems() expect(systems).toContain("windows") expect(systems).toContain("unknown") }) test("isWindows detects Windows host", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) expect(isWindows()).toBe(true) }) test("getSystemContextWindows returns Windows context", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) const context = getSystemContextWindows() expect(context.system).toBe("windows") expect(context.processPlatform).toBe("win32") expect(context.globalThis).toBe(globalThis) }) test("useWindows executes matched handler", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) const result = useWindows( (context) => context.system, () => "fallback", ) expect(result).toBe("windows") }) test("isAndroid detects Android host", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", { userAgentData: { platform: "Android" }, userAgent: "Mozilla/5.0 (Linux; Android 14; Pixel 8)", platform: "Linux armv8l", maxTouchPoints: 5, }) expect(isAndroid()).toBe(true) }) test("getSystemContextAndroid returns Android context", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", { userAgentData: { platform: "Android" }, userAgent: "Mozilla/5.0 (Linux; Android 14; Pixel 8)", platform: "Linux armv8l", maxTouchPoints: 5, }) const context = getSystemContextAndroid() expect(context.system).toBe("android") expect(context.userAgentDataPlatform).toBe("android") }) test("useAndroid executes matched handler", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", { userAgentData: { platform: "Android" }, userAgent: "Mozilla/5.0 (Linux; Android 14; Pixel 8)", platform: "Linux armv8l", maxTouchPoints: 5, }) const result = useAndroid( (context) => context.system, () => "fallback", ) expect(result).toBe("android") }) test("isIos detects iOS host", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", { userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15", platform: "MacIntel", maxTouchPoints: 5, }) expect(isIos()).toBe(true) }) test("getSystemContextIos returns iOS context", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", { userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)", platform: "iPhone", maxTouchPoints: 5, }) const context = getSystemContextIos() expect(context.system).toBe("ios") expect(context.navigatorPlatform).toBe("iphone") }) test("useIos executes matched handler", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", { userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)", platform: "iPhone", maxTouchPoints: 5, }) const result = useIos( (context) => context.system, () => "fallback", ) expect(result).toBe("ios") }) test("isMacos detects macOS host", () => { vi.stubGlobal("process", { platform: "darwin" }) vi.stubGlobal("navigator", undefined) expect(isMacos()).toBe(true) }) test("getSystemContextMacos returns macOS context", () => { vi.stubGlobal("process", { platform: "darwin" }) vi.stubGlobal("navigator", undefined) const context = getSystemContextMacos() expect(context.system).toBe("macos") expect(context.processPlatform).toBe("darwin") }) test("useMacos executes matched handler", () => { vi.stubGlobal("process", { platform: "darwin" }) vi.stubGlobal("navigator", undefined) const result = useMacos( (context) => context.system, () => "fallback", ) expect(result).toBe("macos") }) test("isLinux detects Linux host", () => { vi.stubGlobal("process", { platform: "linux" }) vi.stubGlobal("navigator", undefined) expect(isLinux()).toBe(true) }) test("getSystemContextLinux returns Linux context", () => { vi.stubGlobal("process", { platform: "linux" }) vi.stubGlobal("navigator", undefined) const context = getSystemContextLinux() expect(context.system).toBe("linux") expect(context.processPlatform).toBe("linux") }) test("useLinux executes matched handler", () => { vi.stubGlobal("process", { platform: "linux" }) vi.stubGlobal("navigator", undefined) const result = useLinux( (context) => context.system, () => "fallback", ) expect(result).toBe("linux") }) test("isUnknown detects unmatched system", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", undefined) expect(isUnknown()).toBe(true) }) test("getSystemContextUnknown returns unknown context", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", undefined) const context = getSystemContextUnknown() expect(context.system).toBe("unknown") expect(context.process).toBeUndefined() expect(context.userAgent).toBeUndefined() }) test("useUnknown executes matched handler", () => { vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", undefined) const result = useUnknown( (context) => context.system, () => "fallback", ) expect(result).toBe("unknown") }) test("getSystemRegistryItem returns matched registry item", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) const item = getSystemRegistryItem() expect(item.system).toBe("windows") expect(typeof item.detect).toBe("function") }) test("getSystem resolves current system key", () => { vi.stubGlobal("process", { platform: "darwin" }) vi.stubGlobal("navigator", undefined) expect(getSystem()).toBe("macos") }) test("getSystemFlags resolves detector outputs", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) const flags = getSystemFlags() expect(flags.windows).toBe(true) expect(flags.macos).toBe(false) expect(flags.unknown).toBe(false) }) test("isSatisfiesSystem checks system and throws for missing", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) expect(isSatisfiesSystem("windows")).toBe(true) expect(() => isSatisfiesSystem("system-not-found")).toThrow("is not registered") }) test("isSatisfiesSystems checks system conditions and throws for missing", () => { vi.stubGlobal("process", { platform: "linux" }) vi.stubGlobal("navigator", undefined) expect(isSatisfiesSystems({ linux: true })).toBe(true) expect(isSatisfiesSystems({ linux: false })).toBe(false) expect(() => isSatisfiesSystems({ "system-not-found": true })).toThrow("is not registered") }) test("useSystems executes system specific handler, default handler, and throws without handlers", () => { vi.stubGlobal("process", { platform: "win32" }) vi.stubGlobal("navigator", undefined) const windowsResult = useSystems({ windows: () => "windows", default: () => "default", }) vi.stubGlobal("process", undefined) vi.stubGlobal("navigator", undefined) const defaultResult = useSystems({ windows: () => "windows", default: () => "default", }) expect(windowsResult).toBe("windows") expect(defaultResult).toBe("default") expect(() => useSystems({ windows: () => "windows" })).toThrow( "Neither system-specific nor default handler provided", ) })