|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { consumeDeepLinkQuestion } from "./dashboardAgentOpenRequest"; |
| 3 | + |
| 4 | +const NAMES = ["aiHelp"]; |
| 5 | + |
| 6 | +/** |
| 7 | + * The reader is an effect over `useSearchParams`. Dropping the param is a navigation, so the |
| 8 | + * effect can run again on the original params before it commits; without a record of what was |
| 9 | + * already sent, the deep-linked question is asked twice. |
| 10 | + */ |
| 11 | +describe("consumeDeepLinkQuestion", () => { |
| 12 | + const params = (search: string) => new URLSearchParams(search); |
| 13 | + |
| 14 | + it("hands over the question the first time it sees it", () => { |
| 15 | + expect(consumeDeepLinkQuestion(params("?aiHelp=why+is+it+slow"), NAMES, null)).toEqual({ |
| 16 | + question: "why is it slow", |
| 17 | + sent: "why is it slow", |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it("does not hand it over again while the param is still in the URL", () => { |
| 22 | + const first = consumeDeepLinkQuestion(params("?aiHelp=why"), NAMES, null); |
| 23 | + expect(first.question).toBe("why"); |
| 24 | + expect(consumeDeepLinkQuestion(params("?aiHelp=why"), NAMES, first.sent).question).toBeNull(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("keeps the record until the param is gone, however many renders that takes", () => { |
| 28 | + let sent: string | null = null; |
| 29 | + let asked = 0; |
| 30 | + for (let render = 0; render < 5; render++) { |
| 31 | + const result = consumeDeepLinkQuestion(params("?aiHelp=why"), NAMES, sent); |
| 32 | + sent = result.sent; |
| 33 | + if (result.question !== null) asked++; |
| 34 | + } |
| 35 | + expect(asked).toBe(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it("forgets the question once the URL no longer carries it, so a later visit works", () => { |
| 39 | + const first = consumeDeepLinkQuestion(params("?aiHelp=why"), NAMES, null); |
| 40 | + const cleared = consumeDeepLinkQuestion(params(""), NAMES, first.sent); |
| 41 | + expect(cleared).toEqual({ question: null, sent: null }); |
| 42 | + expect(consumeDeepLinkQuestion(params("?aiHelp=why"), NAMES, cleared.sent).question).toBe( |
| 43 | + "why" |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it("asks a different question that arrives before the first is dropped", () => { |
| 48 | + const first = consumeDeepLinkQuestion(params("?aiHelp=why"), NAMES, null); |
| 49 | + expect(consumeDeepLinkQuestion(params("?aiHelp=how"), NAMES, first.sent).question).toBe("how"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("has nothing to ask when no watched param is present", () => { |
| 53 | + expect(consumeDeepLinkQuestion(params("?other=x"), NAMES, null)).toEqual({ |
| 54 | + question: null, |
| 55 | + sent: null, |
| 56 | + }); |
| 57 | + expect(consumeDeepLinkQuestion(params("?aiHelp=why"), [], null).question).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("ignores an empty question", () => { |
| 61 | + expect(consumeDeepLinkQuestion(params("?aiHelp="), NAMES, null).question).toBeNull(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments