1import { COMPOSER_CONTEXT_KINDS } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 CONTEXT_PRESENTATION_DEFINITIONS,
6 contextPresentationDefinition,
7 createContextPresentationRegistry,
8} from "./contextPresentationRegistry";
9
10describe("contextPresentationRegistry", () => {
11 it("defines presentation capabilities for every known context kind", () => {
12 expect([...CONTEXT_PRESENTATION_DEFINITIONS.keys()]).toEqual(COMPOSER_CONTEXT_KINDS);
13 expect(contextPresentationDefinition("terminal").capabilities).toEqual({
14 details: "popover",
15 expanded: "none",
16 defaultDraftView: "compact",
17 });
18 });
19
20 it("rejects duplicate handlers for one surface", () => {
21 expect(() =>
22 createContextPresentationRegistry<string, undefined, string>({
23 handlers: [
24 { kind: "terminal", render: () => "first" },
25 { kind: "terminal", render: () => "second" },
26 ],
27 fallback: () => "fallback",
28 }),
29 ).toThrowError("Duplicate context presentation handler: terminal");
30 });
31
32 it("rejects a missing handler required by a surface", () => {
33 expect(() =>
34 createContextPresentationRegistry<string, undefined, string>({
35 handlers: [{ kind: "terminal", render: () => "terminal" }],
36 requiredKinds: ["terminal", "review-comment"],
37 fallback: () => "fallback",
38 }),
39 ).toThrowError("Missing context presentation handler: review-comment");
40 });
41
42 it("uses the explicit fallback for unknown, missing, and mismatched records", () => {
43 const registry = createContextPresentationRegistry<string, undefined, string>({
44 handlers: [
45 {
46 kind: "terminal",
47 canRender: (record) => record === "terminal",
48 render: () => "rendered",
49 },
50 ],
51 fallback: (kind) => `fallback:${kind}`,
52 });
53
54 expect(registry.render("terminal", "terminal", undefined)).toBe("rendered");
55 expect(registry.render("terminal", "file", undefined)).toBe("fallback:terminal");
56 expect(registry.render("terminal", undefined, undefined)).toBe("fallback:terminal");
57 expect(registry.render("future-kind", "payload", undefined)).toBe("fallback:future-kind");
58 });
59});