|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { |
| 4 | + type DetectDeps, |
| 5 | + detectOpenCode, |
| 6 | + OPENCODE_DESKTOP_APP_IDS, |
| 7 | + openCodeDesktopSettingsMarkers, |
| 8 | +} from "./opencode-detect"; |
| 9 | + |
| 10 | +const HOME = "/virt/home"; |
| 11 | + |
| 12 | +// Build hermetic deps over a virtual set of "existing" paths and a fixed OS, so |
| 13 | +// no host filesystem / real `opencode` install can leak into the result. |
| 14 | +function deps( |
| 15 | + existing: Set<string>, |
| 16 | + platform: NodeJS.Platform = "darwin", |
| 17 | + onPath: (b: string) => string | null = () => null, |
| 18 | +): DetectDeps { |
| 19 | + return { |
| 20 | + exists: (p) => existing.has(p), |
| 21 | + home: HOME, |
| 22 | + platform, |
| 23 | + env: { |
| 24 | + APPDATA: join(HOME, "AppData", "Roaming"), |
| 25 | + LOCALAPPDATA: join(HOME, "AppData", "Local"), |
| 26 | + USERPROFILE: HOME, |
| 27 | + XDG_CONFIG_HOME: join(HOME, ".config"), |
| 28 | + XDG_DATA_HOME: join(HOME, ".local", "share"), |
| 29 | + }, |
| 30 | + onPath, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe("detectOpenCode", () => { |
| 35 | + it("exposes exactly the three Desktop channel appIds", () => { |
| 36 | + expect([...OPENCODE_DESKTOP_APP_IDS]).toEqual([ |
| 37 | + "ai.opencode.desktop", |
| 38 | + "ai.opencode.desktop.beta", |
| 39 | + "ai.opencode.desktop.dev", |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("reports none when nothing OpenCode-ish exists", () => { |
| 44 | + expect(detectOpenCode(deps(new Set())).kind).toBe("none"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("reports cli when the stock ~/.opencode/bin binary exists", () => { |
| 48 | + const bin = join(HOME, ".opencode", "bin", "opencode"); |
| 49 | + const result = detectOpenCode(deps(new Set([bin]))); |
| 50 | + expect(result).toEqual({ kind: "cli", binary: bin }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("reports cli when a bare opencode is on PATH", () => { |
| 54 | + const result = detectOpenCode(deps(new Set(), "linux", () => "/somewhere/opencode")); |
| 55 | + expect(result).toEqual({ kind: "cli", binary: "/somewhere/opencode" }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("reports desktop when a channel's opencode.settings marker exists", () => { |
| 59 | + const d = deps(new Set()); |
| 60 | + const marker = openCodeDesktopSettingsMarkers(d)[0]; // prod channel |
| 61 | + const result = detectOpenCode(deps(new Set([marker]))); |
| 62 | + expect(result).toEqual({ kind: "desktop", marker }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("reports desktop for the beta/dev channels too", () => { |
| 66 | + const markers = openCodeDesktopSettingsMarkers(deps(new Set())); |
| 67 | + for (const marker of markers) { |
| 68 | + expect(detectOpenCode(deps(new Set([marker]))).kind).toBe("desktop"); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it("reports desktop from the GUI app path when never run (no settings marker)", () => { |
| 73 | + const appPath = "/Applications/OpenCode.app"; |
| 74 | + expect(detectOpenCode(deps(new Set([appPath]), "darwin")).kind).toBe("desktop"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does NOT treat ~/.config/opencode (shared core config) as Desktop", () => { |
| 78 | + const coreConfig = join(HOME, ".config", "opencode", "opencode.jsonc"); |
| 79 | + expect(detectOpenCode(deps(new Set([coreConfig]))).kind).toBe("none"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("prefers cli over desktop when both are present", () => { |
| 83 | + const bin = join(HOME, ".opencode", "bin", "opencode"); |
| 84 | + const marker = openCodeDesktopSettingsMarkers(deps(new Set()))[0]; |
| 85 | + expect(detectOpenCode(deps(new Set([bin, marker]))).kind).toBe("cli"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("finds the Windows Desktop settings marker under %APPDATA%", () => { |
| 89 | + const win = deps(new Set(), "win32"); |
| 90 | + const marker = openCodeDesktopSettingsMarkers(win)[0]; |
| 91 | + expect(marker).toBe( |
| 92 | + join(HOME, "AppData", "Roaming", "ai.opencode.desktop", "opencode.settings"), |
| 93 | + ); |
| 94 | + expect(detectOpenCode(deps(new Set([marker]), "win32")).kind).toBe("desktop"); |
| 95 | + }); |
| 96 | +}); |
0 commit comments