Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

Desktop Mochi: the pure logic and his life cycle, with tests

Claudedde859eefa77
diff --git a/windows/src/mochi/desktop-logic.ts b/windows/src/mochi/desktop-logic.ts
new file mode 100644
index 00000000..fc8d5e6a
--- /dev/null
+++ b/windows/src/mochi/desktop-logic.ts
@@ -0,0 +1,307 @@
+// Mochi on the desktop — the pure logic, port of
+// NotchBuddy/Sources/App/DesktopMochiLogic.swift and of the life cycle in
+// DesktopMochiController (DesktopMochi.swift). No DOM, no Tauri: the island
+// wires it up in src/island/desktop.ts, the desktop window draws him in
+// src/desktop/main.ts, and Rust owns the window (src-tauri/src/desktop.rs).
+//
+// Coordinates are y-down everywhere on a PC, so the Mac's DesktopSpace flip is
+// not needed: a point on the desktop is used as the OS reports it.
+
+import type { BotEmoteName, BotStateName } from "../core/layout";
+import type { Outfit } from "./wardrobe";
+
+/** What the island tells the desktop window, whenever it changes. */
+export interface DesktopSnapshot {
+ state: BotStateName;
+ /** He is always the main Mochi, so always dressed (try-ons included). */
+ outfit: Outfit;
+ soundEnabled: boolean;
+ soundVolume: number;
+ /** Tray → Pause: he dozes off and stays asleep. */
+ paused: boolean;
+}
+
+/** Events between the two windows. Rust adds `desktop-mochi-dropped`. */
+export const DESKTOP_EVENTS = {
+ /** island → desktop window */
+ state: "desktop-mochi-state",
+ emote: "desktop-mochi-emote",
+ /** desktop window → island */
+ ready: "desktop-mochi-ready",
+ home: "desktop-mochi-home",
+ wardrobe: "desktop-mochi-wardrobe",
+ dizzy: "desktop-mochi-dizzy",
+ /** Rust → island: a drag ended, `{ from: "island" | "desktop", home }`. */
+ dropped: "desktop-mochi-dropped",
+ /** Rust → desktop window */
+ cursor: "desktop-cursor",
+ visible: "desktop-visible",
+ flight: "desktop-flight",
+} as const;
+
+// ── Constants (DesktopMochiLogic) ────────────────────────────────────────────
+
+/** Side of the desktop window, logical pixels. */
+export const PANEL_SIZE = 120;
+/** Seconds without agent activity before he may fall asleep. */
+export const SLEEP_TIMEOUT = 120;
+/** He only dozes off once the pointer is at least this far from him. */
+export const SLEEP_MOUSE_DISTANCE = 150;
+/** Clickable body radius, as a fraction of the window side. */
+export const BODY_RADIUS_FRACTION = 0.24;
+
+/** Surprised, then off to the island (DesktopMochi.swift: 0.45 s). */
+export const RETRACT_DELAY_MS = 450;
+/** Pause in the island after the alert is answered, before flying back (0.6 s). */
+export const RETURN_DELAY_MS = 600;
+/** Single click → poke, unless it turns out to be a double click. */
+export const DOUBLE_CLICK_MS = 300;
+/** A press that moves farther than this is a drag, not a click. */
+export const DRAG_THRESHOLD = 3;
+
+// ── Pure helpers ─────────────────────────────────────────────────────────────
+
+export interface Point {
+ x: number;
+ y: number;
+}
+
+/**
+ * Whether Mochi should be asleep: no agent activity for longer than
+ * SLEEP_TIMEOUT (strictly), and the pointer not close to him.
+ */
+export function shouldSleep(sinceAgentActive: number, mouseDistanceToCenter: number): boolean {
+ return sinceAgentActive > SLEEP_TIMEOUT && mouseDistanceToCenter >= SLEEP_MOUSE_DISTANCE;
+}
+
+/** Hit test of the round body inside the square window (window-local). */
+export function isOverBody(local: Point, panelSize = PANEL_SIZE): boolean {
+ const c = panelSize / 2;
+ const r = panelSize * BODY_RADIUS_FRACTION;
+ const dx = local.x - c;
+ const dy = local.y - c;
+ return dx * dx + dy * dy <= r * r;
+}
+
+/** Eye-tracking origin: the window centre, in the same space as the cursor. */
+export function lookOrigin(panelMinX: number, panelMinY: number, panelSize = PANEL_SIZE): Point {
+ return { x: panelMinX + panelSize / 2, y: panelMinY + panelSize / 2 };
+}
+
+/** BotCanvasView.lookX / lookY: x right +, y up +. */
+export function gaze(bot: Point, mouse: Point): { lookX: number; lookY: number } {
+ return {
+ lookX: Math.tanh((mouse.x - bot.x) / 260),
+ lookY: -Math.tanh((mouse.y - bot.y) / 200),
+ };
+}
+
+/** An alert went up during a flight: once landed, he turns right back. */
+export function shouldRetractOnLanding(alertActive: boolean): boolean {
+ return alertActive;
+}
+
+/** Agent work keeps him awake; idle and sleeping don't. */
+export function agentActive(state: BotStateName): boolean {
+ return state !== "idle" && state !== "sleeping";
+}
+
+/**
+ * What sends him back to the island: a permission waiting for a click, or an
+ * agent asking a question. (The Mac's pendingApproval / pendingQuestion.)
+ */
+export function alertActive(s: {
+ pendingApproval: unknown;
+ tasks: ReadonlyArray<{ state: BotStateName }>;
+}): boolean {
+ return s.pendingApproval != null || s.tasks.some((t) => t.state === "question");
+}
+
+/**
+ * Linux has no global cursor (Wayland), so his window only hears the pointer
+ * while it is over his body. That counts as "near" for this long afterwards.
+ */
+export const POINTER_MEMORY_MS = 4000;
+
+/**
+ * Distance from the pointer to his centre, for shouldSleep. With a cursor
+ * poll (Windows) it is measured; without one, the pointer is near while it was
+ * over him a moment ago, and far otherwise.
+ */
+export function pointerDistance(
+ cursorPoll: boolean,
+ cursor: Point | null,
+ sincePointerMs: number,
+ panelSize = PANEL_SIZE,
+): number {
+ if (cursorPoll) {
+ return cursor ? Math.hypot(cursor.x - panelSize / 2, cursor.y - panelSize / 2) : Infinity;
+ }
+ return sincePointerMs < POINTER_MEMORY_MS ? 0 : Infinity;
+}
+
+/**
+ * Layer-shell drag: the surface is stretched over the display, so the pointer
+ * reads in display coordinates — once the compositor has applied it. Until
+ * then the pointer is still relative to the small surface at `origin`.
+ */
+export function layerDragTopLeft(origin: Point, client: Point, grab: Point, overlayApplied: boolean): Point {
+ const base = overlayApplied ? { x: 0, y: 0 } : origin;
+ return { x: base.x + client.x - grab.x, y: base.y + client.y - grab.y };
+}
+
+/** X11 drag: screen coordinates are global there; the window moves in physical pixels. */
+export function windowDragTopLeft(origin: Point, screen: Point, screenAtPress: Point, dpr: number): Point {
+ return {
+ x: origin.x + (screen.x - screenAtPress.x) * dpr,
+ y: origin.y + (screen.y - screenAtPress.y) * dpr,
+ };
+}
+
+// ── Life cycle (DesktopMochiController) ──────────────────────────────────────
+
+export type DesktopPhase =
+ /** No window on screen. */
+ | "home"
+ /** Window flying from the island to his spot. */
+ | "flyingOut"
+ /** On the desktop: the normal state. */
+ | "onDesktop"
+ /** An alert just went up: surprised, about to fly to the island. */
+ | "retracting"
+ /** The alert was answered while he was flying to the island. */
+ | "alertResolvedDuringRetract"
+ /** In the island, showing the alert; back out once it is answered. */
+ | "atNotchForAlert";
+
+/** Everything the life cycle does to the world, injected so it can be tested. */
+export interface DesktopPorts {
+ /** Shows the window at the island and flies it to his spot. False: no spot. */
+ flyOut(): Promise<boolean>;
+ /** Flies the window to the island and hides it. `forget`: he lives there again. */
+ flyHome(forget: boolean): Promise<boolean>;
+ /** The island's own Mochi hides while he is out. */
+ setAway(away: boolean): void;
+ /** An emote on the desktop Mochi. */
+ emote(emote: BotEmoteName, duration?: number): void;
+ play(sound: string): void;
+ alertActive(): boolean;
+ /** The island must be on screen for him to show the alert there. */
+ revealIsland(): void;
+ later(fn: () => void, ms: number): void;
+}
+
+export class DesktopMochiController {
+ phase: DesktopPhase = "home";
+ /** He lives on the desktop (the `onDesktop` preference). */
+ enabled = false;
+
+ private lastState: BotStateName | null = null;
+ private port: DesktopPorts;
+
+ constructor(port: DesktopPorts) {
+ this.port = port;
+ }
+
+ /** Launch, and back from an alert: fly out to his spot if he lives there. */
+ async launchFlyIfNeeded(): Promise<void> {
+ if (!this.enabled || this.phase !== "home") return;
+ // An alert is up: wait in the island, the alert's end flies him out.
+ if (this.port.alertActive()) {
+ this.phase = "atNotchForAlert";
+ return;
+ }
+ this.phase = "flyingOut";
+ this.port.setAway(true);
+ const ok = await this.port.flyOut();
+ if (this.phase !== "flyingOut") return;
+ if (!ok) {
+ // His spot is on a display that is gone: he stays home.
+ this.phase = "home";
+ this.enabled = false;
+ this.port.setAway(false);
+ return;
+ }
+ this.landed();
+ }
+
+ /** Dropped on the desktop after a drag out of the island (Rust placed him). */
+ installed() {
+ if (this.phase !== "home") return;
+ this.enabled = true;
+ this.port.setAway(true);
+ this.port.emote("happy", 0.6);
+ this.port.play("pop");
+ this.landed();
+ }
+
+ /** Double click, or dropped on the island: home for good. */
+ async flyHome(): Promise<void> {
+ // Also in the instant between an alert and his flight to the island: the
+ // user's "home" wins, and the pending retract finds nothing left to do.
+ const out = ["onDesktop", "retracting", "alertResolvedDuringRetract"];
+ if (!out.includes(this.phase)) return;
+ this.phase = "home";
+ this.enabled = false;
+ await this.port.flyHome(true);
+ this.port.play("peek");
+ this.port.setAway(false);
+ }
+
+ /** Call whenever the alert condition may have changed; only edges count. */
+ private alertWas = false;
+ updateAlert(active: boolean) {
+ if (active === this.alertWas) return;
+ this.alertWas = active;
+ if (active) {
+ if (this.phase === "onDesktop") this.beginRetract();
+ return;
+ }
+ if (this.phase === "atNotchForAlert") {
+ this.phase = "home";
+ this.port.later(() => void this.launchFlyIfNeeded(), RETURN_DELAY_MS);
+ } else if (this.phase === "retracting") {
+ this.phase = "alertResolvedDuringRetract";
+ }
+ }
+
+ /** Call with the effective state; a task finishing gets a happy jump. */
+ updateState(state: BotStateName) {
+ const prev = this.lastState;
+ this.lastState = state;
+ if (prev == null || prev === state) return;
+ if (state === "finished" && this.phase === "onDesktop") this.port.emote("happy", 1.2);
+ }
+
+ private landed() {
+ this.phase = "onDesktop";
+ // An alert may have gone up during the flight, when nothing reacts to it.
+ this.alertWas = this.port.alertActive();
+ if (shouldRetractOnLanding(this.alertWas)) this.beginRetract();
+ }
+
+ private beginRetract() {
+ this.port.emote("surprised");
+ this.phase = "retracting";
+ this.port.later(() => {
+ if (this.phase === "retracting") {
+ void this.retractForAlert();
+ } else if (this.phase === "alertResolvedDuringRetract") {
+ // Answered before he even took off: he never left his spot.
+ this.phase = "onDesktop";
+ }
+ }, RETRACT_DELAY_MS);
+ }
+
+ private async retractForAlert() {
+ await this.port.flyHome(false);
+ this.port.setAway(false);
+ if (this.phase === "alertResolvedDuringRetract") {
+ this.phase = "home";
+ await this.launchFlyIfNeeded();
+ } else if (this.phase === "retracting") {
+ this.phase = "atNotchForAlert";
+ this.port.revealIsland();
+ }
+ }
+}
diff --git a/windows/tests/desktop.test.mjs b/windows/tests/desktop.test.mjs
new file mode 100644
index 00000000..2a34b319
--- /dev/null
+++ b/windows/tests/desktop.test.mjs
@@ -0,0 +1,349 @@
+// Mochi on the desktop: the pure logic and the life cycle (src/mochi/desktop-logic.ts).
+// The first half mirrors tests/DesktopMochiTests.swift; the geometry of the
+// window itself (clamping, saved spots, the island's home zone) is tested in
+// src-tauri/src/desktop.rs, where it lives.
+
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import {
+ BODY_RADIUS_FRACTION, DesktopMochiController, PANEL_SIZE, RETRACT_DELAY_MS, RETURN_DELAY_MS,
+ agentActive, alertActive, gaze, isOverBody, layerDragTopLeft, lookOrigin, pointerDistance,
+ shouldRetractOnLanding, shouldSleep, windowDragTopLeft,
+} from "../src/mochi/desktop-logic.ts";
+
+// ── shouldSleep ───────────────────────────────────────────────────────────────
+
+test("shouldSleep: idle long enough and the pointer far away", () => {
+ assert.equal(shouldSleep(5, 300), false, "active agent must not sleep");
+ assert.equal(shouldSleep(200, 50), false, "mouse near panel must not sleep");
+ assert.equal(shouldSleep(200, 200), true, "long idle + far mouse must sleep");
+ assert.equal(shouldSleep(120, 300), false, "exactly at timeout must not sleep");
+ assert.equal(shouldSleep(120.1, 300), true, "just over timeout must sleep");
+ assert.equal(shouldSleep(200, 150), true, "at distance threshold must sleep");
+});
+
+test("agent work keeps him awake, idle and sleeping don't", () => {
+ for (const s of ["working", "thinking", "searching", "approval", "question", "finished", "error"]) {
+ assert.equal(agentActive(s), true, s);
+ }
+ assert.equal(agentActive("idle"), false);
+ assert.equal(agentActive("sleeping"), false);
+});
+
+test("pointer distance: measured with a cursor poll, remembered without one", () => {
+ assert.equal(pointerDistance(true, { x: 60, y: 60 }, 0), 0);
+ assert.equal(pointerDistance(true, { x: 60 + 150, y: 60 }, 0), 150);
+ assert.equal(pointerDistance(true, null, 0), Infinity, "no cursor yet: far");
+ // Linux: near while the pointer was over him a moment ago.
+ assert.equal(pointerDistance(false, null, 100), 0);
+ assert.equal(pointerDistance(false, { x: 60, y: 60 }, 60_000), Infinity);
+});
+
+// ── isOverBody ────────────────────────────────────────────────────────────────
+
+test("isOverBody: the round body, not the square window", () => {
+ const s = 120;
+ const r = s * BODY_RADIUS_FRACTION; // 28.8
+ assert.equal(isOverBody({ x: 60, y: 60 }, s), true, "center must be inside body");
+ assert.equal(isOverBody({ x: 60 + r - 0.5, y: 60 }, s), true, "inside radius must hit");
+ assert.equal(isOverBody({ x: 60 + r + 0.5, y: 60 }, s), false, "outside radius must miss");
+ assert.equal(isOverBody({ x: 0, y: 0 }, s), false, "corner must miss");
+ const diag = r / Math.SQRT2 - 0.5;
+ assert.equal(isOverBody({ x: 60 + diag, y: 60 + diag }, s), true, "diagonal inside must hit");
+ assert.equal(PANEL_SIZE, 120);
+});
+
+// ── lookOrigin / gaze ─────────────────────────────────────────────────────────
+
+const sign = (v) => (v === 0 ? 0 : v < 0 ? -1 : 1);
+
+/** The Mac test's AppKit points (y up from the menu-bar screen's bottom) → y down. */
+const topDown = (p, desktopTop) => ({ x: p.x, y: desktopTop - p.y });
+
+test("lookOrigin: the window centre, in desktop coordinates", () => {
+ // Panel at (200, 300) from the top-left of the desktop.
+ assert.deepEqual(lookOrigin(200, 300, 120), { x: 260, y: 360 });
+ // On a secondary screen to the right: x stays global, not screen-relative.
+ assert.deepEqual(lookOrigin(1540, 100, 120), { x: 1600, y: 160 });
+});
+
+test("gaze signs across a real arrangement of screens", () => {
+ // Menu-bar MacBook 1512×982 at (0,0); external 2560×1440 above it at
+ // (-500, 982); portrait 1080×1920 on the left at (-1080, -600). AppKit
+ // coordinates as in the Swift test, turned y-down like a PC reports them.
+ const top = 982;
+ const look = (bot, mouse) => {
+ const g = gaze(topDown(bot, top), topDown(mouse, top));
+ return [sign(g.lookX), sign(g.lookY)];
+ };
+ // Mochi in the island at the top of the external screen above.
+ const islandOnTop = { x: 780, y: 2422 - 16 };
+ assert.deepEqual(look(islandOnTop, { x: 1400, y: 100 }), [1, -1], "cursor on the MacBook below-right");
+ assert.deepEqual(look(islandOnTop, { x: -1000, y: 0 }), [-1, -1], "cursor on the portrait screen");
+ assert.deepEqual(look(islandOnTop, { x: 780, y: 2500 }), [0, 1], "cursor above Mochi");
+ // Mochi on the desktop of the portrait screen, cursor on the external screen above.
+ const panelMin = topDown({ x: -700, y: -200 + 120 }, top); // AppKit minY is the bottom edge
+ const desktopOnLeft = lookOrigin(panelMin.x, panelMin.y, 120);
+ const mouseAbove = topDown({ x: 1000, y: 2000 }, top);
+ assert.ok(mouseAbove.x > desktopOnLeft.x && mouseAbove.y < desktopOnLeft.y,
+ "desktop Mochi looks right and up at a cursor on another screen");
+ const g = gaze(desktopOnLeft, mouseAbove);
+ assert.ok(g.lookX > 0 && g.lookY > 0);
+});
+
+// ── shouldRetractOnLanding / alerts ───────────────────────────────────────────
+
+test("shouldRetractOnLanding", () => {
+ assert.equal(shouldRetractOnLanding(true), true, "must retract when alert is active on landing");
+ assert.equal(shouldRetractOnLanding(false), false, "must not retract when no alert on landing");
+});
+
+test("a permission or a question sends him back to the island", () => {
+ assert.equal(alertActive({ pendingApproval: null, tasks: [{ state: "working" }] }), false);
+ assert.equal(alertActive({ pendingApproval: { requestId: "r" }, tasks: [] }), true);
+ assert.equal(alertActive({ pendingApproval: null, tasks: [{ state: "idle" }, { state: "question" }] }), true);
+});
+
+// ── Linux drags ───────────────────────────────────────────────────────────────
+
+test("layer-shell drag: display coordinates once the overlay is up", () => {
+ const origin = { x: 500, y: 300 };
+ const grab = { x: 60, y: 62 };
+ // Not applied yet: the pointer is still relative to the small surface.
+ assert.deepEqual(layerDragTopLeft(origin, { x: 70, y: 62 }, grab, false), { x: 510, y: 300 });
+ // Applied: the pointer reads in display coordinates.
+ assert.deepEqual(layerDragTopLeft(origin, { x: 900, y: 462 }, grab, true), { x: 840, y: 400 });
+});
+
+test("X11 drag: screen delta, in physical pixels", () => {
+ assert.deepEqual(
+ windowDragTopLeft({ x: 1000, y: 600 }, { x: 520, y: 310 }, { x: 500, y: 300 }, 2),
+ { x: 1040, y: 620 },
+ );
+});
+
+// ── Life cycle ────────────────────────────────────────────────────────────────
+
+/** A world the controller acts on, with manual flights and timers. */
+function world({ alert = false, flyOutOk = true } = {}) {
+ const log = [];
+ const timers = [];
+ const flights = [];
+ const w = {
+ alert,
+ away: false,
+ log,
+ /** Lands the oldest pending flight. */
+ async land(result) {
+ const f = flights.shift();
+ assert.ok(f, "a flight should be in the air");
+ f.resolve(result ?? f.defaultResult);
+ await new Promise((r) => setImmediate(r));
+ },
+ get flying() {
+ return flights.map((f) => f.kind);
+ },
+ /** Runs the timers due within `ms`. */
+ async tick(ms) {
+ const due = timers.filter((t) => t.ms <= ms);
+ for (const t of due) timers.splice(timers.indexOf(t), 1);
+ for (const t of due) t.fn();
+ await new Promise((r) => setImmediate(r));
+ },
+ port: {
+ flyOut: () => new Promise((resolve) => flights.push({ kind: "out", resolve, defaultResult: flyOutOk })),
+ flyHome: (forget) => {
+ log.push(forget ? "home-forget" : "home-keep");
+ return new Promise((resolve) => flights.push({ kind: "home", resolve, defaultResult: true }));
+ },
+ setAway: (a) => {
+ w.away = a;
+ },
+ emote: (e) => log.push(`emote:${e}`),
+ play: (s) => log.push(`sound:${s}`),
+ alertActive: () => w.alert,
+ revealIsland: () => log.push("reveal"),
+ later: (fn, ms) => timers.push({ fn, ms }),
+ },
+ };
+ w.c = new DesktopMochiController(w.port);
+ return w;
+}
+
+test("launch: flies out to his spot only if he lives on the desktop", async () => {
+ const w = world();
+ await w.c.launchFlyIfNeeded();
+ assert.equal(w.c.phase, "home", "not enabled: stays home");
+ assert.deepEqual(w.flying, []);
+
+ w.c.enabled = true;
+ void w.c.launchFlyIfNeeded();
+ assert.equal(w.c.phase, "flyingOut");
+ assert.equal(w.away, true, "the island's Mochi hides as he leaves");
+ await w.land();
+ assert.equal(w.c.phase, "onDesktop");
+});
+
+test("launch: a spot on a display that is gone keeps him home", async () => {
+ const w = world({ flyOutOk: false });
+ w.c.enabled = true;
+ void w.c.launchFlyIfNeeded();
+ await w.land();
+ assert.equal(w.c.phase, "home");
+ assert.equal(w.c.enabled, false);
+ assert.equal(w.away, false);
+});
+
+test("launch during an alert waits in the island, then flies out once answered", async () => {
+ const w = world({ alert: true });
+ w.c.enabled = true;
+ w.c.updateAlert(true);
+ await w.c.launchFlyIfNeeded();
+ assert.equal(w.c.phase, "atNotchForAlert");
+ assert.deepEqual(w.flying, []);
+
+ w.alert = false;
+ w.c.updateAlert(false);
+ assert.equal(w.c.phase, "home");
+ await w.tick(RETURN_DELAY_MS);
+ assert.deepEqual(w.flying, ["out"]);
+ await w.land();
+ assert.equal(w.c.phase, "onDesktop");
+});
+
+test("alert on the desktop: surprised, flies to the island, comes back once answered", async () => {
+ const w = world();
+ w.c.enabled = true;
+ void w.c.launchFlyIfNeeded();
+ await w.land();
+
+ w.alert = true;
+ w.c.updateAlert(true);
+ assert.equal(w.c.phase, "retracting");
+ assert.ok(w.log.includes("emote:surprised"));
+ await w.tick(RETRACT_DELAY_MS);
+ assert.deepEqual(w.flying, ["home"]);
+ assert.ok(w.log.includes("home-keep"), "he keeps his spot");
+ await w.land();
+ assert.equal(w.c.phase, "atNotchForAlert");
+ assert.equal(w.away, false, "the island's Mochi shows the alert");
+ assert.ok(w.log.includes("reveal"));
+ assert.equal(w.c.enabled, true);
+
+ w.alert = false;
+ w.c.updateAlert(false);
+ await w.tick(RETURN_DELAY_MS);
+ await w.land();
+ assert.equal(w.c.phase, "onDesktop");
+});
+
+test("alert answered before he takes off: he never leaves his spot", async () => {
+ const w = world();
+ w.c.enabled = true;
+ void w.c.launchFlyIfNeeded();
+ await w.land();
+
+ w.alert = true;
+ w.c.updateAlert(true);
+ w.alert = false;
+ w.c.updateAlert(false);
+ assert.equal(w.c.phase, "alertResolvedDuringRetract");
+ await w.tick(RETRACT_DELAY_MS);
+ assert.equal(w.c.phase, "onDesktop");
+ assert.deepEqual(w.flying, []);
+});
+
+test("alert answered during the flight to the island: straight back out", async () => {
+ const w = world();
+ w.c.enabled = true;
+ void w.c.launchFlyIfNeeded();
+ await w.land();
+
+ w.alert = true;
+ w.c.updateAlert(true);
+ await w.tick(RETRACT_DELAY_MS);
+ assert.deepEqual(w.flying, ["home"]);
+ w.alert = false;
+ w.c.updateAlert(false);
+ assert.equal(w.c.phase, "alertResolvedDuringRetract");
+ await w.land();
+ assert.deepEqual(w.flying, ["out"], "relaunched as soon as he reached the island");
+ await w.land();
+ assert.equal(w.c.phase, "onDesktop");
+});
+
+test("an alert that went up during the flight out sends him right back", async () => {
+ const w = world();
+ w.c.enabled = true;
+ void w.c.launchFlyIfNeeded();
+ w.alert = true;
+ w.c.updateAlert(true); // ignored mid-flight
+ assert.equal(w.c.phase, "flyingOut");
+ await w.land();
+ assert.equal(w.c.phase, "retracting");
+ await w.tick(RETRACT_DELAY_MS);
+ assert.deepEqual(w.flying, ["home"]);
+});
+
+test("dropped on the desktop from the island: lands with a pop", () => {
+ const w = world();
+ w.c.installed();
+ assert.equal(w.c.phase, "onDesktop");
+ assert.equal(w.c.enabled, true);
+ assert.equal(w.away, true);
+ assert.ok(w.log.includes("sound:pop"));
+ assert.ok(w.log.includes("emote:happy"));
+});
+
+test("dropped on the desktop while an alert is up: turns right back", () => {
+ const w = world({ alert: true });
+ w.c.installed();
+ assert.equal(w.c.phase, "retracting");
+});
+
+test("double click or dropped on the island: home for good", async () => {
+ const w = world();
+ w.c.installed();
+ void w.c.flyHome();
+ assert.equal(w.c.phase, "home");
+ assert.equal(w.c.enabled, false);
+ assert.ok(w.log.includes("home-forget"));
+ await w.land();
+ assert.equal(w.away, false);
+ assert.ok(w.log.includes("sound:peek"));
+
+ // An alert now leaves him in the island.
+ w.alert = true;
+ w.c.updateAlert(true);
+ assert.equal(w.c.phase, "home");
+});
+
+test("sent home in the instant before an alert takes him: home wins", async () => {
+ const w = world();
+ w.c.installed();
+ w.alert = true;
+ w.c.updateAlert(true);
+ assert.equal(w.c.phase, "retracting");
+ void w.c.flyHome();
+ assert.equal(w.c.phase, "home");
+ await w.land();
+ await w.tick(RETRACT_DELAY_MS);
+ assert.deepEqual(w.flying, [], "the retract has nothing left to do");
+ w.alert = false;
+ w.c.updateAlert(false);
+ await w.tick(RETURN_DELAY_MS);
+ assert.deepEqual(w.flying, [], "and he doesn't come back out");
+});
+
+test("a finished task gets a happy jump, only on the desktop", () => {
+ const w = world();
+ w.c.updateState("working");
+ w.c.updateState("finished");
+ assert.ok(!w.log.includes("emote:happy"), "home: the island's Mochi celebrates");
+ w.c.installed();
+ w.log.length = 0;
+ w.c.updateState("working");
+ w.c.updateState("finished");
+ w.c.updateState("finished");
+ assert.deepEqual(w.log, ["emote:happy"]);
+});