Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import {
3 type ComposerContextClipboardFragment,
4 type ComposerContextId,
5 EnvironmentId,
6} from "@t3tools/contracts";
7import { writeCopiedSelectionComposerContext } from "./selectionCopyComposerContext";
8
9const fragment: ComposerContextClipboardFragment = {
10 version: 1,
11 source: { environmentId: EnvironmentId.make("env-1") },
12 records: [
13 {
14 version: 1,
15 kind: "file",
16 contextId: "file_1" as ComposerContextId,
17 label: "index.ts",
18 name: "index.ts",
19 mimeType: "text/typescript",
20 sizeBytes: 120,
21 attachmentId: "att-1",
22 },
23 ],
24};
25
26describe("writeCopiedSelectionComposerContext", () => {
27 it("attaches context metadata and canonicalizes matched record labels for plain and markdown copies", () => {
28 const attach = vi.fn().mockResolvedValue(undefined);
29 const copied =
30 "Check [short](t3-context://v1/file/file_1) and [other](t3-context://v1/file/unknown_id)";
31 const canonical =
32 "Check [index.ts](t3-context://v1/file/file_1) and [other](t3-context://v1/file/unknown_id)";
33
34 expect(
35 writeCopiedSelectionComposerContext({ format: "plain", text: copied }, fragment, attach),
36 ).toBe(true);
37 expect(
38 writeCopiedSelectionComposerContext({ format: "markdown", text: copied }, fragment, attach),
39 ).toBe(true);
40 expect(attach).toHaveBeenCalledTimes(2);
41 expect(attach).toHaveBeenNthCalledWith(1, canonical, fragment, copied);
42 expect(attach).toHaveBeenNthCalledWith(2, canonical, fragment, copied);
43 });
44
45 it("leaves the native clipboard untouched for image URL copies or selections without matching records", () => {
46 const attach = vi.fn().mockResolvedValue(undefined);
47
48 expect(
49 writeCopiedSelectionComposerContext(
50 { format: "imageUrls", text: "[index.ts](t3-context://v1/file/file_1)" },
51 fragment,
52 attach,
53 ),
54 ).toBe(false);
55 expect(
56 writeCopiedSelectionComposerContext(
57 { format: "plain", text: "Just plain prose with [docs](https://example.com)" },
58 fragment,
59 attach,
60 ),
61 ).toBe(false);
62 expect(
63 writeCopiedSelectionComposerContext(
64 { format: "plain", text: "Unknown [other.ts](t3-context://v1/file/other_id)" },
65 fragment,
66 attach,
67 ),
68 ).toBe(false);
69 expect(attach).not.toHaveBeenCalled();
70 });
71});