|
3 | 3 | * telemetry disclosure and selectable starters — and nothing left over once |
4 | 4 | * the transcript has content. |
5 | 5 | */ |
6 | | -import { describe, expect, test } from "bun:test"; |
| 6 | +import { afterEach, describe, expect, test } from "bun:test"; |
7 | 7 | import type { CapturedSpan } from "@opentui/core"; |
8 | 8 | import { rgbToHex } from "@opentui/core"; |
9 | 9 | import { withTestRenderer, type Harness } from "./harness"; |
@@ -47,6 +47,54 @@ import { UI } from "./theme"; |
47 | 47 | const SIZE = { width: 80, height: 24 } as const; |
48 | 48 | const NOTICE = "Anonymous usage telemetry is enabled. Disable in /settings."; |
49 | 49 |
|
| 50 | +const nativeSetInterval = globalThis.setInterval; |
| 51 | +const nativeClearInterval = globalThis.clearInterval; |
| 52 | + |
| 53 | +afterEach(() => { |
| 54 | + globalThis.setInterval = nativeSetInterval; |
| 55 | + globalThis.clearInterval = nativeClearInterval; |
| 56 | +}); |
| 57 | + |
| 58 | +/** |
| 59 | + * 125 is `LANDING_IDLE_REPAINT_INTERVAL_MS` in shell.ts. Hardcoded so a |
| 60 | + * cadence change fails these tests on purpose rather than tracking a product |
| 61 | + * export. |
| 62 | + */ |
| 63 | +const LANDING_IDLE_REPAINT_INTERVAL_MS = 125; |
| 64 | + |
| 65 | +type IntervalHandle = ReturnType<typeof nativeSetInterval>; |
| 66 | + |
| 67 | +/** Wrap globals; the original handle is returned so `unref` still exists. */ |
| 68 | +function wrapLandingIdleTimer(): { |
| 69 | + armed: IntervalHandle[]; |
| 70 | + cleared: unknown[]; |
| 71 | +} { |
| 72 | + const armed: IntervalHandle[] = []; |
| 73 | + const cleared: unknown[] = []; |
| 74 | + globalThis.setInterval = (( |
| 75 | + handler: Parameters<typeof nativeSetInterval>[0], |
| 76 | + delay?: number, |
| 77 | + ...args: unknown[] |
| 78 | + ) => { |
| 79 | + const handle = nativeSetInterval.call(globalThis, handler, delay, ...args); |
| 80 | + if (delay === LANDING_IDLE_REPAINT_INTERVAL_MS) armed.push(handle); |
| 81 | + return handle; |
| 82 | + }) as typeof nativeSetInterval; |
| 83 | + globalThis.clearInterval = ((handle: Parameters<typeof nativeClearInterval>[0]) => { |
| 84 | + cleared.push(handle); |
| 85 | + return nativeClearInterval.call(globalThis, handle); |
| 86 | + }) as typeof nativeClearInterval; |
| 87 | + return { armed, cleared }; |
| 88 | +} |
| 89 | + |
| 90 | +function soleLandingIdleHandle(armed: readonly IntervalHandle[]): IntervalHandle { |
| 91 | + const handle = armed[0]; |
| 92 | + if (armed.length !== 1 || handle === undefined) { |
| 93 | + throw new Error(`expected exactly one 125ms interval, got ${String(armed.length)}`); |
| 94 | + } |
| 95 | + return handle; |
| 96 | +} |
| 97 | + |
50 | 98 | /** Newly added scroll-box children need a layout pass before they paint. */ |
51 | 99 | async function settle(h: Harness): Promise<void> { |
52 | 100 | await h.renderOnce(); |
@@ -307,6 +355,40 @@ describe("landing screen", () => { |
307 | 355 | }, SIZE); |
308 | 356 | }, 15_000); |
309 | 357 |
|
| 358 | + test("appending a transcript row clears the landing idle timer", async () => { |
| 359 | + const { armed, cleared } = wrapLandingIdleTimer(); |
| 360 | + await withTestRenderer(async (h) => { |
| 361 | + const shell = createAppShell(h.renderer, { |
| 362 | + run: "idle", |
| 363 | + wireKeys: false, |
| 364 | + terminal: { columns: 80, rows: 24 }, |
| 365 | + }); |
| 366 | + try { |
| 367 | + const handle = soleLandingIdleHandle(armed); |
| 368 | + appendStreamRow(shell, { role: "user", text: "first prompt" }); |
| 369 | + expect(isLanding(shell)).toBe(false); |
| 370 | + expect(cleared).toContain(handle); |
| 371 | + await settle(h); |
| 372 | + } finally { |
| 373 | + shell.dispose(); |
| 374 | + } |
| 375 | + }, SIZE); |
| 376 | + }); |
| 377 | + |
| 378 | + test("disposing the shell with no transcript clears the landing idle timer", async () => { |
| 379 | + const { armed, cleared } = wrapLandingIdleTimer(); |
| 380 | + await withTestRenderer(async (h) => { |
| 381 | + const shell = createAppShell(h.renderer, { |
| 382 | + run: "idle", |
| 383 | + wireKeys: false, |
| 384 | + terminal: { columns: 80, rows: 24 }, |
| 385 | + }); |
| 386 | + const handle = soleLandingIdleHandle(armed); |
| 387 | + shell.dispose(); |
| 388 | + expect(cleared).toContain(handle); |
| 389 | + }, SIZE); |
| 390 | + }); |
| 391 | + |
310 | 392 | test("a starter key fills the prompt; a typed prompt keeps its digits", async () => { |
311 | 393 | await withTestRenderer(async (h) => { |
312 | 394 | const shell = createAppShell(h.renderer, { |
|
0 commit comments