import { describe, expect, test } from "bun:test" import type { SessionMessageAssistant, SessionMessageInfo, SessionMessageUser } from "@opencode/client/promise" import type { Visit } from "./history/visits" import { cachedSessionLinks, classifySessionLink, COLD_ASSISTANT_LIMIT, extractTextLinks, sessionLinkKey, sessionLinks, syncSessionLinks, } from "./session-links" const userMessage = (id: string, text: string): SessionMessageUser => ({ // SAFETY: test fixture constructs synthetic branded SessionMessageID values. id: id as SessionMessageUser["id"], type: "user", text, time: { created: 1 }, }) const assistantMessage = ( id: string, content: SessionMessageAssistant["content"], ): SessionMessageAssistant => ({ // SAFETY: test fixture constructs synthetic branded SessionMessageID values. id: id as SessionMessageAssistant["id"], type: "assistant", agent: "build", // SAFETY: test fixture constructs synthetic branded ModelRef values. model: { providerID: "opencode", id: "test" } as SessionMessageAssistant["model"], content, time: { created: 2 }, }) describe("classifySessionLink", () => { test("classifies GitHub links with deterministic titles and ignores GitHub chrome", () => { expect(classifySessionLink("https://github.com/anomalyco/opencode/pull/412")).toMatchObject({ kind: "github", title: "PR #412 · anomalyco/opencode", detail: "github.com/anomalyco/opencode/pull/412", fallbackIcon: "github", }) expect(classifySessionLink("https://github.com/anomalyco/opencode/issues/88")).toMatchObject({ kind: "github", title: "Issue #88 · anomalyco/opencode", }) expect(classifySessionLink("https://github.com/anomalyco/opencode/actions/runs/9999")).toMatchObject({ kind: "github", title: "Actions run · anomalyco/opencode", }) expect(classifySessionLink("https://github.com/anomalyco/opencode/commit/abcdef1234567890")).toMatchObject({ kind: "github", title: "Commit abcdef1 · anomalyco/opencode", }) expect(classifySessionLink("https://github.com/anomalyco/opencode")).toMatchObject({ kind: "github", title: "anomalyco/opencode", }) expect(classifySessionLink("https://github.com/login")).toBeUndefined() expect(classifySessionLink("https://github.com/")).toBeUndefined() }) test("classifies Console and artifact links", () => { expect(classifySessionLink("artifact://art_abc123/report.html")).toMatchObject({ kind: "console", title: "report.html", fallbackIcon: "file-document", }) expect( classifySessionLink("http://127.0.0.1:4096/api/experimental/artifacts/art_abc123/view/index.html", { serverUrl: "http://127.0.0.1:4096", }), ).toMatchObject({ kind: "console", url: "artifact://art_abc123/index.html", title: "index.html", }) expect(classifySessionLink("https://share.opncd.ai/R7TMdJZbCwaU")).toMatchObject({ kind: "console", title: "Artifact", detail: "share.opncd.ai/R7TMdJZbCwaU", fallbackIcon: "file-document", }) expect(classifySessionLink("https://share.opncd.ai/")).toBeUndefined() expect(classifySessionLink("https://opencode.ai/console/acme/go")).toBeUndefined() expect(classifySessionLink("https://opencode.ai/auth/authorize")).toBeUndefined() }) test("classifies local, Tailscale, LAN, workspace, and file links", () => { expect(classifySessionLink("http://localhost:3000/settings")).toMatchObject({ kind: "local", title: "", detail: "localhost:3000/settings", fallbackIcon: "globe", }) expect(classifySessionLink("http://0.0.0.0:5173/")).toMatchObject({ kind: "local", url: "http://localhost:5173/", detail: "localhost:5173/", }) expect(classifySessionLink("http://127.0.0.1:8080")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://[::1]:8080")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://app.localhost:3000")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://devbox.local:4000")).toMatchObject({ kind: "local" }) expect(classifySessionLink("https://macbook.tail1234.ts.net:8443/app")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://100.64.12.34:3000")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://192.168.1.42:3000")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://10.0.0.8:3000")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://172.20.0.2:3000")).toMatchObject({ kind: "local" }) expect(classifySessionLink("http://100.128.0.1:3000")).toBeUndefined() expect( classifySessionLink("workspace://ses_1/preview.html", { sessionID: "ses_1" }), ).toMatchObject({ kind: "local", title: "preview.html", fallbackIcon: "file-document", }) expect(classifySessionLink("workspace://ses_other/preview.html", { sessionID: "ses_1" })).toBeUndefined() expect(classifySessionLink("file:///tmp/demo.html", { builtinServer: true })).toMatchObject({ kind: "local", title: "demo.html", fallbackIcon: "file-document", }) expect(classifySessionLink("file:///tmp/demo.html", { builtinServer: false })).toBeUndefined() }) test("classifies preview deployments and Cloudflare/ngrok tunnels", () => { for (const url of [ "https://my-branch-123.trycloudflare.com", "https://tunnel-abc.cfargotunnel.com/preview", "https://opencode-pr-42.vercel.app", "https://feature-x.my-site.pages.dev", "https://deploy-preview-9--site.netlify.app", "https://abc123.ngrok-free.app/demo", "https://staging.fly.dev", "https://api-pr-1.onrender.com", "https://app-pr-1.railway.app", "https://worker-preview.workers.dev", ]) { expect(classifySessionLink(url)).toMatchObject({ kind: "preview", fallbackIcon: "globe", }) } }) test("ignores general web links and credentialed URLs", () => { expect(classifySessionLink("https://developer.mozilla.org/en-US/docs/Web")).toBeUndefined() expect(classifySessionLink("https://www.npmjs.com/package/solid-js")).toBeUndefined() expect(classifySessionLink("https://user:pass@localhost:3000")).toBeUndefined() }) }) describe("extractTextLinks", () => { test("extracts markdown labels and bare URLs while skipping fenced code blocks", () => { const text = [ "Opened [Fix scroll state](https://github.com/org/repo/pull/42) and [here](https://my-app.vercel.app).", "Local server is running at `http://localhost:3000/dashboard`.", "```bash", "curl http://localhost:9999/should-be-ignored", "open https://github.com/ignored/repo/pull/1", "```", "Tunnel: https://fast-cat.trycloudflare.com.", ].join("\n") expect(extractTextLinks(text)).toEqual([ { url: "https://github.com/org/repo/pull/42", label: "Fix scroll state" }, { url: "https://my-app.vercel.app" }, { url: "http://localhost:3000/dashboard" }, { url: "https://fast-cat.trycloudflare.com." }, ]) }) }) describe("sessionLinks", () => { test("collects links from user, assistant, and explicit URL tool inputs while ignoring noisy shell/read stdout", () => { const messages: SessionMessageInfo[] = [ userMessage("msg_1", "Please check https://github.com/org/repo/issues/10"), assistantMessage("msg_2", [ { type: "tool", id: "tool_read", name: "read", state: { status: "completed", input: { path: "README.md" }, content: [{ type: "text", text: "See https://github.com/noise/repo/issues/999" }], }, time: { created: 2 }, }, { type: "tool", id: "tool_shell", name: "shell", state: { status: "completed", input: { command: "lsof -i" }, content: [{ type: "text", text: "http://localhost:4401 http://localhost:4747/mcp)\\ntotal" }], }, time: { created: 3 }, }, { type: "tool", id: "tool_browser", name: "browser_open", state: { status: "completed", input: { url: "https://github.com/org/repo/pull/42" }, content: [{ type: "text", text: "ok" }], }, time: { created: 4 }, }, { type: "text", text: "Local preview is live at http://localhost:3000/settings and [PR audit](https://share.opncd.ai/R7TMdJZbCwaU).", }, ]), ] const visits: Visit[] = [ { url: "http://localhost:3000/", title: "OpenCode App", icon: "data:image/png;base64,AAA", }, ] const links = sessionLinks(messages, visits, [], { sessionKey: "sidecar\nses_1", sessionID: "ses_1", serverUrl: "http://127.0.0.1:4096", builtinServer: true, }) // GitHub links are prioritized at the top (newest first within each group) expect(links.map((item) => item.url)).toEqual([ "https://github.com/org/repo/pull/42", "https://github.com/org/repo/issues/10", "https://share.opncd.ai/R7TMdJZbCwaU", "http://localhost:3000/settings", ]) // GitHub PR uses deterministic title + github icon expect(links[0]).toMatchObject({ title: "PR #42 · org/repo", fallbackIcon: "github" }) // GitHub issue from user message expect(links[1]).toMatchObject({ title: "Issue #10 · org/repo", fallbackIcon: "github" }) // Console artifact uses markdown label expect(links[2]).toMatchObject({ title: "PR audit", fallbackIcon: "file-document" }) // Local route inherits cached origin favicon from localhost:3000 visit without repeating host in title expect(links[3]).toMatchObject({ title: "", icon: "data:image/png;base64,AAA", fallbackIcon: "globe" }) }) test("prefers exact browser visit title and icon over markdown label and fallback", () => { const messages: SessionMessageInfo[] = [ assistantMessage("msg_1", [ { type: "text", text: "Check [the PR](https://github.com/org/repo/pull/42)" }, ]), ] const visits: Visit[] = [ { url: "https://github.com/org/repo/pull/42", title: "Fix scroll state by arvsrn · Pull Request #42", icon: "data:image/png;base64,GH", }, ] const links = sessionLinks(messages, visits) expect(links).toHaveLength(1) expect(links[0]).toMatchObject({ title: "Fix scroll state by arvsrn · Pull Request #42", icon: "data:image/png;base64,GH", }) expect(sessionLinkKey("https://github.com/org/repo/pull/42/")).toBe(links[0].key) }) }) describe("syncSessionLinks cache", () => { test("cold cache only parses the last 10 assistant messages", () => { const messages: SessionMessageInfo[] = Array.from({ length: COLD_ASSISTANT_LIMIT + 3 }, (_, index) => assistantMessage(`msg_${index + 1}`, [ { type: "text", text: `PR https://github.com/org/repo/pull/${index + 1}` }, ]), ) const next = syncSessionLinks({ sessions: [] }, "sidecar\nses_1", messages) expect(next).toBeDefined() const cached = cachedSessionLinks(next!, "sidecar\nses_1") expect(cached).toHaveLength(COLD_ASSISTANT_LIMIT) expect(cached[0].url).toBe(`https://github.com/org/repo/pull/${COLD_ASSISTANT_LIMIT + 3}`) expect(cached[cached.length - 1].url).toBe("https://github.com/org/repo/pull/4") }) test("warm cache preserves earlier cached links, increments on new/streaming messages, and no-ops when unchanged", () => { const msg1 = assistantMessage("msg_1", [{ type: "text", text: "Early https://github.com/org/repo/pull/1" }]) const first = syncSessionLinks({ sessions: [] }, "sidecar\nses_1", [msg1])! // No-op when nothing changed expect(syncSessionLinks(first, "sidecar\nses_1", [msg1])).toBeUndefined() // Streaming update on the same last message picks up newly arrived link const msg1Updated = assistantMessage("msg_1", [ { type: "text", text: "Early https://github.com/org/repo/pull/1 and https://share.opncd.ai/abc123" }, ]) const streamed = syncSessionLinks(first, "sidecar\nses_1", [msg1Updated])! expect(cachedSessionLinks(streamed, "sidecar\nses_1").map((item) => item.url)).toEqual([ "https://share.opncd.ai/abc123", "https://github.com/org/repo/pull/1", ]) // Subsequent message appends without losing earlier cached link even if older messages are no longer in the window const msg2 = assistantMessage("msg_2", [{ type: "text", text: "Live at http://localhost:3000" }]) const second = syncSessionLinks(streamed, "sidecar\nses_1", [msg1Updated, msg2])! expect(cachedSessionLinks(second, "sidecar\nses_1").map((item) => item.url)).toEqual([ "http://localhost:3000", "https://share.opncd.ai/abc123", "https://github.com/org/repo/pull/1", ]) }) })