|
| 1 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test" |
| 2 | +import { mkdir, writeFile } from "fs/promises" |
| 3 | +import { join } from "path" |
| 4 | +import { |
| 5 | + cleanup, cliVersion, createScratch, createSession, e2eEnabled, installedPackage, |
| 6 | + installObserver, startServer, startTuiWith, stopTuis, writeCliConfig, type Scratch, |
| 7 | +} from "./e2e/harness.js" |
| 8 | +import { NapRun, SPACE, waitUntil, Watch } from "./e2e/nap-run.js" |
| 9 | +import { napOptions, NAP_TIMEOUT_MS } from "./e2e/timing.js" |
| 10 | + |
| 11 | +const FOCUS_IN = "\x1b[I" |
| 12 | +const FOCUS_OUT = "\x1b[O" |
| 13 | + |
| 14 | +function focusReporting(raw: string): boolean { |
| 15 | + let on = false |
| 16 | + // eslint-disable-next-line no-control-regex |
| 17 | + for (const match of raw.matchAll(/\x1b\[\?([\d;]+)([hl])/g)) { |
| 18 | + if (match[1].split(";").includes("1004")) on = match[2] === "h" |
| 19 | + } |
| 20 | + return on |
| 21 | +} |
| 22 | + |
| 23 | +let scratch: Scratch |
| 24 | +let server: Awaited<ReturnType<typeof startServer>> |
| 25 | + |
| 26 | +/** Start the packed plugin only after the real renderer has received focus. */ |
| 27 | +async function openAfterFocus(name: string, report: string) { |
| 28 | + const watch = await Watch.configure(scratch, name, napOptions) |
| 29 | + const gate = join(scratch.root, `${name}-gate`) |
| 30 | + await mkdir(gate) |
| 31 | + await writeFile(join(gate, "package.json"), JSON.stringify({ name: "nap-e2e-focus-gate", private: true, type: "module", exports: { "./tui": "./tui.js" } })) |
| 32 | + await writeFile(join(gate, "tui.js"), ` |
| 33 | + import nap from ${JSON.stringify(join(installedPackage(scratch), "dist/tui.js"))} |
| 34 | + import { appendFileSync } from "node:fs" |
| 35 | + export default { id: "nap-e2e-focus-gate", async setup(api) { |
| 36 | + if (!process.env.NAP_E2E_FOCUS_GATE_PASSED) { |
| 37 | + await new Promise(resolve => { |
| 38 | + const ready = () => { api.renderer.off(${JSON.stringify(report === FOCUS_OUT ? "blur" : "focus")}, ready); resolve() } |
| 39 | + api.renderer.on(${JSON.stringify(report === FOCUS_OUT ? "blur" : "focus")}, ready) |
| 40 | + appendFileSync(${JSON.stringify(watch.observerLog)}, JSON.stringify({event:"waiting-for-focus"}) + "\\n") |
| 41 | + }) |
| 42 | + process.env.NAP_E2E_FOCUS_GATE_PASSED = "1" |
| 43 | + } |
| 44 | + return nap.setup(api) |
| 45 | + } } |
| 46 | + `) |
| 47 | + await writeCliConfig(scratch, [ |
| 48 | + { package: gate, options: { ...napOptions, debugLogFile: watch.napLog } }, |
| 49 | + { package: await installObserver(scratch), options: { logFile: watch.observerLog, controlDir: watch.controlDir } }, |
| 50 | + ]) |
| 51 | + const sessionID = await createSession(scratch, server.url) |
| 52 | + const tui = await startTuiWith(scratch, name, ["--server", server.url, "-s", sessionID, scratch.project]) |
| 53 | + const run = new NapRun(tui, watch) |
| 54 | + await waitUntil(() => tui.output().includes("\x1b[?1004$p") || undefined, 60_000, "the focus mode query") |
| 55 | + tui.write("\x1b[?1004;2$y") |
| 56 | + await waitUntil(() => focusReporting(tui.output()) || undefined, 30_000, "focus reporting on") |
| 57 | + await waitUntil(async () => (await watch.observerEntries()).some(entry => entry.event === "waiting-for-focus") || undefined, 30_000, "delayed plugin setup") |
| 58 | + tui.write(report) |
| 59 | + await run.waitForWatching(1) |
| 60 | + await run.waitForInteractive(0) |
| 61 | + return { tui, run } |
| 62 | +} |
| 63 | + |
| 64 | +describe.skipIf(!e2eEnabled)(`OpenCode ${cliVersion}: focus received before plugin setup`, () => { |
| 65 | + beforeAll(async () => { |
| 66 | + scratch = await createScratch() |
| 67 | + server = await startServer(scratch) |
| 68 | + }, 300_000) |
| 69 | + afterEach(stopTuis, 60_000) |
| 70 | + afterAll(cleanup, 60_000) |
| 71 | + |
| 72 | + it("ac-focus-startup: an already-unfocused window naps and wakes on refocus", async () => { |
| 73 | + const { tui, run } = await openAfterFocus("early-blur", FOCUS_OUT) |
| 74 | + await run.waitForNap(NAP_TIMEOUT_MS) |
| 75 | + expect(focusReporting(tui.output()), await run.context()).toBe(true) |
| 76 | + const focusAt = Date.now() |
| 77 | + tui.write(FOCUS_IN) |
| 78 | + await run.waitForWatching(2) |
| 79 | + await run.waitForInteractive(focusAt) |
| 80 | + }, 120_000) |
| 81 | + |
| 82 | + it("ac-focus-startup: an already-focused window still waits for a key or click", async () => { |
| 83 | + const { tui, run } = await openAfterFocus("early-focus", FOCUS_IN) |
| 84 | + await run.waitForNap(NAP_TIMEOUT_MS) |
| 85 | + // A real terminal sends no focus reports while this mode is disabled. |
| 86 | + expect(focusReporting(tui.output()), await run.context()).toBe(false) |
| 87 | + await run.assertNapping() |
| 88 | + const spaceAt = await run.wake(SPACE) |
| 89 | + await run.waitForWatching(2) |
| 90 | + await run.waitForInteractive(spaceAt) |
| 91 | + }, 120_000) |
| 92 | +}) |
0 commit comments