src / utils.test.ts
import { describe, it, expect } from "vitest"
import { isCdCommand } from "./utils"
describe("isCdCommand", () => {
describe("detects cd commands", () => {
it("detects cd with path", () => {
expect(isCdCommand("cd /home/user")).toBe(true)
})
it("detects cd alone", () => {
expect(isCdCommand("cd")).toBe(true)
})
it("detects cd with leading whitespace", () => {
expect(isCdCommand(" cd /tmp")).toBe(true)
})
it("detects cd before &&", () => {
expect(isCdCommand("cd /tmp && ls")).toBe(true)
})
it("detects cd before ;", () => {
expect(isCdCommand("cd /tmp; ls")).toBe(true)
})
it("detects cd after ;", () => {
expect(isCdCommand("ls /tmp; cd /home")).toBe(true)
})
it("detects cd after &&", () => {
expect(isCdCommand("ls /tmp && cd /home")).toBe(true)
})
it("detects cd after ||", () => {
expect(isCdCommand("false || cd /tmp")).toBe(true)
})
it("detects cd after |", () => {
expect(isCdCommand("echo hi | cd /tmp")).toBe(true)
})
it("detects cd with case insensitivity", () => {
expect(isCdCommand("CD /tmp")).toBe(true)
expect(isCdCommand("Cd /tmp")).toBe(true)
})
})
describe("allows non-cd commands", () => {
it("allows ls", () => {
expect(isCdCommand("ls -la")).toBe(false)
})
it("allows grep for cd in file", () => {
expect(isCdCommand("grep 'cd' file.txt")).toBe(false)
})
it("allows echo mentioning cd", () => {
expect(isCdCommand("echo 'use cd instead'")).toBe(false)
})
it("allows mkdir", () => {
expect(isCdCommand("mkdir -p /tmp/newdir")).toBe(false)
})
it("allows git commands", () => {
expect(isCdCommand("git status")).toBe(false)
})
})
})