| 1 | import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { resolveAssistantCitationCommentDismissal } from "./assistantCitationCommentDismissal"; |
| 5 | |
| 6 | describe("resolveAssistantCitationCommentDismissal", () => { |
| 7 | it("commits typed text when the popover is dismissed by clicking away", () => { |
| 8 | expect( |
| 9 | resolveAssistantCitationCommentDismissal({ |
| 10 | reason: "outside-press", |
| 11 | draft: "needs a retry", |
| 12 | savedComment: undefined, |
| 13 | }), |
| 14 | ).toEqual({ kind: "commit", comment: "needs a retry" }); |
| 15 | }); |
| 16 | |
| 17 | it("commits an edited comment when focus leaves the popover", () => { |
| 18 | expect( |
| 19 | resolveAssistantCitationCommentDismissal({ |
| 20 | reason: "focus-out", |
| 21 | draft: "second thought", |
| 22 | savedComment: "first thought", |
| 23 | }), |
| 24 | ).toEqual({ kind: "commit", comment: "second thought" }); |
| 25 | }); |
| 26 | |
| 27 | it("closes without saving when nothing changed", () => { |
| 28 | expect( |
| 29 | resolveAssistantCitationCommentDismissal({ |
| 30 | reason: "outside-press", |
| 31 | draft: null, |
| 32 | savedComment: "kept", |
| 33 | }), |
| 34 | ).toEqual({ kind: "close" }); |
| 35 | expect( |
| 36 | resolveAssistantCitationCommentDismissal({ |
| 37 | reason: "outside-press", |
| 38 | draft: " kept ", |
| 39 | savedComment: "kept", |
| 40 | }), |
| 41 | ).toEqual({ kind: "close" }); |
| 42 | expect( |
| 43 | resolveAssistantCitationCommentDismissal({ |
| 44 | reason: "outside-press", |
| 45 | draft: "kept", |
| 46 | savedComment: " kept ", |
| 47 | }), |
| 48 | ).toEqual({ kind: "close" }); |
| 49 | }); |
| 50 | |
| 51 | it("clears a comment when the draft was emptied", () => { |
| 52 | expect( |
| 53 | resolveAssistantCitationCommentDismissal({ |
| 54 | reason: "trigger-press", |
| 55 | draft: "", |
| 56 | savedComment: "old", |
| 57 | }), |
| 58 | ).toEqual({ kind: "commit", comment: "" }); |
| 59 | }); |
| 60 | |
| 61 | it("keeps Escape as an explicit discard", () => { |
| 62 | expect( |
| 63 | resolveAssistantCitationCommentDismissal({ |
| 64 | reason: "escape-key", |
| 65 | draft: "unsaved", |
| 66 | savedComment: undefined, |
| 67 | }), |
| 68 | ).toEqual({ kind: "close" }); |
| 69 | }); |
| 70 | |
| 71 | it("keeps the popover open instead of dropping an over-length draft", () => { |
| 72 | expect( |
| 73 | resolveAssistantCitationCommentDismissal({ |
| 74 | reason: "outside-press", |
| 75 | draft: "x".repeat(ASSISTANT_CITATION_MAX_COMMENT_LENGTH + 1), |
| 76 | savedComment: undefined, |
| 77 | }), |
| 78 | ).toEqual({ kind: "keep-open" }); |
| 79 | }); |
| 80 | }); |