-
Notifications
You must be signed in to change notification settings - Fork 675
Fix stale GitHub repo avatars after owner changes #6507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ParkerRex
wants to merge
1
commit into
stablyai:main
Choose a base branch
from
ParkerRex:fix/github-avatar-live-refresh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/renderer/src/components/settings/RepositoryIconPicker.github-avatar-refresh.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // @vitest-environment happy-dom | ||
|
|
||
| import { act } from 'react' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { Repo } from '../../../../shared/types' | ||
| import { RepositoryIconPicker } from './RepositoryIconPicker' | ||
|
|
||
| vi.mock('@/runtime/runtime-rpc-client', () => ({ | ||
| callRuntimeRpc: vi.fn(), | ||
| getActiveRuntimeTarget: () => ({ kind: 'local' }) | ||
| })) | ||
|
|
||
| vi.mock('./RepositoryIconColorSection', () => ({ | ||
| RepositoryIconColorSection: () => null | ||
| })) | ||
|
|
||
| vi.mock('./RepositoryIconTabs', () => ({ | ||
| RepositoryIconTabs: () => null | ||
| })) | ||
|
|
||
| const apiMocks = { | ||
| repoSlug: vi.fn(), | ||
| repoUpstream: vi.fn() | ||
| } | ||
|
|
||
| let container: HTMLDivElement | ||
| let root: Root | ||
|
|
||
| // @ts-expect-error test window mock | ||
| globalThis.window = { api: { gh: apiMocks } } | ||
|
|
||
| function makeRepo(overrides: Partial<Repo> = {}): Repo { | ||
| return { | ||
| id: 'repo-1', | ||
| path: '/workspace/orca', | ||
| displayName: 'orca', | ||
| badgeColor: '#2563eb', | ||
| addedAt: 1, | ||
| kind: 'git', | ||
| ...overrides | ||
| } | ||
| } | ||
|
|
||
| async function flushEffects(): Promise<void> { | ||
| await act(async () => { | ||
| await Promise.resolve() | ||
| await Promise.resolve() | ||
| }) | ||
| } | ||
|
|
||
| describe('RepositoryIconPicker GitHub avatar refresh', () => { | ||
| beforeEach(() => { | ||
| apiMocks.repoSlug.mockReset() | ||
| apiMocks.repoUpstream.mockReset() | ||
| container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| root = createRoot(container) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| act(() => root.unmount()) | ||
| container.remove() | ||
| document.body.replaceChildren() | ||
| }) | ||
|
|
||
| it('refreshes stale GitHub avatar metadata lazily when repo settings opens', async () => { | ||
| const updateRepo = vi.fn() | ||
| const repo = makeRepo({ | ||
| upstream: { owner: 'stablyai', repo: 'orca' }, | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/stablyai.png?size=64', | ||
| source: 'github', | ||
| label: 'stablyai/orca' | ||
| } | ||
| }) | ||
| apiMocks.repoUpstream.mockResolvedValueOnce(null) | ||
| apiMocks.repoSlug.mockResolvedValueOnce({ owner: 'parkerrex', repo: 'orca' }) | ||
|
|
||
| act(() => { | ||
| root.render(<RepositoryIconPicker repo={repo} updateRepo={updateRepo} />) | ||
| }) | ||
| await flushEffects() | ||
|
|
||
| expect(updateRepo).toHaveBeenCalledExactlyOnceWith('repo-1', { | ||
| upstream: null, | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/parkerrex.png?size=64', | ||
| source: 'github', | ||
| label: 'parkerrex/orca' | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/renderer/src/components/settings/repository-icon-github.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { Repo } from '../../../../shared/types' | ||
| import { | ||
| buildRepositoryGitHubAvatarUpdate, | ||
| resolveRepositoryGitHubAvatar | ||
| } from './repository-icon-github' | ||
|
|
||
| vi.mock('@/runtime/runtime-rpc-client', () => ({ | ||
| callRuntimeRpc: vi.fn() | ||
| })) | ||
|
|
||
| const apiMocks = { | ||
| repoSlug: vi.fn(), | ||
| repoUpstream: vi.fn() | ||
| } | ||
|
|
||
| // @ts-expect-error test window mock | ||
| globalThis.window = { api: { gh: apiMocks } } | ||
|
|
||
| function makeRepo(overrides: Partial<Repo> = {}): Repo { | ||
| return { | ||
| id: 'repo-1', | ||
| path: '/workspace/orca', | ||
| displayName: 'orca', | ||
| badgeColor: '#2563eb', | ||
| addedAt: 1, | ||
| kind: 'git', | ||
| ...overrides | ||
| } | ||
| } | ||
|
|
||
| describe('repository GitHub avatar resolution', () => { | ||
| beforeEach(() => { | ||
| apiMocks.repoSlug.mockReset() | ||
| apiMocks.repoUpstream.mockReset() | ||
| }) | ||
|
|
||
| it('uses stored upstream by default to avoid unnecessary live checks', async () => { | ||
| const repo = makeRepo({ upstream: { owner: 'stablyai', repo: 'orca' } }) | ||
|
|
||
| await expect(resolveRepositoryGitHubAvatar({ kind: 'local' }, repo)).resolves.toEqual({ | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/stablyai.png?size=64', | ||
| source: 'github', | ||
| label: 'stablyai/orca' | ||
| }, | ||
| upstream: { owner: 'stablyai', repo: 'orca' } | ||
| }) | ||
|
|
||
| expect(apiMocks.repoUpstream).not.toHaveBeenCalled() | ||
| expect(apiMocks.repoSlug).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('force-resolves live origin when stored upstream/avatar are stale', async () => { | ||
| const repo = makeRepo({ | ||
| upstream: { owner: 'stablyai', repo: 'orca' }, | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/stablyai.png?size=64', | ||
| source: 'github', | ||
| label: 'stablyai/orca' | ||
| } | ||
| }) | ||
| apiMocks.repoUpstream.mockResolvedValueOnce(null) | ||
| apiMocks.repoSlug.mockResolvedValueOnce({ owner: 'parkerrex', repo: 'orca' }) | ||
|
|
||
| const resolution = await resolveRepositoryGitHubAvatar({ kind: 'local' }, repo, { | ||
| forceLive: true | ||
| }) | ||
|
|
||
| expect(resolution).toEqual({ | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/parkerrex.png?size=64', | ||
| source: 'github', | ||
| label: 'parkerrex/orca' | ||
| }, | ||
| upstream: null | ||
| }) | ||
| expect(apiMocks.repoUpstream).toHaveBeenCalledExactlyOnceWith({ | ||
| repoPath: '/workspace/orca', | ||
| repoId: 'repo-1' | ||
| }) | ||
| expect(apiMocks.repoSlug).toHaveBeenCalledExactlyOnceWith({ | ||
| repoPath: '/workspace/orca', | ||
| repoId: 'repo-1' | ||
| }) | ||
| expect(buildRepositoryGitHubAvatarUpdate(repo, resolution)).toEqual({ | ||
| upstream: null, | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/parkerrex.png?size=64', | ||
| source: 'github', | ||
| label: 'parkerrex/orca' | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| it('does not clear a GitHub avatar on passive refresh when live slug is unavailable', async () => { | ||
| const repo = makeRepo({ | ||
| repoIcon: { | ||
| type: 'image', | ||
| src: 'https://github.com/stablyai.png?size=64', | ||
| source: 'github', | ||
| label: 'stablyai/orca' | ||
| } | ||
| }) | ||
|
|
||
| expect(buildRepositoryGitHubAvatarUpdate(repo, { repoIcon: null, upstream: null })).toEqual({ | ||
| upstream: null | ||
| }) | ||
| expect( | ||
| buildRepositoryGitHubAvatarUpdate( | ||
| repo, | ||
| { repoIcon: null, upstream: null }, | ||
| { | ||
| clearMissingIcon: true | ||
| } | ||
| ) | ||
| ).toEqual({ | ||
| upstream: null, | ||
| repoIcon: null | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Preserve happy-dom’s real
windowobject.Lines 30-31 replace
globalThis.windowwith a plain object, which strips the real happy-domwindowofdocument, timers, constructors, and other DOM state. That makes this test run against a broken browser global and can hide future regressions. Patchwindow.apiinstead of replacingwindow.Proposed fix
📝 Committable suggestion