chore: refactoring and tests (#12629)
This commit is contained in:
@@ -2,7 +2,15 @@ export const deepLinkEvent = "opencode:deep-link"
|
||||
|
||||
export const parseDeepLink = (input: string) => {
|
||||
if (!input.startsWith("opencode://")) return
|
||||
const url = new URL(input)
|
||||
if (typeof URL.canParse === "function" && !URL.canParse(input)) return
|
||||
const url = (() => {
|
||||
try {
|
||||
return new URL(input)
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
})()
|
||||
if (!url) return
|
||||
if (url.hostname !== "open-project") return
|
||||
const directory = url.searchParams.get("directory")
|
||||
if (!directory) return
|
||||
|
||||
@@ -12,6 +12,27 @@ describe("layout deep links", () => {
|
||||
expect(parseDeepLink("https://example.com")).toBeUndefined()
|
||||
})
|
||||
|
||||
test("ignores malformed deep links safely", () => {
|
||||
expect(() => parseDeepLink("opencode://open-project/%E0%A4%A%")).not.toThrow()
|
||||
expect(parseDeepLink("opencode://open-project/%E0%A4%A%")).toBeUndefined()
|
||||
})
|
||||
|
||||
test("parses links when URL.canParse is unavailable", () => {
|
||||
const original = Object.getOwnPropertyDescriptor(URL, "canParse")
|
||||
Object.defineProperty(URL, "canParse", { configurable: true, value: undefined })
|
||||
try {
|
||||
expect(parseDeepLink("opencode://open-project?directory=/tmp/demo")).toBe("/tmp/demo")
|
||||
} finally {
|
||||
if (original) Object.defineProperty(URL, "canParse", original)
|
||||
if (!original) Reflect.deleteProperty(URL, "canParse")
|
||||
}
|
||||
})
|
||||
|
||||
test("ignores open-project deep links without directory", () => {
|
||||
expect(parseDeepLink("opencode://open-project")).toBeUndefined()
|
||||
expect(parseDeepLink("opencode://open-project?directory=")).toBeUndefined()
|
||||
})
|
||||
|
||||
test("collects only valid open-project directories", () => {
|
||||
const result = collectOpenProjectDeepLinks([
|
||||
"opencode://open-project?directory=/a",
|
||||
@@ -39,6 +60,14 @@ describe("layout workspace helpers", () => {
|
||||
expect(workspaceKey("C:\\tmp\\demo\\\\")).toBe("C:\\tmp\\demo")
|
||||
})
|
||||
|
||||
test("preserves posix and drive roots in workspace key", () => {
|
||||
expect(workspaceKey("/")).toBe("/")
|
||||
expect(workspaceKey("///")).toBe("/")
|
||||
expect(workspaceKey("C:\\")).toBe("C:\\")
|
||||
expect(workspaceKey("C:\\\\\\")).toBe("C:\\")
|
||||
expect(workspaceKey("C:///")).toBe("C:/")
|
||||
})
|
||||
|
||||
test("keeps local first while preserving known order", () => {
|
||||
const result = syncWorkspaceOrder("/root", ["/root", "/b", "/c"], ["/root", "/c", "/a", "/b"])
|
||||
expect(result).toEqual(["/root", "/c", "/b"])
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { getFilename } from "@opencode-ai/util/path"
|
||||
import { type Session } from "@opencode-ai/sdk/v2/client"
|
||||
|
||||
export const workspaceKey = (directory: string) => directory.replace(/[\\/]+$/, "")
|
||||
export const workspaceKey = (directory: string) => {
|
||||
const drive = directory.match(/^([A-Za-z]:)[\\/]+$/)
|
||||
if (drive) return `${drive[1]}${directory.includes("\\") ? "\\" : "/"}`
|
||||
if (/^[\\/]+$/.test(directory)) return directory.includes("\\") ? "\\" : "/"
|
||||
return directory.replace(/[\\/]+$/, "")
|
||||
}
|
||||
|
||||
export function sortSessions(now: number) {
|
||||
const oneMinuteAgo = now - 60 * 1000
|
||||
|
||||
Reference in New Issue
Block a user