codeflash-internal/js/cf-api/diff_utils.ts
Kevin Turcios d7a8b8f227
perf: fix CI build + lazy-load heavy libs + parallelize DB queries (#2601)
## Summary
- **Fix CI build failure**: Auth0Client crashes during Next.js
prerendering when env vars aren't set. Returns a no-op stub (`getSession
→ null`) when domain is missing — semantically correct for static
generation
- **Lazy-load markdown libs (~260kb)**: ReactMarkdown, remarkGfm, and
react-syntax-highlighter were eagerly imported in monaco-diff-viewer but
only rendered when user expands "Generated Tests". Extracted into a
dynamic component
- **Parallelize repo detail query**: `getRepositoryById` ran the
activity count sequentially after the repo lookup. Since `repoId` is
already available, all three queries now run in parallel

## Test plan
- [ ] CI `build` check passes (was failing since #2598)
- [ ] Trace page still renders generated tests correctly when expanded
- [ ] Repository detail page loads correctly with activity status
2026-04-13 11:03:05 -05:00

60 lines
1.9 KiB
TypeScript

import type { FileDiffContent, Hunk } from "@codeflash-ai/code-suggester/build/src/types.js"
import {
getRawSuggestionHunks,
partitionSuggestedHunksByScope,
} from "@codeflash-ai/code-suggester/build/src/utils/hunk-utils.js"
import { getPullRequestHunks } from "@codeflash-ai/code-suggester/build/src/github/review-pull-request.js"
import type { Octokit } from "@octokit/rest"
export function fileDiffsToMap(obj: Record<string, FileDiffContent>): Map<string, FileDiffContent> {
const map = new Map()
Object.keys(obj).forEach(key => {
if (
obj[key] &&
typeof obj[key] === "object" &&
typeof obj[key].oldContent === "string" &&
typeof obj[key].newContent === "string"
) {
map.set(key, {
oldContent: obj[key].oldContent,
newContent: obj[key].newContent,
} as FileDiffContent)
}
})
return map
}
export async function determineValidHunks(
octokit: Octokit,
remote: { owner: string; repo: string },
pullNumber: number,
pageSize: number,
diffContents: Map<string, FileDiffContent>,
) {
// get the hunks from the pull request
const pullRequestHunks = await getPullRequestHunks(octokit as any, remote, pullNumber, pageSize)
// get the hunks from the suggested change
const allSuggestedHunks: Map<string, Hunk[]> = getRawSuggestionHunks(diffContents)
// split hunks by commentable and uncommentable
const { validHunks, invalidHunks } = partitionSuggestedHunksByScope(
pullRequestHunks,
allSuggestedHunks,
)
return { validHunks, invalidHunks }
}
export function isDiffContentsWellFormed(
diffContents: any,
): diffContents is Record<string, FileDiffContent> {
return Object.values(diffContents).every(
content =>
content !== null &&
typeof content === "object" &&
"oldContent" in content &&
"newContent" in content &&
typeof content.oldContent === "string" &&
typeof content.newContent === "string",
)
}