| ... | ... | @@ -0,0 +1,234 @@ |
| 1 | // @vitest-environment jsdom |
| 2 | import { AuthSourceControlWriteScope, EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | import { AsyncResult } from "effect/reactivity"; |
| 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | const state = vi.hoisted(() => ({ |
| 7 | actions: [] as { |
| 8 | scope: { environmentId: string | null; cwd: string | null }; |
| 9 | input: { threadId?: string; action: string }; |
| 10 | }[], |
| 11 | toasts: [] as { type?: string; description?: string; actionProps?: { onClick: () => void } }[], |
| 12 | scopes: new Set<string>(), |
| 13 | primaryScopes: new Set<string>(), |
| 14 | })); |
| 15 | |
| 16 | vi.mock("@effect/atom-react", () => ({ |
| 17 | useAtomValue: (atom: unknown) => (atom === "vcs-state" ? { isRunning: false } : null), |
| 18 | })); |
| 19 | vi.mock("~/state/entities", () => ({ |
| 20 | useThreadProjection: () => null, |
| 21 | useThreadShell: (ref: { threadId: string }) => ({ branch: "main", id: ref.threadId }), |
| 22 | })); |
| 23 | vi.mock("~/state/session", () => ({ |
| 24 | useEnvironmentScope: (environmentId: unknown, scope: string) => |
| 25 | (environmentId === "environment" ? state.scopes : state.primaryScopes).has(scope), |
| 26 | readEnvironmentScope: (environmentId: unknown, scope: string) => |
| 27 | (environmentId === "environment" ? state.scopes : state.primaryScopes).has(scope), |
| 28 | })); |
| 29 | vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: (command: unknown) => command })); |
| 30 | vi.mock("~/state/server", () => ({ serverEnvironment: { configValueAtom: () => null } })); |
| 31 | vi.mock("~/state/sourceControl", () => ({ sourceControlEnvironment: { discovery: () => null } })); |
| 32 | vi.mock("~/state/vcs", () => ({ |
| 33 | vcsEnvironment: { status: () => null }, |
| 34 | vcsActionManager: { stateAtom: () => "vcs-state" }, |
| 35 | })); |
| 36 | vi.mock("~/state/threads", () => ({ |
| 37 | threadEnvironment: { updateMetadata: async () => AsyncResult.success(undefined) }, |
| 38 | })); |
| 39 | vi.mock("~/state/query", () => ({ |
| 40 | useEnvironmentQuery: () => ({ |
| 41 | data: { |
| 42 | sourceControlProviders: [], |
| 43 | isRepo: true, |
| 44 | refName: "main", |
| 45 | isDefaultRef: false, |
| 46 | hasPrimaryRemote: true, |
| 47 | hasWorkingTreeChanges: true, |
| 48 | workingTree: { |
| 49 | files: [{ path: "file.ts", status: "modified", insertions: 1, deletions: 0 }], |
| 50 | }, |
| 51 | }, |
| 52 | error: null, |
| 53 | }), |
| 54 | })); |
| 55 | vi.mock("@tanstack/react-router", () => ({ useNavigate: () => () => {} })); |
| 56 | vi.mock("~/composerDraftStore", () => ({ |
| 57 | useComposerDraftStore: ( |
| 58 | select: (store: { |
| 59 | getDraftSession: () => null; |
| 60 | getDraftThreadByRef: () => null; |
| 61 | setDraftThreadContext: () => void; |
| 62 | }) => unknown, |
| 63 | ) => |
| 64 | select({ |
| 65 | getDraftSession: () => null, |
| 66 | getDraftThreadByRef: () => null, |
| 67 | setDraftThreadContext: () => {}, |
| 68 | }), |
| 69 | })); |
| 70 | vi.mock("~/lib/sourceControlActions", () => ({ |
| 71 | useSourceControlActionRunning: () => false, |
| 72 | useVcsInitAction: () => ({}), |
| 73 | useVcsPullAction: () => ({}), |
| 74 | useSourceControlPublishRepositoryAction: () => ({}), |
| 75 | useGitStackedAction: (scope: { environmentId: string | null; cwd: string | null }) => ({ |
| 76 | run: async (input: { featureBranch?: boolean; threadId?: string; action: string }) => { |
| 77 | state.actions.push({ scope, input }); |
| 78 | return { |
| 79 | _tag: "Success", |
| 80 | value: { |
| 81 | branch: { status: "skipped_not_requested" }, |
| 82 | toast: { |
| 83 | title: "Committed", |
| 84 | description: "Committed", |
| 85 | cta: { |
| 86 | kind: "run_action", |
| 87 | label: "Push & create PR", |
| 88 | action: { kind: "commit_push_pr" }, |
| 89 | }, |
| 90 | }, |
| 91 | }, |
| 92 | }; |
| 93 | }, |
| 94 | }), |
| 95 | })); |
| 96 | vi.mock("~/lib/utils", () => ({ cn: () => "", randomUUID: () => "action" })); |
| 97 | vi.mock("~/editorPreferences", () => ({ useOpenInPreferredEditor: () => () => {} })); |
| 98 | vi.mock("~/browser/useOpenLink", () => ({ useOpenLink: () => () => {} })); |
| 99 | vi.mock("~/lib/openPullRequestLink", () => ({ useOpenPrLink: () => () => {} })); |
| 100 | vi.mock("~/components/ui/toast", () => ({ |
| 101 | stackedThreadToast: (input: unknown) => input, |
| 102 | toastManager: { |
| 103 | add: (input: { |
| 104 | type?: string; |
| 105 | description?: string; |
| 106 | actionProps?: { onClick: () => void }; |
| 107 | }) => { |
| 108 | state.toasts.push(input); |
| 109 | return "toast"; |
| 110 | }, |
| 111 | update: () => {}, |
| 112 | close: () => {}, |
| 113 | }, |
| 114 | })); |
| 115 | vi.mock("~/components/ui/dialog", () => ({ |
| 116 | Dialog: "Dialog", |
| 117 | DialogDescription: "DialogDescription", |
| 118 | DialogFooter: "DialogFooter", |
| 119 | DialogHeader: "DialogHeader", |
| 120 | DialogPanel: "DialogPanel", |
| 121 | DialogPopup: "DialogPopup", |
| 122 | DialogTitle: "DialogTitle", |
| 123 | })); |
| 124 | vi.mock("~/components/ui/group", () => ({ Group: "Group", GroupSeparator: "GroupSeparator" })); |
| 125 | vi.mock("~/components/ui/menu", () => ({ |
| 126 | Menu: "Menu", |
| 127 | MenuItem: "MenuItem", |
| 128 | MenuItemLabel: "MenuItemLabel", |
| 129 | MenuPopup: "MenuPopup", |
| 130 | MenuTrigger: "MenuTrigger", |
| 131 | })); |
| 132 | vi.mock("~/components/ui/popover", () => ({ |
| 133 | Popover: "Popover", |
| 134 | PopoverPopup: "PopoverPopup", |
| 135 | PopoverTrigger: "PopoverTrigger", |
| 136 | })); |
| 137 | vi.mock("~/components/ui/tooltip", () => ({ |
| 138 | Tooltip: "Tooltip", |
| 139 | TooltipPopup: "TooltipPopup", |
| 140 | TooltipTrigger: "TooltipTrigger", |
| 141 | })); |
| 142 | vi.mock("~/components/ui/button", () => ({ Button: "button" })); |
| 143 | vi.mock("~/components/ui/checkbox", () => ({ Checkbox: "Checkbox" })); |
| 144 | vi.mock("~/components/ui/input", () => ({ Input: "Input" })); |
| 145 | vi.mock("~/components/ui/radio-group", () => ({ RadioGroup: "RadioGroup" })); |
| 146 | vi.mock("~/components/ui/scroll-area", () => ({ ScrollArea: "ScrollArea" })); |
| 147 | vi.mock("~/components/ui/spinner", () => ({ Spinner: "Spinner" })); |
| 148 | vi.mock("~/components/ui/textarea", () => ({ Textarea: "Textarea" })); |
| 149 | vi.mock("~/components/ui/toggle", () => ({ toggleVariants: () => "" })); |
| 150 | vi.mock("./AnimatedHeight", () => ({ AnimatedHeight: "AnimatedHeight" })); |
| 151 | |
| 152 | import GitActionsControl from "./GitActionsControl"; |
| 153 | |
| 154 | import { createElement, memo } from "react"; |
| 155 | import { act, create } from "react-test-renderer"; |
| 156 | |
| 157 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 158 | |
| 159 | const renderers: ReturnType<typeof create>[] = []; |
| 160 | // ThreadDetailsPanel keeps this component mounted behind React.memo when threads change. |
| 161 | const MemoGitActionsControl = memo(GitActionsControl); |
| 162 | const props = (thread: string, cwd: string) => ({ |
| 163 | gitCwd: cwd, |
| 164 | activeThreadRef: { |
| 165 | environmentId: EnvironmentId.make("environment"), |
| 166 | threadId: ThreadId.make(thread), |
| 167 | }, |
| 168 | }); |
| 169 | |
| 170 | async function renderActions(thread: string, cwd: string) { |
| 171 | let renderer!: ReturnType<typeof create>; |
| 172 | await act(async () => { |
| 173 | renderer = create(createElement(MemoGitActionsControl, props(thread, cwd))); |
| 174 | }); |
| 175 | renderers.push(renderer); |
| 176 | return renderer; |
| 177 | } |
| 178 | |
| 179 | async function clickQuickAction(renderer: ReturnType<typeof create>) { |
| 180 | const button = renderer.root |
| 181 | .findAllByType("button") |
| 182 | .find((button) => button.props.onClick && !button.props.disabled); |
| 183 | expect(button).toBeDefined(); |
| 184 | await act(async () => { |
| 185 | button!.props.onClick({}); |
| 186 | }); |
| 187 | } |
| 188 | |
| 189 | beforeEach(() => { |
| 190 | state.scopes = new Set([AuthSourceControlWriteScope]); |
| 191 | state.primaryScopes = new Set([AuthSourceControlWriteScope]); |
| 192 | state.actions = []; |
| 193 | state.toasts = []; |
| 194 | }); |
| 195 | |
| 196 | afterEach(async () => { |
| 197 | await act(async () => { |
| 198 | for (const renderer of renderers.splice(0)) renderer.unmount(); |
| 199 | }); |
| 200 | }); |
| 201 | |
| 202 | describe("Git action targets across thread navigation", () => { |
| 203 | it("runs a retained PR toast in the thread that produced it", async () => { |
| 204 | const renderer = await renderActions("first", "/first"); |
| 205 | await clickQuickAction(renderer); |
| 206 | const toast = state.toasts.find((toast) => toast.actionProps)!; |
| 207 | expect(toast).toBeDefined(); |
| 208 | await act(async () => { |
| 209 | renderer.update(createElement(MemoGitActionsControl, props("second", "/second"))); |
| 210 | }); |
| 211 | await act(async () => { |
| 212 | toast.actionProps!.onClick(); |
| 213 | }); |
| 214 | expect(state.actions.at(-1)).toMatchObject({ |
| 215 | scope: { cwd: "/first" }, |
| 216 | input: { threadId: "first", action: "commit_push_pr" }, |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | it("runs the memoized panel button in the current thread after navigation", async () => { |
| 221 | const renderer = await renderActions("first", "/first"); |
| 222 | for (const [thread, cwd] of [ |
| 223 | ["first", "/first"], |
| 224 | ["second", "/second"], |
| 225 | ["first", "/first"], |
| 226 | ] as const) { |
| 227 | await act(async () => { |
| 228 | renderer.update(createElement(MemoGitActionsControl, props(thread, cwd))); |
| 229 | }); |
| 230 | await clickQuickAction(renderer); |
| 231 | expect(state.actions.at(-1)).toMatchObject({ scope: { cwd }, input: { threadId: thread } }); |
| 232 | } |
| 233 | }); |
| 234 | }); |