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

composerInlineTokenPaste.test.ts

77 lines3.2 KB

1import { threadChipCopyTarget } from "@t3tools/client-runtime/thread-reference";
2import { type ComposerContextClipboardFragment, EnvironmentId, ThreadId } from "@t3tools/contracts";
3import {
4 COMPOSER_CONTEXT_CLIPBOARD_MIME,
5 encodeComposerContextFragment,
6} from "@t3tools/shared/composerContextClipboard";
7import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
8import { describe, expect, it } from "vite-plus/test";
9​
10import { importPastedComposerText, readPastedComposerClipboard } from "./composerInlineTokenPaste";
11​
12const thread = {
13 environmentId: EnvironmentId.make("env-1"),
14 threadId: ThreadId.make("thread-1"),
15};
16const clipboard = (data: Record<string, string>) =>
17 readPastedComposerClipboard({ getData: (type) => data[type] ?? "" });
18const chip = (title: string) =>
19 `[${title}](t3-context://v1/thread/${threadContextRecord(thread, title).contextId})`;
20​
21/** Imports every record it is offered, as the composer does for a thread its agent can read. */
22const importAll =
23 (imported: Array<ComposerContextClipboardFragment>) =>
24 (fragment: ComposerContextClipboardFragment) => {
25 imported.push(fragment);
26 return new Map(fragment.records.map((record) => [record.contextId, record.contextId]));
27 };
28​
29describe("importPastedComposerText", () => {
30 it("pastes a copied thread link back as the chip it was copied from", () => {
31 const target = threadChipCopyTarget(thread, "Fix the login flow");
32 const imported: Array<ComposerContextClipboardFragment> = [];
33 const text = importPastedComposerText(
34 clipboard({
35 "text/plain": `See ${target.value}`,
36 [COMPOSER_CONTEXT_CLIPBOARD_MIME]: encodeComposerContextFragment(target.fragment!)!,
37 }),
38 importAll(imported),
39 );
40 expect(text).toBe(`See ${chip("Fix the login flow")}`);
41 expect(imported.map((fragment) => fragment.records)).toEqual([target.fragment!.records]);
42 });
43​
44 it("makes a chip of a v2 link pasted without T3's clipboard format", () => {
45 const text = importPastedComposerText(
46 clipboard({ "text/plain": "[Login](t3-thread://v2/env-1/thread-1)" }),
47 importAll([]),
48 );
49 expect(text).toBe(chip("Login"));
50 const withBrackets = clipboard({
51 "text/plain": String.raw`[Fix \[login\]](https://app.t3.codes/threads/env-1/thread-1)`,
52 });
53 expect(importPastedComposerText(withBrackets, importAll([]))).toBe(chip("Fix login"));
54 });
55​
56 it("keeps the link when the composer cannot take the thread", () => {
57 const pasted = "https://app.t3.codes/threads/env-1/thread-1";
58 expect(importPastedComposerText(clipboard({ "text/plain": pasted }), () => new Map())).toBe(
59 pasted,
60 );
61 });
62​
63 it("drops a leading BOM so a fenced link example stays literal and the link after it a chip", () => {
64 const fenced = "~~~\n<t3-thread://v2/env-1/thread-1>\n~~~";
65 const text = importPastedComposerText(
66 clipboard({ "text/plain": `\uFEFF${fenced}\n<t3-thread://v2/env-1/thread-1>` }),
67 importAll([]),
68 );
69 expect(text).toBe(`${fenced}\n${chip("Thread")}`);
70 });
71​
72 it("imports CRLF and CR clipboard text with LF line breaks", () => {
73 expect(
74 importPastedComposerText(clipboard({ "text/plain": "x\r\n\r\ny\rz" }), () => new Map()),
75 ).toBe("x\n\ny\nz");
76 });
77});