|
4 | 4 | import { EventEmitter } from "node:events" |
5 | 5 | import { describe, expect, test } from "bun:test" |
6 | 6 | import type { PermissionRequest } from "../permission/types.js" |
7 | | -import { withTestRenderer } from "./harness.js" |
| 7 | +import type { KeyEvent } from "@opentui/core" |
| 8 | +import { withTestRenderer, type Harness } from "./harness.js" |
8 | 9 | import { OVERLAY_MAX_FRACTION } from "./geometry/index.js" |
9 | 10 | import { |
10 | 11 | acceptOverlaySelection, |
11 | 12 | createAppShell, |
| 13 | + exitOverlayAnswerMode, |
| 14 | + handleOverlayAnswerKey, |
| 15 | + moveOverlaySelection, |
| 16 | + setOverlayAnswerActive, |
12 | 17 | toggleOverlayExpand, |
13 | 18 | type AppShell, |
14 | 19 | } from "./shell.js" |
@@ -479,3 +484,176 @@ describe("permission overlay height", () => { |
479 | 484 | expect(capped).toBeLessThan(await hostRowsFor(rows, 1) + 40) |
480 | 485 | }) |
481 | 486 | }) |
| 487 | + |
| 488 | +describe("operator question overlay", () => { |
| 489 | + const emitOperator = ( |
| 490 | + shell: AppShell, |
| 491 | + options: readonly string[], |
| 492 | + onResolve: (result: unknown) => void, |
| 493 | + ): void => { |
| 494 | + const emitter = new EventEmitter() |
| 495 | + wireGates(emitter, shell) |
| 496 | + emitter.emit("operator.gate", { |
| 497 | + question: "Scope for this run is still <SCOPE>. What should it be?", |
| 498 | + options: [...options], |
| 499 | + resolve: onResolve, |
| 500 | + }) |
| 501 | + } |
| 502 | + |
| 503 | + const keyOf = (seq: string, name?: string): KeyEvent => |
| 504 | + ({ |
| 505 | + name: name ?? seq, |
| 506 | + sequence: seq, |
| 507 | + ctrl: false, |
| 508 | + meta: false, |
| 509 | + option: false, |
| 510 | + }) as unknown as KeyEvent |
| 511 | + |
| 512 | + const withOperator = async ( |
| 513 | + rows: number, |
| 514 | + options: readonly string[], |
| 515 | + body: ( |
| 516 | + h: Harness, |
| 517 | + shell: AppShell, |
| 518 | + resolved: () => unknown, |
| 519 | + ) => void | Promise<void>, |
| 520 | + ): Promise<void> => { |
| 521 | + await withTestRenderer( |
| 522 | + async (h) => { |
| 523 | + const shell = createAppShell(h.renderer, { |
| 524 | + terminal: { columns: 96, rows }, |
| 525 | + run: "idle", |
| 526 | + }) |
| 527 | + let resolved: unknown = undefined |
| 528 | + try { |
| 529 | + emitOperator(shell, options, (r) => { |
| 530 | + resolved = r |
| 531 | + }) |
| 532 | + await body(h, shell, () => resolved) |
| 533 | + } finally { |
| 534 | + shell.dispose() |
| 535 | + } |
| 536 | + }, |
| 537 | + { width: 96, height: rows }, |
| 538 | + ) |
| 539 | + } |
| 540 | + |
| 541 | + const frameOf = async ( |
| 542 | + h: Harness, |
| 543 | + ): Promise<string> => { |
| 544 | + await h.renderOnce() |
| 545 | + await h.renderOnce() |
| 546 | + return h.captureCharFrame() |
| 547 | + } |
| 548 | + |
| 549 | + for (const rows of [24, 60]) { |
| 550 | + test(`several options render and resolve by index at ${rows} rows`, async () => { |
| 551 | + await withOperator(rows, ["repo only", "docs too", "everything"], (h, shell, resolved) => { |
| 552 | + expect(shell.overlayKind).toBe("operator") |
| 553 | + expect(shell.overlayItems).toEqual(["repo only", "docs too", "everything"]) |
| 554 | + moveOverlaySelection(shell, 1) |
| 555 | + acceptOverlaySelection(shell) |
| 556 | + expect(resolved()).toEqual({ kind: "option", index: 1 }) |
| 557 | + void h |
| 558 | + }) |
| 559 | + }) |
| 560 | + |
| 561 | + test(`a single option still renders a choosable row at ${rows} rows`, async () => { |
| 562 | + await withOperator(rows, ["only this"], (h, shell, resolved) => { |
| 563 | + expect(shell.overlayItems).toEqual(["only this"]) |
| 564 | + acceptOverlaySelection(shell) |
| 565 | + expect(resolved()).toEqual({ kind: "option", index: 0 }) |
| 566 | + void h |
| 567 | + }) |
| 568 | + }) |
| 569 | + |
| 570 | + test(`no options opens straight into the answer field at ${rows} rows`, async () => { |
| 571 | + await withOperator(rows, [], async (h, shell, resolved) => { |
| 572 | + expect(shell.overlayKind).toBe("operator") |
| 573 | + expect(shell.overlayItems).toEqual([]) |
| 574 | + const frame = await frameOf(h) |
| 575 | + // Never offer a chooser with nothing to choose. |
| 576 | + expect(frame).not.toContain("Enter choose") |
| 577 | + expect(frame).toContain("Enter send") |
| 578 | + expect(frame).toContain("answer>") |
| 579 | + // Enter with nothing typed must not resolve the gate at all. |
| 580 | + acceptOverlaySelection(shell) |
| 581 | + expect(resolved()).toBeUndefined() |
| 582 | + }) |
| 583 | + }) |
| 584 | + } |
| 585 | + |
| 586 | + test("the answer field is advertised on screen next to the choices", async () => { |
| 587 | + await withOperator(40, ["repo only", "everything"], async (h) => { |
| 588 | + const frame = await frameOf(h) |
| 589 | + expect(frame).toContain("Tab type an answer") |
| 590 | + expect(frame).toContain("type your own answer") |
| 591 | + }) |
| 592 | + }) |
| 593 | + |
| 594 | + test("a typed answer round-trips as a custom OperatorResult", async () => { |
| 595 | + await withOperator(40, ["repo only", "everything"], (h, shell, resolved) => { |
| 596 | + expect(setOverlayAnswerActive(shell, true)).toBe(true) |
| 597 | + for (const ch of "src and docs") { |
| 598 | + expect(handleOverlayAnswerKey(shell, keyOf(ch))).toBe(true) |
| 599 | + } |
| 600 | + expect(handleOverlayAnswerKey(shell, keyOf("x", "backspace"))).toBe(true) |
| 601 | + expect(handleOverlayAnswerKey(shell, keyOf("", "return"))).toBe(true) |
| 602 | + expect(resolved()).toEqual({ kind: "custom", text: "src and doc" }) |
| 603 | + // Submitting closes the overlay, so the host is free for the next gate. |
| 604 | + expect(shell.overlayList).toBeNull() |
| 605 | + void h |
| 606 | + }) |
| 607 | + }) |
| 608 | + |
| 609 | + test("Esc in the answer field returns to the choices instead of cancelling", async () => { |
| 610 | + await withOperator(40, ["repo only"], (h, shell, resolved) => { |
| 611 | + setOverlayAnswerActive(shell, true) |
| 612 | + expect(exitOverlayAnswerMode(shell)).toBe(true) |
| 613 | + expect(shell.overlayList).not.toBeNull() |
| 614 | + expect(resolved()).toBeUndefined() |
| 615 | + void h |
| 616 | + }) |
| 617 | + }) |
| 618 | + |
| 619 | + test("a gate arriving while another overlay is open opens once that one closes", async () => { |
| 620 | + await withTestRenderer( |
| 621 | + async (h) => { |
| 622 | + const shell = createAppShell(h.renderer, { |
| 623 | + terminal: { columns: 96, rows: 40 }, |
| 624 | + run: "idle", |
| 625 | + }) |
| 626 | + const emitter = new EventEmitter() |
| 627 | + let approved: unknown = undefined |
| 628 | + let answered: unknown = undefined |
| 629 | + try { |
| 630 | + wireGates(emitter, shell) |
| 631 | + emitter.emit("permission.gate", { |
| 632 | + request: baseRequest(), |
| 633 | + resolve: (o: unknown) => { |
| 634 | + approved = o |
| 635 | + }, |
| 636 | + }) |
| 637 | + emitter.emit("operator.gate", { |
| 638 | + question: "Scope for this run?", |
| 639 | + options: ["repo only"], |
| 640 | + resolve: (r: unknown) => { |
| 641 | + answered = r |
| 642 | + }, |
| 643 | + }) |
| 644 | + expect(shell.overlayKind).toBe("permissions") |
| 645 | + |
| 646 | + acceptOverlaySelection(shell) |
| 647 | + expect(approved).toEqual({ allow: false }) |
| 648 | + // The queued question is not lost: it takes the host as it frees up. |
| 649 | + expect(shell.overlayKind).toBe("operator") |
| 650 | + acceptOverlaySelection(shell) |
| 651 | + expect(answered).toEqual({ kind: "option", index: 0 }) |
| 652 | + } finally { |
| 653 | + shell.dispose() |
| 654 | + } |
| 655 | + }, |
| 656 | + { width: 96, height: 40 }, |
| 657 | + ) |
| 658 | + }) |
| 659 | +}) |
0 commit comments