Skip to content
Open
Show file tree
Hide file tree
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
94 changes: 94 additions & 0 deletions tools/cli-tools/src/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import stripAnsi from "strip-ansi"
import { describe, expect, it } from "vitest"

import {
demoFooter,
demoHeader,
errorMessage,
link,
sectionHeader,
successMessage,
wordWrap,
} from "./formatters"

describe("sectionHeader", () => {
it("creates a header with dividers matching the message width", () => {
const header = sectionHeader("Test Section")
const plain = stripAnsi(header)

const lines = plain.trim().split("\n")
expect(lines).toHaveLength(3)
// Divider length matches the message length
expect(lines[0]!.length).toBe(lines[1]!.length)
})

it("returns header with step number when provided", () => {
const header = sectionHeader("Do the thing", { step: 3 })
const plain = stripAnsi(header)

expect(plain).toContain("Step 3:")
expect(plain).toContain("Do the thing")
})

it("returns header without step prefix when no step is given", () => {
const header = sectionHeader("No step here")
const plain = stripAnsi(header)

expect(plain).not.toContain("Step")
})
})

describe("successMessage", () => {
it("returns message prefixed with a check mark", () => {
const msg = stripAnsi(successMessage("it worked"))
expect(msg).toBe("✓ it worked")
})
})

describe("errorMessage", () => {
it("returns message prefixed with an X", () => {
const msg = stripAnsi(errorMessage("it broke"))
expect(msg).toBe("✗ it broke")
})
})

describe("wordWrap", () => {
it("returns wrapped text at the specified width", () => {
const longText = "word ".repeat(30).trim()
const wrapped = wordWrap(longText, 20)

for (const line of wrapped.split("\n")) {
expect(line.length).toBeLessThanOrEqual(20)
}
})

it("returns text wrapped at 80 characters by default", () => {
const longText = "word ".repeat(50).trim()
const wrapped = wordWrap(longText)

for (const line of wrapped.split("\n")) {
expect(line.length).toBeLessThanOrEqual(80)
}
})
})

describe("demoHeader", () => {
it("returns a non-empty string", () => {
const header = demoHeader("ACK")
expect(header.length).toBeGreaterThan(0)
})
})

describe("demoFooter", () => {
it("returns a non-empty string", () => {
const footer = demoFooter("Done")
expect(footer.length).toBeGreaterThan(0)
})
})

describe("link", () => {
it("returns the URL with formatting applied", () => {
const result = link("https://example.com")
expect(stripAnsi(result)).toBe("https://example.com")
})
})
86 changes: 86 additions & 0 deletions tools/cli-tools/src/prompts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import stripAnsi from "strip-ansi"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"

import { log, logJson } from "./prompts"

// Capture console.log output
const logged: string[] = []

describe("log", () => {
beforeEach(() => {
vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => {
logged.push(args.map(String).join(" "))
})
logged.length = 0
})

afterEach(() => {
vi.restoreAllMocks()
})

it("returns output with the message printed to console", () => {
log("hello")
expect(logged.some((l) => stripAnsi(l).includes("hello"))).toBe(true)
})

it("returns wrapped text by default", () => {
const long = "word ".repeat(30).trim()
log(long)

// With wrapping, output should have multiple lines
const output = logged.join("\n")
expect(stripAnsi(output).split("\n").length).toBeGreaterThan(1)
})

it("returns unwrapped text when wrap is false", () => {
const long = "word ".repeat(30).trim()
log(long, { wrap: false })

// Without wrapping, the full string appears on one line
expect(logged.some((l) => stripAnsi(l) === long)).toBe(true)
})

it("returns output containing all provided messages", () => {
log("first", "second", "third")

const output = stripAnsi(logged.join(" "))
expect(output).toContain("first")
expect(output).toContain("second")
expect(output).toContain("third")
})

it("returns text wrapped at custom width", () => {
const long = "word ".repeat(30).trim()
log(long, { width: 20 })

// Each logged line should be within the custom width
for (const entry of logged) {
for (const line of stripAnsi(entry).split("\n")) {
if (line.length > 0) {
expect(line.length).toBeLessThanOrEqual(20)
}
}
}
})
})

describe("logJson", () => {
beforeEach(() => {
vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => {
logged.push(args.map(String).join(" "))
})
logged.length = 0
})

afterEach(() => {
vi.restoreAllMocks()
})

it("returns formatted JSON output", () => {
logJson({ key: "value" })

const output = stripAnsi(logged.join("\n"))
expect(output).toContain('"key"')
expect(output).toContain('"value"')
})
})
78 changes: 78 additions & 0 deletions tools/cli-tools/src/update-env-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import fs from "node:fs/promises"
import os from "node:os"
import path from "node:path"

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"

import { updateEnvFile } from "./update-env-file"

let tmpDir: string
let envPath: string

beforeEach(async () => {
vi.spyOn(console, "log").mockImplementation(() => {})
vi.spyOn(console, "error").mockImplementation(() => {})
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "cli-tools-test-"))
envPath = path.join(tmpDir, ".env")
})

afterEach(async () => {
await fs.rm(tmpDir, { recursive: true, force: true })
vi.restoreAllMocks()
})

describe("updateEnvFile", () => {
it("creates a new .env file when none exists", async () => {
await updateEnvFile({ API_KEY: "abc123" }, envPath)

const content = await fs.readFile(envPath, "utf8")
expect(content).toContain("API_KEY=abc123")
})

it("creates updated file with existing key replaced in-place", async () => {
await fs.writeFile(envPath, "API_KEY=old\nOTHER=keep\n")

await updateEnvFile({ API_KEY: "new" }, envPath)

const content = await fs.readFile(envPath, "utf8")
expect(content).toContain("API_KEY=new")
expect(content).toContain("OTHER=keep")
expect(content).not.toContain("API_KEY=old")
})

it("creates entry for keys that don't exist yet", async () => {
await fs.writeFile(envPath, "EXISTING=yes\n")

await updateEnvFile({ NEW_KEY: "hello" }, envPath)

const content = await fs.readFile(envPath, "utf8")
expect(content).toContain("EXISTING=yes")
expect(content).toContain("NEW_KEY=hello")
})

it("creates updated file preserving comments and blank lines", async () => {
await fs.writeFile(envPath, "# This is a comment\n\nAPI_KEY=old\n")

await updateEnvFile({ API_KEY: "new" }, envPath)

const content = await fs.readFile(envPath, "utf8")
expect(content).toContain("# This is a comment")
expect(content).toContain("API_KEY=new")
})

it("creates entries for multiple keys at once", async () => {
await updateEnvFile({ KEY_A: "a", KEY_B: "b", KEY_C: "c" }, envPath)

const content = await fs.readFile(envPath, "utf8")
expect(content).toContain("KEY_A=a")
expect(content).toContain("KEY_B=b")
expect(content).toContain("KEY_C=c")
})

it("creates entry with values containing equals signs", async () => {
await updateEnvFile({ URL: "https://example.com?a=1&b=2" }, envPath)

const content = await fs.readFile(envPath, "utf8")
expect(content).toContain("URL=https://example.com?a=1&b=2")
})
})