tui-console.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import assert from "node:assert/strict";
  2. import { Console } from "node:console";
  3. import { Writable } from "node:stream";
  4. import { test } from "node:test";
  5. import { generateText } from "ai";
  6. import { MockLanguageModelV3 } from "ai/test";
  7. import { extractDisallowedToolsArgs } from "../src/arguments.js";
  8. import { installStderrConsoleBoundary } from "../src/protocol-console.js";
  9. import { interceptKnownRuntimeWarnings } from "../src/runtime-warnings.js";
  10. import { interceptTuiStderr, isTuiInvocation } from "../src/tui-stderr.js";
  11. function output() {
  12. let text = "";
  13. const stream = new Writable({
  14. write(chunk, _encoding, callback) {
  15. text += chunk.toString();
  16. callback();
  17. },
  18. }) as NodeJS.WriteStream;
  19. return { stream, read: () => text };
  20. }
  21. test("TUI console captures real AI SDK warnings while preserving model output and explicit errors", async () => {
  22. const stdout = output();
  23. const stderr = output();
  24. const previousConsole = globalThis.console;
  25. const previousWarningLogger = globalThis.AI_SDK_LOG_WARNINGS;
  26. globalThis.console = new Console({ stdout: stdout.stream, stderr: stderr.stream });
  27. const restoreConsole = installStderrConsoleBoundary(stderr.stream);
  28. const warnings = interceptKnownRuntimeWarnings(stderr.stream);
  29. const tui = interceptTuiStderr(stderr.stream);
  30. globalThis.AI_SDK_LOG_WARNINGS = undefined;
  31. try {
  32. const result = await generateText({
  33. prompt: "test",
  34. model: new MockLanguageModelV3({
  35. doGenerate: {
  36. content: [{ type: "text", text: "model response" }],
  37. finishReason: { unified: "stop", raw: "stop" },
  38. usage: {
  39. inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 },
  40. outputTokens: { total: 1, text: 1, reasoning: 0 },
  41. },
  42. warnings: [{ type: "other", message: "sdk-warning-regression" }],
  43. },
  44. }),
  45. });
  46. console.info("dependency info");
  47. console.debug("dependency debug");
  48. console.log("dependency log");
  49. console.error("dependency error");
  50. assert.equal(result.text, "model response");
  51. assert.equal(stdout.read(), "");
  52. assert.equal(stderr.read(), "");
  53. assert.match(tui.bufferedOutput, /AI SDK Warning System/);
  54. assert.match(tui.bufferedOutput, /sdk-warning-regression/);
  55. assert.match(tui.bufferedOutput, /dependency info/);
  56. assert.match(tui.bufferedOutput, /dependency debug/);
  57. assert.match(tui.bufferedOutput, /dependency log/);
  58. assert.match(tui.bufferedOutput, /dependency error/);
  59. tui.passthrough.write("Error: actionable CLI failure\n");
  60. assert.equal(stderr.read(), "Error: actionable CLI failure\n");
  61. tui.restore({ flush: true });
  62. assert.match(stderr.read(), /sdk-warning-regression/);
  63. assert.doesNotMatch(stderr.read(), /AI SDK Warning System/);
  64. } finally {
  65. tui.restore();
  66. warnings.restore();
  67. restoreConsole();
  68. globalThis.console = previousConsole;
  69. globalThis.AI_SDK_LOG_WARNINGS = previousWarningLogger;
  70. }
  71. });
  72. test("TUI output protection recognizes the same supported flags as command routing", () => {
  73. for (const args of [
  74. [],
  75. ["tui"],
  76. ["--locale", "zh-CN", "--mode", "yolo"],
  77. ["--cwd", "app-server", "--locale=en-US", "tui"],
  78. ["--browser-use=headless", "--browser-executable", "/test/browser"],
  79. ["--force-mcs", "--continue"],
  80. ["tui", "--disallowedTools", "Bash", "web_search", "--locale", "en-US"],
  81. ["--disallowed-tools=Bash(rm:*)", "tui"],
  82. ]) {
  83. assert.equal(isTuiInvocation(args), true, JSON.stringify(args));
  84. }
  85. });
  86. test("console isolation never captures ordinary CLI output or mistakes flag values for TUI", () => {
  87. for (const args of [
  88. ["--help"],
  89. ["--version"],
  90. ["--prompt", "tui"],
  91. ["-p", "tui"],
  92. ["--target", "tui"],
  93. ["--cwd", "tui", "login"],
  94. ["login", "bigmodel", "--no-browser"],
  95. ["app-server", "--stdio"],
  96. ["agent-server", "--stdio"],
  97. ["tui", "--invalid-option"],
  98. ]) {
  99. assert.equal(isTuiInvocation(args), false, JSON.stringify(args));
  100. }
  101. });
  102. test("shared tool-argument extraction preserves normalization and rejects missing values", () => {
  103. assert.deepEqual(
  104. extractDisallowedToolsArgs([
  105. "tui",
  106. "--disallowedTools",
  107. "Bash(rm:*,cp:*)",
  108. "web_search",
  109. "web_search",
  110. "--locale",
  111. "en-US",
  112. "--disallowed-tools=Edit,Read",
  113. ]),
  114. {
  115. args: ["tui", "--locale", "en-US"],
  116. toolDisallowlist: ["Bash(rm:*,cp:*)", "WebSearch", "Edit", "Read"],
  117. },
  118. );
  119. assert.throws(() => extractDisallowedToolsArgs(["--disallowedTools", "--locale", "en-US"]));
  120. });
  121. test("console boundary restores the original console once and keeps diagnostics off protocol stdout", () => {
  122. const previousConsole = globalThis.console;
  123. const stderr = output();
  124. const restore = installStderrConsoleBoundary(stderr.stream);
  125. try {
  126. console.info("protocol-safe info");
  127. assert.equal(stderr.read(), "protocol-safe info\n");
  128. restore();
  129. assert.equal(globalThis.console, previousConsole);
  130. restore();
  131. assert.equal(globalThis.console, previousConsole);
  132. } finally {
  133. restore();
  134. }
  135. });
  136. test("long-running TUI diagnostics are bounded and write callbacks still complete", async () => {
  137. const stderr = output();
  138. const tui = interceptTuiStderr(stderr.stream);
  139. try {
  140. await new Promise<void>((resolve, reject) => {
  141. stderr.stream.write(`${"x".repeat(100_000)}latest diagnostic`, (error) => {
  142. if (error) reject(error);
  143. else resolve();
  144. });
  145. });
  146. assert.equal(stderr.read(), "");
  147. assert.ok(tui.bufferedOutput.length <= 64 * 1024);
  148. assert.ok(tui.bufferedOutput.endsWith("latest diagnostic"));
  149. } finally {
  150. tui.restore();
  151. }
  152. });