tui-effort-suggestions.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { completeEffortCommand } from "../src/app-keyboard.js";
  4. import { EffortSuggestionPanel } from "../src/app-effort-suggestion-panel.js";
  5. import {
  6. effortCommandQuery,
  7. filterEffortOptions,
  8. selectedEffortOption,
  9. visibleEffortOptionWindow,
  10. } from "../src/app-input.js";
  11. import { palette } from "../src/app-model.js";
  12. import type { TuiEffortOption } from "../src/types.js";
  13. const efforts: TuiEffortOption[] = [
  14. { description: "Quick checks", id: "low", label: "low" },
  15. { description: "Balanced reasoning", id: "medium", label: "medium" },
  16. { description: "Deep reasoning", id: "high", label: "high" },
  17. ];
  18. test("effort command popup filters effort choices from the composer draft", () => {
  19. assert.equal(effortCommandQuery("/effort"), "");
  20. assert.equal(effortCommandQuery("/effort hi"), "hi");
  21. assert.equal(effortCommandQuery("/efforthigh"), "high");
  22. assert.equal(effortCommandQuery("/variantmedium"), "medium");
  23. assert.equal(effortCommandQuery("/model"), undefined);
  24. assert.deepEqual(
  25. filterEffortOptions("/effort high", efforts).map((effort) => effort.id),
  26. ["high"],
  27. );
  28. assert.deepEqual(
  29. filterEffortOptions("/variant balanced", efforts).map((effort) => effort.id),
  30. ["medium"],
  31. );
  32. });
  33. test("effort command popup selection completes and submits explicit effort commands", () => {
  34. let draft = "";
  35. assert.equal(
  36. completeEffortCommand(efforts, { selectedIndex: 2 }, (value) => {
  37. draft = value;
  38. }),
  39. true,
  40. );
  41. assert.equal(draft, "/effort high");
  42. assert.equal(
  43. selectedEffortOption("/variant", { selectedIndex: 1 }, filterEffortOptions("/variant", efforts))
  44. ?.id,
  45. "medium",
  46. );
  47. });
  48. test("keeps selected effort option inside the visible window", () => {
  49. const manyEfforts: TuiEffortOption[] = Array.from({ length: 10 }, (_, index) => ({
  50. id: `level-${index}`,
  51. label: `level-${index}`,
  52. }));
  53. assert.deepEqual(visibleEffortOptionWindow(manyEfforts, 9, 6), {
  54. efforts: manyEfforts.slice(4, 10),
  55. selectedIndex: 5,
  56. startIndex: 4,
  57. });
  58. });
  59. test("renders effort suggestions with effort labels and muted descriptions", () => {
  60. const panel = EffortSuggestionPanel({
  61. contentWidth: 80,
  62. currentEffort: "medium",
  63. efforts,
  64. selectedIndex: 2,
  65. });
  66. assert.ok(panel);
  67. const lines = collectTextNodes(panel);
  68. assert.ok(lines.some((line) => line.text === "> "));
  69. assert.ok(lines.some((line) => line.text === "high"));
  70. assert.ok(lines.some((line) => line.text.includes("Deep reasoning")));
  71. assert.ok(lines.some((line) => line.text.includes("current")));
  72. assert.equal(lines.find((line) => line.text === "high")?.style.fg, palette.accent);
  73. assert.equal(lines.find((line) => line.text.includes("Deep reasoning"))?.style.fg, palette.muted);
  74. });
  75. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  76. const lines: ReturnType<typeof collectTextNodes> = [];
  77. const visit = (value: unknown) => {
  78. if (Array.isArray(value)) {
  79. for (const child of value) visit(child);
  80. return;
  81. }
  82. if (typeof value !== "object" || value === null || !("props" in value)) return;
  83. const element = value as {
  84. props?: {
  85. children?: unknown;
  86. style?: Record<string, unknown>;
  87. };
  88. type?: unknown;
  89. };
  90. if (typeof element.type === "function") {
  91. visit(element.type(element.props));
  92. return;
  93. }
  94. if (element.type === "text") {
  95. lines.push({
  96. style: element.props?.style ?? {},
  97. text: collectTextLines(element.props?.children).join(""),
  98. });
  99. }
  100. visit(element.props?.children);
  101. };
  102. visit(node);
  103. return lines;
  104. }
  105. function collectTextLines(node: unknown): string[] {
  106. const lines: string[] = [];
  107. const visit = (value: unknown) => {
  108. if (typeof value === "string") {
  109. lines.push(value);
  110. return;
  111. }
  112. if (Array.isArray(value)) {
  113. for (const child of value) visit(child);
  114. return;
  115. }
  116. if (typeof value === "object" && value !== null && "props" in value) {
  117. visit((value as { props?: { children?: unknown } }).props?.children);
  118. }
  119. };
  120. visit(node);
  121. return lines;
  122. }