import { describe, expect, it } from "bun:test";
import {
contentOf,
isPlanned,
readServeAt,
readSessionPatch,
readViewsCapability,
} from "../src/views/protocol";
import { ViewsState } from "../src/views/state";
import { webviewPage } from "../src/views/webview";
import { fill, percentOf, sessionSummary } from "../src/webview/format";
import { createClientOptions } from "../src/client-options";
const A = "file:///a.gram";
const B = "file:///b.gram";
const PAUSED = {
title: "Generation paused",
description: "The recipe contains blocking errors.",
code: "PARSE_ERROR",
message: "Expected a name",
range: { start: 3, end: 3 },
};
function recorder() {
const sent: [string, string[]][] = [];
return { sent, state: new ViewsState((uri, ids) => sent.push([uri, ids])) };
}
describe("ViewsState: what is open", () => {
it("sends the open set of the followed recipe, once", () => {
const { sent, state } = recorder();
state.syncOpen(A, ["runSheet", "recipe"]);
state.syncOpen(A, ["recipe", "runSheet"]);
expect(sent).toEqual([[A, ["recipe", "runSheet"]]]);
});
it("closes everything on the old recipe when the panels follow another", () => {
const { sent, state } = recorder();
state.syncOpen(A, ["recipe"]);
state.syncOpen(B, ["recipe"]);
expect(sent).toEqual([
[A, ["recipe"]],
[A, []],
[B, ["recipe"]],
]);
});
it("says so when the last panel closes", () => {
const { sent, state } = recorder();
state.syncOpen(A, ["recipe"]);
state.syncOpen(A, []);
state.syncOpen(A, []);
expect(sent).toEqual([
[A, ["recipe"]],
[A, []],
]);
});
it("sends the set again for a recipe closed and reopened", () => {
const { sent, state } = recorder();
state.syncOpen(A, ["recipe"]);
state.forget(A);
state.syncOpen(A, ["recipe"]);
expect(sent).toEqual([
[A, ["recipe"]],
[A, ["recipe"]],
]);
});
});
describe("ViewsState: what a panel shows", () => {
it("keeps the last content under a pause, and drops the pause on the next content", () => {
const { state } = recorder();
state.apply({
uri: A,
outputId: "recipe",
kind: "renderer",
state: "ok",
payload: "
Tart
",
});
const paused = state.apply({
uri: A,
outputId: "recipe",
kind: "renderer",
state: "paused",
error: PAUSED,
});
expect(paused).toEqual({
content: { type: "html", html: "Tart
" },
paused: PAUSED,
});
const back = state.apply({
uri: A,
outputId: "recipe",
kind: "renderer",
state: "ok",
payload: "Pie
",
});
expect(back).toEqual({
content: { type: "html", html: "Pie
" },
paused: undefined,
});
});
it("shows a pause alone for an output that never had content", () => {
const { state } = recorder();
const view = state.apply({
uri: A,
outputId: "ast",
kind: "inspector",
state: "paused",
error: PAUSED,
});
expect(view).toEqual({ content: undefined, paused: PAUSED });
});
it("keeps each recipe's content apart, and forgets a closed one", () => {
const { state } = recorder();
state.apply({
uri: A,
outputId: "recipe",
kind: "renderer",
state: "ok",
payload: "A
",
});
expect(state.viewOf(B, "recipe").content).toBeUndefined();
state.forget(A);
expect(state.viewOf(A, "recipe").content).toBeUndefined();
});
});
describe("contentOf", () => {
it("reads every payload the server sends", () => {
expect(contentOf(null)).toEqual({ type: "empty" });
expect(contentOf("x
")).toEqual({ type: "html", html: "x
" });
expect(
contentOf({ presentation: "code", language: "json", text: "{}" }),
).toEqual({ type: "code", language: "json", text: "{}" });
expect(
contentOf({ presentation: "tree", text: '' }),
).toEqual({ type: "tree", html: '
' });
});
});
describe("readViewsCapability", () => {
it("reads the outputs of the initialize result", () => {
const gramViews = {
outputs: [{ id: "recipe", kind: "renderer", label: "Recipe" }],
strings: { showError: "Show the error", outputEmpty: "Nothing." },
};
expect(
readViewsCapability({ capabilities: { experimental: { gramViews } } }),
).toEqual(gramViews);
});
it("is undefined from a server without the views protocol", () => {
expect(readViewsCapability({ capabilities: {} })).toBeUndefined();
expect(readViewsCapability(undefined)).toBeUndefined();
});
});
describe("the page of a view panel", () => {
const page = webviewPage({
nonce: "abc123",
cspSource: "vscode-resource:",
styles: {
preview: "media/preview.css",
jsonTree: "media/json-tree.css",
view: "dist/webview/view.css",
},
script: "dist/webview/view.js",
title: 'Plan "de" ',
showError: "Voir l'erreur",
outputEmpty: "Rien ",
});
it("only runs its own script, from its own file", () => {
expect(page).toContain("script-src 'nonce-abc123'");
expect(page.match(/',
);
});
it("escapes what it is given", () => {
expect(page).toContain(
"Plan "de" <travail>",
);
expect(page).not.toContain("alert(1)");
});
it("loads the renderer's stylesheets and its own", () => {
expect(page).toContain('href="media/preview.css"');
expect(page).toContain('href="media/json-tree.css"');
expect(page).toContain('href="dist/webview/view.css"');
expect(page).not.toContain("