type Kind = "prevent-app-suspension" | "prevent-display-sleep" | "none"; type State = { activeJobs: number; mediaPlaying: boolean; userWantsAwake: boolean; onBattery: boolean; }; function decide(state: State): Kind { if (!Number.isFinite(state.activeJobs) || state.activeJobs < 0) { throw new Error("BAD_ACTIVE_JOBS"); } if (state.activeJobs === 0 && !state.mediaPlaying && !state.userWantsAwake) { return "none"; } if (state.mediaPlaying && !state.onBattery) { return "prevent-display-sleep"; } if (state.activeJobs > 0) { return "prevent-app-suspension"; } return state.onBattery ? "prevent-app-suspension" : "prevent-display-sleep"; } function parseArgs(argv: readonly string[]): { args: string[]; isAutostart: boolean; isHidden: boolean; } { const args = argv.filter((arg) => arg.startsWith("--")); return { args, isAutostart: args.includes("--autostart"), isHidden: args.includes("--hidden"), }; } function plan(input: { current: boolean; desired: boolean; packaged: boolean; platform: string; }): { action: string; args: string[]; warnings: string[] } { const action = input.current === input.desired ? "noop" : input.desired ? "enable" : "disable"; const warnings: string[] = []; if (!input.packaged) warnings.push("DEV_BUILD"); if (input.desired && input.platform === "linux") warnings.push("LINUX_DESKTOP"); return { action, args: input.desired ? ["--autostart", "--hidden"] : [], warnings, }; } let badJobs = ""; try { decide({ activeJobs: -1, mediaPlaying: false, userWantsAwake: false, onBattery: false, }); } catch (error) { badJobs = error instanceof Error ? error.message : String(error); } const idle = decide({ activeJobs: 0, mediaPlaying: false, userWantsAwake: false, onBattery: false, }); const mediaAc = decide({ activeJobs: 0, mediaPlaying: true, userWantsAwake: false, onBattery: false, }); const jobsBattery = decide({ activeJobs: 2, mediaPlaying: false, userWantsAwake: false, onBattery: true, }); const mediaBattery = decide({ activeJobs: 0, mediaPlaying: true, userWantsAwake: false, onBattery: true, }); const awakeAc = decide({ activeJobs: 0, mediaPlaying: false, userWantsAwake: true, onBattery: false, }); const awakeBattery = decide({ activeJobs: 0, mediaPlaying: false, userWantsAwake: true, onBattery: true, }); const parsed = parseArgs(["--autostart", "--hidden", "--foo"]); const enable = plan({ current: false, desired: true, packaged: true, platform: "darwin", }); const disable = plan({ current: true, desired: false, packaged: true, platform: "darwin", }); const noop = plan({ current: true, desired: true, packaged: true, platform: "darwin", }); const linux = plan({ current: false, desired: true, packaged: true, platform: "linux", }); const ok = badJobs === "BAD_ACTIVE_JOBS" && idle === "none" && mediaAc === "prevent-display-sleep" && jobsBattery === "prevent-app-suspension" && mediaBattery === "prevent-app-suspension" && awakeAc === "prevent-display-sleep" && awakeBattery === "prevent-app-suspension" && parsed.isAutostart && parsed.isHidden && parsed.args.length === 3 && enable.action === "enable" && enable.args.join(" ") === "--autostart --hidden" && enable.warnings.length === 0 && disable.action === "disable" && disable.args.length === 0 && noop.action === "noop" && noop.warnings.length === 0 && linux.warnings.includes("LINUX_DESKTOP"); console.log( JSON.stringify({ evt: "smoke", ok, decisions: { idle, mediaAc, jobsBattery, mediaBattery, awakeAc, awakeBattery, }, parsed, plans: { enable, disable, noop, linux }, badJobs, }), );