فهرست منبع

Add explicit TTY link output tests

dan mackinlay 1 ماه پیش
والد
کامیت
1bada2eba6
2فایلهای تغییر یافته به همراه20 افزوده شده و 6 حذف شده
  1. 2 2
      src/cli/qmd.ts
  2. 18 4
      test/cli.test.ts

+ 2 - 2
src/cli/qmd.ts

@@ -1904,8 +1904,8 @@ export function buildEditorUri(template: string, absolutePath: string, line: num
     .replace(/\{column\}/g, String(safeCol));
 }
 
-function termLink(text: string, url: string): string {
-  if (!process.stdout.isTTY) return text;
+export function termLink(text: string, url: string, isTTY: boolean = !!process.stdout.isTTY): string {
+  if (!isTTY) return text;
   return `\x1b]8;;${url}\x07${text}\x1b]8;;\x07`;
 }
 

+ 18 - 4
test/cli.test.ts

@@ -13,7 +13,7 @@ import { join, dirname } from "path";
 import { fileURLToPath } from "url";
 import { spawn } from "child_process";
 import { setTimeout as sleep } from "timers/promises";
-import { buildEditorUri } from "../src/cli/qmd.ts";
+import { buildEditorUri, termLink } from "../src/cli/qmd.ts";
 
 // Test fixtures directory and database path
 let testDir: string;
@@ -837,7 +837,8 @@ describe("CLI ls Command", () => {
   test("lists files in a collection", async () => {
     const { stdout, exitCode } = await runQmd(["ls", "fixtures"], { dbPath: localDbPath });
     expect(exitCode).toBe(0);
-    expect(stdout).toContain("qmd://fixtures/README.md");
+    // handelize converts to lowercase
+    expect(stdout).toContain("qmd://fixtures/readme.md");
     expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
   });
 
@@ -1174,13 +1175,14 @@ describe("search output formats", () => {
     expect(stdout).not.toMatch(/\/home\//);
   });
 
-  test("search default CLI format includes qmd:// path, docid, and context", async () => {
+  test("search default CLI format includes plain qmd:// path, docid, and context in non-TTY mode", async () => {
     const { stdout, exitCode } = await runQmd(["search", "test", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
     expect(exitCode).toBe(0);
 
-    // First line should have qmd:// path and docid
+    // runQmd uses piped stdio, so stdout is non-TTY and should not contain OSC 8 links.
     expect(stdout).toMatch(new RegExp(`^qmd://${collName}/.*#[a-f0-9]{6}`, "m"));
     expect(stdout).toContain("Context: Test fixtures for QMD");
+    expect(stdout).not.toContain("\x1b]8;;");
     // Ensure no full filesystem paths
     expect(stdout).not.toMatch(/\/Users\//);
     expect(stdout).not.toMatch(/\/home\//);
@@ -1209,6 +1211,18 @@ describe("editor URI templates", () => {
 
     expect(uri).toBe("cursor://file//tmp/docs/api.md:7:3");
   });
+
+  test("termLink returns plain text when stdout is not a TTY", () => {
+    const linked = termLink("docs/api.md:12", "vscode://file//tmp/docs/api.md:12:1", false);
+
+    expect(linked).toBe("docs/api.md:12");
+  });
+
+  test("termLink emits OSC 8 hyperlinks when stdout is a TTY", () => {
+    const linked = termLink("docs/api.md:12", "vscode://file//tmp/docs/api.md:12:1", true);
+
+    expect(linked).toBe("\x1b]8;;vscode://file//tmp/docs/api.md:12:1\x07docs/api.md:12\x1b]8;;\x07");
+  });
 });
 
 // =============================================================================