|
| 1 | +import type { ReactNode } from "react"; |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 5 | +import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; |
| 6 | + |
| 7 | +const state = vi.hoisted(() => ({ |
| 8 | + suggest: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("react-native", () => ({ |
| 12 | + View: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 13 | +})); |
| 14 | +vi.mock("@react-navigation/native", () => ({ |
| 15 | + StackActions: { push: (name: string, params: unknown) => ({ name, params }) }, |
| 16 | + useIsFocused: () => true, |
| 17 | + useNavigation: () => ({ dispatch: vi.fn() }), |
| 18 | +})); |
| 19 | +vi.mock("../../components/ui/Button", () => ({ |
| 20 | + Button: ({ label, onPress }: { label: string; onPress: () => void }) => ( |
| 21 | + <button type="button" onClick={onPress}> |
| 22 | + {label} |
| 23 | + </button> |
| 24 | + ), |
| 25 | +})); |
| 26 | +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => true })); |
| 27 | +vi.mock("../../state/threads", () => ({ |
| 28 | + threadEnvironment: { suggestSplit: { permissionAtom: () => true } }, |
| 29 | +})); |
| 30 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.suggest })); |
| 31 | +vi.mock("../../state/use-composer-drafts", () => ({ |
| 32 | + createNewTaskDraft: () => "draft-new", |
| 33 | + getComposerDraftSnapshot: () => ({ text: "", attachments: [], context: undefined }), |
| 34 | + setComposerDraftText: vi.fn(), |
| 35 | + updateComposerDraftSettings: vi.fn(), |
| 36 | +})); |
| 37 | + |
| 38 | +import { ThreadSplitSuggestion } from "./ThreadSplitSuggestion"; |
| 39 | + |
| 40 | +const environmentId = EnvironmentId.make("environment-test"); |
| 41 | +const threadId = ThreadId.make("thread-test"); |
| 42 | +const projectId = ProjectId.make("project-test"); |
| 43 | + |
| 44 | +function mount(key: string, message: string) { |
| 45 | + const container = document.createElement("div"); |
| 46 | + const root = createRoot(container); |
| 47 | + act(() => |
| 48 | + root.render( |
| 49 | + <ThreadSplitSuggestion |
| 50 | + key={key} |
| 51 | + environmentId={environmentId} |
| 52 | + threadId={threadId} |
| 53 | + projectId={projectId} |
| 54 | + draftKey="draft-test" |
| 55 | + message={message} |
| 56 | + enabled |
| 57 | + />, |
| 58 | + ), |
| 59 | + ); |
| 60 | + return { container, root }; |
| 61 | +} |
| 62 | + |
| 63 | +describe("ThreadSplitSuggestion", () => { |
| 64 | + it("keeps a dismissed suggestion hidden across remounts until the draft is cleared", async () => { |
| 65 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 66 | + vi.useFakeTimers(); |
| 67 | + try { |
| 68 | + state.suggest.mockResolvedValue({ _tag: "Success", value: { suggest: true } }); |
| 69 | + const first = mount("a", "Write release notes"); |
| 70 | + await act(async () => { |
| 71 | + await vi.advanceTimersByTimeAsync(1000); |
| 72 | + }); |
| 73 | + expect(state.suggest).toHaveBeenCalledTimes(1); |
| 74 | + const dismiss = first.container.querySelector("button:last-of-type"); |
| 75 | + expect(first.container.textContent).toContain("Start a new thread"); |
| 76 | + act(() => dismiss?.dispatchEvent(new MouseEvent("click", { bubbles: true }))); |
| 77 | + expect(first.container.textContent).not.toContain("Start a new thread"); |
| 78 | + act(() => first.root.unmount()); |
| 79 | + |
| 80 | + const second = mount("b", "Write release notes"); |
| 81 | + await act(async () => { |
| 82 | + await vi.advanceTimersByTimeAsync(1000); |
| 83 | + }); |
| 84 | + expect(state.suggest).toHaveBeenCalledTimes(1); |
| 85 | + expect(second.container.textContent).not.toContain("Start a new thread"); |
| 86 | + act(() => second.root.unmount()); |
| 87 | + |
| 88 | + const cleared = mount("c", ""); |
| 89 | + await act(async () => { |
| 90 | + await vi.advanceTimersByTimeAsync(1000); |
| 91 | + }); |
| 92 | + act(() => cleared.root.unmount()); |
| 93 | + |
| 94 | + const fresh = mount("d", "A different draft"); |
| 95 | + await act(async () => { |
| 96 | + await vi.advanceTimersByTimeAsync(1000); |
| 97 | + }); |
| 98 | + expect(state.suggest).toHaveBeenCalledTimes(2); |
| 99 | + await act(async () => { |
| 100 | + await Promise.resolve(); |
| 101 | + }); |
| 102 | + expect(fresh.container.textContent).toContain("Start a new thread"); |
| 103 | + act(() => fresh.root.unmount()); |
| 104 | + } finally { |
| 105 | + vi.useRealTimers(); |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
0 commit comments