Repository navigation
Fix integration scope save latency #90
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,282 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| type FetchCall = { | ||
| url: string | ||
| init?: RequestInit | ||
| } | ||
|
|
||
| const mock = vi.hoisted(() => { | ||
| const fetchCalls: FetchCall[] = [] | ||
| const initialIntegration = () => ({ | ||
| id: 'slack-integration-1', | ||
| name: 'Slack', | ||
| type: 'slack', | ||
| provider: 'slack', | ||
| integrationId: 'slack-integration-1', | ||
| scope: {}, | ||
| mountPaths: ['/slack/channels'], | ||
| connectedAt: '2026-06-05T00:00:00.000Z', | ||
| notifyAgent: true, | ||
| subscribeAgent: false, | ||
| downloadHistoricalData: false, | ||
| visibleInProject: true | ||
| }) | ||
| const store = { | ||
| projects: [ | ||
| { | ||
| id: 'project-1', | ||
| name: 'Project 1', | ||
| rootPath: '/tmp/project-1', | ||
| roots: [], | ||
| channels: ['general'], | ||
| channelPeople: {}, | ||
| integrations: [initialIntegration()] | ||
| } | ||
| ], | ||
| activeProjectId: 'project-1' | ||
| } | ||
| let mountReconcilePromise: Promise<void> = Promise.resolve() | ||
|
|
||
| const jsonResponse = (payload: unknown, status = 200, statusText = 'OK') => ({ | ||
| ok: status >= 200 && status < 300, | ||
| status, | ||
| statusText, | ||
| json: async () => payload | ||
| }) | ||
|
|
||
| const fetch = vi.fn(async (url: string | URL | Request, init?: RequestInit) => { | ||
| const normalizedUrl = String(url) | ||
| fetchCalls.push({ url: normalizedUrl, init }) | ||
| const parsed = new URL(normalizedUrl) | ||
|
|
||
| if (parsed.pathname === '/api/v1/integrations/catalog') { | ||
| return jsonResponse({ | ||
| providers: [ | ||
| { | ||
| id: 'slack', | ||
| displayName: 'Slack', | ||
| version: '1', | ||
| backends: ['nango'], | ||
| defaultMountPaths: ['/slack/channels'] | ||
| }, | ||
| { | ||
| id: 'google-mail', | ||
| displayName: 'Gmail', | ||
| version: '1', | ||
| backends: ['nango'], | ||
| defaultMountPaths: ['/google-mail/messages'] | ||
| } | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| if (parsed.pathname === '/api/v1/workspaces/account-workspace-id/integrations/google-mail/options/channels') { | ||
| return jsonResponse({ | ||
| options: [ | ||
| { value: 'inbox', label: 'Inbox', hint: 'mail' }, | ||
| { value: 'sent' } | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| if (parsed.pathname === '/api/v1/workspaces/account-workspace-id/integrations/slack/options/channels') { | ||
| return jsonResponse({ error: 'not found' }, 404, 'Not Found') | ||
| } | ||
|
|
||
| if ( | ||
| parsed.pathname === '/api/v1/workspaces/account-workspace-id/integrations/slack/channels/available' && | ||
| parsed.searchParams.get('cursor') === 'cursor-2' | ||
| ) { | ||
| return jsonResponse({ | ||
| channels: [{ id: 'C456', name: 'private-team', is_private: true }] | ||
| }) | ||
| } | ||
|
|
||
| if (parsed.pathname === '/api/v1/workspaces/account-workspace-id/integrations/slack/channels/available') { | ||
| return jsonResponse({ | ||
| channels: [{ id: 'C123', name: 'general' }], | ||
| nextCursor: 'cursor-2' | ||
| }) | ||
| } | ||
|
|
||
| throw new Error(`unexpected fetch: ${normalizedUrl}`) | ||
| }) | ||
|
|
||
| return { | ||
| fetchCalls, | ||
| store, | ||
| fetch, | ||
| browserWindow: { | ||
| getAllWindows: vi.fn(() => []) | ||
| }, | ||
| resolveCloudAuth: vi.fn(async () => ({ | ||
| apiUrl: 'https://cloud.example', | ||
| accessToken: 'account-token' | ||
| })), | ||
| getAccountWorkspaceId: vi.fn(async () => 'account-workspace-id'), | ||
| accountWorkspaceReadyRetryOptions: vi.fn(() => ({ retryAttempts: 1, retryDelayMs: 0 })), | ||
| loadStore: vi.fn(() => store), | ||
| saveStore: vi.fn(() => undefined), | ||
| integrationMountManager: { | ||
| ensureMounted: vi.fn(() => mountReconcilePromise), | ||
| currentWorkspaceId: vi.fn(() => null), | ||
| localPathsFor: vi.fn(() => []), | ||
| stop: vi.fn(async () => undefined) | ||
| }, | ||
| integrationEventBridge: { | ||
| reconcile: vi.fn(async () => undefined), | ||
| closeAll: vi.fn(async () => undefined) | ||
| }, | ||
| cloudAgentManager: { | ||
| updateMountPaths: vi.fn(async () => undefined) | ||
| }, | ||
| brokerManager: { | ||
| listAgents: vi.fn(async () => []), | ||
| sendMessage: vi.fn(async () => undefined) | ||
| }, | ||
| ensureProjectIntegrationsLink: vi.fn(async () => undefined), | ||
| removeProjectIntegrationsLink: vi.fn(async () => undefined), | ||
| resetStore() { | ||
| store.projects[0].integrations = [initialIntegration()] | ||
| }, | ||
| setMountReconcilePromise(value: Promise<void>) { | ||
| mountReconcilePromise = value | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('electron', () => ({ | ||
| BrowserWindow: mock.browserWindow, | ||
| shell: { | ||
| openExternal: vi.fn(async () => undefined) | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('./auth', () => ({ | ||
| resolveCloudAuth: mock.resolveCloudAuth, | ||
| getAccountWorkspaceId: mock.getAccountWorkspaceId, | ||
| accountWorkspaceReadyRetryOptions: mock.accountWorkspaceReadyRetryOptions, | ||
| getApiUrl: vi.fn(() => 'https://cloud.example') | ||
| })) | ||
|
|
||
| vi.mock('./store', () => ({ | ||
| loadStore: mock.loadStore, | ||
| saveStore: mock.saveStore | ||
| })) | ||
|
|
||
| vi.mock('./integration-mounts', () => ({ | ||
| integrationMountManager: mock.integrationMountManager, | ||
| integrationMountRootForWorkspace: vi.fn((workspaceId: string) => `/tmp/relayfile/${workspaceId}`) | ||
| })) | ||
|
|
||
| vi.mock('./integration-event-bridge', () => ({ | ||
| integrationEventBridge: mock.integrationEventBridge, | ||
| integrationSubscriptionSummaries: vi.fn(() => []) | ||
| })) | ||
|
|
||
| vi.mock('./cloud-agent', () => ({ | ||
| cloudAgentManager: mock.cloudAgentManager | ||
| })) | ||
|
|
||
| vi.mock('./broker', () => ({ | ||
| brokerManager: mock.brokerManager | ||
| })) | ||
|
|
||
| vi.mock('./integration-symlinks', () => ({ | ||
| PROJECT_INTEGRATIONS_LINK_NAME: '.integrations', | ||
| ensureProjectIntegrationsLink: mock.ensureProjectIntegrationsLink, | ||
| removeProjectIntegrationsLink: mock.removeProjectIntegrationsLink | ||
| })) | ||
|
|
||
| vi.mock('./relay-workspace', () => ({ | ||
| getRelayWorkspaceManager: vi.fn(() => ({ | ||
| withHandle: vi.fn(), | ||
| getWorkspaceHandle: vi.fn() | ||
| })) | ||
| })) | ||
|
|
||
| import { IntegrationsManager } from './integrations' | ||
|
|
||
| describe('IntegrationsManager', () => { | ||
| beforeEach(() => { | ||
| mock.fetchCalls.splice(0) | ||
| mock.fetch.mockClear() | ||
| mock.browserWindow.getAllWindows.mockClear() | ||
| mock.resolveCloudAuth.mockClear() | ||
| mock.getAccountWorkspaceId.mockClear() | ||
| mock.accountWorkspaceReadyRetryOptions.mockClear() | ||
| mock.loadStore.mockClear() | ||
| mock.saveStore.mockClear() | ||
| mock.integrationMountManager.ensureMounted.mockClear() | ||
| mock.integrationMountManager.currentWorkspaceId.mockClear() | ||
| mock.integrationMountManager.localPathsFor.mockClear() | ||
| mock.integrationMountManager.stop.mockClear() | ||
| mock.integrationEventBridge.reconcile.mockClear() | ||
| mock.cloudAgentManager.updateMountPaths.mockClear() | ||
| mock.brokerManager.listAgents.mockClear() | ||
| mock.brokerManager.sendMessage.mockClear() | ||
| mock.ensureProjectIntegrationsLink.mockClear() | ||
| mock.removeProjectIntegrationsLink.mockClear() | ||
| mock.resetStore() | ||
| mock.setMountReconcilePromise(Promise.resolve()) | ||
| vi.stubGlobal('fetch', mock.fetch) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| it('lists integration options through the account workspace API', async () => { | ||
| const manager = new IntegrationsManager() | ||
|
|
||
| await expect(manager.listOptions('project-1', 'gmail', 'channels')).resolves.toEqual([ | ||
| { value: 'inbox', label: 'Inbox', hint: 'mail' }, | ||
| { value: 'sent', label: 'sent' } | ||
| ]) | ||
|
|
||
| const optionsCall = mock.fetchCalls.find((call) => | ||
| call.url.endsWith('/api/v1/workspaces/account-workspace-id/integrations/google-mail/options/channels') | ||
| ) | ||
| expect(optionsCall?.init?.headers).toMatchObject({ | ||
| Authorization: 'Bearer account-token' | ||
| }) | ||
| }) | ||
|
|
||
| it('falls back to legacy Slack channel options through the account workspace API', async () => { | ||
| const manager = new IntegrationsManager() | ||
|
|
||
| await expect(manager.listOptions('project-1', 'slack', 'channels')).resolves.toEqual([ | ||
| { value: 'C123', label: '#general' }, | ||
| { value: 'C456', label: '#private-team', hint: 'private' } | ||
| ]) | ||
|
|
||
| expect(mock.fetchCalls.map((call) => new URL(call.url).pathname + new URL(call.url).search)).toContain( | ||
| '/api/v1/workspaces/account-workspace-id/integrations/slack/channels/available?cursor=cursor-2' | ||
| ) | ||
| }) | ||
|
|
||
| it('does not block updateScope on integration state sync', async () => { | ||
| let finishMountReconcile!: () => void | ||
| mock.setMountReconcilePromise(new Promise((resolve) => { | ||
| finishMountReconcile = resolve | ||
| })) | ||
| const manager = new IntegrationsManager() | ||
|
|
||
| const updated = await manager.updateScope( | ||
| 'project-1', | ||
| 'slack-integration-1', | ||
| { channels: ['C123'] }, | ||
| ['/slack/channels/C123'] | ||
| ) | ||
|
|
||
| expect(updated).toMatchObject({ | ||
| integrationId: 'slack-integration-1', | ||
| scope: { channels: ['C123'] }, | ||
| mountPaths: ['/slack/channels/C123'] | ||
| }) | ||
| expect(mock.integrationMountManager.ensureMounted).toHaveBeenCalled() | ||
|
|
||
| finishMountReconcile() | ||
| }) | ||
| }) |
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.
Instead of hardcoding the exact error message string comparison, use the existing
isWorkspaceNotStartedError(err)helper function. This is more robust, handles non-Errorthrown values safely, and maintains consistency with how this error is checked elsewhere in the codebase.