|
| 1 | +/** |
| 2 | + * Tests for ideas module |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, test, beforeEach, afterEach } from "bun:test" |
| 6 | +import { mkdirSync, rmSync, existsSync } from "node:fs" |
| 7 | +import { join } from "node:path" |
| 8 | +import { |
| 9 | + loadAllIdeas, |
| 10 | + formatIdeasForSelection, |
| 11 | + getIdeaSummary, |
| 12 | + removeIdea, |
| 13 | + parseIdeaSelection, |
| 14 | + countIdeas, |
| 15 | +} from "../src/ideas.ts" |
| 16 | + |
| 17 | +const TEST_DIR = "/tmp/opencoder-test-ideas" |
| 18 | + |
| 19 | +describe("ideas", () => { |
| 20 | + beforeEach(() => { |
| 21 | + if (existsSync(TEST_DIR)) { |
| 22 | + rmSync(TEST_DIR, { recursive: true }) |
| 23 | + } |
| 24 | + mkdirSync(TEST_DIR, { recursive: true }) |
| 25 | + }) |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + if (existsSync(TEST_DIR)) { |
| 29 | + rmSync(TEST_DIR, { recursive: true }) |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + describe("loadAllIdeas", () => { |
| 34 | + test("returns empty array for non-existent directory", async () => { |
| 35 | + const ideas = await loadAllIdeas("/nonexistent/path") |
| 36 | + expect(ideas.length).toBe(0) |
| 37 | + }) |
| 38 | + |
| 39 | + test("loads markdown files from directory", async () => { |
| 40 | + await Bun.write(join(TEST_DIR, "idea1.md"), "# First Idea\nDescription") |
| 41 | + await Bun.write(join(TEST_DIR, "idea2.md"), "# Second Idea\nContent") |
| 42 | + await Bun.write(join(TEST_DIR, "not-an-idea.txt"), "Ignored file") |
| 43 | + |
| 44 | + const ideas = await loadAllIdeas(TEST_DIR) |
| 45 | + |
| 46 | + expect(ideas.length).toBe(2) |
| 47 | + expect(ideas.some((i) => i.filename === "idea1.md")).toBe(true) |
| 48 | + expect(ideas.some((i) => i.filename === "idea2.md")).toBe(true) |
| 49 | + }) |
| 50 | + |
| 51 | + test("skips empty files", async () => { |
| 52 | + await Bun.write(join(TEST_DIR, "empty.md"), "") |
| 53 | + await Bun.write(join(TEST_DIR, "whitespace.md"), " \n\t ") |
| 54 | + await Bun.write(join(TEST_DIR, "valid.md"), "Valid content") |
| 55 | + |
| 56 | + const ideas = await loadAllIdeas(TEST_DIR) |
| 57 | + |
| 58 | + expect(ideas.length).toBe(1) |
| 59 | + expect(ideas[0]?.filename).toBe("valid.md") |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + describe("getIdeaSummary", () => { |
| 64 | + test("extracts first line as summary", () => { |
| 65 | + const summary = getIdeaSummary("First line\nSecond line") |
| 66 | + expect(summary).toBe("First line") |
| 67 | + }) |
| 68 | + |
| 69 | + test("removes markdown headers", () => { |
| 70 | + const summary = getIdeaSummary("# Header Title\nContent") |
| 71 | + expect(summary).toBe("Header Title") |
| 72 | + }) |
| 73 | + |
| 74 | + test("truncates long summaries", () => { |
| 75 | + const longContent = "A".repeat(150) |
| 76 | + const summary = getIdeaSummary(longContent) |
| 77 | + |
| 78 | + expect(summary.length).toBeLessThanOrEqual(103) // 100 + "..." |
| 79 | + expect(summary.endsWith("...")).toBe(true) |
| 80 | + }) |
| 81 | + |
| 82 | + test("skips empty lines to find summary", () => { |
| 83 | + const summary = getIdeaSummary("\n\n# Title\nContent") |
| 84 | + expect(summary).toBe("Title") |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + describe("formatIdeasForSelection", () => { |
| 89 | + test("formats ideas with numbers and content", () => { |
| 90 | + const ideas = [ |
| 91 | + { path: "/a.md", filename: "a.md", content: "# Idea A\nContent A" }, |
| 92 | + { path: "/b.md", filename: "b.md", content: "# Idea B\nContent B" }, |
| 93 | + ] |
| 94 | + |
| 95 | + const formatted = formatIdeasForSelection(ideas) |
| 96 | + |
| 97 | + expect(formatted).toContain("## Idea 1: a.md") |
| 98 | + expect(formatted).toContain("## Idea 2: b.md") |
| 99 | + expect(formatted).toContain("Content A") |
| 100 | + expect(formatted).toContain("Content B") |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + describe("parseIdeaSelection", () => { |
| 105 | + test("parses valid selection response", () => { |
| 106 | + const response = "SELECTED_IDEA: 2\nREASON: It's simpler" |
| 107 | + const index = parseIdeaSelection(response) |
| 108 | + |
| 109 | + expect(index).toBe(1) // 0-indexed |
| 110 | + }) |
| 111 | + |
| 112 | + test("handles case insensitive matching", () => { |
| 113 | + const response = "selected_idea: 3\nreason: Quick win" |
| 114 | + const index = parseIdeaSelection(response) |
| 115 | + |
| 116 | + expect(index).toBe(2) |
| 117 | + }) |
| 118 | + |
| 119 | + test("returns null for invalid response", () => { |
| 120 | + const response = "I think we should do idea 2" |
| 121 | + const index = parseIdeaSelection(response) |
| 122 | + |
| 123 | + expect(index).toBeNull() |
| 124 | + }) |
| 125 | + |
| 126 | + test("returns null for zero selection", () => { |
| 127 | + const response = "SELECTED_IDEA: 0\nREASON: Invalid" |
| 128 | + const index = parseIdeaSelection(response) |
| 129 | + |
| 130 | + expect(index).toBeNull() |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + describe("countIdeas", () => { |
| 135 | + test("returns 0 for non-existent directory", async () => { |
| 136 | + const count = await countIdeas("/nonexistent") |
| 137 | + expect(count).toBe(0) |
| 138 | + }) |
| 139 | + |
| 140 | + test("counts markdown files", async () => { |
| 141 | + await Bun.write(join(TEST_DIR, "a.md"), "idea") |
| 142 | + await Bun.write(join(TEST_DIR, "b.md"), "idea") |
| 143 | + await Bun.write(join(TEST_DIR, "c.txt"), "not counted") |
| 144 | + |
| 145 | + const count = await countIdeas(TEST_DIR) |
| 146 | + expect(count).toBe(2) |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + describe("removeIdea", () => { |
| 151 | + test("removes existing file", async () => { |
| 152 | + const filePath = join(TEST_DIR, "to-remove.md") |
| 153 | + await Bun.write(filePath, "content") |
| 154 | + |
| 155 | + const result = removeIdea(filePath) |
| 156 | + |
| 157 | + expect(result).toBe(true) |
| 158 | + expect(existsSync(filePath)).toBe(false) |
| 159 | + }) |
| 160 | + |
| 161 | + test("returns false for non-existent file", () => { |
| 162 | + const result = removeIdea("/nonexistent/file.md") |
| 163 | + expect(result).toBe(false) |
| 164 | + }) |
| 165 | + }) |
| 166 | +}) |
0 commit comments