Files
lam-agent-vicinae/src/__tests__/vision.test.ts
T
adrianbonpin 5e083d8142 feat: snip a region into chat, and make links clickable
Two changes: screenshot input gated on the model really reporting vision,
and a fix for URLs that arrived as code.

Links
- A fence whose body is all URLs, a lone `https://...` inline span, and a
  line that is only a URL are rewritten into [url](url) markdown links.
  Vicinae attaches only the table and strikethrough GFM extensions, so
  autolink is off and a bare URL is dead text; clickability comes from
  MarkdownModel turning CMARK_NODE_LINK into an <a href>. A fenced URL was
  a dark panel with a Copy button and no way to open it.
- promoteStandaloneCodeSpans was itself promoting a lone URL span into a
  fence, so it made this worse. Link restoration now runs first.
- The default prompt says never to fence a URL.
- Anything that is not one of those three shapes is copied through, so a
  curl invocation containing an address is never rewritten.

Screenshots
- capture.ts shells out to an auto-detected tool: grimblast, grim + slurp,
  spectacle, maim, scrot, flameshot, or screencapture on macOS. There is
  no screenshot call in @vicinae/api and Clipboard moves no binary, so the
  extension owns the capture and reads the file it wrote rather than
  guessing at the clipboard. Cancel is detected by whether an image landed
  on disk, because the tools disagree on exit codes.
- In chat: "Snip a Region" in the action panel (cmd+shift+S) attaches to
  the current conversation. Standalone: "Snip to Lam" starts a new one.
- vision.ts caches the /api/show capability answer per host and model. A
  failed check is "unknown", never "no", so a dropped connection is not
  reported as a fact about the user's model. Actions still fail closed.
- Only the path is stored, base64 is read at send time, and the image goes
  on its own turn only. Capture is never a model-callable tool.

Verified: 244 tests, tsc, eslint, and vici build (both entrypoints).
2026-10-04 15:13:15 +08:00

122 lines
4.2 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { __resetLocalStorage } from "../__mocks__/@vicinae/api";
import { forgetVisionSupport, visionSupport } from "../lib/vision";
/**
* The capability check decides whether a snip feature is offered at all, so the
* cases that matter are the ones where the answer is not a clean yes or no: a
* dead network must not be mistaken for "this model cannot see".
*/
const fetchMock = vi.fn();
function showResponse(capabilities: string[]): Response {
return new Response(JSON.stringify({ capabilities }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
beforeEach(() => {
__resetLocalStorage();
fetchMock.mockReset();
vi.stubGlobal("fetch", fetchMock);
});
afterEach(() => {
vi.unstubAllGlobals();
});
const base = { host: "https://ollama.com", apiKey: "k", model: "gemma4" };
describe("visionSupport", () => {
it("reports yes when Ollama lists the vision capability", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["completion", "vision"]));
await expect(visionSupport(base)).resolves.toBe("yes");
});
it("reports no for a text-only model", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["completion", "tools"]));
await expect(visionSupport(base)).resolves.toBe("no");
});
it("asks /api/show for the model", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await visionSupport(base);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe("https://ollama.com/api/show");
expect(JSON.parse(init.body)).toEqual({ model: "gemma4" });
});
it("serves a repeat question from the cache", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await visionSupport(base);
await expect(visionSupport(base)).resolves.toBe("yes");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("caches a negative too, so a text model is not re-checked on every mount", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["tools"]));
await visionSupport(base);
await expect(visionSupport(base)).resolves.toBe("no");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("keys the cache per model, so switching model re-checks", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await visionSupport(base);
fetchMock.mockResolvedValueOnce(showResponse(["tools"]));
await expect(visionSupport({ ...base, model: "gpt-oss:120b" })).resolves.toBe("no");
expect(fetchMock).toHaveBeenCalledTimes(2);
});
it("re-checks when explicitly asked to, even with a cache entry", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["tools"]));
await visionSupport(base);
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await expect(visionSupport({ ...base, refresh: true })).resolves.toBe("yes");
});
it("reports unknown rather than no when the request fails", async () => {
// A dead network is not evidence that a model cannot see, and treating it as
// such would hide the feature for a reason the user cannot see.
fetchMock.mockRejectedValueOnce(new Error("offline"));
await expect(visionSupport(base)).resolves.toBe("unknown");
});
it("does not cache a failure", async () => {
fetchMock.mockRejectedValueOnce(new Error("offline"));
await visionSupport(base);
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await expect(visionSupport(base)).resolves.toBe("yes");
expect(fetchMock).toHaveBeenCalledTimes(2);
});
it("reports a rejected key as unknown rather than crashing", async () => {
fetchMock.mockResolvedValueOnce(new Response("unauthorized", { status: 401 }));
await expect(visionSupport(base)).resolves.toBe("unknown");
});
});
describe("forgetVisionSupport", () => {
it("drops every cached answer, so a re-pulled model is not pinned to no", async () => {
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await visionSupport(base);
await forgetVisionSupport();
fetchMock.mockResolvedValueOnce(showResponse(["vision"]));
await visionSupport(base);
expect(fetchMock).toHaveBeenCalledTimes(2);
});
});