From f365568f1b17f4c836c9a7ee66c5c03362ec998d Mon Sep 17 00:00:00 2001 From: Shakker Date: Fri, 5 Jun 2026 00:11:34 +0100 Subject: [PATCH] test: shorten tool metadata home paths --- src/auto-reply/tool-meta.test.ts | 77 +++++++++++++++++--------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/auto-reply/tool-meta.test.ts b/src/auto-reply/tool-meta.test.ts index 2f27f4dbd981..deb90f519529 100644 --- a/src/auto-reply/tool-meta.test.ts +++ b/src/auto-reply/tool-meta.test.ts @@ -1,49 +1,54 @@ /** Tests compact tool metadata formatting for auto-reply progress output. */ import path from "node:path"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { describe, expect, it } from "vitest"; +import { withEnv } from "../test-utils/env.js"; import { formatToolAggregate, formatToolPrefix, shortenMeta, shortenPath } from "./tool-meta.js"; // Use path.resolve so inputs match the resolved HOME on every platform. const home = path.resolve("/Users/test"); -describe("tool meta formatting", () => { - beforeEach(() => { - vi.unstubAllEnvs(); - }); +function withHome(run: () => T): T { + return withEnv({ HOME: home }, run); +} +describe("tool meta formatting", () => { it("shortens paths under HOME", () => { - vi.stubEnv("HOME", home); - expect(shortenPath(home)).toBe("~"); - expect(shortenPath(`${home}/a/b.txt`)).toBe("~/a/b.txt"); - expect(shortenPath("/opt/x")).toBe("/opt/x"); + withHome(() => { + expect(shortenPath(home)).toBe("~"); + expect(shortenPath(`${home}/a/b.txt`)).toBe("~/a/b.txt"); + expect(shortenPath("/opt/x")).toBe("/opt/x"); + }); }); it("shortens meta strings with optional colon suffix", () => { - vi.stubEnv("HOME", home); - expect(shortenMeta(`${home}/a.txt`)).toBe("~/a.txt"); - expect(shortenMeta(`${home}/a.txt:12`)).toBe("~/a.txt:12"); - expect(shortenMeta(`cd ${home}/dir && ls`)).toBe("cd ~/dir && ls"); - expect(shortenMeta("")).toBe(""); + withHome(() => { + expect(shortenMeta(`${home}/a.txt`)).toBe("~/a.txt"); + expect(shortenMeta(`${home}/a.txt:12`)).toBe("~/a.txt:12"); + expect(shortenMeta(`cd ${home}/dir && ls`)).toBe("cd ~/dir && ls"); + expect(shortenMeta("")).toBe(""); + }); }); it("formats aggregates with grouping and brace-collapse", () => { - vi.stubEnv("HOME", home); - const out = formatToolAggregate(" fs ", [ - `${home}/dir/a.txt`, - `${home}/dir/b.txt`, - "note", - "a→b", - ]); - expect(out).toMatch(/^🧩 Fs/); - expect(out).toContain("~/dir/{a.txt, b.txt}"); - expect(out).toContain("note"); - expect(out).toContain("a→b"); + withHome(() => { + const out = formatToolAggregate(" fs ", [ + `${home}/dir/a.txt`, + `${home}/dir/b.txt`, + "note", + "a→b", + ]); + expect(out).toMatch(/^🧩 Fs/); + expect(out).toContain("~/dir/{a.txt, b.txt}"); + expect(out).toContain("note"); + expect(out).toContain("a→b"); + }); }); it("wraps aggregate meta in backticks when markdown is enabled", () => { - vi.stubEnv("HOME", home); - const out = formatToolAggregate("fs", [`${home}/dir/a.txt`], { markdown: true }); - expect(out).toContain("`~/dir/a.txt`"); + withHome(() => { + const out = formatToolAggregate("fs", [`${home}/dir/a.txt`], { markdown: true }); + expect(out).toContain("`~/dir/a.txt`"); + }); }); it("uses a longer inline code delimiter when meta contains backticks", () => { @@ -52,16 +57,18 @@ describe("tool meta formatting", () => { }); it("keeps exec flags outside markdown and moves them to the front", () => { - vi.stubEnv("HOME", home); - const out = formatToolAggregate("exec", [`cd ${home}/dir && gemini 2>&1 · elevated`], { - markdown: true, + withHome(() => { + const out = formatToolAggregate("exec", [`cd ${home}/dir && gemini 2>&1 · elevated`], { + markdown: true, + }); + expect(out).toBe("🛠️ elevated · `cd ~/dir && gemini 2>&1`"); }); - expect(out).toBe("🛠️ elevated · `cd ~/dir && gemini 2>&1`"); }); it("formats prefixes with default labels", () => { - vi.stubEnv("HOME", home); - expect(formatToolPrefix(undefined, undefined)).toBe("🧩 Tool"); - expect(formatToolPrefix("x", `${home}/a.txt`)).toBe("🧩 X: ~/a.txt"); + withHome(() => { + expect(formatToolPrefix(undefined, undefined)).toBe("🧩 Tool"); + expect(formatToolPrefix("x", `${home}/a.txt`)).toBe("🧩 X: ~/a.txt"); + }); }); });