|
| 1 | +import type { Difference } from "@aws-sdk/client-codecommit"; |
| 2 | +import { render } from "ink-testing-library"; |
| 3 | +import React from "react"; |
| 4 | +import { bench, describe } from "vitest"; |
| 5 | +import { PullRequestDetail } from "../src/components/PullRequestDetail.js"; |
| 6 | + |
| 7 | +const noop = () => {}; |
| 8 | + |
| 9 | +function makeLines(count: number, prefix: string): string { |
| 10 | + return Array.from({ length: count }, (_, i) => `${prefix} line ${i} with some content`).join( |
| 11 | + "\n", |
| 12 | + ); |
| 13 | +} |
| 14 | + |
| 15 | +function makeFixture(fileCount: number, linesPerFile: number) { |
| 16 | + const differences: Difference[] = []; |
| 17 | + const diffTexts = new Map<string, { before: string; after: string }>(); |
| 18 | + const diffTextStatus = new Map<string, "loading" | "loaded" | "error">(); |
| 19 | + |
| 20 | + for (let f = 0; f < fileCount; f++) { |
| 21 | + const beforeBlobId = `before-${f}`; |
| 22 | + const afterBlobId = `after-${f}`; |
| 23 | + differences.push({ |
| 24 | + beforeBlob: { blobId: beforeBlobId, path: `src/file-${f}.ts` }, |
| 25 | + afterBlob: { blobId: afterBlobId, path: `src/file-${f}.ts` }, |
| 26 | + changeType: "M", |
| 27 | + }); |
| 28 | + const key = `${beforeBlobId}:${afterBlobId}`; |
| 29 | + diffTexts.set(key, { |
| 30 | + before: makeLines(linesPerFile, `f${f}-old`), |
| 31 | + after: makeLines(linesPerFile, `f${f}-new`), |
| 32 | + }); |
| 33 | + diffTextStatus.set(key, "loaded"); |
| 34 | + } |
| 35 | + |
| 36 | + return { differences, diffTexts, diffTextStatus }; |
| 37 | +} |
| 38 | + |
| 39 | +const pullRequest = { |
| 40 | + pullRequestId: "42", |
| 41 | + title: "perf: benchmark fixture", |
| 42 | + authorArn: "arn:aws:iam::123456789012:user/watany", |
| 43 | + pullRequestStatus: "OPEN", |
| 44 | + creationDate: new Date("2026-02-13T10:00:00Z"), |
| 45 | + pullRequestTargets: [ |
| 46 | + { |
| 47 | + destinationReference: "refs/heads/main", |
| 48 | + sourceReference: "refs/heads/feature/perf", |
| 49 | + }, |
| 50 | + ], |
| 51 | +}; |
| 52 | + |
| 53 | +const asyncActionProps = { |
| 54 | + onPost: noop, |
| 55 | + isProcessing: false, |
| 56 | + error: null, |
| 57 | + onClearError: noop, |
| 58 | +}; |
| 59 | + |
| 60 | +const fixture = makeFixture(10, 200); |
| 61 | + |
| 62 | +function renderDetail() { |
| 63 | + return render( |
| 64 | + <PullRequestDetail |
| 65 | + pullRequest={pullRequest as never} |
| 66 | + differences={fixture.differences} |
| 67 | + commentThreads={[]} |
| 68 | + diffTexts={fixture.diffTexts} |
| 69 | + diffTextStatus={fixture.diffTextStatus} |
| 70 | + onBack={noop} |
| 71 | + onHelp={noop} |
| 72 | + onShowActivity={noop} |
| 73 | + comment={asyncActionProps} |
| 74 | + inlineComment={asyncActionProps} |
| 75 | + reply={asyncActionProps} |
| 76 | + approval={{ |
| 77 | + approvals: [], |
| 78 | + evaluation: null, |
| 79 | + onApprove: noop, |
| 80 | + onRevoke: noop, |
| 81 | + isProcessing: false, |
| 82 | + error: null, |
| 83 | + onClearError: noop, |
| 84 | + }} |
| 85 | + merge={{ |
| 86 | + onMerge: noop, |
| 87 | + onCheckConflicts: () => |
| 88 | + Promise.resolve({ mergeable: true, conflictCount: 0, conflictFiles: [] }), |
| 89 | + isProcessing: false, |
| 90 | + error: null, |
| 91 | + onClearError: noop, |
| 92 | + }} |
| 93 | + close={{ onClose: noop, isProcessing: false, error: null, onClearError: noop }} |
| 94 | + commitView={{ |
| 95 | + commits: [], |
| 96 | + differences: [], |
| 97 | + diffTexts: new Map(), |
| 98 | + isLoading: false, |
| 99 | + onLoad: noop, |
| 100 | + commitsAvailable: false, |
| 101 | + }} |
| 102 | + editComment={{ onUpdate: noop, isProcessing: false, error: null, onClearError: noop }} |
| 103 | + deleteComment={{ onDelete: noop, isProcessing: false, error: null, onClearError: noop }} |
| 104 | + reaction={{ |
| 105 | + byComment: new Map(), |
| 106 | + onReact: noop, |
| 107 | + isProcessing: false, |
| 108 | + error: null, |
| 109 | + onClearError: noop, |
| 110 | + }} |
| 111 | + />, |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +const flush = () => new Promise<void>((r) => setTimeout(r, 0)); |
| 116 | + |
| 117 | +describe("PullRequestDetail rendering (10 files x 200 lines)", () => { |
| 118 | + bench("initial mount + unmount", async () => { |
| 119 | + const instance = renderDetail(); |
| 120 | + await flush(); |
| 121 | + instance.unmount(); |
| 122 | + }); |
| 123 | + |
| 124 | + // Alternate down/up so the cursor keeps moving (a clamped cursor skips re-render) |
| 125 | + const navInstance = renderDetail(); |
| 126 | + let navDown = true; |
| 127 | + bench("j/k keystroke (cursor move + re-render)", async () => { |
| 128 | + navInstance.stdin.write(navDown ? "j" : "k"); |
| 129 | + navDown = !navDown; |
| 130 | + await flush(); |
| 131 | + }); |
| 132 | + |
| 133 | + // Unhandled key: measures stdin parsing + flush overhead without a re-render |
| 134 | + const noopInstance = renderDetail(); |
| 135 | + bench("no-op keystroke (no re-render)", async () => { |
| 136 | + noopInstance.stdin.write("z"); |
| 137 | + await flush(); |
| 138 | + }); |
| 139 | + |
| 140 | + const pageInstance = renderDetail(); |
| 141 | + let pageDown = true; |
| 142 | + bench("Ctrl+d/u half-page scroll + re-render", async () => { |
| 143 | + pageInstance.stdin.write(pageDown ? "\x04" : "\x15"); |
| 144 | + pageDown = !pageDown; |
| 145 | + await flush(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments