From db96bd833c337f0b486a44ef314ba458dff8cee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20=C5=A0=C4=87eki=C4=87?= Date: Fri, 20 Feb 2026 13:00:22 +0100 Subject: [PATCH] test(vscode): extract git diff URI parsing and add scheme/ref tests --- .../context/VisibleCodeTracker.ts | 35 +------------- .../context/visible-code-utils.ts | 19 ++++++++ .../tests/unit/visible-code-utils.test.ts | 47 +++++++++++++++++++ 3 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 packages/kilo-vscode/src/services/autocomplete/context/visible-code-utils.ts create mode 100644 packages/kilo-vscode/tests/unit/visible-code-utils.test.ts diff --git a/packages/kilo-vscode/src/services/autocomplete/context/VisibleCodeTracker.ts b/packages/kilo-vscode/src/services/autocomplete/context/VisibleCodeTracker.ts index 7003b04ed9f..dfe2b6a8f24 100644 --- a/packages/kilo-vscode/src/services/autocomplete/context/VisibleCodeTracker.ts +++ b/packages/kilo-vscode/src/services/autocomplete/context/VisibleCodeTracker.ts @@ -18,6 +18,7 @@ function toRelativePath(absolutePath: string, workspacePath: string): string { } import { VisibleCodeContext, VisibleEditorInfo, VisibleRange, DiffInfo } from "../types" +import { extractDiffInfo as _extractDiffInfo } from "./visible-code-utils" // Git-related URI schemes that should be captured for diff support const GIT_SCHEMES = ["git", "gitfs", "file", "vscode-remote"] @@ -120,38 +121,6 @@ export class VisibleCodeTracker { * Git URIs typically look like: git:/path/to/file.ts?ref=HEAD~1 */ private extractDiffInfo(uri: vscode.Uri): DiffInfo | undefined { - const scheme = uri.scheme - - // Only extract diff info for git-related schemes - if (scheme === "git" || scheme === "gitfs") { - // Parse query parameters for git reference - const query = uri.query - let gitRef: string | undefined - - if (query) { - // Common patterns: ref=HEAD, ref=abc123 - const refMatch = query.match(/ref=([^&]+)/) - if (refMatch) { - gitRef = refMatch[1] - } - } - - return { - scheme, - side: "old", // Git scheme documents are typically the "old" side - gitRef, - originalPath: uri.fsPath, - } - } - - // File scheme in a diff view is the "new" side - // We can't always tell if it's in a diff, so we mark it as new when there's a paired git doc - if (scheme === "file") { - // This will be marked as diffInfo only if we detect it's paired with a git document - // For now, we don't set diffInfo for regular file scheme documents - return undefined - } - - return undefined + return _extractDiffInfo(uri.scheme, uri.query, uri.fsPath) } } diff --git a/packages/kilo-vscode/src/services/autocomplete/context/visible-code-utils.ts b/packages/kilo-vscode/src/services/autocomplete/context/visible-code-utils.ts new file mode 100644 index 00000000000..53f5c02dada --- /dev/null +++ b/packages/kilo-vscode/src/services/autocomplete/context/visible-code-utils.ts @@ -0,0 +1,19 @@ +import type { DiffInfo } from "../types" + +/** + * Extract git diff metadata from a URI. + * Returns DiffInfo for git/gitfs scheme URIs, undefined for regular file URIs. + */ +export function extractDiffInfo(scheme: string, query: string, fsPath: string): DiffInfo | undefined { + if (scheme === "git" || scheme === "gitfs") { + let gitRef: string | undefined + if (query) { + const refMatch = query.match(/ref=([^&]+)/) + if (refMatch) { + gitRef = refMatch[1] + } + } + return { scheme, side: "old", gitRef, originalPath: fsPath } + } + return undefined +} diff --git a/packages/kilo-vscode/tests/unit/visible-code-utils.test.ts b/packages/kilo-vscode/tests/unit/visible-code-utils.test.ts new file mode 100644 index 00000000000..2609cc44925 --- /dev/null +++ b/packages/kilo-vscode/tests/unit/visible-code-utils.test.ts @@ -0,0 +1,47 @@ +import { describe, it, expect } from "bun:test" +import { extractDiffInfo } from "../../src/services/autocomplete/context/visible-code-utils" + +describe("extractDiffInfo", () => { + it("extracts info for git scheme with ref query param", () => { + const result = extractDiffInfo("git", "ref=HEAD", "/workspace/foo.ts") + expect(result).not.toBeUndefined() + expect(result?.scheme).toBe("git") + expect(result?.side).toBe("old") + expect(result?.gitRef).toBe("HEAD") + expect(result?.originalPath).toBe("/workspace/foo.ts") + }) + + it("extracts info for gitfs scheme", () => { + const result = extractDiffInfo("gitfs", "ref=abc123", "/workspace/bar.ts") + expect(result?.scheme).toBe("gitfs") + expect(result?.gitRef).toBe("abc123") + }) + + it("extracts ref with additional query params", () => { + const result = extractDiffInfo("git", "ref=main&other=value", "/f.ts") + expect(result?.gitRef).toBe("main") + }) + + it("extracts commit SHA as ref", () => { + const result = extractDiffInfo("git", "ref=a1b2c3d4e5f6", "/f.ts") + expect(result?.gitRef).toBe("a1b2c3d4e5f6") + }) + + it("handles git scheme with no query (no ref)", () => { + const result = extractDiffInfo("git", "", "/f.ts") + expect(result).not.toBeUndefined() + expect(result?.gitRef).toBeUndefined() + }) + + it("returns undefined for file scheme", () => { + expect(extractDiffInfo("file", "", "/f.ts")).toBeUndefined() + }) + + it("returns undefined for unknown scheme", () => { + expect(extractDiffInfo("https", "", "/f.ts")).toBeUndefined() + }) + + it("returns undefined for vscode-remote scheme", () => { + expect(extractDiffInfo("vscode-remote", "", "/f.ts")).toBeUndefined() + }) +})