|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { after, afterEach, before, test } from "node:test"; |
| 3 | + |
| 4 | +import { JSDOM } from "jsdom"; |
| 5 | + |
| 6 | +import { buildProjectDetailAgentContext } from "../lib/projectDetailAgentContext.ts"; |
| 7 | +import { projectDetailSelectionItem } from "../lib/projectDetailSelectionItem.ts"; |
| 8 | +import { reviewDiffWorkspaceBranch } from "../lib/projectReviewDisplay.ts"; |
| 9 | +import { buildProjectDetailCrumbs } from "./useProjectDetailCrumbs.ts"; |
| 10 | + |
| 11 | +const OWNER = "a".repeat(64); |
| 12 | +const REVIEW_A_ID = "b".repeat(64); |
| 13 | + |
| 14 | +const repository = { |
| 15 | + id: `${OWNER}:buzz`, |
| 16 | + cloneUrls: ["https://example.com/buzz.git"], |
| 17 | + defaultBranch: "main", |
| 18 | + channelId: "trusted-repository-channel", |
| 19 | + name: "buzz", |
| 20 | + repoAddress: `30617:${OWNER}:buzz`, |
| 21 | +}; |
| 22 | + |
| 23 | +const reviewA = { |
| 24 | + id: REVIEW_A_ID, |
| 25 | + title: "Ship the retained review", |
| 26 | + author: "alice", |
| 27 | + status: "Open", |
| 28 | + branchName: "feature-a", |
| 29 | + targetBranch: "main", |
| 30 | + cloneUrls: repository.cloneUrls, |
| 31 | + repoAddress: repository.repoAddress, |
| 32 | + channelId: "forged-origin-channel", |
| 33 | +}; |
| 34 | + |
| 35 | +const noop = () => {}; |
| 36 | + |
| 37 | +function productionConsumers({ activeRepoPullRequest, selectedPullRequest }) { |
| 38 | + const crumbs = buildProjectDetailCrumbs({ |
| 39 | + activeTab: "prs", |
| 40 | + commit: null, |
| 41 | + issue: null, |
| 42 | + pullRequest: selectedPullRequest, |
| 43 | + setRequestedTab: noop, |
| 44 | + setSelectedCommitHash: noop, |
| 45 | + setSelectedIssueId: noop, |
| 46 | + setSelectedPullRequestId: noop, |
| 47 | + setTabsResetKey: noop, |
| 48 | + }); |
| 49 | + const contextItem = projectDetailSelectionItem({ |
| 50 | + projectChannelId: "trusted-project-channel", |
| 51 | + projectId: "project-id", |
| 52 | + pullRequest: selectedPullRequest, |
| 53 | + repository, |
| 54 | + }); |
| 55 | + const agent = buildProjectDetailAgentContext({ |
| 56 | + activeTab: "prs", |
| 57 | + branch: "feature-a", |
| 58 | + project: { name: "buzz" }, |
| 59 | + repository: { |
| 60 | + name: repository.name, |
| 61 | + repoAddress: repository.repoAddress, |
| 62 | + }, |
| 63 | + source: "remote", |
| 64 | + workItems: [null, null, selectedPullRequest], |
| 65 | + }); |
| 66 | + return { |
| 67 | + agentReviewId: agent.workItem?.id ?? null, |
| 68 | + crumbTitle: crumbs.activeWorkItemCrumb?.title ?? null, |
| 69 | + contextId: contextItem?.id ?? null, |
| 70 | + diffQueryId: activeRepoPullRequest?.id ?? null, |
| 71 | + diffWorkspaceBranch: reviewDiffWorkspaceBranch({ |
| 72 | + activeBranch: "feature-a", |
| 73 | + defaultBranch: repository.defaultBranch, |
| 74 | + pullRequest: activeRepoPullRequest, |
| 75 | + }), |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +const dom = new JSDOM("<!doctype html><html><body></body></html>", { |
| 80 | + url: "http://localhost", |
| 81 | +}); |
| 82 | + |
| 83 | +before(() => { |
| 84 | + Object.assign(globalThis, { |
| 85 | + document: dom.window.document, |
| 86 | + HTMLElement: dom.window.HTMLElement, |
| 87 | + IS_REACT_ACT_ENVIRONMENT: true, |
| 88 | + window: dom.window, |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +afterEach(async () => { |
| 93 | + const { cleanup } = await import("@testing-library/react"); |
| 94 | + cleanup(); |
| 95 | +}); |
| 96 | + |
| 97 | +after(() => dom.window.close()); |
| 98 | + |
| 99 | +let hookModule; |
| 100 | +before(async () => { |
| 101 | + hookModule = await import("./useRetainedProjectGitViews.ts"); |
| 102 | +}); |
| 103 | + |
| 104 | +async function renderSelection(initialProps) { |
| 105 | + const { renderHook } = await import("@testing-library/react"); |
| 106 | + return renderHook( |
| 107 | + (props) => hookModule.useRetainedPullRequestSelection(props), |
| 108 | + { initialProps }, |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +test("selected review chrome and diff query stay aligned across fetch phases", async () => { |
| 113 | + const populated = { |
| 114 | + activeBranch: "feature-a", |
| 115 | + isFetching: false, |
| 116 | + pullRequests: [reviewA], |
| 117 | + repository, |
| 118 | + selectedPullRequestId: REVIEW_A_ID, |
| 119 | + }; |
| 120 | + const { rerender, result } = await renderSelection(populated); |
| 121 | + |
| 122 | + const populatedConsumers = productionConsumers(result.current); |
| 123 | + assert.equal(result.current.selectedPullRequest, reviewA); |
| 124 | + assert.equal(result.current.activeRepoPullRequest, reviewA); |
| 125 | + assert.deepEqual(populatedConsumers, { |
| 126 | + agentReviewId: REVIEW_A_ID, |
| 127 | + crumbTitle: "Ship the retained review", |
| 128 | + contextId: `review:${REVIEW_A_ID}`, |
| 129 | + diffQueryId: REVIEW_A_ID, |
| 130 | + diffWorkspaceBranch: "main", |
| 131 | + }); |
| 132 | + |
| 133 | + rerender({ |
| 134 | + ...populated, |
| 135 | + isFetching: true, |
| 136 | + pullRequests: [], |
| 137 | + }); |
| 138 | + const fetchingConsumers = productionConsumers(result.current); |
| 139 | + assert.equal(result.current.selectedPullRequest, reviewA); |
| 140 | + assert.equal( |
| 141 | + result.current.selectedPullRequest, |
| 142 | + result.current.activeRepoPullRequest, |
| 143 | + ); |
| 144 | + assert.deepEqual(fetchingConsumers, populatedConsumers); |
| 145 | + |
| 146 | + rerender({ |
| 147 | + ...populated, |
| 148 | + isFetching: false, |
| 149 | + pullRequests: [], |
| 150 | + }); |
| 151 | + const completedConsumers = productionConsumers(result.current); |
| 152 | + assert.equal(result.current.selectedPullRequest, null); |
| 153 | + assert.equal(result.current.activeRepoPullRequest, null); |
| 154 | + assert.deepEqual(completedConsumers, { |
| 155 | + agentReviewId: null, |
| 156 | + crumbTitle: null, |
| 157 | + contextId: null, |
| 158 | + diffQueryId: null, |
| 159 | + diffWorkspaceBranch: "feature-a", |
| 160 | + }); |
| 161 | +}); |
0 commit comments