Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 appendInlineContextReference,
5 collectInlineContextIds,
6 ensureInlineContextReferences,
7 formatInlineContextReference,
8 insertInlineContextReference,
9 removeInlineContextReference,
10 stripInlineContextReferences,
11 toComposerContextId,
12 toKindScopedComposerContextId,
13} from "./composerContextReferences";
14
15const review = { kind: "review-comment", contextId: "rc-1", label: "a.ts L4" };
16const preview = { kind: "preview-annotation", contextId: "pa-1", label: "Checkout" };
17const reviewLink = "[a.ts L4](t3-context://v1/review-comment/rc-1)";
18const previewLink = "[Checkout](t3-context://v1/preview-annotation/pa-1)";
19
20describe("composerContextReferences", () => {
21 it("formats, collects and strips references of any kind", () => {
22 expect(formatInlineContextReference(review)).toBe(reviewLink);
23 const prompt = `x ${reviewLink} y ${previewLink} ${reviewLink}`;
24 expect(collectInlineContextIds(prompt)).toEqual(["rc-1", "pa-1"]);
25 expect(stripInlineContextReferences(prompt)).toBe("x y ");
26 });
27
28 it("inserts at the cursor and appends at the end with boundary spacing", () => {
29 expect(insertInlineContextReference("ab", 1, review)).toEqual({
30 prompt: `a ${reviewLink} b`,
31 cursor: 2 + reviewLink.length + 1,
32 });
33 expect(appendInlineContextReference("hello", preview)).toBe(`hello ${previewLink} `);
34 expect(appendInlineContextReference("", preview)).toBe(`${previewLink} `);
35 });
36
37 it("removes every occurrence of an id and reports the earliest cursor", () => {
38 const prompt = `${reviewLink} mid ${reviewLink} end`;
39 expect(removeInlineContextReference(prompt, "rc-1")).toEqual({
40 prompt: "mid end",
41 cursor: 0,
42 });
43 expect(removeInlineContextReference("plain", "rc-1")).toEqual({ prompt: "plain", cursor: 5 });
44 });
45
46 it("appends links only for records the prompt does not mention", () => {
47 expect(ensureInlineContextReferences(`see ${reviewLink}`, [review, preview])).toBe(
48 `see ${reviewLink} ${previewLink} `,
49 );
50 expect(ensureInlineContextReferences("", [review])).toBe(`${reviewLink} `);
51 expect(ensureInlineContextReferences(reviewLink, [review])).toBe(reviewLink);
52 });
53});
54
55describe("toComposerContextId", () => {
56 it("keeps same-kind raw IDs distinct when one contains the namespace prefix", () => {
57 expect(toKindScopedComposerContextId("image", "x")).toBe("image_x");
58 expect(toKindScopedComposerContextId("image", "image_x")).toBe("image_image_x");
59 });
60 it("keeps ids that already fit the grammar and folds the rest deterministically", () => {
61 expect(toComposerContextId("file-comment-1700-1")).toBe("file-comment-1700-1");
62 const folded = toComposerContextId("pull-request-selection:src/a.ts:4-9");
63 expect(folded).toMatch(/^pull-request-selection-src-a-ts-4-9-[0-9a-f]{16}$/);
64 expect(toComposerContextId("pull-request-selection:src/a.ts:4-9")).toBe(folded);
65 expect(toComposerContextId("pull-request-selection:src/b.ts:4-9")).not.toBe(folded);
66 expect(toComposerContextId("::")).toMatch(/^ctx-[0-9a-f]{16}$/);
67 });
68 it("tells apart producer ids that agree past the slug's truncation point", () => {
69 // The slug keeps 48 characters, so only the digest distinguishes these two.
70 const shared = `pull-request-finding:${"a".repeat(60)}`;
71 const first = toComposerContextId(`${shared}:1`);
72 const second = toComposerContextId(`${shared}:2`);
73
74 expect(first).not.toBe(second);
75 expect(first.length).toBeLessThanOrEqual(128);
76 });
77});