Files
orca/src/renderer/src/components/right-sidebar/source-control-create-pr-intent-flow.test.ts
T
JinjingandOrca 1ef0551bc1 Create pr not working for stacked worktree (#8651)
* Fix stacked-worktree PR creation targeting a local-only parent branch

- Resolve the eligibility default base to a remote-tracking ref instead
  of blindly trusting the submitted parent branch, since a stacked
  worktree's base is often a local-only branch the remote can't resolve
- Add a create-time hard block (base_not_on_remote) so a stale or
  unpushed submitted base fails with actionable copy instead of the
  provider's opaque error
- Update the dialog's default-base resolution and blocked-action/
  dropdown copy to match the new remote-validated default

* Split hosted-review-creation.test.ts to fix max-lines lint error

Moved getHostedReviewCreationEligibility tests to a separate file (hosted-review-creation-eligibility.test.ts) to reduce the original file size from 880 to 579 lines, satisfying the max-lines lint constraint.

Co-authored-by: Orca <help@stably.ai>

* Fix Create PR intent flow to use remote-validated eligibility default fo

Prefer eligibilityDefaultBaseRef over the raw compare base when resolving
the review base for the one-click Create PR intent flow, since eligibility
is recomputed from the same compare base right before creation and already
corrects a local-only stacked parent to the repo default. Falls back to
the compare base only when eligibility supplies no default.

* Simplify base-ref remote existence check into a single for-each-ref call

Combine the wildcard and exact-tracking-ref lookups into one for-each-ref
invocation with multiple patterns instead of two sequential git calls,
removing the redundant rev-parse fallback path.

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-13 20:10:50 -07:00

282 lines
8.7 KiB
TypeScript

import { join, sep } from 'node:path'
import { describe, expect, it, vi } from 'vitest'
import {
createCreatePrIntentRunToken,
createPrIntentCurrentTargetConflictsWithToken,
createPrIntentGitStatusMatchesToken,
createPrIntentRunTokenMatches,
getCreatePrIntentCommitFailureNoticeMessage,
getCreatePrIntentStagePaths,
resolveCreatePrIntentReviewBase,
resolveCreatePrIntentRemoteStep
} from './source-control-create-pr-intent-flow'
import type { GitStatusEntry } from '../../../../shared/types'
describe('source-control Create PR intent flow helpers', () => {
it('matches async completions only to the original repo, worktree, path, branch, and base', () => {
const now = vi.spyOn(Date, 'now').mockReturnValue(123)
try {
const token = createCreatePrIntentRunToken({
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath: '/repo',
branch: 'feature',
baseRef: 'origin/main'
})
expect(token.startedAt).toBe(123)
expect(createPrIntentRunTokenMatches(token, token)).toBe(true)
expect(
createPrIntentRunTokenMatches(token, { ...token, baseRef: 'refs/remotes/origin/main' })
).toBe(true)
expect(createPrIntentRunTokenMatches(token, { ...token, branch: 'other' })).toBe(false)
expect(createPrIntentRunTokenMatches(token, { ...token, worktreeId: 'wt-2' })).toBe(false)
expect(createPrIntentRunTokenMatches(token, { ...token, baseRef: 'upstream/main' })).toBe(
false
)
} finally {
now.mockRestore()
}
})
it('matches strict git status snapshots to the original branch', () => {
const token = createCreatePrIntentRunToken({
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath: '/repo',
branch: 'feature/pr'
})
expect(createPrIntentGitStatusMatchesToken(token, { branch: 'refs/heads/feature/pr' })).toBe(
true
)
expect(createPrIntentGitStatusMatchesToken(token, { branch: 'feature/pr' })).toBe(true)
expect(createPrIntentGitStatusMatchesToken(token, { branch: 'refs/heads/other' })).toBe(false)
expect(createPrIntentGitStatusMatchesToken(token, { branch: null })).toBe(false)
})
it('does not treat navigating to another worktree as an intent conflict', () => {
const wt1Path = join(sep, 'repo', 'wt-1')
const wt2Path = join(sep, 'repo', 'wt-2')
const token = createCreatePrIntentRunToken({
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath: wt1Path,
branch: 'feature/pr'
})
expect(
createPrIntentCurrentTargetConflictsWithToken(token, {
repoId: 'repo-1',
worktreeId: 'wt-2',
worktreePath: wt2Path,
branch: 'other'
})
).toBe(false)
expect(
createPrIntentCurrentTargetConflictsWithToken(token, {
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath: wt1Path,
branch: 'other'
})
).toBe(true)
})
it('treats same-worktree base changes as intent conflicts', () => {
const worktreePath = join(sep, 'repo', 'wt-1')
const token = createCreatePrIntentRunToken({
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath,
branch: 'feature/pr',
baseRef: 'refs/remotes/origin/main'
})
expect(
createPrIntentCurrentTargetConflictsWithToken(token, {
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath,
branch: 'feature/pr',
baseRef: 'remotes/origin/main'
})
).toBe(false)
expect(
createPrIntentCurrentTargetConflictsWithToken(token, {
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath,
branch: 'feature/pr',
baseRef: 'upstream/main'
})
).toBe(true)
expect(
createPrIntentCurrentTargetConflictsWithToken(token, {
repoId: 'repo-1',
worktreeId: 'wt-1',
worktreePath,
branch: 'feature/pr',
baseRef: 'origin/release'
})
).toBe(true)
})
it('stages only safe unstaged and untracked paths', () => {
const unresolved = {
path: 'conflicted.ts',
status: 'modified',
area: 'unstaged',
conflictKind: 'both_modified',
conflictStatus: 'unresolved'
} satisfies GitStatusEntry
expect(
getCreatePrIntentStagePaths({
unstaged: [{ path: 'safe.ts', status: 'modified', area: 'unstaged' }, unresolved],
untracked: [{ path: 'new.ts', status: 'untracked', area: 'untracked' }]
})
).toEqual(['safe.ts', 'new.ts'])
})
it('prefers the remote-validated eligibility default so it cannot diverge from the composer', () => {
// Why: the intent flow's eligibility is recomputed from the same compare
// base right before creation, so its default already corrects a local-only
// stacked parent to the repo default. The one-click path must target that
// same base as the composer, not the raw (possibly unpushable) compare base.
expect(
resolveCreatePrIntentReviewBase({
currentBaseRef: 'stacked-parent',
eligibilityDefaultBaseRef: 'refs/remotes/origin/main',
composerBaseRef: 'main'
})
).toBe('main')
expect(
resolveCreatePrIntentReviewBase({
currentBaseRef: null,
eligibilityDefaultBaseRef: 'refs/remotes/upstream/develop',
composerBaseRef: 'main'
})
).toBe('develop')
})
it('falls back to the compare base when eligibility supplies no default', () => {
// Why: never blank the base. If the main process could not resolve a default
// (no candidate on remote and repo default unavailable), keep the user's
// compare base rather than dropping to an empty target.
expect(
resolveCreatePrIntentReviewBase({
currentBaseRef: 'refs/remotes/origin/release',
eligibilityDefaultBaseRef: null,
composerBaseRef: 'main'
})
).toBe('release')
})
it('resolves safe remote steps for publish, push, and patch-equivalent force-push', () => {
expect(
resolveCreatePrIntentRemoteStep({
upstreamStatus: { hasUpstream: false, ahead: 0, behind: 0 },
branchCommitsAhead: 2,
hasCurrentBranch: true,
hostedReviewCreation: {
provider: 'github',
review: null,
canCreate: false,
blockedReason: 'no_upstream',
nextAction: 'publish'
}
})
).toBe('publish')
expect(
resolveCreatePrIntentRemoteStep({
upstreamStatus: { hasUpstream: true, ahead: 2, behind: 0 },
hasCurrentBranch: true,
hostedReviewCreation: {
provider: 'github',
review: null,
canCreate: false,
blockedReason: 'needs_push',
nextAction: 'push'
}
})
).toBe('push')
expect(
resolveCreatePrIntentRemoteStep({
upstreamStatus: {
hasUpstream: true,
ahead: 3,
behind: 2,
behindCommitsArePatchEquivalent: true
},
branchCommitsAhead: 3,
hasCurrentBranch: true,
hostedReviewCreation: {
provider: 'github',
review: null,
canCreate: false,
blockedReason: 'needs_sync',
nextAction: 'sync'
}
})
).toBe('force_push')
})
it('blocks ordinary diverged branches and unpublished branches without commits', () => {
expect(
resolveCreatePrIntentRemoteStep({
upstreamStatus: { hasUpstream: true, ahead: 1, behind: 1 },
hasCurrentBranch: true,
hostedReviewCreation: {
provider: 'github',
review: null,
canCreate: false,
blockedReason: 'needs_sync',
nextAction: 'sync'
}
})
).toBe('blocked')
expect(
resolveCreatePrIntentRemoteStep({
upstreamStatus: { hasUpstream: false, ahead: 0, behind: 0 },
branchCommitsAhead: 0,
hasCurrentBranch: true,
hostedReviewCreation: {
provider: 'github',
review: null,
canCreate: false,
blockedReason: 'no_upstream',
nextAction: 'publish'
}
})
).toBe('blocked')
})
it('surfaces the commit failure summary in the Create PR intent notice', () => {
expect(
getCreatePrIntentCommitFailureNoticeMessage(
'husky - pre-commit hook\neslint found 2 errors\nfull output'
)
).toBe('Commit blocked: Lint failed during commit. Fix the issue, then retry Create PR.')
expect(getCreatePrIntentCommitFailureNoticeMessage(null)).toBe(
'Could not commit changes. Fix the issue, then retry Create PR.'
)
expect(
getCreatePrIntentCommitFailureNoticeMessage('pre-commit hook failed', {
fallback: 'fallback',
withSummary: (summary) => `localized ${summary}`
})
).toBe('localized Pre-commit hook failed.')
})
})