test(vscode): extract git diff URI parsing and add scheme/ref tests

This commit is contained in:
Igor Šćekić
2026-02-20 13:00:22 +01:00
parent 51430b228c
commit db96bd833c
3 changed files with 68 additions and 33 deletions
@@ -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)
}
}
@@ -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
}
@@ -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()
})
})