diff --git a/packages/editor/App.tsx b/packages/editor/App.tsx index 73ef1d9..f66abb6 100644 --- a/packages/editor/App.tsx +++ b/packages/editor/App.tsx @@ -506,7 +506,11 @@ const App: React.FC = () => { } if (bearSettings.enabled) { - body.bear = { plan: markdown }; + body.bear = { + plan: markdown, + customTags: bearSettings.customTags, + tagPosition: bearSettings.tagPosition, + }; } // Include annotations as feedback if any exist (for OpenCode "approve with notes") @@ -731,7 +735,12 @@ const App: React.FC = () => { } } if (target === 'bear') { - body.bear = { plan: markdown }; + const bs = getBearSettings(); + body.bear = { + plan: markdown, + customTags: bs.customTags, + tagPosition: bs.tagPosition, + }; } try { diff --git a/packages/server/integrations.test.ts b/packages/server/integrations.test.ts new file mode 100644 index 0000000..9c050bf --- /dev/null +++ b/packages/server/integrations.test.ts @@ -0,0 +1,164 @@ +/** + * Bear Integration Tests + * + * Run: bun test packages/server/integrations.test.ts + */ + +import { describe, expect, test } from "bun:test"; +import { + extractTitle, + extractTags, + stripH1, + buildHashtags, + buildBearContent, +} from "./integrations"; + +describe("extractTitle", () => { + test("extracts plain H1", () => { + expect(extractTitle("# My Plan\n\nContent")).toBe("My Plan"); + }); + + test("strips Implementation Plan: prefix", () => { + expect(extractTitle("# Implementation Plan: Auth Flow\n\nContent")).toBe("Auth Flow"); + }); + + test("strips Plan: prefix", () => { + expect(extractTitle("# Plan: Database Migration\n\nContent")).toBe("Database Migration"); + }); + + test("falls back to 'Plan' when no H1", () => { + expect(extractTitle("No heading here")).toBe("Plan"); + }); + + test("truncates to 50 chars", () => { + const long = "A".repeat(60); + expect(extractTitle(`# ${long}`).length).toBe(50); + }); + + test("removes special characters", () => { + expect(extractTitle("# Fix [bug] #123")).toBe("Fix bug 123"); + }); +}); + +describe("stripH1", () => { + test("strips first H1 line", () => { + expect(stripH1("# My Plan\n\n## Section\nContent")).toBe("## Section\nContent"); + }); + + test("strips H1 with any wording", () => { + expect(stripH1("# Whatever Title Here\nBody")).toBe("Body"); + }); + + test("only strips first H1, not subsequent ones", () => { + const input = "# First\n\n# Second\nBody"; + expect(stripH1(input)).toBe("# Second\nBody"); + }); + + test("handles plan with no H1", () => { + expect(stripH1("Just text\nMore text")).toBe("Just text\nMore text"); + }); + + test("does not strip ## H2 headings", () => { + expect(stripH1("## Not H1\nBody")).toBe("## Not H1\nBody"); + }); +}); + +describe("buildHashtags", () => { + test("uses custom tags when provided", () => { + expect(buildHashtags("plan, work", ["plannotator"])).toBe("#plan #work"); + }); + + test("falls back to auto tags when custom is empty", () => { + expect(buildHashtags("", ["plannotator", "myproject"])).toBe("#plannotator #myproject"); + }); + + test("falls back to auto tags when custom is undefined", () => { + expect(buildHashtags(undefined, ["plannotator"])).toBe("#plannotator"); + }); + + test("filters empty tags from trailing comma", () => { + expect(buildHashtags("plan, work,", ["plannotator"])).toBe("#plan #work"); + }); + + test("handles whitespace-only custom tags as empty", () => { + expect(buildHashtags(" ", ["auto"])).toBe("#auto"); + }); +}); + +describe("buildBearContent", () => { + test("appends tags by default", () => { + const result = buildBearContent("Body text", "#plan #work", "append"); + expect(result).toBe("Body text\n\n#plan #work"); + }); + + test("prepends tags when configured", () => { + const result = buildBearContent("Body text", "#plan #work", "prepend"); + expect(result).toBe("#plan #work\n\nBody text"); + }); +}); + +describe("full Bear content pipeline", () => { + const plan = "# Add user authentication flow\n\n## Context\nSome content here"; + + test("no double title — H1 stripped from body", () => { + const body = stripH1(plan); + expect(body).not.toContain("# Add user"); + expect(body).toStartWith("## Context"); + }); + + test("custom tags prepended after title removal", () => { + const body = stripH1(plan); + const hashtags = buildHashtags("plan, work", []); + const content = buildBearContent(body, hashtags, "prepend"); + expect(content).toStartWith("#plan #work"); + expect(content).toContain("## Context"); + expect(content).not.toContain("# Add user"); + }); + + test("auto tags appended when no custom tags", () => { + const body = stripH1(plan); + const hashtags = buildHashtags("", ["plannotator", "dev"]); + const content = buildBearContent(body, hashtags, "append"); + expect(content).toEndWith("#plannotator #dev"); + expect(content).toStartWith("## Context"); + }); +}); + +describe("extractTags", () => { + test("always includes plannotator tag", async () => { + const tags = await extractTags("# Simple Plan\n\nContent"); + expect(tags).toContain("plannotator"); + }); + + test("extracts words from title", async () => { + const tags = await extractTags("# Authentication Service Refactor\n\nContent"); + expect(tags).toContain("authentication"); + expect(tags).toContain("service"); + expect(tags).toContain("refactor"); + }); + + test("filters stop words from title", async () => { + const tags = await extractTags("# Implementation Plan for the System\n\nContent"); + expect(tags).not.toContain("implementation"); + expect(tags).not.toContain("plan"); + expect(tags).not.toContain("the"); + expect(tags).not.toContain("for"); + }); + + test("extracts code fence languages", async () => { + const tags = await extractTags("# Plan\n\n```typescript\ncode\n```\n\n```rust\ncode\n```"); + expect(tags).toContain("typescript"); + expect(tags).toContain("rust"); + }); + + test("skips generic languages", async () => { + const tags = await extractTags("# Plan\n\n```json\n{}\n```\n\n```yaml\nfoo\n```"); + expect(tags).not.toContain("json"); + expect(tags).not.toContain("yaml"); + }); + + test("limits to 7 tags", async () => { + const tags = await extractTags("# One Two Three Four\n\n```go\n```\n```python\n```\n```ruby\n```\n```swift\n```"); + expect(tags.length).toBeLessThanOrEqual(7); + }); +}); diff --git a/packages/server/integrations.ts b/packages/server/integrations.ts index 1cd4c38..a62c429 100644 --- a/packages/server/integrations.ts +++ b/packages/server/integrations.ts @@ -19,6 +19,8 @@ export interface ObsidianConfig { export interface BearConfig { plan: string; + customTags?: string; + tagPosition?: 'prepend' | 'append'; } export interface IntegrationResult { @@ -274,25 +276,42 @@ export async function saveToObsidian(config: ObsidianConfig): Promise `#${t.trim()}`).filter(t => t !== '#').join(' '); + } + return autoTags.map(t => `#${t}`).join(' '); +} + +export function buildBearContent(body: string, hashtags: string, tagPosition: 'prepend' | 'append'): string { + return tagPosition === 'prepend' + ? `${hashtags}\n\n${body}` + : `${body}\n\n${hashtags}`; +} + /** * Save plan to Bear using x-callback-url */ export async function saveToBear(config: BearConfig): Promise { try { - const { plan } = config; + const { plan, customTags, tagPosition = 'append' } = config; - // Extract title and tags const title = extractTitle(plan); - const tags = await extractTags(plan); - const hashtags = tags.map(t => `#${t}`).join(' '); + const body = stripH1(plan); + + const tags = customTags?.trim() + ? undefined + : await extractTags(plan); + const hashtags = buildHashtags(customTags, tags ?? []); - // Append hashtags to content - const content = `${plan}\n\n${hashtags}`; + const content = buildBearContent(body, hashtags, tagPosition); - // Build Bear URL const url = `bear://x-callback-url/create?title=${encodeURIComponent(title)}&text=${encodeURIComponent(content)}&open_note=no`; - // Open Bear via URL scheme await $`open ${url}`.quiet(); return { success: true }; diff --git a/packages/ui/components/Settings.tsx b/packages/ui/components/Settings.tsx index 6ada4ab..4e58a1f 100644 --- a/packages/ui/components/Settings.tsx +++ b/packages/ui/components/Settings.tsx @@ -12,6 +12,7 @@ import { import { getBearSettings, saveBearSettings, + normalizeTags, type BearSettings, } from '../utils/bear'; import { @@ -75,7 +76,7 @@ export const Settings: React.FC = ({ taterMode, onTaterModeChange }); const [detectedVaults, setDetectedVaults] = useState([]); const [vaultsLoading, setVaultsLoading] = useState(false); - const [bear, setBear] = useState({ enabled: false }); + const [bear, setBear] = useState({ enabled: false, customTags: '', tagPosition: 'append' }); const [agent, setAgent] = useState({ switchTo: 'build' }); const [planSave, setPlanSave] = useState({ enabled: true, customPath: null }); const [uiPrefs, setUiPrefs] = useState({ tocEnabled: true, stickyActionsEnabled: true }); @@ -159,8 +160,8 @@ export const Settings: React.FC = ({ taterMode, onTaterModeChange saveObsidianSettings(newSettings); }; - const handleBearChange = (enabled: boolean) => { - const newSettings = { enabled }; + const handleBearChange = (updates: Partial) => { + const newSettings = { ...bear, ...updates }; setBear(newSettings); saveBearSettings(newSettings); }; @@ -1163,28 +1164,59 @@ tags: [plan, ...] {/* === BEAR TAB === */} {activeTab === 'bear' && ( -
-
-
Bear Notes
-
- Auto-save approved plans to Bear + <> +
+
+
Bear Notes
+
+ Auto-save approved plans to Bear +
-
- -
+ > + + +
+ {bear.enabled && ( +
+
+ + handleBearChange({ customTags: e.target.value })} + onBlur={(e) => handleBearChange({ customTags: normalizeTags(e.target.value) })} + placeholder="plan, work" + className="w-full px-3 py-2 bg-muted rounded-lg text-xs font-mono placeholder:text-muted-foreground/50 focus:outline-none focus:ring-1 focus:ring-primary/50" + /> +
+ Comma-separated, kebab-case. Leave empty for auto-generated tags. +
+
+
+ + +
+
+ )} + )}
diff --git a/packages/ui/utils/bear.ts b/packages/ui/utils/bear.ts index b83f69d..9fd857f 100644 --- a/packages/ui/utils/bear.ts +++ b/packages/ui/utils/bear.ts @@ -8,12 +8,18 @@ import { storage } from './storage'; const STORAGE_KEY_ENABLED = 'plannotator-bear-enabled'; +const STORAGE_KEY_CUSTOM_TAGS = 'plannotator-bear-custom-tags'; +const STORAGE_KEY_TAG_POSITION = 'plannotator-bear-tag-position'; + +export type TagPosition = 'prepend' | 'append'; /** * Bear integration settings */ export interface BearSettings { enabled: boolean; + customTags: string; + tagPosition: TagPosition; } /** @@ -22,6 +28,8 @@ export interface BearSettings { export function getBearSettings(): BearSettings { return { enabled: storage.getItem(STORAGE_KEY_ENABLED) === 'true', + customTags: storage.getItem(STORAGE_KEY_CUSTOM_TAGS) ?? '', + tagPosition: (storage.getItem(STORAGE_KEY_TAG_POSITION) as TagPosition) || 'append', }; } @@ -30,4 +38,18 @@ export function getBearSettings(): BearSettings { */ export function saveBearSettings(settings: BearSettings): void { storage.setItem(STORAGE_KEY_ENABLED, String(settings.enabled)); + storage.setItem(STORAGE_KEY_CUSTOM_TAGS, settings.customTags); + storage.setItem(STORAGE_KEY_TAG_POSITION, settings.tagPosition); +} + +/** + * Normalize raw tag input to clean kebab-case, comma-separated string. + * Strips # prefixes, lowercases, replaces spaces with hyphens, removes invalid chars. + */ +export function normalizeTags(raw: string): string { + return raw + .split(',') + .map(t => t.trim().replace(/^#+/, '').toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '')) + .filter(Boolean) + .join(', '); }