Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerContextReferences.test.ts

63 lines2.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 appendInlineContextReference,
5 collectInlineContextIds,
6 ensureInlineContextReferences,
7 formatInlineContextReference,
8 insertInlineContextReference,
9 inlineContextReferenceReplacement,
10 removeInlineContextReference,
11 stripInlineContextReferences,
12} from "./composerContextReferences";
13​
14const review = { kind: "review-comment", contextId: "rc-1", label: "a.ts L4" };
15const preview = { kind: "preview-annotation", contextId: "pa-1", label: "Checkout" };
16const reviewLink = "[a.ts L4](t3-context://v1/review-comment/rc-1)";
17const previewLink = "[Checkout](t3-context://v1/preview-annotation/pa-1)";
18​
19describe("composerContextReferences", () => {
20 it.each([
21 { prompt: "before selected after", start: 7, end: 15, expected: `before ${reviewLink} after` },
22 { prompt: "selected", start: 0, end: 8, expected: `${reviewLink} ` },
23 { prompt: "aSELECTb", start: 1, end: 7, expected: `a ${reviewLink} b` },
24 ])(
25 "replaces selected text in '$prompt' with the attachment chip",
26 ({ prompt, start, end, expected }) => {
27 const edit = inlineContextReferenceReplacement(prompt, { start, end }, [review]);
28 expect(`${prompt.slice(0, edit.start)}${edit.text}${prompt.slice(edit.end)}`).toBe(expected);
29 },
30 );
31 it("formats, collects and strips references of any kind", () => {
32 expect(formatInlineContextReference(review)).toBe(reviewLink);
33 const prompt = `x ${reviewLink} y ${previewLink} ${reviewLink}`;
34 expect(collectInlineContextIds(prompt)).toEqual(["rc-1", "pa-1"]);
35 expect(stripInlineContextReferences(prompt)).toBe("x y ");
36 });
37​
38 it("inserts at the cursor and appends at the end with boundary spacing", () => {
39 expect(insertInlineContextReference("ab", 1, review)).toEqual({
40 prompt: `a ${reviewLink} b`,
41 cursor: 2 + reviewLink.length + 1,
42 });
43 expect(appendInlineContextReference("hello", preview)).toBe(`hello ${previewLink} `);
44 expect(appendInlineContextReference("", preview)).toBe(`${previewLink} `);
45 });
46​
47 it("removes every occurrence of an id and reports the earliest cursor", () => {
48 const prompt = `${reviewLink} mid ${reviewLink} end`;
49 expect(removeInlineContextReference(prompt, "rc-1")).toEqual({
50 prompt: "mid end",
51 cursor: 0,
52 });
53 expect(removeInlineContextReference("plain", "rc-1")).toEqual({ prompt: "plain", cursor: 5 });
54 });
55​
56 it("appends links only for records the prompt does not mention", () => {
57 expect(ensureInlineContextReferences(`see ${reviewLink}`, [review, preview])).toBe(
58 `see ${reviewLink} ${previewLink} `,
59 );
60 expect(ensureInlineContextReferences("", [review])).toBe(`${reviewLink} `);
61 expect(ensureInlineContextReferences(reviewLink, [review])).toBe(reviewLink);
62 });
63});