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
| 1 | import { expect, it } from "vite-plus/test"; |
| 2 | import { collectMobileAssistantTextSelectors } from "./assistantCitationSelection"; |
| 3 | import { |
| 4 | findAssistantCitationText, |
| 5 | serializeAssistantCitation, |
| 6 | expandAssistantCitationsForProvider, |
| 7 | } from "@t3tools/shared/assistantCitations"; |
| 8 | import { EnvironmentId, ThreadId, MessageId } from "@t3tools/contracts"; |
| 9 | |
| 10 | it("offers distinct source choices within a block and maps recursive folds without merging occurrences", () => { |
| 11 | const paragraph = `${"x".repeat(40)}**quote**${"x".repeat(40)}`; |
| 12 | const markdown = `${paragraph}\n\n<details><summary>Outer</summary>\n\n<details><summary>Inner</summary>\n\n${paragraph}\n\n</details>\n\n</details>`; |
| 13 | const [first] = collectMobileAssistantTextSelectors(markdown, "0:0", "quote"); |
| 14 | const [second] = collectMobileAssistantTextSelectors(markdown, "1/1/0:0", "quote"); |
| 15 | expect([first?.index, first?.count, second?.index, second?.count]).toEqual([0, 2, 1, 2]); |
| 16 | const rendered = `${"x".repeat(40)}quote${"x".repeat(40)} ${"x".repeat(40)}quote${"x".repeat(40)}`; |
| 17 | expect(second?.selector.start).toBe(rendered.lastIndexOf("quote")); |
| 18 | // Identical context on both sides stays ambiguous rather than guessing. |
| 19 | expect(findAssistantCitationText(rendered, second!.selector)).toBeNull(); |
| 20 | const markers = [first!, second!].map(({ selector }) => |
| 21 | serializeAssistantCitation({ |
| 22 | version: 1, |
| 23 | environmentId: EnvironmentId.make("env"), |
| 24 | threadId: ThreadId.make("thread"), |
| 25 | messageId: MessageId.make("assistant"), |
| 26 | ...selector, |
| 27 | }), |
| 28 | ); |
| 29 | expect(markers[0]).not.toBe(markers[1]); |
| 30 | expect(expandAssistantCitationsForProvider(markers.join(" "))).toContain( |
| 31 | '"id": "assistant-quote-2"', |
| 32 | ); |
| 33 | const [reference] = collectMobileAssistantTextSelectors( |
| 34 | "before [label][id] after\n\n<details><summary>More</summary>\n\n[id]: https://example.com\n\n</details>", |
| 35 | "0:0", |
| 36 | "before label after", |
| 37 | ); |
| 38 | expect(reference?.selector.text).toBe("before label after"); |
| 39 | const sameBlock = collectMobileAssistantTextSelectors( |
| 40 | "first **quote** then **quote** last", |
| 41 | "0:0", |
| 42 | "quote", |
| 43 | ); |
| 44 | expect(sameBlock[1]?.selector.start).toBe(17); |
| 45 | expect(sameBlock[1]?.selector.prefix).toBe("first quote then "); |
| 46 | const [leadingSpace] = collectMobileAssistantTextSelectors( |
| 47 | "x\n\n<details><summary>More</summary>\n\n` quote`\n\n</details>", |
| 48 | "1/0:0", |
| 49 | "quote", |
| 50 | ); |
| 51 | expect(leadingSpace?.selector.start).toBe(2); |
| 52 | expect(leadingSpace?.count).toBe(1); |
| 53 | const codeChoices = collectMobileAssistantTextSelectors( |
| 54 | "quote\n\n```text\nquote\n```\n\nquote", |
| 55 | "0:0", |
| 56 | "quote", |
| 57 | ); |
| 58 | expect(codeChoices.map((choice) => [choice.index, choice.count])).toEqual([ |
| 59 | [0, 3], |
| 60 | [1, 3], |
| 61 | [2, 3], |
| 62 | ]); |
| 63 | for (const { source, path } of [ |
| 64 | { source: "\n\nquote quote\n\n", path: "0:0" }, |
| 65 | { |
| 66 | source: 'quote\n\n```mermaid\nflowchart TD\nA["quote"]\n```\n\nquote', |
| 67 | path: "0:0", |
| 68 | }, |
| 69 | { |
| 70 | source: |
| 71 | "\n\n<details><summary>More</summary>\n\nquote quote\n\n</details>\n\n", |
| 72 | path: "1/0:0", |
| 73 | }, |
| 74 | ]) { |
| 75 | const selectors = collectMobileAssistantTextSelectors(source, path, "quote"); |
| 76 | expect(selectors).toHaveLength(2); |
| 77 | for (const [index, { selector }] of selectors.entries()) { |
| 78 | expect(findAssistantCitationText("quote quote", selector)).toEqual({ |
| 79 | start: index * 6, |
| 80 | end: index * 6 + 5, |
| 81 | }); |
| 82 | } |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | it("rejects unmappable or blank selections instead of inventing a source", () => { |
| 87 | expect(collectMobileAssistantTextSelectors("quote quote", "0:0", " ")).toEqual([]); |
| 88 | expect(collectMobileAssistantTextSelectors("quote ".repeat(21), "0:0", "quote")).toEqual([]); |
| 89 | expect(collectMobileAssistantTextSelectors("quote quote", "0:0", "other")).toEqual([]); |
| 90 | expect(collectMobileAssistantTextSelectors("quote quote", "missing", "quote")).toEqual([]); |
| 91 | }); |
| 92 | |
| 93 | it("uses the web math representation around a selected passage", () => { |
| 94 | for (const formula of ["$x$", "\\(x\\)"]) { |
| 95 | const [choice] = collectMobileAssistantTextSelectors(`Before ${formula} after`, "0:0", "after"); |
| 96 | expect(choice?.selector.prefix).toBe("Before \\(x\\) "); |
| 97 | expect(findAssistantCitationText("Before \\(x\\) after", choice!.selector)).toEqual({ |
| 98 | start: 13, |
| 99 | end: 18, |
| 100 | }); |
| 101 | } |
| 102 | const [display] = collectMobileAssistantTextSelectors( |
| 103 | "Before\n\n$$\nx\n$$\n\nafter", |
| 104 | "0:0", |
| 105 | "after", |
| 106 | ); |
| 107 | expect(display?.selector.prefix).toBe("Before \\[ x \\] "); |
| 108 | }); |