|
| 1 | +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + AGENT_HANDOFF_FILE, |
| 7 | + DESKTOP_DOWNLOAD_URL, |
| 8 | + HANDOFF_READY, |
| 9 | + agentSession, |
| 10 | + desktopHint, |
| 11 | + desktopInstalled, |
| 12 | + openCommandFor, |
| 13 | + openInDesktop, |
| 14 | +} from "./desktopApp.js"; |
| 15 | + |
| 16 | +const NO_AGENT = {}; |
| 17 | + |
| 18 | +describe("openInDesktop", () => { |
| 19 | + it("hands the folder to the released app when it is installed", () => { |
| 20 | + const asked: string[] = []; |
| 21 | + const result = openInDesktop("/films/a", { |
| 22 | + platform: "darwin", |
| 23 | + open: (id, dir) => (asked.push(`${id} ${dir}`), true), |
| 24 | + env: NO_AGENT, |
| 25 | + }); |
| 26 | + expect(result).toEqual({ opened: true, bundleId: "dev.hyperframes.desktop", handedOver: null }); |
| 27 | + expect(asked).toEqual(["dev.hyperframes.desktop /films/a"]); |
| 28 | + }); |
| 29 | + |
| 30 | + it("falls back to Canary when only Canary is installed", () => { |
| 31 | + const result = openInDesktop("/films/a", { |
| 32 | + platform: "darwin", |
| 33 | + open: (id) => id.endsWith(".canary"), |
| 34 | + env: NO_AGENT, |
| 35 | + }); |
| 36 | + expect(result).toMatchObject({ opened: true, bundleId: "dev.hyperframes.desktop.canary" }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("points to the download when no app is installed", () => { |
| 40 | + const result = openInDesktop("/films/a", { platform: "darwin", open: () => false }); |
| 41 | + expect(result).toEqual({ |
| 42 | + opened: false, |
| 43 | + reason: "not-installed", |
| 44 | + downloadUrl: DESKTOP_DOWNLOAD_URL, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("never runs `open` off macOS", () => { |
| 49 | + const result = openInDesktop("C:\\films\\a", { |
| 50 | + platform: "win32", |
| 51 | + open: () => { |
| 52 | + throw new Error("must not run"); |
| 53 | + }, |
| 54 | + }); |
| 55 | + expect(result).toMatchObject({ opened: false, reason: "unsupported-platform" }); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe("agent hand-off", () => { |
| 60 | + let dir: string | undefined; |
| 61 | + afterEach(() => dir && rmSync(dir, { recursive: true, force: true })); |
| 62 | + const handoff = () => join(dir!, AGENT_HANDOFF_FILE); |
| 63 | + |
| 64 | + it("names the conversation the command runs in, Claude Code first", () => { |
| 65 | + expect(agentSession({ CLAUDE_CODE_SESSION_ID: "c-1", CODEX_THREAD_ID: "x-1" })).toEqual({ |
| 66 | + engine: "claude", |
| 67 | + sessionId: "c-1", |
| 68 | + }); |
| 69 | + expect(agentSession({ CODEX_THREAD_ID: "x-1" })).toEqual({ engine: "codex", sessionId: "x-1" }); |
| 70 | + expect(agentSession({})).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("leaves the app the conversation once it has the folder", () => { |
| 74 | + dir = mkdtempSync(join(tmpdir(), "hf-handoff-")); |
| 75 | + const result = openInDesktop(dir, { |
| 76 | + platform: "darwin", |
| 77 | + open: () => true, |
| 78 | + env: { CLAUDE_CODE_SESSION_ID: "0a8eed95-0869-45bf-83ec-5d71fcc5236c" }, |
| 79 | + }); |
| 80 | + expect(result).toMatchObject({ opened: true, handedOver: { engine: "claude" } }); |
| 81 | + expect(JSON.parse(readFileSync(handoff(), "utf8"))).toEqual({ |
| 82 | + engine: "claude", |
| 83 | + sessionId: "0a8eed95-0869-45bf-83ec-5d71fcc5236c", |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("writes nothing when no app took the folder, or no agent runs the command", () => { |
| 88 | + dir = mkdtempSync(join(tmpdir(), "hf-handoff-")); |
| 89 | + openInDesktop(dir, { platform: "darwin", open: () => false, env: { CODEX_THREAD_ID: "x-1" } }); |
| 90 | + openInDesktop(dir, { platform: "darwin", open: () => true, env: {} }); |
| 91 | + expect(existsSync(handoff())).toBe(false); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("openCommandFor", () => { |
| 96 | + it("names the project relative to where the person is", () => { |
| 97 | + expect(openCommandFor("/work/films/a", "/work/films/a")).toBe("hyperframes open ."); |
| 98 | + expect(openCommandFor("/work/films/a", "/work")).toBe("hyperframes open films/a"); |
| 99 | + expect(openCommandFor("/work/my film", "/work")).toBe('hyperframes open "my film"'); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe("desktopInstalled", () => { |
| 104 | + const nothing = { |
| 105 | + platform: "darwin", |
| 106 | + home: "/Users/a", |
| 107 | + exists: () => false, |
| 108 | + spotlight: () => false, |
| 109 | + }; |
| 110 | + |
| 111 | + it("finds the app where the DMG and the installer put it, Canary included", () => { |
| 112 | + expect( |
| 113 | + desktopInstalled({ |
| 114 | + ...nothing, |
| 115 | + exists: (p: string) => p === "/Applications/HyperFrames Canary.app", |
| 116 | + }), |
| 117 | + ).toBe(true); |
| 118 | + expect( |
| 119 | + desktopInstalled({ |
| 120 | + ...nothing, |
| 121 | + exists: (p: string) => p === "/Users/a/Applications/HyperFrames.app", |
| 122 | + }), |
| 123 | + ).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it("asks Spotlight for one installed anywhere else", () => { |
| 127 | + expect( |
| 128 | + desktopInstalled({ ...nothing, spotlight: (id: string) => id === "dev.hyperframes.desktop" }), |
| 129 | + ).toBe(true); |
| 130 | + expect(desktopInstalled(nothing)).toBe(false); |
| 131 | + }); |
| 132 | + |
| 133 | + it("is never there off macOS", () => { |
| 134 | + expect(desktopInstalled({ ...nothing, platform: "linux", exists: () => true })).toBe(false); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe("desktopHint", () => { |
| 139 | + const OUTSIDE = {}; |
| 140 | + const download = `Edit it by chatting with Framey in HyperFrames Studio → ${DESKTOP_DOWNLOAD_URL}`; |
| 141 | + |
| 142 | + it("points to the download when the app is missing", () => { |
| 143 | + expect(desktopHint(process.cwd(), { env: OUTSIDE, installed: false })).toBe(download); |
| 144 | + }); |
| 145 | + |
| 146 | + it("points an installed app to `hyperframes open` once the app opens handed-over projects", () => { |
| 147 | + expect(desktopHint(process.cwd(), { env: OUTSIDE, installed: true })).toBe( |
| 148 | + HANDOFF_READY ? "Edit it with Framey: hyperframes open ." : download, |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it("says nothing inside the app, whose runs carry HYPERFRAMES_DESKTOP_PROJECT", () => { |
| 153 | + expect( |
| 154 | + desktopHint(process.cwd(), { env: { HYPERFRAMES_DESKTOP_PROJECT: "/p" }, installed: true }), |
| 155 | + ).toBeNull(); |
| 156 | + }); |
| 157 | +}); |
0 commit comments