Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/rtk/hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { describe, it, expect, beforeEach } from "vitest";
import { describe, it, expect, beforeEach, vi } from "vitest";
import { getGains, resetGains, rewrite } from "./hook.js";
import * as childProcess from "node:child_process";

vi.mock("node:child_process", () => ({
execSync: vi.fn(),
}));

const mockExecSync = vi.mocked(childProcess.execSync);

describe("RTK stats tracking", () => {
beforeEach(() => {
Expand All @@ -22,17 +29,31 @@ describe("RTK stats tracking", () => {
});

describe("rtk rewrite", () => {
beforeEach(() => {
mockExecSync.mockReset();
});

it("rewrites known commands", () => {
mockExecSync.mockReturnValue("rtk git status");
const result = rewrite("git status");
expect(result).toBe("rtk git status");
});

it("rewrites ls", () => {
mockExecSync.mockReturnValue("rtk ls -la");
const result = rewrite("ls -la");
expect(result).toBe("rtk ls -la");
});

it("returns rewrite from exit code 3 stderr", () => {
const err = Object.assign(new Error("exit 3"), { stdout: "rtk cat README.md" });
mockExecSync.mockImplementation(() => { throw err; });
const result = rewrite("cat README.md");
expect(result).toBe("rtk cat README.md");
});

it("returns null for unknown commands", () => {
mockExecSync.mockImplementation(() => { throw new Error("exit 1"); });
const result = rewrite("python3 -c 'print(1)'");
expect(result).toBeNull();
});
Expand Down
Loading