Remove uses of the VSCode API (#6124)

This commit is contained in:
Sarah Fortune authored and GitHub committed 2025-09-10 21:46:11 -07:00
1 parent 7ec918261c
commit 0936634fe5
4 files changed
+170 -23

No files matched your search

+2 -1
View File
@@ -225,7 +225,8 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(DIFF_VIEW_URI_SCHEME, diffContentProvider))
const handleUri = async (uri: vscode.Uri) => {
const success = await SharedUriHandler.handleUri(uri)
const url = decodeURIComponent(uri.toString())
const success = await SharedUriHandler.handleUri(url)
if (!success) {
console.warn("Extension URI handler: Failed to process URI:", uri.toString())
}
+1 -2
View File
@@ -159,10 +159,9 @@ export class AuthHandler {
try {
// Convert HTTP URL to vscode.Uri and use shared handler directly
const fullUrl = `http://127.0.0.1:${this.port}${req.url}`
const uri = SharedUriHandler.convertHttpUrlToUri(fullUrl)
// Use SharedUriHandler directly - it handles all validation and processing
const success = await SharedUriHandler.handleUri(uri)
const success = await SharedUriHandler.handleUri(fullUrl)
if (success) {
this.sendResponse(res, 200, "text/html", TOKEN_REQUEST_VIEW)
+151
View File
@@ -0,0 +1,151 @@
import { expect } from "chai"
import { afterEach, beforeEach, describe, it } from "mocha"
import * as sinon from "sinon"
import { WebviewProvider } from "@/core/webview"
import { SharedUriHandler } from "./SharedUriHandler"
describe("SharedUriHandler", () => {
let sandbox: sinon.SinonSandbox
let handleOpenRouterCallbackStub: sinon.SinonStub
let handleAuthCallbackStub: sinon.SinonStub
beforeEach(() => {
sandbox = sinon.createSandbox()
handleOpenRouterCallbackStub = sandbox.stub().resolves()
handleAuthCallbackStub = sandbox.stub().resolves()
const mockWebviewProvider = {
controller: {
handleOpenRouterCallback: handleOpenRouterCallbackStub,
handleAuthCallback: handleAuthCallbackStub,
},
} as any
sandbox.stub(WebviewProvider, "getVisibleInstance").returns(mockWebviewProvider)
})
afterEach(() => {
sandbox.restore()
})
describe("handleUri", () => {
describe("OpenRouter callback handling", () => {
it("should successfully handle OpenRouter callback with code", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter?code=test123")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleOpenRouterCallbackStub, "test123")
})
it("should return false when OpenRouter code is missing", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter")
expect(result).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
it("should handle URL with plus signs in code parameter", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter?code=test+123+abc")
expect(result).to.be.true
// Plus signs in query params are preserved
sinon.assert.calledOnceWithExactly(handleOpenRouterCallbackStub, "test+123+abc")
})
})
describe("Auth callback handling", () => {
it("should successfully handle auth callback with idToken", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/auth?idToken=jwt123&provider=google")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", "google")
})
it("should successfully handle auth callback without provider", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/auth?idToken=jwt123")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", null)
})
it("should return false when idToken is missing", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/auth?provider=google")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
})
})
describe("Unknown path handling", () => {
it("should return false for unknown paths", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/unknown?param=value")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
})
describe("Error handling", () => {
it("should catch and log errors from controller methods", async () => {
handleOpenRouterCallbackStub.rejects(new Error("Controller error"))
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter?code=test123")
expect(result).to.be.false
})
it("should handle malformed URIs gracefully", async () => {
const result = await SharedUriHandler.handleUri("invalid://uri")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
})
describe("Query parameter parsing", () => {
it("should correctly parse multiple query parameters", async () => {
const result = await SharedUriHandler.handleUri(
"vscode://cline.cline/auth?idToken=jwt123&provider=github&extra=param",
)
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", "github")
})
it("should handle URL-encoded parameters", async () => {
const result = await SharedUriHandler.handleUri(
"vscode://cline.cline/auth?idToken=jwt%20with%20spaces&provider=google",
)
expect(result).to.be.true
// URLSearchParams should decode %20 to spaces
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt with spaces", "google")
})
it("should handle empty query string", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
})
describe("Different URI schemes", () => {
it("should handle HTTP scheme URIs", async () => {
const result = await SharedUriHandler.handleUri("http://localhost:3000/openrouter?code=test123")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleOpenRouterCallbackStub, "test123")
})
it("should handle HTTPS scheme URIs", async () => {
const result = await SharedUriHandler.handleUri("https://example.com/auth?idToken=jwt123&provider=github")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", "github")
})
})
})
})
+16 -20
View File
@@ -1,4 +1,3 @@
import * as vscode from "vscode"
import { WebviewProvider } from "@/core/webview"
/**
@@ -7,18 +6,24 @@ import { WebviewProvider } from "@/core/webview"
export class SharedUriHandler {
/**
* Processes a URI and routes it to the appropriate handler
* @param uri The URI to process (can be from VSCode or converted from HTTP)
* @param url The URI to process (can be from VSCode or converted from HTTP)
* @returns Promise<boolean> indicating success (true) or failure (false)
*/
public static async handleUri(uri: vscode.Uri): Promise<boolean> {
public static async handleUri(url: string): Promise<boolean> {
const parsedUrl = new URL(url)
const path = parsedUrl.pathname
// Create URLSearchParams from the query string, but preserve plus signs
// by replacing them with a placeholder before parsing
const queryString = parsedUrl.search.slice(1) // Remove leading '?'
const query = new URLSearchParams(queryString.replace(/\+/g, "%2B"))
console.log("SharedUriHandler: Processing URI:", {
path: uri.path,
query: uri.query,
scheme: uri.scheme,
path: path,
query: query,
scheme: parsedUrl.protocol,
})
const path = uri.path
const query = new URLSearchParams(uri.query.replace(/\+/g, "%2B"))
const visibleWebview = WebviewProvider.getVisibleInstance()
if (!visibleWebview) {
@@ -38,10 +43,10 @@ export class SharedUriHandler {
return false
}
case "/auth": {
console.log("SharedUriHandler: Auth callback received:", { path: uri.path, provider: query.get("provider") })
const token = query.get("idToken")
const provider = query.get("provider")
const token = query.get("idToken")
console.log("SharedUriHandler: Auth callback received:", { path: path, provider: provider })
if (token) {
await visibleWebview.controller.handleAuthCallback(token, provider)
@@ -59,13 +64,4 @@ export class SharedUriHandler {
return false
}
}
/**
* Converts an HTTP URL to a vscode.Uri for unified processing
* @param httpUrl The HTTP URL to convert
* @returns vscode.Uri representation of the URL
*/
public static convertHttpUrlToUri(httpUrl: string): vscode.Uri {
return vscode.Uri.parse(httpUrl)
}
}