-
Notifications
You must be signed in to change notification settings - Fork 100
test(cli-tools): add tests for formatters, prompts, and updateEnvFile #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ak68a
wants to merge
3
commits into
agentcommercekit:main
Choose a base branch
from
ak68a:test/cli-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.