@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 65 lines 2.55 KB
Raw
1 import { threadChipCopyTarget } from "@t3tools/client-runtime/thread-reference";
2 import { type ComposerContextClipboardFragment, EnvironmentId, ThreadId } from "@t3tools/contracts";
3 import {
4 COMPOSER_CONTEXT_CLIPBOARD_MIME,
5 encodeComposerContextFragment,
6 } from "@t3tools/shared/composerContextClipboard";
7 import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
8 import { describe, expect, it } from "vite-plus/test";
9
10 import { importPastedComposerText } from "./composerInlineTokenPaste";
11
12 const thread = {
13 environmentId: EnvironmentId.make("env-1"),
14 threadId: ThreadId.make("thread-1"),
15 };
16 const clipboard = (data: Record<string, string>) => ({
17 getData: (type: string) => data[type] ?? "",
18 });
19 const 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. */
23 const 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
30 describe("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 });