Fix stale GitHub status cache updates (#2483)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
/* eslint-disable max-lines -- Why: coordinator tests cover queueing, coalescing,
|
||||
request timestamps, and follow-up scheduling against shared module state. */
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { GitHubPRRefreshCandidate, PRInfo } from '../../shared/types'
|
||||
|
||||
@@ -289,6 +291,68 @@ describe('pr-refresh-coordinator', () => {
|
||||
expect(getPRForBranchOutcomeMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('includes request start time on manual refresh events', async () => {
|
||||
const { refreshPRNow } = await import('./pr-refresh-coordinator')
|
||||
getPRForBranchOutcomeMock.mockResolvedValueOnce({
|
||||
kind: 'found',
|
||||
pr: makePR({ checksStatus: 'success' }),
|
||||
fetchedAt: Date.now() + 5
|
||||
})
|
||||
|
||||
await refreshPRNow(makeCandidate())
|
||||
|
||||
const events = sendMock.mock.calls.map(([, event]) => event)
|
||||
const inFlight = events.find((event) => event.status === 'in-flight')
|
||||
const outcome = events.find((event) => event.outcome)
|
||||
expect(inFlight?.requestStartedAt).toBe(1_000)
|
||||
expect(outcome?.requestStartedAt).toBe(1_000)
|
||||
expect(outcome?.sequence).toBe(inFlight?.sequence)
|
||||
})
|
||||
|
||||
it('does not coalesce local and SSH refreshes for the same branch', async () => {
|
||||
const { enqueuePRRefresh } = await import('./pr-refresh-coordinator')
|
||||
getPRForBranchOutcomeMock
|
||||
.mockResolvedValueOnce({
|
||||
kind: 'found',
|
||||
pr: makePR({ number: 12 }),
|
||||
fetchedAt: Date.now()
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
kind: 'found',
|
||||
pr: makePR({ number: 44 }),
|
||||
fetchedAt: Date.now()
|
||||
})
|
||||
|
||||
enqueuePRRefresh(makeCandidate({ cacheKey: 'local::repo-1::feature/test' }), 'active', 80, 1)
|
||||
enqueuePRRefresh(
|
||||
makeCandidate({
|
||||
cacheKey: 'ssh:ssh-1::repo-1::feature/test',
|
||||
connectionId: 'ssh-1'
|
||||
}),
|
||||
'active',
|
||||
80,
|
||||
1
|
||||
)
|
||||
await vi.runOnlyPendingTimersAsync()
|
||||
await vi.runOnlyPendingTimersAsync()
|
||||
|
||||
expect(getPRForBranchOutcomeMock).toHaveBeenCalledTimes(2)
|
||||
expect(getPRForBranchOutcomeMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/repo',
|
||||
'feature/test',
|
||||
null,
|
||||
null
|
||||
)
|
||||
expect(getPRForBranchOutcomeMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/repo',
|
||||
'feature/test',
|
||||
null,
|
||||
'ssh-1'
|
||||
)
|
||||
})
|
||||
|
||||
it('preserves coalesced aliases across visible follow-up refreshes', async () => {
|
||||
const { reportVisiblePRRefreshCandidates } = await import('./pr-refresh-coordinator')
|
||||
getPRForBranchOutcomeMock
|
||||
|
||||
@@ -65,10 +65,11 @@ function broadcast(event: Omit<GitHubPRRefreshEvent, 'sequence'>, sequenceOverri
|
||||
}
|
||||
|
||||
function refreshKey(candidate: GitHubPRRefreshCandidate): string {
|
||||
const connectionScope = candidate.connectionId ?? 'local'
|
||||
if (typeof candidate.linkedPRNumber === 'number') {
|
||||
return `${candidate.repoPath}::pr::${candidate.linkedPRNumber}`
|
||||
return `${connectionScope}::${candidate.repoPath}::pr::${candidate.linkedPRNumber}`
|
||||
}
|
||||
return `${candidate.repoPath}::branch::${candidate.branch}`
|
||||
return `${connectionScope}::${candidate.repoPath}::branch::${candidate.branch}`
|
||||
}
|
||||
|
||||
function isVisibleKey(key: string): boolean {
|
||||
@@ -381,7 +382,11 @@ async function drainQueue(): Promise<void> {
|
||||
continue
|
||||
}
|
||||
const requestSequence = nextSequence()
|
||||
broadcast({ aliases, reason: next.reason, status: 'in-flight' }, requestSequence)
|
||||
const requestStartedAt = Date.now()
|
||||
broadcast(
|
||||
{ aliases, reason: next.reason, status: 'in-flight', requestStartedAt },
|
||||
requestSequence
|
||||
)
|
||||
|
||||
if (isBackground(next.reason)) {
|
||||
const rateLimit = await getRateLimit()
|
||||
@@ -430,7 +435,7 @@ async function drainQueue(): Promise<void> {
|
||||
next.candidate.connectionId ?? null
|
||||
)
|
||||
outcomeObserver?.(next.candidate, outcome)
|
||||
broadcast({ aliases, reason: next.reason, outcome }, requestSequence)
|
||||
broadcast({ aliases, reason: next.reason, outcome, requestStartedAt }, requestSequence)
|
||||
scheduleVisibleFollowUp(
|
||||
next.key,
|
||||
next.candidate,
|
||||
@@ -456,7 +461,8 @@ export function enqueuePRRefresh(
|
||||
repoId: candidate.repoId,
|
||||
repoPath: candidate.repoPath,
|
||||
branch: candidate.branch,
|
||||
worktreeId: candidate.worktreeId
|
||||
worktreeId: candidate.worktreeId,
|
||||
connectionId: candidate.connectionId ?? null
|
||||
}
|
||||
const key = refreshKey(candidate)
|
||||
const skippedReason = validateCandidate(candidate)
|
||||
@@ -539,7 +545,8 @@ export async function refreshPRNow(candidate: GitHubPRRefreshCandidate): Promise
|
||||
repoId: candidate.repoId,
|
||||
repoPath: candidate.repoPath,
|
||||
branch: candidate.branch,
|
||||
worktreeId: candidate.worktreeId
|
||||
worktreeId: candidate.worktreeId,
|
||||
connectionId: candidate.connectionId ?? null
|
||||
}
|
||||
const key = refreshKey(candidate)
|
||||
const existing = queue.get(key)
|
||||
@@ -562,7 +569,8 @@ export async function refreshPRNow(candidate: GitHubPRRefreshCandidate): Promise
|
||||
|
||||
queue.delete(key)
|
||||
const requestSequence = nextSequence()
|
||||
broadcast({ aliases, reason: 'manual', status: 'in-flight' }, requestSequence)
|
||||
const requestStartedAt = Date.now()
|
||||
broadcast({ aliases, reason: 'manual', status: 'in-flight', requestStartedAt }, requestSequence)
|
||||
const outcome = await getPRForBranchOutcome(
|
||||
candidate.repoPath,
|
||||
candidate.branch,
|
||||
@@ -570,7 +578,7 @@ export async function refreshPRNow(candidate: GitHubPRRefreshCandidate): Promise
|
||||
candidate.connectionId ?? null
|
||||
)
|
||||
outcomeObserver?.(candidate, outcome)
|
||||
broadcast({ aliases, reason: 'manual', outcome }, requestSequence)
|
||||
broadcast({ aliases, reason: 'manual', outcome, requestStartedAt }, requestSequence)
|
||||
scheduleVisibleFollowUp(key, candidate, outcome, 40, aliases)
|
||||
return outcome
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user