import { describe, expect, test, vi } from "vitest"; import rtkExtension, { __test } from "../index.ts"; const { localFallbackRewrite, parseRtkVersion, shouldSkipCommand } = __test; type Handler = (event: any, ctx: any) => unknown | Promise; function fakePi(exec: (command: string, args: string[]) => Promise) { const handlers: Record = {}; const notes: Array<[string, string]> = []; return { handlers, notes, ctx: { hasUI: true, ui: { notify: vi.fn((message, level) => notes.push([message, level])) }, }, pi: { exec: vi.fn(exec), on: vi.fn((event: string, handler: Handler) => { if (!handlers[event]) handlers[event] = []; handlers[event].push(handler); }), ui: { notify: vi.fn((message, level) => notes.push([message, level])) }, }, }; } async function loadWithRewrite(code: number, stdout = "") { const fx = fakePi(async (_command, args) => args[0] === "--version" ? { code: 0, stdout: "rtk 0.42.4" } : { code, stdout }, ); await rtkExtension(fx.pi as never, fx.ctx as never); return fx; } async function emit( fx: Awaited>, command: string, toolName = "bash", ) { const event = { toolName, input: { command } }; await fx.handlers.tool_call?.[0]?.(event, { signal: AbortSignal.abort() }); return event.input.command; } describe("rtk extension", () => { test("parses versions and skip guards", () => { expect(parseRtkVersion("rtk 0.42.4")).toEqual([0, 42, 4]); expect(parseRtkVersion("garbage")).toBeNull(); expect(shouldSkipCommand("rtk git status")).toBe(true); expect(shouldSkipCommand("env RTK_DISABLED=1 git status")).toBe(true); expect(shouldSkipCommand("git status")).toBe(false); }); test("maps local noisy-output misses only", () => { expect(localFallbackRewrite("journalctl --user -n 100 --no-pager")).toBe( "rtk summary journalctl --user -n 100 --no-pager", ); expect(localFallbackRewrite("systemctl --user status pipewire")).toBe( "rtk summary systemctl --user status pipewire", ); expect(localFallbackRewrite("hyprctl clients")).toBe( "rtk summary hyprctl clients", ); expect(localFallbackRewrite("ps aux")).toBe("rtk summary ps aux"); expect( localFallbackRewrite("mise run test -- extensions/rtk"), ).toBeUndefined(); expect( localFallbackRewrite("mise exec -- biome check extensions/rtk"), ).toBeUndefined(); expect( localFallbackRewrite("journalctl --user | grep pipewire"), ).toBeUndefined(); }); test("warns once and registers nothing when rtk is missing", async () => { const fx = fakePi(async () => ({ code: 127, stdout: "" })); await rtkExtension(fx.pi as never, fx.ctx as never); expect(fx.handlers.tool_call).toBeUndefined(); expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]); }); test("warns through pi ui when no load ctx is passed", async () => { const fx = fakePi(async () => ({ code: 127, stdout: "" })); await rtkExtension(fx.pi as never); expect(fx.handlers.tool_call).toBeUndefined(); expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]); }); test("warns on startup hook when load ui is unavailable", async () => { const fx = fakePi(async () => ({ code: 127, stdout: "" })); const pi = { ...fx.pi, ui: undefined }; await rtkExtension(pi as never); expect(fx.handlers.tool_call).toBeUndefined(); expect(fx.notes).toEqual([]); fx.handlers.before_agent_start?.[0]?.({}, fx.ctx); expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]); fx.handlers.session_start?.[0]?.({}, fx.ctx); expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]); }); test("warns once and registers nothing when rtk is old", async () => { const fx = fakePi(async () => ({ code: 0, stdout: "rtk 0.22.9" })); await rtkExtension(fx.pi as never, fx.ctx as never); expect(fx.handlers.tool_call).toBeUndefined(); expect(fx.notes).toEqual([ ["rtk disabled: too old (rtk 0.22.9); need >= 0.23.0", "warning"], ]); }); test("registers only tool_call when rtk is available", async () => { const fx = await loadWithRewrite(1); expect(fx.pi.on).toHaveBeenCalledWith("tool_call", expect.any(Function)); expect(fx.notes).toEqual([]); }); test("rewrites via rtk and falls back only on no-match", async () => { expect( await emit(await loadWithRewrite(0, "rtk git status\n"), "git status"), ).toBe("rtk git status"); expect( await emit(await loadWithRewrite(3, "rtk cargo test\n"), "cargo test"), ).toBe("rtk cargo test"); expect(await emit(await loadWithRewrite(1), "ps aux")).toBe( "rtk summary ps aux", ); expect(await emit(await loadWithRewrite(2), "ps aux")).toBe("ps aux"); }); test("uses local fallback when pi.exec throws exit 1", async () => { const fx = fakePi(async (_command, args) => { if (args[0] === "--version") return { code: 0, stdout: "rtk 0.42.4" }; throw Object.assign(new Error("no match"), { exitCode: 1 }); }); await rtkExtension(fx.pi as never, fx.ctx as never); expect(await emit(fx as never, "ps aux")).toBe("rtk summary ps aux"); }); test("leaves non-bash and skipped commands untouched", async () => { const fx = await loadWithRewrite(0, "rtk git status\n"); expect(await emit(fx, "git status", "read")).toBe("git status"); expect(await emit(fx, "rtk git status")).toBe("rtk git status"); }); });