Skip to content
Source
Sign inSign up

composerInlineTokenPaste.test.ts

65 lines2.6 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 } 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 getData: (type: string) => data[type] ?? "",
18});
19const chip = (title: string) =>
20 `[${title}](t3-context://v1/thread/${threadContextRecord(thread, title).contextId})`;
21​
22/** Imports every record it is offered, as the composer does for a thread its agent can read. */
23const importAll =
24 (imported: Array<ComposerContextClipboardFragment>) =>
25 (fragment: ComposerContextClipboardFragment) => {
26 imported.push(fragment);
27 return new Map(fragment.records.map((record) => [record.contextId, record.contextId]));
28 };
29​
30describe("importPastedComposerText", () => {
31 it("pastes a copied thread link back as the chip it was copied from", () => {
32 const target = threadChipCopyTarget(thread, "Fix the login flow");
33 const imported: Array<ComposerContextClipboardFragment> = [];
34 const text = importPastedComposerText(
35 clipboard({
36 "text/plain": `See ${target.value}`,
37 [COMPOSER_CONTEXT_CLIPBOARD_MIME]: encodeComposerContextFragment(target.fragment!)!,
38 }),
39 importAll(imported),
40 );
41 expect(text).toBe(`See ${chip("Fix the login flow")}`);
42 expect(imported.map((fragment) => fragment.records)).toEqual([target.fragment!.records]);
43 });
44​
45 it("makes a chip of a v2 link pasted without T3's clipboard format", () => {
46 const text = importPastedComposerText(
47 clipboard({ "text/plain": "[Login](t3-thread://v2/env-1/thread-1)" }),
48 importAll([]),
49 );
50 expect(text).toBe(chip("Login"));
51 });
52​
53 it("keeps the link when the composer cannot take the thread", () => {
54 const pasted = "https://app.t3.codes/threads/env-1/thread-1";
55 expect(importPastedComposerText(clipboard({ "text/plain": pasted }), () => new Map())).toBe(
56 pasted,
57 );
58 });
59​
60 it("imports CRLF and CR clipboard text with LF line breaks", () => {
61 expect(
62 importPastedComposerText(clipboard({ "text/plain": "x\r\n\r\ny\rz" }), () => new Map()),
63 ).toBe("x\n\ny\nz");
64 });
65});